cond_op.h 3.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Z
cond op  
zchen0211 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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
S
Siddharth Goyal 已提交
16
#include <string>
Z
cond op  
zchen0211 已提交
17 18
#include <vector>
#include "glog/logging.h"
Y
Yi Wang 已提交
19 20 21 22 23
#include "paddle/fluid/framework/ddim.h"
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/operators/net_op.h"
Z
cond op  
zchen0211 已提交
24 25 26 27

namespace paddle {
namespace operators {

Z
zchen0211 已提交
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
      : OperatorBase(type, inputs, outputs, attrs) {
44
    sub_net_op_.resize(BRANCH_NUM);
Z
cond op  
zchen0211 已提交
45 46 47 48 49 50 51 52 53
  }

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

54 55 56
  framework::Scope& AddSubScope(const framework::Scope& scope) const;
  std::vector<framework::Scope*>& GetSubScopes(
      const framework::Scope& scope) const;
Z
cond op  
zchen0211 已提交
57

58 59 60
  framework::LoDTensor& AddIndexTensor(const framework::Scope& scope) const;
  std::vector<framework::LoDTensor>& GetIndexTensors(
      const framework::Scope& scope) const;
Z
cond op  
zchen0211 已提交
61

62 63 64 65
  void PrepareDataForSubnet(const framework::Scope& scope,
                            const platform::DeviceContext& dev_ctx) const;
  void MergeDataFromSubnet(const framework::Scope& scope,
                           const platform::DeviceContext& dev_ctx) const;
Z
cond op  
zchen0211 已提交
66

Z
zchen0211 已提交
67 68 69
  /*
   * Set True Block
   */
Z
zchen0211 已提交
70
  void set_truenet(std::unique_ptr<OperatorBase>&& net) {
71
    sub_net_op_[TRUE_BRANCH] = std::move(net);
Z
cond op  
zchen0211 已提交
72 73
  }

Z
zchen0211 已提交
74 75 76
  /*
   * Set False Block
   */
Z
zchen0211 已提交
77
  void set_falsenet(std::unique_ptr<OperatorBase>&& net) {
78
    sub_net_op_[FALSE_BRANCH] = std::move(net);
Z
cond op  
zchen0211 已提交
79 80
  }

81 82 83
 private:
  void RunImpl(const framework::Scope& scope,
               const platform::Place& place) const override;
Z
cond op  
zchen0211 已提交
84 85

 private:
86 87 88 89
  const int TRUE_BRANCH = 0;
  const int FALSE_BRANCH = 1;
  const int BRANCH_NUM = 2;

Z
cond op  
zchen0211 已提交
90 91 92 93 94 95 96
  // sub_net_op_[0]: subnet_t
  // sub_net_op_[1]: subnet_f
  std::vector<std::unique_ptr<framework::OperatorBase>> sub_net_op_;
};

}  // namespace operators
}  // namespace paddle