cond_op.h 2.3 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
class CondOp : public framework::OperatorBase {
Z
cond op  
zchen0211 已提交
28
 public:
Z
zchen0211 已提交
29 30 31
  CondOp(const std::string& type, const framework::VariableNameMap& inputs,
         const framework::VariableNameMap& outputs,
         const framework::AttributeMap& attrs)
Z
cond op  
zchen0211 已提交
32 33 34 35 36 37 38 39 40 41 42 43
      : 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 已提交
44
  void CreateScope(const framework::Scope& scope) const;
Z
cond op  
zchen0211 已提交
45

Z
zchen0211 已提交
46
  void CreateIndexTensor(const framework::Scope& scope) const;
Z
cond op  
zchen0211 已提交
47 48 49 50

  /**
   * InferShape must be called before Run.
   */
Z
zchen0211 已提交
51
  void InferShape(const framework::Scope& scope) const override;
Z
cond op  
zchen0211 已提交
52 53 54 55 56 57 58 59 60 61 62 63

  // Set True Block
  void set_truenet(std::unique_ptr<OperatorBase> net) {
    sub_net_op_[0] = std::move(net);
  }

  // Set False Block
  void set_falsenet(std::unique_ptr<OperatorBase> net) {
    sub_net_op_[1] = std::move(net);
  }

  void Run(const framework::Scope& scope,
Z
zchen0211 已提交
64
           const platform::DeviceContext& dev_ctx) const override;
Z
cond op  
zchen0211 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77

 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