“ad0c144e5159bf76d532e9196afd10d3c5137c56”上不存在“paddle/legacy/math/Matrix.cpp”
fetch_op.cc 3.0 KB
Newer Older
Q
qijun 已提交
1 2
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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

Y
Yu Yang 已提交
15 16
#include "paddle/framework/feed_fetch_type.h"
#include "paddle/framework/op_registry.h"
D
dzhwinter 已提交
17
#include "paddle/platform/device_context.h"
Q
qijun 已提交
18 19 20 21

namespace paddle {
namespace operators {

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

Y
Yu Yang 已提交
29
  void Run(const framework::Scope &scope,
D
dzhwinter 已提交
30
           const platform::Place &place) const override {
Y
Yu Yang 已提交
31
    auto fetch_var_name = Input("X");
Y
Yu Yang 已提交
32 33 34 35
    auto *fetch_var = scope.FindVar(fetch_var_name);
    PADDLE_ENFORCE(fetch_var != nullptr,
                   "Cannot find fetch variable in scope, fetch_var_name is %s",
                   fetch_var_name);
Q
qijun 已提交
36

Y
Yu Yang 已提交
37 38 39 40 41
    auto out_name = this->Output("Out");
    auto *out_var = scope.FindVar(out_name);
    PADDLE_ENFORCE(out_var != nullptr,
                   "Cannot find out_var in scope, out_var_name is %s",
                   out_name);
Q
qijun 已提交
42

Y
Yu Yang 已提交
43 44 45 46 47 48 49 50 51 52 53 54
    auto col = static_cast<size_t>(Attr<int>("col"));

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

    if (col >= fetch_list->size()) {
      fetch_list->resize(col + 1);
    }
    auto &dst_item = fetch_list->at(col);

    // FIXME(yuyang18): Should we assume the fetch operator always generate
    // CPU outputs?
Y
Yu Yang 已提交
55
    platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
Q
qiaolongfei 已提交
56
    auto &dev_ctx = *pool.Get(src_item.place());
D
dzhwinter 已提交
57

58
    Copy(src_item, platform::CPUPlace(), dev_ctx, &dst_item);
Y
Yu Yang 已提交
59
    dev_ctx.Wait();
60
    dst_item.set_lod(src_item.lod());
Y
Yu Yang 已提交
61 62

    VLOG(3) << "Fetch variable " << fetch_var_name << " to " << out_name;
Q
qijun 已提交
63 64 65
  }
};

Y
Yu Yang 已提交
66 67
class FetchOpInfoMaker : public framework::OpProtoAndCheckerMaker {
 public:
68
  FetchOpInfoMaker(OpProto *proto, OpAttrChecker *op_checker)
Y
Yu Yang 已提交
69 70 71
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("X", "The input of fetch op");
    AddOutput("Out", "The output of fetch op");
K
kexinzhao 已提交
72 73 74 75 76 77 78
    AddAttr<int>("col", "(int) The column of fetch");
    AddComment(R"DOC(
Fetch Operator.

It should not be configured by users directly.

)DOC");
Y
Yu Yang 已提交
79 80
  }
};
Q
qijun 已提交
81 82 83
}  // namespace operators
}  // namespace paddle

Y
Yu Yang 已提交
84
REGISTER_OPERATOR(fetch, paddle::operators::FetchOp,
Y
Yu Yang 已提交
85 86
                  paddle::framework::EmptyGradOpMaker,
                  paddle::operators::FetchOpInfoMaker);