fc_op.h 2.3 KB
Newer Older
S
superjomn 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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 <string>
#include <vector>
S
superjomn 已提交
17
#include "paddle/fluid/lite/core/kernel.h"
S
update  
superjomn 已提交
18
#include "paddle/fluid/lite/core/op_lite.h"
S
superjomn 已提交
19
#include "paddle/fluid/lite/core/scope.h"
S
update  
superjomn 已提交
20
#include "paddle/fluid/lite/core/tensor.h"
S
superjomn 已提交
21
#include "paddle/fluid/lite/operators/op_params.h"
S
superjomn 已提交
22 23 24 25 26 27 28 29 30 31
#include "paddle/fluid/lite/utils/all.h"

namespace paddle {
namespace lite {
namespace operators {

class FcOpLite : public OpLite {
 public:
  FcOpLite() {}

S
superjomn 已提交
32 33
  FcOpLite(const std::string &type) : OpLite(type) {}

S
superjomn 已提交
34 35 36 37
  bool CheckShape() const override;

  bool InferShape() const override;

S
superjomn 已提交
38 39 40 41 42
  bool Run() override {
    CHECK(kernel_);
    kernel_->Run();
    return true;
  }
S
superjomn 已提交
43

S
superjomn 已提交
44
  // TODO(Superjomn) replace framework::OpDesc with a lite one.
S
superjomn 已提交
45
  bool Attach(const framework::OpDesc &op_desc, lite::Scope *scope) override {
S
superjomn 已提交
46 47
    auto input = op_desc.Input("Input").front();
    auto W = op_desc.Input("W").front();
S
superjomn 已提交
48 49
    auto bias = op_desc.Input("Bias").front();
    auto out = op_desc.Output("Out").front();
S
superjomn 已提交
50 51 52 53 54 55

    param_.input = scope->FindVar(input)->GetMutable<Tensor>();
    param_.w = scope->FindVar(W)->GetMutable<Tensor>();
    param_.bias = scope->FindVar(bias)->GetMutable<Tensor>();
    param_.output = scope->FindVar(out)->GetMutable<Tensor>();
    param_.in_num_col_dims =
S
superjomn 已提交
56 57 58
        boost::get<int>(op_desc.GetAttr("in_num_col_dims"));

    kernel_->SetParam(param_);
S
superjomn 已提交
59 60

    return true;
S
superjomn 已提交
61 62 63 64
  }

  std::string DebugString() const override { return "fc"; }

S
superjomn 已提交
65 66 67 68
  void StaticPickKernel(const std::vector<Place> &valid_targets) override {
    auto kernels = CreateKernels(valid_targets);
    kernel_ = std::move(kernels.front());
  }
S
superjomn 已提交
69 70 71 72 73 74 75 76

 private:
  mutable FcParam param_;
};

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