cond_op.h 2.9 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 16 17

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"
Y
Yi Wang 已提交
18 19 20 21 22
#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 已提交
23 24 25 26

namespace paddle {
namespace operators {

Z
zchen0211 已提交
27 28 29 30 31 32 33 34 35 36
/*
 * @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 已提交
37
class CondOp : public framework::OperatorBase {
Z
cond op  
zchen0211 已提交
38
 public:
Z
zchen0211 已提交
39 40 41
  CondOp(const std::string& type, const framework::VariableNameMap& inputs,
         const framework::VariableNameMap& outputs,
         const framework::AttributeMap& attrs)
Z
cond op  
zchen0211 已提交
42
      : OperatorBase(type, inputs, outputs, attrs) {
43
    sub_net_op_.resize(BRANCH_NUM);
Z
cond op  
zchen0211 已提交
44 45 46 47 48 49 50 51 52
  }

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

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

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

61 62 63 64
  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 已提交
65

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

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

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

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

Z
cond op  
zchen0211 已提交
89 90 91 92 93 94 95
  // 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