DLPrimitives
batch_normalization.hpp
1 #pragma once
2 #include <dlprim/operator.hpp>
3 namespace dlprim {
4  namespace json { class value; }
5  namespace core { class BatchNormFwdBwd; }
6 
7  struct BatchNormConfig {
8  int features = -1;
9  float eps = 1e-5f;
10  float momentum = 0.1;
11  bool affine = true;
12  bool use_global_stats = false;
13  static BatchNormConfig from_json(json::value const &v);
14  };
15 
16 
17  class BatchNorm : public Operator {
18  public:
19 
20  BatchNorm(Context &ctx,BatchNormConfig const &config = BatchNormConfig(),DataType dtype=float_data);
21  virtual ~BatchNorm();
22 
23  virtual char const *operator_type() const
24  {
25  return "BatchNorm";
26  }
27 
28  virtual void initialize_params(std::vector<Tensor> &parameters,ExecutionContext const &e);
29  virtual void mode(CalculationsMode m);
30  virtual CalculationsMode mode() { return Operator::mode(); }
31 
32  virtual void setup(std::vector<TensorSpecs> const &in,
33  std::vector<TensorSpecs> &out,
34  std::vector<TensorSpecs> &parameters,
35  size_t &workspace);
36 
37  virtual void reshape(std::vector<Shape> const &in,
38  std::vector<Shape> &out,
39  size_t &ws);
40 
41  virtual void forward(std::vector<Tensor> &input,
42  std::vector<Tensor> &output,
43  std::vector<Tensor> &parameters,
44  Tensor &workspace,
45  ExecutionContext const &ctx);
46 
47  virtual void backward(std::vector<TensorAndGradient> &input,
48  std::vector<TensorAndGradient> &output,
49  std::vector<TensorAndGradient> &parameters,
50  Tensor &workspace,
51  ExecutionContext const &ctx);
52 
53 
54  private:
55  void backward_cpu(std::vector<TensorAndGradient> &input,
56  std::vector<TensorAndGradient> &output,
57  std::vector<TensorAndGradient> &parameters,
58  Tensor &workspace);
59  void forward_cpu(std::vector<Tensor> &input,
60  std::vector<Tensor> &output,
61  std::vector<Tensor> &parameters,
62  Tensor &workspace);
63  void cpu_backward_data(Tensor &x,Tensor &dx,Tensor &dy,float *mean,float *var,float *dy_sum,float *dyx_sum,float *gamma_in);
64  void cpu_forward_data(Tensor &x,Tensor &y,Tensor &scale,Tensor &offset);
65  void get_batch_stats(Tensor &x,Tensor &mean,Tensor &var);
66  void update_sums(int M,Tensor &cm,Tensor &cv,Tensor &sm,Tensor &sv);
67  void compute_conv_parameters(Tensor &mean,Tensor &var,Tensor *at,Tensor *bt);
68  template<bool CalcDX>
69  void cpu_backward(Tensor &xt,Tensor *dxt,Tensor &dyt,Tensor &scale,Tensor &dscale,Tensor &dbias,float dx_factor);
70  static int plane_size(Shape const &s);
71 
72  Tensor current_mean_,current_var_;
73  Tensor combined_scale_,combined_bias_;
74  BatchNormConfig config_;
75  DataType dtype_;
76  Shape setup_shape_;
77 
78  std::unique_ptr<core::BatchNormFwdBwd> bn_gpu_;
79  };
80 } //
Performs batch normalization computations over channel #1 (when #0 is batch)
Definition: bn.hpp:65
Tensor shape.
Definition: shape.hpp:18
CalculationsMode
Operation mode of layers - inference of training.
Definition: definitions.hpp:283
virtual CalculationsMode mode()
get current mode
Definition: batch_normalization.hpp:30
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
Definition: batch_normalization.hpp:7
virtual CalculationsMode mode()
get current mode
Definition: operator.hpp:62
Mane namespace.
Definition: context.hpp:9
Central Data Contrainer - Tensor.
Definition: tensor.hpp:99
Definition: batch_normalization.hpp:17
virtual char const * operator_type() const
name of the operator type
Definition: batch_normalization.hpp:23
This class is used to pass cl::Events that the kernel should wait for and/or signal event completion...
Definition: context.hpp:121