fc_op.h 3.5 KB
Newer Older
M
mozga-intel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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. */

#pragma once

17 18
#include <string>
#include <vector>
M
mozga-intel 已提交
19
#include "paddle/fluid/framework/op_registry.h"
20
#include "paddle/fluid/operators/math/fc.h"
M
mozga-intel 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

class FCOpGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override;

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override;
};

L
luotao1 已提交
38 39 40
inline void FCOutputSize(const framework::DDim& in_dims,
                         const framework::DDim& w_dims,
                         std::vector<int64_t>& out_dims,  // NOLINT
41
                         int in_num_col_dims, bool padding_weights) {
L
luotao1 已提交
42
  auto in_mat_dims = framework::flatten_to_2d(in_dims, in_num_col_dims);
43 44 45 46 47 48 49
  auto w_dims0 = padding_weights ? w_dims[0] - 4 : w_dims[0];
  auto w_dims1 = padding_weights ? w_dims[1] - 4 : w_dims[1];
  PADDLE_ENFORCE_EQ(in_mat_dims[1], w_dims0,
                    platform::errors::InvalidArgument(
                        "Fully Connected input and weigth size do not match. "
                        "input width: %d,weight height: %d",
                        in_mat_dims[1], w_dims0));
L
luotao1 已提交
50 51 52 53 54

  out_dims.reserve(static_cast<size_t>(in_num_col_dims + 1));
  for (int i = 0; i < in_num_col_dims; ++i) {
    out_dims.push_back(in_dims[i]);
  }
55
  out_dims.push_back(w_dims1);
L
luotao1 已提交
56 57
}

58 59 60 61 62 63 64 65 66 67 68 69 70
template <typename DeviceContext, typename T>
class FCOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const paddle::framework::ExecutionContext& ctx) const override {
    auto* input = ctx.Input<framework::LoDTensor>("Input");
    auto* w = ctx.Input<Tensor>("W");
    auto* bias = ctx.Input<Tensor>("Bias");
    auto* output = ctx.Output<framework::LoDTensor>("Out");
    int in_num_col_dims = ctx.Attr<int>("in_num_col_dims");
    bool with_relu =
        (ctx.Attr<std::string>("activation_type") == "relu") ? true : false;

    auto w_dims = w->dims();
71
    bool padding_weights = ctx.Attr<bool>("padding_weights");
72 73

    std::vector<int64_t> output_dims;
74 75
    FCOutputSize(input->dims(), w_dims, output_dims, in_num_col_dims,
                 padding_weights);
76 77 78 79
    output->Resize(framework::make_ddim(output_dims));
    output->set_lod(input->lod());

    auto out_dims = output->dims();
80 81 82
    auto w_dims0 = padding_weights ? w_dims[0] - 4 : w_dims[0];
    auto w_dims1 = padding_weights ? w_dims[1] - 4 : w_dims[1];
    int M = framework::product(out_dims) / w_dims1;
83 84 85 86 87 88 89

    const T* input_data = input->data<T>();
    const T* w_data = w->data<T>();
    T* output_data = output->mutable_data<T>(ctx.GetPlace());

    auto& dev_ctx = ctx.template device_context<DeviceContext>();
    math::FCFunctor<DeviceContext, T> fc;
90 91
    fc(dev_ctx, M, w_dims1, w_dims0, input_data, w_data, output_data,
       bias ? bias->data<T>() : NULL, with_relu, padding_weights);
92 93 94
  }
};

M
mozga-intel 已提交
95 96
}  // namespace operators
}  // namespace paddle