Caffe2 - C++ API
A deep learning, cross platform ML framework
fully_connected_fake_lowp_op.h
1 
17 #pragma once
18 
19 #include <immintrin.h>
20 #include "caffe2/core/context.h"
21 #include "caffe2/core/operator.h"
22 #include "caffe2/utils/conversions.h"
23 #include "caffe2/utils/math.h"
24 
25 namespace caffe2 {
26 
27 // convert to float16 reducing mantissa, preserving exponent
28 void fp32_to_bfp16(const float* source, size_t size, float* dest);
29 
30 // convert to float24 reducing mantissa, preserving exponent
31 void fp32_to_bfp24(const float* source, size_t size, float* dest);
32 
33 // convert to float14 reducing mantissa, preserving exponent
34 void fp32_to_bfp14(const float* source, size_t size, float* dest);
35 
36 void fp32_to_bfp16_scalar(const float* source, size_t size, float* dest);
37 
38 // convert to IEEE float16
39 void fp32_to_fp16(const float* source, size_t size, float* dest);
40 
41 // fp32 -> int32 -> += 1<< 15 -> fp32 -> truncation
42 void fp32_to_bfp16_round(const float* source, size_t size, float* dest);
43 
44 // This is Caffe's InnerProductOp, with a name that fits its purpose better.
45 template <
46  void (*Q)(const float*, size_t, float*),
47  class Context,
48  class Engine = DefaultEngine,
49  bool TransposeWeight = true>
51  public:
52  USE_OPERATOR_CONTEXT_FUNCTIONS;
53  FullyConnectedFakeLowpFPOp(const OperatorDef& operator_def, Workspace* ws)
54  : Operator<Context>(operator_def, ws),
55  axis_(this->template GetSingleArgument<int32_t>("axis", 1)),
56  axis_w_(this->template GetSingleArgument<int32_t>("axis_w", 1)),
57  float16_compute_(
58  this->template GetSingleArgument<bool>("float16_compute", false)) {}
60 
61  template <
62  typename T_X,
63  typename T_W,
64  typename T_B,
65  typename T_Y,
66  typename MATH>
67  bool DoRunWithType();
68 
69  bool RunOnDevice() override {
70  return DoRunWithType<
71  float, // X
72  float, // W
73  float, // B
74  float, // Y
75  float>(); // Math
76  }
77 
78  protected:
79  size_t axis_{1};
80  size_t axis_w_{1};
81  // A local vector to cache the output shape so we don't need to recreate
82  // a vector object every time we run Run().
83  vector<int64_t> Y_shape_cache_;
84  Tensor bias_multiplier_;
85 
86  bool float16_compute_;
87 };
88 
89 template <
90  void (*Q)(const float*, size_t, float*),
91  class Context,
92  class Engine = DefaultEngine,
93  bool TransposeWeight = true>
95  public:
96  USE_OPERATOR_CONTEXT_FUNCTIONS;
98  const OperatorDef& operator_def,
99  Workspace* ws)
100  : Operator<Context>(operator_def, ws),
101  axis_(this->template GetSingleArgument<int32_t>("axis", 1)),
102  axis_w_(this->template GetSingleArgument<int32_t>("axis_w", 1)),
103  float16_compute_(
104  this->template GetSingleArgument<bool>("float16_compute", false)) {}
106 
107  template <
108  typename T_X,
109  typename T_W,
110  typename T_DY,
111  typename T_B,
112  typename T_DX,
113  typename T_DW,
114  typename T_DB,
115  typename MATH>
116  bool DoRunWithType();
117 
118  bool RunOnDevice() override {
119  return DoRunWithType<
120  float, // X
121  float, // W
122  float, // dY
123  float, // B
124  float, // dX
125  float, // dW
126  float, // dB
127  float>(); // Math
128  }
129 
130  protected:
131  size_t axis_{1};
132  size_t axis_w_{1};
133  Tensor bias_multiplier_;
134  bool float16_compute_;
135 };
136 
137 } // namespace caffe2
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:47
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13