mul_op.cc 3.4 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

L
liuwei1031 已提交
34 35 36 37 38 39 40 41 42 43
  // 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]);
S
superjomn 已提交
44 45 46 47 48 49 50 51
  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 已提交
52 53
  std::vector<int64_t> out_dims(
      param_.x_num_col_dims + y_dims.size() - param_.y_num_col_dims, 0);
S
superjomn 已提交
54 55 56
  for (int i = 0; i < param_.x_num_col_dims; ++i) {
    out_dims[i] = x_dims[i];
  }
L
liuwei1031 已提交
57 58 59 60 61

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

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

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

L
liuwei1031 已提交
70 71
#ifdef LITE_WITH_X86

L
liuwei1031 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
bool MulGradOpLite::CheckShape() const {
  CHECK_OR_FALSE(param_.x);
  CHECK_OR_FALSE(param_.y);
  CHECK_OR_FALSE(param_.output_grad);
  CHECK_OR_FALSE(param_.x_grad);
  CHECK_OR_FALSE(param_.y_grad);

  return true;
}

bool MulGradOpLite::InferShape() const {
  param_.x_grad->Resize(param_.x->dims());
  param_.y_grad->Resize(param_.y->dims());
  return true;
}

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

  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);
  param_.x_grad = GetMutableVar<lite::Tensor>(scope, X_grad_name);
  param_.y_grad = GetMutableVar<lite::Tensor>(scope, Y_grad_name);

  return true;
}
L
liuwei1031 已提交
103
#endif
L
liuwei1031 已提交
104

S
superjomn 已提交
105 106 107 108 109
}  // namespace operators
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_OP(mul, paddle::lite::operators::MulOpLite);