DLPrimitives
elementwise.hpp
1 #pragma once
2 #include <dlprim/operator.hpp>
3 namespace dlprim {
4  namespace json { class value; }
5  namespace core {
6  class PointwiseOperationBroadcastReduce;
7  }
9  enum Operation {
10  elementwise_sum,
11  elementwise_prod,
12  elementwise_max
13  };
14 
15  Operation op = elementwise_sum;
16  float coeff[2] = {1.0f,1.0f};
17  StandardActivations activation = StandardActivations::identity;
18 
19  static ElementwiseConfig from_json(json::value const &v);
20  };
21 
22  class Elementwise : public Operator {
23  public:
24 
26  virtual ~Elementwise();
27 
28  virtual char const *operator_type() const
29  {
30  return "Elementwise";
31  }
32 
33  virtual void setup(std::vector<TensorSpecs> const &in,
34  std::vector<TensorSpecs> &out,
35  std::vector<TensorSpecs> &parameters,
36  size_t &workspace);
37 
38  virtual void reshape(std::vector<Shape> const &in,
39  std::vector<Shape> &out,
40  size_t &ws);
41 
42  virtual void forward(std::vector<Tensor> &input,
43  std::vector<Tensor> &output,
44  std::vector<Tensor> &parameters,
45  Tensor &workspace,
46  ExecutionContext const &ctx);
47 
48  virtual void backward(std::vector<TensorAndGradient> &input,
49  std::vector<TensorAndGradient> &output,
50  std::vector<TensorAndGradient> &parameters,
51  Tensor &workspace,
52  ExecutionContext const &ctx);
53 
54 
55 
56  private:
57  void forward_gpu(Tensor &a,Tensor &b,Tensor &output,ExecutionContext const &ctx);
58  void forward_cpu(Tensor &a,Tensor &b,Tensor &output);
59 
60  template<int index>
61  struct StridePos;
62 
63  template<int dim,typename F>
64  void loop_strides_dim(Shape s,float *a,Shape a_strides,float *b,Shape b_strides,float *c,F const &func);
65 
66  template<typename F>
67  void loop_strides(Shape s,float *a,Shape a_strides,float *b,Shape b_strides,float *c,F const &func);
68 
69  template<typename F,typename R>
70  void loops_reduce(Shape s,float *a,Shape as,float *r,Shape rs,F const &func,R const &reduce);
71  template<typename F,typename R>
72  void loops_reduce(Shape s,float *a,Shape as,float *b,Shape bs,float *r,Shape rs,F const &func,R const &reduce);
73  template<typename F,typename R>
74  void loops_reduce(Shape s,float *a,Shape as,float *b,Shape bs,float *c,Shape cs,float *r,Shape rs,F const &func,R const &reduce);
75 
76 
77  template<int dim,typename F,typename R>
78  void loops_reduce_dim(Shape s,float *a,Shape as,float *r,Shape rs,F const &func,R const &reduce);
79  template<int dim,typename F,typename R>
80  void loops_reduce_dim(Shape s,float *a,Shape as,float *b,Shape bs,float *r,Shape rs,F const &func,R const &reduce);
81  template<int dim,typename F,typename R>
82  void loops_reduce_dim(Shape s,float *a,Shape as,float *b,Shape bs,float *c,Shape cs,float *r,Shape rs,F const &func,R const &reduce);
83 
84 
85 
86 
87  void backward_cpu(Tensor &a,Tensor &da,
88  Tensor &b,Tensor &db,
89  Tensor &c,Tensor &dc,
90  bool l,bool r,
91  float ba,float bb);
92 
93  void backward_gpu(Tensor &a,Tensor &da,
94  Tensor &b,Tensor &db,
95  Tensor &c,Tensor &dc,
96  Tensor &ws,
97  bool l,bool r,
98  float ba,float bb,
99  ExecutionContext const &e);
100  void setup_bwd_gpu(std::vector<TensorSpecs> const &in,std::vector<TensorSpecs> &out,size_t &ws);
101 
102  ElementwiseConfig config_;
103  DataType dtype_;
104  std::unique_ptr<core::PointwiseOperationBroadcastReduce> bwd_l_,bwd_r_,bwd_both_;
105  };
106 } // namespace
107 
Tensor shape.
Definition: shape.hpp:18
virtual char const * operator_type() const
name of the operator type
Definition: elementwise.hpp:28
Definition: elementwise.hpp:8
Base class for backward/forward propogation calculations for internal network.
Definition: operator.hpp:15
This is main object that represent the pair of OpenCL platform and device all other objects use it...
Definition: context.hpp:302
DataType
type definition
Definition: definitions.hpp:70
This class is central representation of json objects.
Definition: json.hpp:652
Mane namespace.
Definition: context.hpp:9
Central Data Contrainer - Tensor.
Definition: tensor.hpp:99
StandardActivations
Parameterless Activations that can be embedded to general kernels like inner product or convolution...
Definition: definitions.hpp:266
Definition: elementwise.hpp:22
This class is used to pass cl::Events that the kernel should wait for and/or signal event completion...
Definition: context.hpp:121