4545 "__rpow__" : "A **= B" ,
4646 "__floordiv__" : "A //B" ,
4747 "__mod__" : "A % B" ,
48+ "__matmul__" : "A @ B" ,
4849 "__eq__" : "A == B" ,
4950 "__ne__" : "A != B" ,
5051 "__lt__" : "A < B" ,
@@ -195,6 +196,28 @@ def _scalar_op_(var, scale, bias):
195196 def _neg_ (var ):
196197 return _scalar_op_ (var , - 1.0 , 0.0 )
197198
199+ @property
200+ def _ndim_ (self ):
201+ """
202+ Returns the dimension of current Variable
203+
204+ Returns:
205+ the dimension
206+
207+ Examples:
208+ .. code-block:: python
209+
210+ import paddle
211+
212+ paddle.enable_static()
213+
214+ # create a static Variable
215+ x = paddle.static.data(name='x', shape=[3, 2, 1])
216+ # print the dimension of the Variable
217+ print(x.ndim)
218+ """
219+ return len (self .shape )
220+
198221 def _scalar_add_ (var , value ):
199222 return _scalar_op_ (var , 1.0 , value )
200223
@@ -228,17 +251,17 @@ def __impl__(self, other_var):
228251 other_var = float (other_var )
229252 # division is a special case
230253 # NOTE(chenweihang): because we cast tensor to float32 instead float64,
231- # the division result can only guarantee the numerical accuracy of 6 digits
232- # after the decimal point. The result of numpy calculation is of float64 type,
233- # so the calculation result here and the calculation result of numpy are
254+ # the division result can only guarantee the numerical accuracy of 6 digits
255+ # after the decimal point. The result of numpy calculation is of float64 type,
256+ # so the calculation result here and the calculation result of numpy are
234257 # different after 6 decimal point. If necessary, we can also use float64 here.
235258 # torch's behavior here is consistent with ours
236259 if op_type == 'elementwise_div' and self .dtype in _supported_int_dtype_ :
237260 self = astype (self , 'float32' )
238261 # here use `scale` replace `elementwise` to get better performance
239262 # but only +, -, * can use this method
240263 # NOTE(chentianyu03): / can not use `scale` method,because the result of
241- # `scale` method (self*(1/other_var)) do not exactly equal with the result
264+ # `scale` method (self*(1/other_var)) do not exactly equal with the result
242265 # of `elementwise_div` method.
243266 if scalar_method is not None :
244267 return scalar_method (self , other_var )
@@ -321,6 +344,9 @@ def __impl__(self, other_var):
321344 # b=-a
322345 ('__neg__' , _neg_ ),
323346 ('astype' , astype ),
347+ ('dim' , lambda x : len (x .shape )),
348+ ('ndimension' , lambda x : len (x .shape )),
349+ ('ndim' , _ndim_ ),
324350 ('__add__' , _binary_creator_ ('__add__' , 'elementwise_add' , False ,
325351 _scalar_add_ )),
326352 # a+b == b+a. Do not need to reverse explicitly
@@ -347,6 +373,8 @@ def __impl__(self, other_var):
347373 'elementwise_floordiv' , False , None )),
348374 ('__mod__' , _binary_creator_ ('__mod__' , 'elementwise_mod' , False ,
349375 None )),
376+ ('__matmul__' , _binary_creator_ ('__matmul__' , "matmul_v2" , False ,
377+ None )),
350378 # for logical compare
351379 ('__eq__' , _binary_creator_ ('__eq__' , 'equal' , False , None )),
352380 ('__ne__' , _binary_creator_ ('__ne__' , 'not_equal' , False , None )),
0 commit comments