cond_op.h 2.5 KB
Newer Older
Z
cond op  
zchen0211 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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
#include <vector>
#include "glog/logging.h"
#include "paddle/framework/ddim.h"
#include "paddle/framework/eigen.h"
#include "paddle/framework/operator.h"
#include "paddle/framework/tensor.h"
Z
zchen0211 已提交
22
#include "paddle/operators/net_op.h"
Z
cond op  
zchen0211 已提交
23 24 25 26

namespace paddle {
namespace operators {

Z
zchen0211 已提交
27 28 29 30 31 32 33 34 35 36 37
/*
 * @brief CondOp is a dynamic if-else Operator
 *
 * It has a input tensor named cond indicating which netop each instance will
 * run.
 *
 * if cond == 1, it will run true_net, which is a NetOp.
 *
 * if cond == 0, it will run false_net, which is another NetOp.
 */

Z
zchen0211 已提交
38
class CondOp : public framework::OperatorBase {
Z
cond op  
zchen0211 已提交
39
 public:
Z
zchen0211 已提交
40 41 42
  CondOp(const std::string& type, const framework::VariableNameMap& inputs,
         const framework::VariableNameMap& outputs,
         const framework::AttributeMap& attrs)
Z
cond op  
zchen0211 已提交
43 44 45 46 47 48 49 50 51 52 53 54
      : OperatorBase(type, inputs, outputs, attrs) {
    index_.resize(2);
    sub_net_op_.resize(2);
  }

  CondOp(const CondOp& o)
      : framework::OperatorBase(
            static_cast<const framework::OperatorBase&>(o)) {
    // TODO(yuyang18): Implement copy ctor well.
    PADDLE_THROW("Not implemented");
  }

Z
zchen0211 已提交
55
  void CreateScope(const framework::Scope& scope) const;
Z
cond op  
zchen0211 已提交
56

Z
zchen0211 已提交
57
  void CreateIndexTensor(const framework::Scope& scope) const;
Z
cond op  
zchen0211 已提交
58

Z
zchen0211 已提交
59
  /*
Z
cond op  
zchen0211 已提交
60 61
   * InferShape must be called before Run.
   */
Z
zchen0211 已提交
62
  void InferShape(const framework::Scope& scope) const override;
Z
cond op  
zchen0211 已提交
63 64

  // Set True Block
Z
zchen0211 已提交
65
  void set_truenet(std::unique_ptr<OperatorBase>&& net) {
Z
cond op  
zchen0211 已提交
66 67 68 69
    sub_net_op_[0] = std::move(net);
  }

  // Set False Block
Z
zchen0211 已提交
70
  void set_falsenet(std::unique_ptr<OperatorBase>&& net) {
Z
cond op  
zchen0211 已提交
71 72 73 74
    sub_net_op_[1] = std::move(net);
  }

  void Run(const framework::Scope& scope,
Z
zchen0211 已提交
75
           const platform::DeviceContext& dev_ctx) const override;
Z
cond op  
zchen0211 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88

 private:
  // sub_net_op_[0]: subnet_t
  // sub_net_op_[1]: subnet_f
  std::vector<std::unique_ptr<framework::OperatorBase>> sub_net_op_;

  // index_[0]: True_index;
  // index_[1]: False_index;
  mutable std::vector<std::vector<int>> index_;
};

}  // namespace operators
}  // namespace paddle