@@ -27,21 +27,21 @@ namespace paddle {
27
27
*
28
28
*/
29
29
template <>
30
- void CosSimForward<DEVICE_TYPE_CPU>(CpuMatrix* out_mat,
31
- const CpuMatrix* in1_mat,
32
- const CpuMatrix* in2_mat,
30
+ void CosSimForward<DEVICE_TYPE_CPU>(CpuMatrix& out_mat,
31
+ const CpuMatrix& in1_mat,
32
+ const CpuMatrix& in2_mat,
33
33
real scale) {
34
- CHECK (out_mat && in1_mat && in2_mat);
35
- size_t num_samples = out_mat-> getHeight ();
36
- size_t dim = in1_mat-> getWidth ();
34
+ CHECK (out_mat. getData () && in1_mat. getData () && in2_mat. getData () );
35
+ size_t num_samples = out_mat. getHeight ();
36
+ size_t dim = in1_mat. getWidth ();
37
37
// / column vector [nSamples, 1]
38
- real* out = out_mat-> getData ();
39
- const real* x = in1_mat-> getData ();
40
- const real* y = in2_mat-> getData ();
38
+ real* out = out_mat. getData ();
39
+ const real* x = in1_mat. getData ();
40
+ const real* y = in2_mat. getData ();
41
41
42
42
// / in2 might only have one row or full rows
43
- CHECK (in2_mat-> getHeight () == 1LU || in2_mat-> getHeight () == num_samples);
44
- size_t inc = (in2_mat-> getHeight () == 1LU) ? 0 : dim;
43
+ CHECK (in2_mat. getHeight () == 1LU || in2_mat. getHeight () == num_samples);
44
+ size_t inc = (in2_mat. getHeight () == 1LU) ? 0 : dim;
45
45
for (size_t i = 0 ; i < num_samples; ++i, x += dim, y += inc) {
46
46
real square_sum_x = 0 ;
47
47
real square_sum_y = 0 ;
@@ -75,26 +75,26 @@ class CosSimForwardFunc : public FunctionBase {
75
75
scale_ = config.get <real>(" scale" );
76
76
}
77
77
78
- void calc (const Arguments& inputs,
79
- const Arguments& outputs,
80
- const Arguments& inouts) override {
78
+ void calc (const BufferArgs& inputs, const BufferArgs& outputs) override {
81
79
CHECK_EQ (inputs.size (), 2 );
82
80
CHECK_EQ (outputs.size (), 1 );
83
- CHECK_EQ (inouts.size (), 0 );
84
81
85
- CHECK_EQ (inputs[0 ].dims_ [ 0 ], outputs[ 0 ]. dims_ [ 0 ] );
86
- CHECK_EQ (inputs[0 ]. dims_ [ 1 ], inputs[ 1 ]. dims_ [ 1 ] );
87
- CHECK_EQ (outputs[0 ].dims_ [ 1 ], 1UL );
82
+ CHECK_EQ (inputs[0 ].shape (). ndims (), ( size_t ) 2 );
83
+ CHECK_EQ (inputs[1 ]. shape (). ndims (), ( size_t ) 2 );
84
+ CHECK_EQ (outputs[0 ].shape (). ndims (), ( size_t ) 2 );
88
85
89
- CHECK (outputs[0 ].getData () && inputs[0 ].getData () && inputs[1 ].getData ());
90
- auto out_mat = std::make_shared<typename MatrixT<Device>::type>(
91
- outputs[0 ].getData (), outputs[0 ].dims_ [0 ], outputs[0 ].dims_ [1 ]);
92
- const auto in1_mat = std::make_shared<typename MatrixT<Device>::type>(
93
- inputs[0 ].getData (), inputs[0 ].dims_ [0 ], inputs[0 ].dims_ [1 ]);
94
- const auto in2_mat = std::make_shared<typename MatrixT<Device>::type>(
95
- inputs[1 ].getData (), inputs[1 ].dims_ [0 ], inputs[1 ].dims_ [1 ]);
86
+ CHECK_EQ (inputs[0 ].shape ()[0 ], outputs[0 ].shape ()[0 ]);
87
+ CHECK_EQ (inputs[0 ].shape ()[1 ], inputs[1 ].shape ()[1 ]);
88
+ CHECK_EQ (outputs[0 ].shape ()[1 ], 1UL );
96
89
97
- CosSimForward<Device>(out_mat.get (), in1_mat.get (), in2_mat.get (), scale_);
90
+ CHECK (outputs[0 ].data () && inputs[0 ].data () && inputs[1 ].data ());
91
+
92
+ CHECK_EQ (outputs[0 ].getArgType (), ASSIGN_TO);
93
+ auto out_mat = outputs[0 ].matrix <Device>();
94
+ const auto in1_mat = inputs[0 ].matrix <Device>();
95
+ const auto in2_mat = inputs[1 ].matrix <Device>();
96
+
97
+ CosSimForward<Device>(out_mat, in1_mat, in2_mat, scale_);
98
98
}
99
99
100
100
private:
@@ -116,28 +116,29 @@ class CosSimForwardFunc : public FunctionBase {
116
116
* \param scale, default 1.0
117
117
*/
118
118
template <>
119
- void CosSimBackward<DEVICE_TYPE_CPU>(const CpuMatrix* out_grad,
120
- const CpuMatrix* out_val,
121
- const CpuMatrix* in1_val,
122
- const CpuMatrix* in2_val,
123
- CpuMatrix* in1_grad,
124
- CpuMatrix* in2_grad,
119
+ void CosSimBackward<DEVICE_TYPE_CPU>(const CpuMatrix& out_grad,
120
+ const CpuMatrix& out_val,
121
+ const CpuMatrix& in1_val,
122
+ const CpuMatrix& in2_val,
123
+ CpuMatrix& in1_grad,
124
+ CpuMatrix& in2_grad,
125
125
real scale) {
126
- CHECK (out_grad && out_val && in1_val && in2_val && in1_grad && in2_grad);
127
- CHECK_EQ (out_val->useGpu_ , false ) << " Matrix type are GPU, CPU required" ;
128
-
129
- const real* grad = out_grad->getData ();
130
- const real* out = out_val->getData ();
131
- const real* prev_out_x = in1_val->getData ();
132
- const real* prev_out_y = in2_val->getData ();
133
- real* prev_grad_x = in1_grad->getData ();
134
- real* prev_grad_y = in2_grad->getData ();
135
-
136
- size_t num_samples = out_grad->getHeight ();
137
- size_t dim = in1_val->getWidth ();
138
- CHECK_EQ (in2_val->getHeight (), in2_grad->getHeight ());
139
- CHECK (in2_val->getHeight () == 1LU || in2_val->getHeight () == num_samples);
140
- size_t inc = (in2_val->getHeight () == 1LU) ? 0 : dim;
126
+ CHECK (out_grad.getData () && out_val.getData () && in1_val.getData () &&
127
+ in2_val.getData () && in1_grad.getData () && in2_grad.getData ());
128
+ CHECK_EQ (out_val.useGpu_ , false ) << " Matrix type are GPU, CPU required" ;
129
+
130
+ const real* grad = out_grad.getData ();
131
+ const real* out = out_val.getData ();
132
+ const real* prev_out_x = in1_val.getData ();
133
+ const real* prev_out_y = in2_val.getData ();
134
+ real* prev_grad_x = in1_grad.getData ();
135
+ real* prev_grad_y = in2_grad.getData ();
136
+
137
+ size_t num_samples = out_grad.getHeight ();
138
+ size_t dim = in1_val.getWidth ();
139
+ CHECK_EQ (in2_val.getHeight (), in2_grad.getHeight ());
140
+ CHECK (in2_val.getHeight () == 1LU || in2_val.getHeight () == num_samples);
141
+ size_t inc = (in2_val.getHeight () == 1LU) ? 0 : dim;
141
142
for (size_t i = 0 ; i < num_samples; ++i,
142
143
prev_out_x += dim,
143
144
prev_out_y += inc,
@@ -178,8 +179,8 @@ void CosSimBackward<DEVICE_TYPE_CPU>(const CpuMatrix* out_grad,
178
179
/* *
179
180
* Cosine Similarity backward Derivative
180
181
*
181
- * \param inouts [0] forward input grad 1, size: nSamples * dim.
182
- * \param inouts [1] forward input grad 2,
182
+ * \param outputs [0] forward input grad 1, size: nSamples * dim.
183
+ * \param outputs [1] forward input grad 2,
183
184
* size: n2 * dim (n2 == 1 or n2 == nSamples).
184
185
*
185
186
* \param inputs[0] backward loss output grad, size : nSamples * 1.
@@ -194,46 +195,36 @@ class CosSimBackwardFunc : public FunctionBase {
194
195
scale_ = config.get <real>(" scale" );
195
196
}
196
197
197
- void calc (const Arguments& inputs,
198
- const Arguments& outputs,
199
- const Arguments& inouts) override {
198
+ void calc (const BufferArgs& inputs, const BufferArgs& outputs) override {
200
199
CHECK_EQ (inputs.size (), 4 );
201
- CHECK_EQ (outputs.size (), 0 );
202
- CHECK_EQ (inouts.size (), 2 );
200
+ CHECK_EQ (outputs.size (), 2 );
203
201
// / dim of out_grad and out_val == 1, column vector
204
- CHECK_EQ (inputs[0 ].dims_ [1 ], 1UL );
205
- CHECK_EQ (inputs[1 ].dims_ [1 ], 1UL );
202
+ CHECK_EQ (inputs[0 ].shape () [1 ], 1UL );
203
+ CHECK_EQ (inputs[1 ].shape () [1 ], 1UL );
206
204
// / nSamples of out_grad == out_val == in_val1 == in_grad1
207
- CHECK_EQ (inputs[1 ].dims_ [0 ], inputs[0 ].dims_ [0 ]);
208
- CHECK_EQ (inputs[0 ].dims_ [0 ], inputs[0 ].dims_ [0 ]);
209
- CHECK_EQ (inouts [0 ].dims_ [0 ], inputs[0 ].dims_ [0 ]);
205
+ CHECK_EQ (inputs[1 ].shape () [0 ], inputs[0 ].shape () [0 ]);
206
+ CHECK_EQ (inputs[0 ].shape () [0 ], inputs[0 ].shape () [0 ]);
207
+ CHECK_EQ (outputs [0 ].shape () [0 ], inputs[0 ].shape () [0 ]);
210
208
// / dim of in1_val1 == in_val2 == in_grad1 == in_grad2
211
- CHECK_EQ (inputs[3 ].dims_ [1 ], inputs[2 ].dims_ [1 ]);
212
- CHECK_EQ (inouts[0 ].dims_ [1 ], inputs[2 ].dims_ [1 ]);
213
- CHECK_EQ (inouts[1 ].dims_ [1 ], inputs[2 ].dims_ [1 ]);
214
-
215
- CHECK (inputs[0 ].getData () && inputs[1 ].getData () && inputs[2 ].getData () &&
216
- inputs[3 ].getData () && inouts[0 ].getData () && inouts[1 ].getData ());
217
- const auto out_grad = std::make_shared<typename MatrixT<Device>::type>(
218
- inputs[0 ].getData (), inputs[0 ].dims_ [0 ], inputs[0 ].dims_ [1 ]);
219
- const auto out_val = std::make_shared<typename MatrixT<Device>::type>(
220
- inputs[1 ].getData (), inputs[1 ].dims_ [0 ], inputs[1 ].dims_ [1 ]);
221
- const auto in1_val = std::make_shared<typename MatrixT<Device>::type>(
222
- inputs[2 ].getData (), inputs[2 ].dims_ [0 ], inputs[2 ].dims_ [1 ]);
223
- const auto in2_val = std::make_shared<typename MatrixT<Device>::type>(
224
- inputs[3 ].getData (), inputs[3 ].dims_ [0 ], inputs[3 ].dims_ [1 ]);
225
- auto in1_grad = std::make_shared<typename MatrixT<Device>::type>(
226
- inouts[0 ].getData (), inouts[0 ].dims_ [0 ], inouts[0 ].dims_ [1 ]);
227
- auto in2_grad = std::make_shared<typename MatrixT<Device>::type>(
228
- inouts[1 ].getData (), inouts[1 ].dims_ [0 ], inouts[1 ].dims_ [1 ]);
229
-
230
- CosSimBackward<Device>(out_grad.get (),
231
- out_val.get (),
232
- in1_val.get (),
233
- in2_val.get (),
234
- in1_grad.get (),
235
- in2_grad.get (),
236
- scale_);
209
+ CHECK_EQ (inputs[3 ].shape ()[1 ], inputs[2 ].shape ()[1 ]);
210
+ CHECK_EQ (outputs[0 ].shape ()[1 ], inputs[2 ].shape ()[1 ]);
211
+ CHECK_EQ (outputs[1 ].shape ()[1 ], inputs[2 ].shape ()[1 ]);
212
+
213
+ CHECK (inputs[0 ].data () && inputs[1 ].data () && inputs[2 ].data () &&
214
+ inputs[3 ].data () && outputs[0 ].data () && outputs[1 ].data ());
215
+
216
+ CHECK_EQ (outputs[0 ].getArgType (), ADD_TO);
217
+ CHECK_EQ (outputs[1 ].getArgType (), ADD_TO);
218
+
219
+ const auto out_grad = inputs[0 ].matrix <Device>();
220
+ const auto out_val = inputs[1 ].matrix <Device>();
221
+ const auto in1_val = inputs[2 ].matrix <Device>();
222
+ const auto in2_val = inputs[3 ].matrix <Device>();
223
+ auto in1_grad = outputs[0 ].matrix <Device>();
224
+ auto in2_grad = outputs[1 ].matrix <Device>();
225
+
226
+ CosSimBackward<Device>(
227
+ out_grad, out_val, in1_val, in2_val, in1_grad, in2_grad, scale_);
237
228
}
238
229
239
230
private:
0 commit comments