@@ -3257,3 +3257,125 @@ def geometric_(
32573257 x .uniform_ (min = float (tiny ), max = float (1 ))
32583258 x .log_ ().divide_ (paddle .log1p (- (probs )))
32593259 return x
3260+
3261+
3262+ @inplace_apis_in_dygraph_only
3263+ def set_ (
3264+ x : paddle .Tensor ,
3265+ source : paddle .Tensor | None = None ,
3266+ shape : Sequence [int ] | None = None ,
3267+ stride : Sequence [int ] | None = None ,
3268+ offset : int = 0 ,
3269+ name : str | None = None ,
3270+ ) -> paddle .Tensor :
3271+ """
3272+ set x with specified source Tensor's underlying storage, shape, stride and offset.
3273+
3274+ Note that the ``x`` will share the same data with ``source`` Tensor.
3275+
3276+ Args:
3277+ x (Tensor): An arbitrary Tensor. The data type supports ``bfloat16``, ``float16``, ``float32``, ``float64``,
3278+ ``bool``, ``int8``, ``int16``, ``int32``, ``int64``, ``uint8``, ``complex64`` or ``complex128``.
3279+ source (Tensor|None, optional): Define the target Tensor to use. The data type supports `bfloat16`, ``float16``,
3280+ ``float32``, ``float64``, ``bool``, ``int8``, ``int16``, ``int32``, ``int64``, ``uint8``, ``complex64`` or
3281+ ``complex128``. Default: None, which means to set ``x`` with an empty source tensor.
3282+ shape (list|tuple|None, optional): Define the target shape. Each element of it should be integer. Default: None,
3283+ which means it will use the specified ``source``'s shape as default value.
3284+ stride (list|tuple|None, optional): Define the target stride. Each element of it should be integer. Default: None,
3285+ and when ``shape`` is also None, it will use the specified ``source``'s stride as default value; when ``shape``
3286+ is specified, it will use the default stride corresponding to the specified ``shape``.
3287+ offset (int, optional): Define the target offset from x's holder. Default: 0.
3288+ name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
3289+
3290+ Returns:
3291+ Tensor, the Tensor with the same data type as ``x``.
3292+
3293+ Examples:
3294+ .. code-block:: python
3295+
3296+ >>> import paddle
3297+
3298+ >>> src = paddle.to_tensor([[11., 22., 33.]])
3299+ >>> src2 = paddle.to_tensor([11., 22., 33., 44., 55., 66.])
3300+
3301+ >>> x = paddle.to_tensor([1., 2., 3., 4., 5.])
3302+ >>> x.set_()
3303+ >>> print(x)
3304+ Tensor(shape=[0], dtype=float32, place=Place(cpu), stop_gradient=True,
3305+ [])
3306+
3307+ >>> x = paddle.to_tensor([1., 2., 3., 4., 5.])
3308+ >>> x.set_(src)
3309+ >>> print(x)
3310+ Tensor(shape=[1, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
3311+ [[11., 22., 33.]])
3312+
3313+ >>> print(x._is_shared_buffer_with(src))
3314+ True
3315+
3316+ >>> x = paddle.to_tensor([1., 2., 3., 4., 5.])
3317+ >>> x.set_(src, shape=[2, 1])
3318+ >>> print(x)
3319+ Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
3320+ [[11.],
3321+ [22.]])
3322+
3323+ >>> x = paddle.to_tensor([1., 2., 3., 4., 5.])
3324+ >>> x.set_(src2, shape=[3], stride=[2])
3325+ >>> print(x)
3326+ Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
3327+ [11., 33., 55.])
3328+
3329+ >>> x = paddle.to_tensor([1., 2., 3., 4., 5.])
3330+ >>> x.set_(src2, shape=[5], offset=4)
3331+ >>> print(x)
3332+ Tensor(shape=[5], dtype=float32, place=Place(cpu), stop_gradient=True,
3333+ [22., 33., 44., 55., 66.])
3334+
3335+ """
3336+ if in_dynamic_mode ():
3337+ # set_ doesn't have backward op so EagerUtils::CheckInplace will not be
3338+ # called in eager_generator.cc. Here to keep consistent with other inplace
3339+ # op, manually check whether x is leaf node and doesn't stop gradient.
3340+ if x .is_leaf and not x .stop_gradient :
3341+ raise ValueError (
3342+ f"(InvalidArgument) Leaf Tensor { x .name } that doesn't stop gradient can't use "
3343+ "inplace strategy."
3344+ )
3345+ if source is None :
3346+ source = paddle .empty ([0 ], dtype = x .dtype )
3347+ shape = [0 ]
3348+ stride = [0 ]
3349+ else :
3350+ if not isinstance (source , (Variable , core .eager .Tensor )):
3351+ raise ValueError (
3352+ f"Input (source) should be paddle.Tensor but received { type (source )} "
3353+ )
3354+ check_dtype (
3355+ source .dtype ,
3356+ 'source' ,
3357+ [
3358+ 'bool' ,
3359+ 'float16' ,
3360+ 'uint16' ,
3361+ 'float32' ,
3362+ 'float64' ,
3363+ 'int8' ,
3364+ 'int16' ,
3365+ 'int32' ,
3366+ 'int64' ,
3367+ 'uint8' ,
3368+ 'complex64' ,
3369+ 'complex128' ,
3370+ ],
3371+ 'set' ,
3372+ )
3373+ if stride is None :
3374+ if shape is None :
3375+ stride = source .strides
3376+ else :
3377+ stride = paddle .empty (shape ).strides
3378+ if shape is None :
3379+ shape = source .shape
3380+
3381+ return _C_ops .set_ (x , source , shape , stride , offset )
0 commit comments