Dragon - C++ API
A Computation Graph Virtual Machine Based Deep Learning Framework
eltwise_op.h
Go to the documentation of this file.
1 
13 #ifndef DRAGON_OPERATORS_ARITHMETIC_ELTWISE_OP_H_
14 #define DRAGON_OPERATORS_ARITHMETIC_ELTWISE_OP_H_
15 
16 #include "core/operator.h"
17 
18 namespace dragon {
19 
20 template <class Context>
21 class EltwiseOp final : public Operator<Context> {
22  public:
23  EltwiseOp(const OperatorDef& def, Workspace* ws)
24  : Operator<Context>(def, ws),
25  coef_(OpArgs<float>("coef")),
26  operation_(OpArg<string>("operation", "SUM")) {
27  // Check the number of coeffients
28  if (coef_.size() > 0) {
29  CHECK_EQ(coef_.size(), XSize())
30  << "\nOp has " << XSize() << " inputs, "
31  << "while providing " << coef_.size() << " coefs.";
32  } else {
33  coef_.resize((size_t)XSize(), 1.f);
34  }
35  // Compute the alpha for product operation
36  for (auto e : coef_) { if (e != 1.f) alpha_ *= e; }
37  }
39 
40  void RunOnDevice() override;
41  template <typename T> void RunImpl();
42  template <typename T> void SumRunImpl();
43  template <typename T> void ProdRunImpl();
44 
45  protected:
46  string operation_;
47  float alpha_ = 1.f;
48  vector<float> coef_;
49 };
50 
51 template <class Context>
52 class EltwiseGradientOp final : public Operator<Context> {
53  public:
54  EltwiseGradientOp(const OperatorDef& def, Workspace* ws)
55  : Operator<Context>(def, ws),
56  coef_(OpArgs<float>("coef")),
57  operation_(OpArg<string>("operation", "SUM")) {
58  if (coef_.size() > 0) {
59  CHECK_EQ(coef_.size(), YSize())
60  << "\nOp has " << YSize() << " inputs, "
61  << "while providing " << coef_.size() << " coefs.";
62  } else {
63  coef_.resize(YSize(), 1.f);
64  }
65  // Compute the alpha for product operation
66  for (auto e : coef_) { if (e != 1.f) alpha_ *= e; }
67  }
69 
70  void RunOnDevice() override;
71  template <typename T> void RunImpl();
72  template <typename T> void SumRunImpl();
73  template <typename T> void ProdRunImpl();
74 
75  protected:
76  string operation_;
77  float alpha_ = 1.f;
78  vector<float> coef_;
79 };
80 
81 } // namespace dragon
82 
83 #endif // DRAGON_OPERATORS_ARITHMETIC_ELTWISE_OP_H_
vector< float > coef_
Definition: eltwise_op.h:48
int YSize()
Return the number of outputs.
Definition: operator.h:49
void RunOnDevice() override
Implement the detailed execution.
Definition: eltwise_op.cc:45
void SumRunImpl()
Definition: eltwise_op.cc:7
Definition: workspace.h:20
void RunOnDevice() override
Implement the detailed execution.
Definition: eltwise_op.cc:119
float alpha_
Definition: eltwise_op.h:47
EltwiseOp(const OperatorDef &def, Workspace *ws)
Definition: eltwise_op.h:23
void RunImpl()
Definition: eltwise_op.cc:38
const OperatorDef & def() const
Return the stored def.
Definition: operator.h:114
Workspace * ws() const
Return the parent workspace.
Definition: operator.h:87
void RunImpl()
Definition: eltwise_op.cc:104
vector< float > coef_
Definition: eltwise_op.h:78
USE_OPERATOR_FUNCTIONS
Definition: eltwise_op.h:68
USE_OPERATOR_FUNCTIONS
Definition: eltwise_op.h:38
Definition: eltwise_op.h:21
#define OpArgs
Definition: operator.h:236
void SumRunImpl()
Definition: eltwise_op.cc:62
Definition: operator.h:149
#define OpArg
Definition: operator.h:235
int XSize()
Return the number of inputs.
Definition: operator.h:46
void ProdRunImpl()
Definition: eltwise_op.cc:75
string operation_
Definition: eltwise_op.h:46
void ProdRunImpl()
Definition: eltwise_op.cc:18
float alpha_
Definition: eltwise_op.h:77
string operation_
Definition: eltwise_op.h:76
Definition: common.h:41
Definition: eltwise_op.h:52
EltwiseGradientOp(const OperatorDef &def, Workspace *ws)
Definition: eltwise_op.h:54
#define CHECK_EQ(val1, val2)
Definition: logging.h:48