DLPrimitives
softmax.hpp
1 #pragma once
2 #include <dlprim/operator.hpp>
3 
4 namespace dlprim {
5  namespace json { class value; }
6  class Scal;
7 
8  struct SoftmaxConfig {
9  bool log=false;
10  static SoftmaxConfig from_json(json::value const &v);
11  };
12 
13  class SoftmaxBase {
14  public:
15 
16  protected:
17  bool setup_kernel_params(int sm_range);
18 
19  int wg_size_ = 0;
20  int items_per_wi_ = 0;
21  int sm_range_ = -1;
22  int nd_range_;
23  };
24 
25  class Softmax : public Operator, public SoftmaxBase {
26  public:
27  Softmax(Context &ctx,SoftmaxConfig const &cfg=SoftmaxConfig());
28  virtual ~Softmax();
29 
30  virtual char const *operator_type() const
31  {
32  return "Softmax";
33  }
34 
35  virtual void setup(std::vector<TensorSpecs> const &in,
36  std::vector<TensorSpecs> &out,
37  std::vector<TensorSpecs> &parameters,
38  size_t &workspace);
39 
40  virtual void reshape(std::vector<Shape> const &in,
41  std::vector<Shape> &out,
42  size_t &ws);
43 
44  virtual void forward(std::vector<Tensor> &input,
45  std::vector<Tensor> &output,
46  std::vector<Tensor> &parameters,
47  Tensor &workspace,
48  ExecutionContext const &ctx);
49 
50  virtual void backward( std::vector<TensorAndGradient> &input,
51  std::vector<TensorAndGradient> &output,
52  std::vector<TensorAndGradient> &,
53  Tensor &,
54  ExecutionContext const &e);
55 
56 
57  private:
58  void forward_cpu(Tensor &input,Tensor &output);
59  void backward_cpu(Tensor &dx,Tensor &y,Tensor &dy,float accum);
60  SoftmaxConfig cfg_;
61  DataType dtype_;
62  };
63 
64  class SoftmaxWithLoss : public Operator, public SoftmaxBase {
65  public:
67  virtual ~SoftmaxWithLoss();
68 
69  virtual char const *operator_type() const
70  {
71  return "SoftmaxWithLoss";
72  }
73 
74  virtual void setup(std::vector<TensorSpecs> const &in,
75  std::vector<TensorSpecs> &out,
76  std::vector<TensorSpecs> &parameters,
77  size_t &workspace);
78 
79  virtual void reshape(std::vector<Shape> const &in,
80  std::vector<Shape> &out,
81  size_t &ws);
82 
83  virtual void forward(std::vector<Tensor> &input,
84  std::vector<Tensor> &output,
85  std::vector<Tensor> &parameters,
86  Tensor &workspace,
87  ExecutionContext const &ctx);
88 
89  virtual void backward( std::vector<TensorAndGradient> &input,
90  std::vector<TensorAndGradient> &output,
91  std::vector<TensorAndGradient> &,
92  Tensor &,
93  ExecutionContext const &e);
94 
95  private:
96  template<typename IndexType>
97  void forward_cpu_loss(Tensor &input,Tensor &label,Tensor &loss);
98  template<typename IndexType>
99  void backward_cpu_loss(Tensor &x,Tensor &dx,Tensor &label,Tensor &loss,float factor);
100  void backward_gpu_loss(Tensor &input,Tensor &diff, Tensor &label,Tensor &output,float factor, ExecutionContext const &ctx);
101  void forward_gpu_loss(Tensor &input,Tensor &label,Tensor &output,ExecutionContext const &ctx);
102  void setup_kernel(int sm_range);
103  DataType dtype_;
104  std::string itype_;
105  cl::Kernel kernel_,kernel_bwd_;
106  std::unique_ptr<Scal> scal_;
107  };
108 }
Definition: softmax.hpp:64
virtual char const * operator_type() const
name of the operator type
Definition: softmax.hpp:69
Definition: softmax.hpp:25
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
Definition: softmax.hpp:8
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
virtual char const * operator_type() const
name of the operator type
Definition: softmax.hpp:30
Definition: softmax.hpp:13
This class is used to pass cl::Events that the kernel should wait for and/or signal event completion...
Definition: context.hpp:121