enqueue_op.cc 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.

#include <string>
W
wanghuancoder 已提交
16

17 18 19
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
W
wanghuancoder 已提交
20
#include "paddle/fluid/framework/var_type_traits.h"
21 22
#include "paddle/fluid/operators/reader/lod_tensor_blocking_queue.h"

W
wanghuancoder 已提交
23 24 25 26 27 28 29 30 31 32 33
namespace paddle {
namespace framework {
class OpDesc;
template <typename T>
class EmptyGradOpMaker;
}  // namespace framework
namespace imperative {
class OpBase;
}  // namespace imperative
}  // namespace paddle

34 35 36 37 38 39 40 41 42
using LoDTensor = paddle::framework::LoDTensor;
using LoDTensorBlockingQueueHolder =
    paddle::operators::reader::LoDTensorBlockingQueueHolder;

namespace paddle {
namespace operators {

class EnqueueOp : public framework::OperatorBase {
 public:
43 44
  EnqueueOp(const std::string& type,
            const framework::VariableNameMap& inputs,
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
            const framework::VariableNameMap& outputs,
            const framework::AttributeMap& attrs)
      : OperatorBase(type, inputs, outputs, attrs) {}

 private:
  void RunImpl(const framework::Scope& scope,
               const platform::Place& dev_place) const override {
    const std::string& queue_name = Attr<std::string>("queue_name");
    auto* queue_holder_var = scope.FindVar(queue_name);
    PADDLE_ENFORCE_NOT_NULL(
        queue_holder_var,
        platform::errors::NotFound(
            "No LoDTensorBlockingQueueHolder variable with name %s found.",
            queue_name));
    const std::string& var_name = Input("X");
    auto* in_var = scope.FindVar(var_name);
61 62 63
    PADDLE_ENFORCE_NOT_NULL(in_var,
                            platform::errors::NotFound(
                                "No variable with name %s found.", var_name));
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    auto* in_tensor = in_var->GetMutable<LoDTensor>();
    auto* queue_holder =
        queue_holder_var->template GetMutable<LoDTensorBlockingQueueHolder>();

    std::vector<LoDTensor> lod_tensor_vec;
    lod_tensor_vec.emplace_back(*in_tensor);
    queue_holder->GetQueue()->Push(lod_tensor_vec);
  }
};

class EnqueueOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "`lod_tensor` to enqueue");
    AddAttr<std::string>("queue_name",
                         "Name of the `LoDTensorBlockingQueueHolder` variable");
    AddComment(R"DOC(
			Enqueue operator.
      )DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = ::paddle::operators;

REGISTER_OP_WITHOUT_GRADIENT(enqueue, ops::EnqueueOp, ops::EnqueueOpMaker);