DLPrimitives
pointwise.hpp
1 #pragma once
2 #include <dlprim/operator.hpp>
3 namespace dlprim {
4 
5  class PointwiseBase : public Operator {
6  public:
7 
9  virtual ~PointwiseBase();
10 
11  virtual void setup(std::vector<TensorSpecs> const &in,
12  std::vector<TensorSpecs> &out,
13  std::vector<TensorSpecs> &parameters,
14  size_t &workspace);
15 
16 
17  virtual void reshape(std::vector<Shape> const &in,
18  std::vector<Shape> &out,
19  size_t &ws);
20 
21  virtual void forward(std::vector<Tensor> &input,
22  std::vector<Tensor> &output,
23  std::vector<Tensor> &parameters,
24  Tensor &ws,
25  ExecutionContext const &q);
26 
27  virtual void backward(std::vector<TensorAndGradient> &input,
28  std::vector<TensorAndGradient> &output,
29  std::vector<TensorAndGradient> &parameters,
30  Tensor &workspace,
31  ExecutionContext const &ctx);
32  protected:
33  virtual void forward_cpu(Tensor &x,Tensor &y);
34  virtual void backward_cpu(Tensor &x,Tensor &dx,Tensor &y,Tensor &dy,float beta);
35 
36  virtual void forward_gpu(Tensor &x,Tensor &y,ExecutionContext const &q) = 0;
37  virtual void backward_gpu(Tensor &x,Tensor &dx,Tensor &y,Tensor &dy,float beta,ExecutionContext const &q) = 0;
38  virtual void forward_cpu_float(size_t n,float const *x,float *y) = 0;
39  virtual void backward_cpu_float(size_t n,float const *x,float *dx,float const *y,float const *dy,float beta) = 0;
40  };
41 
42  struct ThresholdConfig {
43  float threshold = 0.0f;
44  static ThresholdConfig from_json(json::value const &v);
45  };
46 
47  class Threshold : public PointwiseBase {
48  public:
49  Threshold(Context &ctx,ThresholdConfig const &cfg = ThresholdConfig()) :
50  PointwiseBase(ctx),
51  cfg_(cfg)
52  {
53  }
54  virtual char const *operator_type() const { return "Threshold"; }
55  virtual void forward_cpu_float(size_t n,float const *x,float *y);
56  virtual void backward_cpu_float(size_t n,float const *x,float *dx,float const *y,float const *dy,float beta);
57  virtual void forward_gpu(Tensor &x,Tensor &y,ExecutionContext const &q);
58  virtual void backward_gpu(Tensor &x,Tensor &dx,Tensor &y,Tensor &dy,float beta,ExecutionContext const &q);
59  private:
60  ThresholdConfig cfg_;
61  };
62 
63  struct HardtanhConfig {
64  float min_val = -1.0f;
65  float max_val = 1.0f;
66  static HardtanhConfig from_json(json::value const &v);
67  };
68 
69  class Hardtanh : public PointwiseBase {
70  public:
71  Hardtanh(Context &ctx,HardtanhConfig const &cfg = HardtanhConfig()) :
72  PointwiseBase(ctx),
73  cfg_(cfg)
74  {
75  }
76  virtual char const *operator_type() const { return "Hardtanh"; }
77  virtual void forward_cpu_float(size_t n,float const *x,float *y);
78  virtual void backward_cpu_float(size_t n,float const *,float *,float const *,float const *,float beta);
79  virtual void forward_gpu(Tensor &x,Tensor &y,ExecutionContext const &q);
80  virtual void backward_gpu(Tensor &x,Tensor &dx,Tensor &y,Tensor &dy,float beta,ExecutionContext const &q);
81  private:
82  HardtanhConfig cfg_;
83  };
84 
85 
86 } // namespace
87 
Definition: pointwise.hpp:63
Definition: pointwise.hpp:47
Definition: pointwise.hpp:42
virtual void reshape(std::vector< Shape > const &in, std::vector< Shape > &out, size_t &ws)
Reshape layer according to new input size.
virtual char const * operator_type() const
name of the operator type
Definition: pointwise.hpp:54
virtual void setup(std::vector< TensorSpecs > const &in, std::vector< TensorSpecs > &out, std::vector< TensorSpecs > &parameters, size_t &workspace)
Convigure operator.
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
virtual void backward(std::vector< TensorAndGradient > &input, std::vector< TensorAndGradient > &output, std::vector< TensorAndGradient > &parameters, Tensor &workspace, ExecutionContext const &ctx)
Enqueue backward propogation computations.
Definition: pointwise.hpp:5
This class is central representation of json objects.
Definition: json.hpp:652
Definition: pointwise.hpp:69
Mane namespace.
Definition: context.hpp:9
Central Data Contrainer - Tensor.
Definition: tensor.hpp:99
virtual char const * operator_type() const
name of the operator type
Definition: pointwise.hpp:76
virtual void forward(std::vector< Tensor > &input, std::vector< Tensor > &output, std::vector< Tensor > &parameters, Tensor &ws, ExecutionContext const &q)
Enqueue forward propogation computations.
This class is used to pass cl::Events that the kernel should wait for and/or signal event completion...
Definition: context.hpp:121