@@ -217,13 +217,13 @@ def dense(input, weight, bias=None):
217217 Tensor: The result of the dense operation.
218218 """
219219 if ON_ORANGE_PI :
220+ dtype = input .dtype
220221 input = cast (input , mindspore .float16 )
221222 weight = cast (weight , mindspore .float16 )
222- if bias is None :
223- return pyboost .dense_op (input , weight )
224-
225- bias = cast (bias , mindspore .float16 )
226- return add (pyboost .dense_op (input , weight ), bias )
223+ out = cast (pyboost .dense_op (input , weight ), dtype )
224+ if bias is not None :
225+ out = add (out , bias )
226+ return out
227227
228228 if use_pyboost ():
229229 return pyboost .dense_op (input , weight , bias )
@@ -820,9 +820,13 @@ def argmin(input, axis, keepdims):
820820
821821
822822def bmm (input , other ):
823+ if ON_ORANGE_PI :
824+ dtype = input .dtype
825+ out = pyboost .bmm_ext_op (cast (input , mindspore .float16 ), cast (other , mindspore .float16 ))
826+ return cast (out , dtype )
823827 if use_pyboost ():
824828 return pyboost .bmm_ext_op (input , other )
825- return legacy .batch_mat_mul (input , other )
829+ return legacy .batch_mat_mul (input , other , False , False )
826830
827831def topk (input , k , dim , largest , sorted ):
828832 if use_pyboost ():
@@ -1198,14 +1202,47 @@ def roll(input, shifts, axis):
11981202 return legacy .roll (input , shifts , axis )
11991203
12001204def conv1d (input , weight , bias = None , stride = 1 , padding = 0 , dilation = 1 , groups = 1 ):
1201- if use_pyboost ():
1205+ if use_pyboost () and not ON_ORANGE_PI :
12021206 return pyboost .conv1d_ext_op (input , weight , bias , stride , padding , dilation , groups )
1203- return legacy .conv1d (input , weight , bias , pad , stride , dilation )
1207+ return conv1d_legacy (input , weight , bias , stride , padding , dilation , groups )
1208+
1209+ def conv1d_legacy (input , weight , bias = None , stride = 1 , padding = 0 , dilation = 1 , groups = 1 ):
1210+ pad_mode = 'pad'
1211+ pad = padding
1212+ if isinstance (padding , tuple ):
1213+ pad = (0 , 0 , padding [0 ], padding [0 ])
1214+ elif isinstance (padding , int ):
1215+ pad = (0 , 0 ) + (padding ,) * 2
1216+ if not isinstance (padding , (int , tuple )):
1217+ pad_mode = padding
1218+ pad = (0 ,) * 4
1219+
1220+ input = expand_dims (input , 2 )
1221+ weight = expand_dims (weight , 2 )
1222+
1223+ output = legacy .conv2_d (
1224+ input , weight ,
1225+ weight .shape [0 ],
1226+ (1 , weight .shape [- 1 ]),
1227+ 1 ,#mode=1,
1228+ pad_mode , #pad_mode=pad_mode,
1229+ pad , #pad=pad,
1230+ (1 , stride ) if isinstance (stride , int ) else (1 , * stride ), #stride=tuple(stride),
1231+ (1 , dilation ) if isinstance (dilation , int ) else (1 , * dilation ), #dilation=dilation,
1232+ groups , #group=groups,
1233+ "NCHW" , #data_format="NCHW"
1234+ )
1235+
1236+ if bias is not None :
1237+ output = legacy .bias_add (output , bias , "NCHW" )
1238+
1239+ output = squeeze (output , 2 )
1240+ return output
12041241
12051242def conv1d_padding (input , weight , bias = None , stride = 1 , padding = 0 , dilation = 1 , groups = 1 ):
1206- if use_pyboost ():
1243+ if use_pyboost () and not ON_ORANGE_PI :
12071244 return pyboost .conv1d_padding_op (input , weight , bias , stride , padding , dilation , groups )
1208- return legacy . conv1d (input , weight , bias , pad , stride , dilation )
1245+ return conv1d_legacy (input , weight , bias , stride , padding , dilation , groups )
12091246
12101247def square (input ):
12111248 if use_pyboost ():
@@ -1233,14 +1270,14 @@ def split_with_size(input, size, dim=0):
12331270 return legacy .split_with_size (input , size , dim )
12341271
12351272def softplus (input , beta = 1 , threshold = 20 ):
1236- if use_pyboost ():
1273+ if use_pyboost () and not ON_ORANGE_PI :
12371274 return pyboost .softplus_ext_op (input , beta , threshold )
1238- return legacy .softplus (input , beta , threshold )
1275+ return legacy .softplus (input )
12391276
12401277def remainder_tensor_scalar (input , other ):
1241- if use_pyboost ():
1278+ if use_pyboost () and not ON_ORANGE_PI :
12421279 return pyboost .remainder_tensor_scalar_op (input , other )
1243- out = input - floor_div (input , other ) * other
1280+ out = sub ( input , mul ( floor_div (input , other ), other ), 1 )
12441281 return out
12451282
12461283def baddbmm (input , batch1 , batch2 , alpha = 1 , beta = 1 ):
@@ -1253,28 +1290,107 @@ def floor(input):
12531290 return pyboost .floor_op (input )
12541291 return legacy .floor (input )
12551292
1293+
1294+ def _deconv_output_length (pad_mode , filter_size , stride_size , dilation_size , padding ):
1295+ """Calculate the width and height of output."""
1296+ length = 0
1297+ filter_size = filter_size + (filter_size - 1 ) * (dilation_size - 1 )
1298+ if pad_mode == 'valid' :
1299+ if filter_size - stride_size > 0 :
1300+ length = filter_size - stride_size
1301+ elif pad_mode == 'pad' :
1302+ length = - padding + filter_size - stride_size
1303+
1304+ return length
1305+
12561306def conv_transpose2d (input , weight , bias = None , stride = 1 , padding = 0 , output_padding = 0 , groups = 1 , dilation = 1 ):
1257- if use_pyboost ():
1307+ if use_pyboost () and not ON_ORANGE_PI :
12581308 return pyboost .conv_transpose2d_op (input , weight , bias , stride , padding , output_padding , groups , dilation )
1259- return legacy .conv_transpose2d (input , weight , bias , stride , padding , output_padding , groups , dilation )
1309+ pad_mode = 'pad'
1310+ pad = padding
1311+ if isinstance (padding , tuple ):
1312+ pad = (padding [0 ], padding [0 ], padding [1 ], padding [1 ])
1313+ elif isinstance (padding , int ):
1314+ pad = (padding ,) * 4
1315+ if not isinstance (padding , (int , tuple )):
1316+ pad_mode = padding
1317+ pad = (0 ,) * 4
1318+
1319+ if isinstance (dilation , int ):
1320+ dilation = (dilation , dilation )
1321+
1322+ in_channel , out_channels = weight .shape [0 ], weight .shape [1 ] * groups
1323+ kernel_size = weight .shape [2 :]
1324+ n , _ , h , w = input .shape
1325+ h_add = _deconv_output_length (pad_mode , kernel_size [0 ], stride [0 ], dilation [0 ], pad [0 ] + pad [1 ])
1326+ w_add = _deconv_output_length (pad_mode , kernel_size [1 ], stride [1 ], dilation [1 ], pad [2 ] + pad [3 ])
1327+
1328+ out = legacy .conv2_d_transpose (
1329+ input , weight ,
1330+ (n , out_channels , h * stride [0 ] + h_add , w * stride [1 ] + w_add ),
1331+ out_channels ,
1332+ kernel_size ,
1333+ pad_mode ,
1334+ pad ,
1335+ None ,
1336+ 1 ,
1337+ stride ,
1338+ dilation ,
1339+ groups ,
1340+ 'NCHW'
1341+ )
1342+ if bias is not None :
1343+ out = legacy .bias_add (out , bias , 'NCHW' )
1344+ return out
1345+
1346+
12601347
12611348def relu (input ):
12621349 if use_pyboost ():
12631350 return pyboost .relu_op (input )
12641351 return legacy .re_lu (input )
12651352
1353+ def _check_maxpool_padding (padding , nd ):
1354+ """Calculate maxpool padding before call primitive"""
1355+ if isinstance (padding , int ):
1356+ return (0 ,) * (3 - nd ) + (padding ,) * nd
1357+ if isinstance (padding , (tuple , list )):
1358+ if len (padding ) == 1 :
1359+ return (0 ,) * (3 - nd ) + tuple (padding * nd )
1360+ if len (padding ) != nd :
1361+ raise ValueError (f"For { cls_name } , the length of padding must equal to { nd } , but got { len (padding )} ." )
1362+ return (0 ,) * (3 - nd ) + tuple (padding )
1363+ return padding
1364+
12661365def max_pool2d (input , kernel_size , stride = 1 , padding = 0 , dilation = 1 , ceil_mode = False , return_indices = False ):
12671366 # out, indices = legacy.max_pool_with_argmax_v2(input, kernel_size, stride, padding, dilation, ceil_mode)
1268-
1269- out , indices = legacy .max_pool_with_indices (input , kernel_size , stride , padding , dilation , ceil_mode )
1367+ if not ON_ORANGE_PI :
1368+ out , indices = legacy .max_pool_with_indices (input , kernel_size , stride , padding , dilation , ceil_mode )
1369+ if return_indices :
1370+ return out , indices
1371+ return out
1372+
1373+ if isinstance (kernel_size , tuple ):
1374+ kernel_size = (1 ,) + kernel_size
1375+ elif isinstance (kernel_size , int ):
1376+ kernel_size = (1 , kernel_size , kernel_size )
1377+ if isinstance (stride , tuple ):
1378+ stride = (1 ,) + stride
1379+ elif isinstance (stride , int ):
1380+ stride = (1 , stride , stride )
1381+ padding = _check_maxpool_padding (padding , 2 )
1382+
1383+ input = expand_dims (input , 2 )
1384+ out , indices = legacy .max_pool3_d_with_argmax (input , kernel_size , stride , padding ,
1385+ dilation , ceil_mode , 'NCDHW' , mindspore .int64 )
12701386 if return_indices :
1271- return out , indices
1272- return out
1387+ return squeeze ( out , 2 ), squeeze ( indices , 2 )
1388+ return squeeze ( out , 2 )
12731389
12741390def upsample_bilinear2d (input , size = None , scale_factor = None , align_corners = False ):
1275- if use_pyboost ():
1391+ if use_pyboost () and not ON_ORANGE_PI :
12761392 return pyboost .upsample_bilinear2d_op (input , size , scale_factor , align_corners )
1277- return legacy .resize_bilinear_v2 (input , size , scale_factor , align_corners )
1393+ return legacy .resize_bilinear_v2 (input , size , align_corners , not align_corners )
12781394
12791395def group_norm (input , num_groups , weight = None , bias = None , eps = 1e-5 ):
12801396 if use_pyboost ():
0 commit comments