@@ -207,161 +207,165 @@ LanguageUnit_p cuda::Dot::emit_function_body()
207207 else if (dtype == element::f16 )
208208 {
209209 // case 1: Scalar * Tensor
210- if (arg0_shape.empty () || arg1_shape.empty ())
211- {
212- auto & second = (arg0_shape.empty () ? arg1_shape : arg0_shape);
213- size_t count = nnfusion::shape_size (second);
210+ if (arg0_shape.empty () || arg1_shape.empty ())
211+ {
212+ auto & second = (arg0_shape.empty () ? arg1_shape : arg0_shape);
213+ size_t count = nnfusion::shape_size (second);
214214
215- string firstarg = (arg0_shape.empty () ? " input1" : " input0" );
216- string secondarg = (arg0_shape.empty () ? " input0" : " input1" );
215+ string firstarg = (arg0_shape.empty () ? " input1" : " input0" );
216+ string secondarg = (arg0_shape.empty () ? " input0" : " input1" );
217217
218- lu << " cublasSetPointerMode(cublas_handle, CUBLAS_POINTER_MODE_DEVICE);\n " ;
218+ lu << " cublasSetPointerMode(cublas_handle, CUBLAS_POINTER_MODE_DEVICE);\n " ;
219219
220- lu << " CUDA_SAFE_CALL(cudaMemcpy(outupt0, " << firstarg << " , " << count << " , cudaMemcpyDeviceToDevice));\n " ; // copy `firstarg` to `output0`
221- lu << " CUBLAS_SAFE_CALL(nnfusionHalfScale(" << secondarg << " , output0, " << count << " ));\n " ;
222- }
220+ lu << " CUDA_SAFE_CALL(cudaMemcpy(outupt0, " << firstarg << " , " << count
221+ << " , cudaMemcpyDeviceToDevice));\n " ; // copy `firstarg` to `output0`
222+ lu << " CUBLAS_SAFE_CALL(nnfusionHalfScale(" << secondarg << " , output0, " << count
223+ << " ));\n " ;
224+ }
223225 // // case 2: 1d Dot
224- else if ((arg0_shape.size () == arg1_shape.size ()) && (arg0_shape.size () == reduction_axes))
225- {
226- for (int i = 0 ; i < arg0_shape.size (); i++)
227- {
228- if (arg0_shape[i] != arg1_shape[i])
229- {
230- std::vector<std::string> arg_vec{" arg0" , " arg1" };
231- std::vector<nnfusion::Shape> shape_vec{arg0_shape, arg1_shape};
232-
233- NNFUSION_CHECK_FAIL () << nnfusion::join (arg_vec) << " with "
234- << nnfusion::join (shape_vec) << " respectively, at Node "
235- << m_context->gnode ->get_name ()
236- << " , do not match for dot op" ;
237- }
238- }
239-
240- size_t count = nnfusion::shape_size (arg0_shape);
241- lu << " cublasSetPointerMode(cublas_handle, CUBLAS_POINTER_MODE_DEVICE);\n " ;
242-
243- lu << " CUBLAS_SAFE_CALL(cublasSdot(cublas_handle, " << count
244- << " , static_cast<const float*>(input0), 1, static_cast<const float*>(input1), 1, "
245- " static_cast<float*>(output0)));\n " ;
246- }
247- // // matrix * vector
248- else if ((arg0_shape.size () == 2 ) && (arg1_shape.size () == 1 ) && (reduction_axes == 1 ))
249- {
250- lu << " const float alpha = 1.0;\n const float beta = 0;\n " ;
251- lu << " CUBLAS_SAFE_CALL(cublasSgemv(cublas_handle, " ;
252- if (trans_A)
253- lu << " CUBLAS_OP_N, " << arg0_shape[0 ] << " , " << arg0_shape[1 ] << " , " ;
254- else
255- lu << " CUBLAS_OP_T, " << arg0_shape[1 ] << " , " << arg0_shape[0 ] << " , " ;
256- lu << " &alpha,"
257- << " static_cast<const float*>(input0)," << arg0_shape[1 ] << " , "
258- << " static_cast<const float*>(input1),"
259- << " 1,"
260- << " &beta,"
261- << " static_cast<float*>(output0),"
262- << " 1));\n " ;
263- }
264- else if ((arg0_shape.size () == 2 ) && (arg1_shape.size () == 2 ) && (reduction_axes == 1 ) &&
265- (trans_A || trans_B))
266- {
267- int m = trans_B ? arg1_shape[0 ] : arg1_shape[1 ];
268- int n = trans_A ? arg0_shape[1 ] : arg0_shape[0 ];
269- int k = trans_A ? arg0_shape[0 ] : arg0_shape[1 ];
270-
271- lu << " const half alpha = 1.0;\n const half beta = 0;\n " ;
272-
273- lu << " CUBLAS_SAFE_CALL(cublasHgemm(cublas_handle,"
274- << (trans_B ? " CUBLAS_OP_T," : " CUBLAS_OP_N," )
275- << (trans_A ? " CUBLAS_OP_T," : " CUBLAS_OP_N," ) << " " << m << " ,"
276- << " " << n << " ,"
277- << " " << k << " ,"
278- << " &alpha,"
279- << " static_cast<const half*>(input1),"
280- << " " << arg1_shape[1 ] << " ,"
281- << " static_cast<const half*>(input0),"
282- << " " << arg0_shape[1 ] << " ,"
283- << " &beta,"
284- << " static_cast<half*>(output0),"
285- << " " << m << " ));\n " ;
286- } else {
287- size_t axes_for_m_count = arg0_shape.size () - reduction_axes;
288- size_t axes_for_n_count = arg1_shape.size () - reduction_axes;
289- size_t axes_for_k_count = reduction_axes;
290- size_t m = 1 ;
291- size_t n = 1 ;
292- size_t k = 1 ;
293-
294- // check if input and output size correct
295- // check and calculate k for arg0 and arg1
296- size_t arg0_k_idx = axes_for_m_count; // first axe in arg0 for k
297- size_t arg1_k_idx = 0 ; // first axe in arg1 for k
298-
299- for (size_t i = 0 ; i < axes_for_k_count; i++)
226+ else if ((arg0_shape.size () == arg1_shape.size ()) && (arg0_shape.size () == reduction_axes))
300227 {
301- k *= arg0_shape[arg0_k_idx];
302- if (arg0_shape[arg0_k_idx++] != arg1_shape[arg1_k_idx++])
228+ for (int i = 0 ; i < arg0_shape.size (); i++)
303229 {
304- std::vector<std::string> arg_vec{" arg0" , " arg1" };
305- std::vector<nnfusion::Shape> shape_vec{arg0_shape, arg1_shape};
230+ if (arg0_shape[i] != arg1_shape[i])
231+ {
232+ std::vector<std::string> arg_vec{" arg0" , " arg1" };
233+ std::vector<nnfusion::Shape> shape_vec{arg0_shape, arg1_shape};
306234
307- NNFUSION_CHECK_FAIL () << nnfusion::join (arg_vec) << " with "
308- << nnfusion::join (shape_vec) << " respectively, at Node "
309- << m_context->gnode ->get_name ()
310- << " , do not match for dot op" ;
235+ NNFUSION_CHECK_FAIL () << nnfusion::join (arg_vec) << " with "
236+ << nnfusion::join (shape_vec) << " respectively, at Node "
237+ << m_context->gnode ->get_name ()
238+ << " , do not match for dot op" ;
239+ }
311240 }
241+
242+ size_t count = nnfusion::shape_size (arg0_shape);
243+ lu << " cublasSetPointerMode(cublas_handle, CUBLAS_POINTER_MODE_DEVICE);\n " ;
244+
245+ lu << " CUBLAS_SAFE_CALL(cublasSdot(cublas_handle, " << count
246+ << " , static_cast<const float*>(input0), 1, static_cast<const float*>(input1), 1, "
247+ " static_cast<float*>(output0)));\n " ;
312248 }
313- // check and calculate m for arg0 and out
314- size_t arg0_m_idx = 0 ; // first axe in arg0 for m
315- size_t out_m_idx = 0 ; // first axe in out for m
316- for (size_t i = 0 ; i < axes_for_m_count; i++)
249+ // // matrix * vector
250+ else if ((arg0_shape.size () == 2 ) && (arg1_shape.size () == 1 ) && (reduction_axes == 1 ))
317251 {
318- m *= arg0_shape[arg0_m_idx];
319- if (arg0_shape[arg0_m_idx++] != out_shape[out_m_idx++])
320- {
321- std::vector<std::string> arg_vec{" arg0" , " output" };
322- std::vector<nnfusion::Shape> shape_vec{arg0_shape, out_shape};
252+ lu << " const float alpha = 1.0;\n const float beta = 0;\n " ;
253+ lu << " CUBLAS_SAFE_CALL(cublasSgemv(cublas_handle, " ;
254+ if (trans_A)
255+ lu << " CUBLAS_OP_N, " << arg0_shape[0 ] << " , " << arg0_shape[1 ] << " , " ;
256+ else
257+ lu << " CUBLAS_OP_T, " << arg0_shape[1 ] << " , " << arg0_shape[0 ] << " , " ;
258+ lu << " &alpha,"
259+ << " static_cast<const float*>(input0)," << arg0_shape[1 ] << " , "
260+ << " static_cast<const float*>(input1),"
261+ << " 1,"
262+ << " &beta,"
263+ << " static_cast<float*>(output0),"
264+ << " 1));\n " ;
265+ }
266+ else if ((arg0_shape.size () == 2 ) && (arg1_shape.size () == 2 ) && (reduction_axes == 1 ) &&
267+ (trans_A || trans_B))
268+ {
269+ int m = trans_B ? arg1_shape[0 ] : arg1_shape[1 ];
270+ int n = trans_A ? arg0_shape[1 ] : arg0_shape[0 ];
271+ int k = trans_A ? arg0_shape[0 ] : arg0_shape[1 ];
323272
324- NNFUSION_CHECK_FAIL () << nnfusion::join (arg_vec) << " with "
325- << nnfusion::join (shape_vec) << " respectively, at Node "
326- << m_context->gnode ->get_name ()
327- << " , do not match for dot op" ;
328- }
273+ lu << " const half alpha = 1.0;\n const half beta = 0;\n " ;
274+
275+ lu << " CUBLAS_SAFE_CALL(cublasHgemm(cublas_handle,"
276+ << (trans_B ? " CUBLAS_OP_T," : " CUBLAS_OP_N," )
277+ << (trans_A ? " CUBLAS_OP_T," : " CUBLAS_OP_N," ) << " " << m << " ,"
278+ << " " << n << " ,"
279+ << " " << k << " ,"
280+ << " &alpha,"
281+ << " static_cast<const half*>(input1),"
282+ << " " << arg1_shape[1 ] << " ,"
283+ << " static_cast<const half*>(input0),"
284+ << " " << arg0_shape[1 ] << " ,"
285+ << " &beta,"
286+ << " static_cast<half*>(output0),"
287+ << " " << m << " ));\n " ;
329288 }
330- // check and calculate n for arg1 and out
331- size_t arg1_n_idx = axes_for_k_count; // first axe in arg1 for n
332- size_t out_n_idx = axes_for_m_count; // first axe in arg1 for n
333- for (size_t i = 0 ; i < axes_for_n_count; i++)
289+ else
334290 {
335- n *= arg1_shape[arg1_n_idx];
336- if (arg1_shape[arg1_n_idx++] != out_shape[out_n_idx++])
291+ size_t axes_for_m_count = arg0_shape.size () - reduction_axes;
292+ size_t axes_for_n_count = arg1_shape.size () - reduction_axes;
293+ size_t axes_for_k_count = reduction_axes;
294+ size_t m = 1 ;
295+ size_t n = 1 ;
296+ size_t k = 1 ;
297+
298+ // check if input and output size correct
299+ // check and calculate k for arg0 and arg1
300+ size_t arg0_k_idx = axes_for_m_count; // first axe in arg0 for k
301+ size_t arg1_k_idx = 0 ; // first axe in arg1 for k
302+
303+ for (size_t i = 0 ; i < axes_for_k_count; i++)
337304 {
338- std::vector<std::string> arg_vec{" arg1" , " output" };
339- std::vector<nnfusion::Shape> shape_vec{arg1_shape, out_shape};
305+ k *= arg0_shape[arg0_k_idx];
306+ if (arg0_shape[arg0_k_idx++] != arg1_shape[arg1_k_idx++])
307+ {
308+ std::vector<std::string> arg_vec{" arg0" , " arg1" };
309+ std::vector<nnfusion::Shape> shape_vec{arg0_shape, arg1_shape};
340310
341- NNFUSION_CHECK_FAIL () << nnfusion::join (arg_vec) << " with "
342- << nnfusion::join (shape_vec) << " respectively, at Node "
343- << m_context->gnode ->get_name ()
344- << " , do not match for dot op" ;
311+ NNFUSION_CHECK_FAIL () << nnfusion::join (arg_vec) << " with "
312+ << nnfusion::join (shape_vec) << " respectively, at Node "
313+ << m_context->gnode ->get_name ()
314+ << " , do not match for dot op" ;
315+ }
345316 }
346- }
317+ // check and calculate m for arg0 and out
318+ size_t arg0_m_idx = 0 ; // first axe in arg0 for m
319+ size_t out_m_idx = 0 ; // first axe in out for m
320+ for (size_t i = 0 ; i < axes_for_m_count; i++)
321+ {
322+ m *= arg0_shape[arg0_m_idx];
323+ if (arg0_shape[arg0_m_idx++] != out_shape[out_m_idx++])
324+ {
325+ std::vector<std::string> arg_vec{" arg0" , " output" };
326+ std::vector<nnfusion::Shape> shape_vec{arg0_shape, out_shape};
327+
328+ NNFUSION_CHECK_FAIL () << nnfusion::join (arg_vec) << " with "
329+ << nnfusion::join (shape_vec) << " respectively, at Node "
330+ << m_context->gnode ->get_name ()
331+ << " , do not match for dot op" ;
332+ }
333+ }
334+ // check and calculate n for arg1 and out
335+ size_t arg1_n_idx = axes_for_k_count; // first axe in arg1 for n
336+ size_t out_n_idx = axes_for_m_count; // first axe in arg1 for n
337+ for (size_t i = 0 ; i < axes_for_n_count; i++)
338+ {
339+ n *= arg1_shape[arg1_n_idx];
340+ if (arg1_shape[arg1_n_idx++] != out_shape[out_n_idx++])
341+ {
342+ std::vector<std::string> arg_vec{" arg1" , " output" };
343+ std::vector<nnfusion::Shape> shape_vec{arg1_shape, out_shape};
347344
348- lu << " const half alpha = 1.0f;\n const half beta = 0.f;\n " ;
349-
350- lu << " CUBLAS_SAFE_CALL(cublasHgemm(cublas_handle,"
351- << " CUBLAS_OP_N,"
352- << " CUBLAS_OP_N,"
353- << " " << n << " ,"
354- << " " << m << " ,"
355- << " " << k << " ,"
356- << " &alpha,"
357- << " static_cast<const half*>(input1),"
358- << " " << n << " ,"
359- << " static_cast<const half*>(input0),"
360- << " " << k << " ,"
361- << " &beta,"
362- << " static_cast<half*>(output0),"
363- << " " << n << " ));\n " ;
364- }
345+ NNFUSION_CHECK_FAIL () << nnfusion::join (arg_vec) << " with "
346+ << nnfusion::join (shape_vec) << " respectively, at Node "
347+ << m_context->gnode ->get_name ()
348+ << " , do not match for dot op" ;
349+ }
350+ }
351+
352+ lu << " const half alpha = 1.0f;\n const half beta = 0.f;\n " ;
353+
354+ lu << " CUBLAS_SAFE_CALL(cublasHgemm(cublas_handle,"
355+ << " CUBLAS_OP_N,"
356+ << " CUBLAS_OP_N,"
357+ << " " << n << " ,"
358+ << " " << m << " ,"
359+ << " " << k << " ,"
360+ << " &alpha,"
361+ << " static_cast<const half*>(input1),"
362+ << " " << n << " ,"
363+ << " static_cast<const half*>(input0),"
364+ << " " << k << " ,"
365+ << " &beta,"
366+ << " static_cast<half*>(output0),"
367+ << " " << n << " ));\n " ;
368+ }
365369 }
366370 else
367371 {
0 commit comments