write_to_array_op.cc 1.5 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// 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 "lite/operators/write_to_array_op.h"
#include "lite/core/op_registry.h"

namespace paddle {
namespace lite {
namespace operators {

22 23 24 25 26 27
bool WriteToArrayOp::CheckShape() const {
  CHECK(param_.X);
  CHECK(param_.I);
  CHECK(param_.Out);
  return true;
}
Y
Yan Chunwei 已提交
28

29
bool WriteToArrayOp::InferShapeImpl() const {
30 31 32
  int id = param_.I->data<int64_t>()[0];
  if (param_.Out->size() < id + 1) {
    param_.Out->resize(id + 1);
Y
Yan Chunwei 已提交
33 34 35 36 37 38
  }
  return true;
}

bool WriteToArrayOp::AttachImpl(const cpp::OpDesc &opdesc, lite::Scope *scope) {
  auto inputs = opdesc.Input("X").front();
39
  param_.X = scope->FindTensor(inputs);
Y
Yan Chunwei 已提交
40 41

  auto id = opdesc.Input("I").front();
42
  param_.I = scope->FindTensor(id);
Y
Yan Chunwei 已提交
43 44

  auto out = opdesc.Output("Out").front();
45
  param_.Out = scope->FindVar(out)->GetMutable<std::vector<Tensor>>();
Y
Yan Chunwei 已提交
46 47 48 49 50 51 52 53
  return true;
}

}  // namespace operators
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_OP(write_to_array, paddle::lite::operators::WriteToArrayOp);