mul_op.cc 3.6 KB
Newer Older
S
superjomn 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/fluid/lite/operators/mul_op.h"
#include "paddle/fluid/lite/core/op_registry.h"

namespace paddle {
namespace lite {
namespace operators {

bool MulOpLite::CheckShape() const {
  CHECK_OR_FALSE(param_.x);
  CHECK_OR_FALSE(param_.y);
  CHECK_OR_FALSE(param_.output);
  // bias is optional.

  const auto x_dims = param_.x->dims();
  const auto y_dims = param_.y->dims();

  CHECK_GT_OR_FALSE(x_dims.size(), static_cast<size_t>(param_.x_num_col_dims));
L
liuwei1031 已提交
32
  CHECK_GT_OR_FALSE(y_dims.size(), static_cast<size_t>(param_.y_num_col_dims));
S
superjomn 已提交
33

34 35 36 37 38 39 40 41 42 43 44 45
#ifndef LITE_WITH_LIGHT_WEIGHT_FRAMEWORK
  auto x_mat_dims =
      framework::flatten_to_2d(x_dims.data(), param_.x_num_col_dims);
  auto y_mat_dims =
      framework::flatten_to_2d(y_dims.data(), param_.y_num_col_dims);

  PADDLE_ENFORCE_EQ(x_mat_dims[1], y_mat_dims[0],
                    "First matrix's width must be equal with second matrix's"
                    "height. %s, %s",
                    x_mat_dims[1], y_mat_dims[0]);
#endif

S
superjomn 已提交
46 47 48 49 50 51 52 53
  return true;
}

bool MulOpLite::InferShape() const {
  const auto x_dims = param_.x->dims();
  const auto y_dims = param_.y->dims();

  // Set output dims
L
liuwei1031 已提交
54 55
  std::vector<int64_t> out_dims(
      param_.x_num_col_dims + y_dims.size() - param_.y_num_col_dims, 0);
S
superjomn 已提交
56 57 58
  for (int i = 0; i < param_.x_num_col_dims; ++i) {
    out_dims[i] = x_dims[i];
  }
L
liuwei1031 已提交
59 60 61 62 63

  for (auto i = static_cast<size_t>(param_.y_num_col_dims); i < y_dims.size();
       ++i) {
    out_dims[i] = y_dims[i];
  }
64

65
  param_.output->Resize(lite::DDim(out_dims));
S
superjomn 已提交
66 67 68 69 70 71

  // share LoD
  // param_.output->set_lod(param_.input->lod());
  return true;
}

L
liuwei1031 已提交
72 73
#ifdef LITE_WITH_X86

L
liuwei1031 已提交
74 75 76 77 78 79 80 81 82
bool MulGradOpLite::CheckShape() const {
  CHECK_OR_FALSE(param_.x);
  CHECK_OR_FALSE(param_.y);
  CHECK_OR_FALSE(param_.output_grad);

  return true;
}

bool MulGradOpLite::InferShape() const {
83 84
  if (param_.x_grad) param_.x_grad->Resize(param_.x->dims());
  if (param_.y_grad) param_.y_grad->Resize(param_.y->dims());
L
liuwei1031 已提交
85 86 87
  return true;
}

Y
Yan Chunwei 已提交
88
bool MulGradOpLite::AttachImpl(const cpp::OpDesc &op_desc, lite::Scope *scope) {
L
liuwei1031 已提交
89 90
  auto X_name = op_desc.Input("X").front();
  auto Y_name = op_desc.Input("Y").front();
91 92 93 94 95 96 97 98 99 100 101
  auto Out_grad_name = op_desc.Input(framework::GradVarName("Out")).front();

  if (op_desc.Output(framework::GradVarName("X")).size()) {
    auto X_grad_name = op_desc.Output(framework::GradVarName("X")).front();
    param_.x_grad = GetMutableVar<lite::Tensor>(scope, X_grad_name);
  }

  if (op_desc.Output(framework::GradVarName("Y")).size()) {
    auto Y_grad_name = op_desc.Output(framework::GradVarName("Y")).front();
    param_.y_grad = GetMutableVar<lite::Tensor>(scope, Y_grad_name);
  }
L
liuwei1031 已提交
102 103 104 105 106 107 108

  param_.x = GetVar<lite::Tensor>(scope, X_name);
  param_.y = GetVar<lite::Tensor>(scope, Y_name);
  param_.output_grad = GetVar<lite::Tensor>(scope, Out_grad_name);

  return true;
}
L
liuwei1031 已提交
109
#endif
L
liuwei1031 已提交
110

S
superjomn 已提交
111 112 113 114 115
}  // namespace operators
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_OP(mul, paddle::lite::operators::MulOpLite);
116 117 118
#ifdef LITE_WITH_X86
REGISTER_LITE_OP(mul_grad, paddle::lite::operators::MulGradOpLite);
#endif