fetch_op.cc 4.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Q
qijun 已提交
2

L
Luo Tao 已提交
3 4 5
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
Q
qijun 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Q
qijun 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
Q
qijun 已提交
14

15
#include "paddle/fluid/framework/data_layout_transform.h"
Y
Yi Wang 已提交
16 17 18
#include "paddle/fluid/framework/feed_fetch_type.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/platform/device_context.h"
19
#include "paddle/fluid/platform/profiler.h"
Q
qijun 已提交
20 21 22 23

namespace paddle {
namespace operators {

Y
Yu Yang 已提交
24
class FetchOp : public framework::OperatorBase {
Q
qijun 已提交
25
 public:
Y
Yu Yang 已提交
26 27 28 29
  FetchOp(const std::string &type, const framework::VariableNameMap &inputs,
          const framework::VariableNameMap &outputs,
          const framework::AttributeMap &attrs)
      : OperatorBase(type, inputs, outputs, attrs) {}
Q
qijun 已提交
30

31 32 33
 private:
  void RunImpl(const framework::Scope &scope,
               const platform::Place &place) const override {
34 35 36
    OP_INOUT_CHECK(HasInputs("X"), "Input", "X", "Fetch");
    OP_INOUT_CHECK(HasOutputs("Out"), "Output", "Out", "Fetch");

Y
Yu Yang 已提交
37
    auto fetch_var_name = Input("X");
Y
Yu Yang 已提交
38
    auto *fetch_var = scope.FindVar(fetch_var_name);
39 40 41 42 43
    PADDLE_ENFORCE_NOT_NULL(
        fetch_var,
        platform::errors::NotFound(
            "Input variable(%s) cannot be found in scope for operator 'Fetch'.",
            fetch_var_name));
Q
qijun 已提交
44

45
    auto out_name = Output("Out");
Y
Yu Yang 已提交
46
    auto *out_var = scope.FindVar(out_name);
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    PADDLE_ENFORCE_NOT_NULL(out_var, platform::errors::NotFound(
                                         "Output variable(%s) cannot be found "
                                         "in scope for operator 'Fetch'.",
                                         out_name));

    int col = Attr<int>("col");
    PADDLE_ENFORCE_GE(
        col, 0, platform::errors::InvalidArgument(
                    "Expected the column index (the attribute 'col' of "
                    "operator 'Fetch') of current fetching variable to be "
                    "no less than 0. But received column index = %d.",
                    col));

    VLOG(3) << "Fetch variable " << fetch_var_name << " to variable "
            << out_name << "'s " << col << " column.";
Y
Yu Yang 已提交
62 63 64 65

    auto *fetch_list = out_var->GetMutable<framework::FeedFetchList>();
    auto &src_item = fetch_var->Get<framework::FeedFetchType>();

66
    if (static_cast<size_t>(col) >= fetch_list->size()) {
Y
Yu Yang 已提交
67 68 69 70 71 72
      fetch_list->resize(col + 1);
    }
    auto &dst_item = fetch_list->at(col);

    // FIXME(yuyang18): Should we assume the fetch operator always generate
    // CPU outputs?
Q
qingqing01 已提交
73
    if (src_item.IsInitialized() && src_item.numel() > 0) {
74
#ifdef PADDLE_WITH_MKLDNN
75 76 77
      // Conversion from MKL-DNN to Paddle
      if (src_item.layout() == framework::DataLayout::kMKLDNN) {
        framework::Tensor out;
78 79
        // Convert to desired Paddle layout, apart from grads of filter
        // as params are not a subject to paddle's data_format
80
        framework::innerTransDataLayoutFromMKLDNN(
81 82 83 84
            src_item.layout(),
            fetch_var_name == framework::GradVarName("Filter")
                ? framework::DataLayout::kNCHW
                : paddle::platform::get_cur_paddle_data_layout(),
85
            src_item, &out, platform::CPUPlace());
86 87 88 89
        TensorCopySync(out, platform::CPUPlace(), &dst_item);
      } else {
        TensorCopySync(src_item, platform::CPUPlace(), &dst_item);
      }
90 91 92
#else
      TensorCopySync(src_item, platform::CPUPlace(), &dst_item);
#endif
Q
qingqing01 已提交
93 94 95 96 97
    } else {
      // Not copy, if the src tensor is empty.
      dst_item.clear();
      dst_item.Resize({0});
    }
98
    dst_item.set_lod(src_item.lod());
Q
qijun 已提交
99 100 101
  }
};

Y
Yu Yang 已提交
102 103
class FetchOpInfoMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
104
  void Make() override {
105 106 107 108 109 110 111
    AddInput("X",
             "(LoDTensor) The resulted LoDTensor which is expected to return "
             "to users.");
    AddOutput("Out",
              "(vector<LoDTensor>) A fetching list of LoDTensor which may have "
              "different dimension, shape and data type.");
    AddAttr<int>("col", "(int) The column index of fetching object.");
K
kexinzhao 已提交
112 113 114 115 116 117
    AddComment(R"DOC(
Fetch Operator.

It should not be configured by users directly.

)DOC");
Y
Yu Yang 已提交
118 119
  }
};
Q
qijun 已提交
120 121 122
}  // namespace operators
}  // namespace paddle

H
hong 已提交
123 124 125 126 127
REGISTER_OPERATOR(
    fetch, paddle::operators::FetchOp,
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
    paddle::operators::FetchOpInfoMaker);