lookup_op.cpp 2.0 KB
Newer Older
xiebaiyuan's avatar
xiebaiyuan 已提交
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
/* Copyright (c) 2018 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. */

#ifdef LOOKUP_OP

#include <vector>

#include "common/enforce.h"
#include "operators/lookup_op.h"

namespace paddle_mobile {
namespace operators {

template <typename Dtype, typename T>
void LookupOp<Dtype, T>::InferShape() const {
  PADDLE_MOBILE_ENFORCE(this->param_.InputW() != nullptr,
                        "Input(W) of LookupTableOp should not be null.");
  auto *ids_t = this->param_.InputIds();

  PADDLE_MOBILE_ENFORCE(ids_t != nullptr,
                        "Input(Ids) of LookupTableOp should not be null.");
  PADDLE_MOBILE_ENFORCE(this->param_.Out() != nullptr,
                        "Output(Out) of LookupTableOp should not be null.");
  //    this->param__.InputW()->

  auto table_dims = this->param_.InputW()->dims();
  auto ids_dims = ids_t->dims();

  int ids_rank = ids_dims.size();

  PADDLE_MOBILE_ENFORCE(table_dims.size() == 2,
                        "table_dims.size()==2 check failed");

  PADDLE_MOBILE_ENFORCE(ids_dims[ids_rank - 1] == 1,
                        "The last dimension of the 'Ids' tensor must be 1.");

  auto output_dims =
      framework::vectorize(framework::slice_ddim(ids_dims, 0, ids_rank - 1));
  output_dims.push_back(table_dims[1]);

  this->param_.Out()->Resize(framework::make_ddim(output_dims));
}

}  // namespace operators
}  // namespace paddle_mobile

namespace ops = paddle_mobile::operators;
#ifdef PADDLE_MOBILE_CPU
REGISTER_OPERATOR_CPU(lookup_table, ops::LookupOp);
#endif
xiebaiyuan's avatar
xiebaiyuan 已提交
62

xiebaiyuan's avatar
xiebaiyuan 已提交
63 64 65 66
#ifdef PADDLE_MOBILE_FPGA
#endif

#endif