Skip to content
8 changes: 4 additions & 4 deletions python/paddle/incubate/nn/layer/fused_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ class FusedFeedForward(Layer):
this property. For more information, please refer to :ref:`api_guide_Name`.

Examples:
.. code-block:: python
.. code-block:: pycon

>>> # doctest: +REQUIRES(env:GPU)
>>> import paddle
Expand All @@ -588,7 +588,7 @@ class FusedFeedForward(Layer):
>>> x = paddle.rand((1, 8, 8))
>>> out = fused_feedforward_layer(x)
>>> print(out.shape)
[1, 8, 8]
paddle.Size([1, 8, 8])
"""

name: str | None
Expand Down Expand Up @@ -789,7 +789,7 @@ class FusedTransformerEncoderLayer(Layer):


Examples:
.. code-block:: python
.. code-block:: pycon

>>> # doctest: +REQUIRES(env:GPU)
>>> import paddle
Expand All @@ -803,7 +803,7 @@ class FusedTransformerEncoderLayer(Layer):
>>> encoder_layer = FusedTransformerEncoderLayer(128, 2, 512)
>>> enc_output = encoder_layer(enc_input, attn_mask)
>>> print(enc_output.shape)
[2, 4, 128]
paddle.Size([2, 4, 128])

"""

Expand Down
3 changes: 2 additions & 1 deletion python/paddle/jit/dy2static/transformers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ class ForLoopTuplePreTransformer(BaseTransformer):

will be changed into :

>>> UUID_iterator = _jst.Indexable(B) # make iterator-only to indexable list.
>>> # make iterator-only to indexable list.
>>> UUID_iterator = _jst.Indexable(B)
>>> for UUID_target in UUID_iterator:
>>> A = _jst.Unpack(UUID_target, structure)
>>> C
Expand Down
10 changes: 6 additions & 4 deletions python/paddle/jit/dy2static/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,16 +476,18 @@ def wrap_as_closure(tree: gast.AST, closure_vars: list[str]) -> gast.AST:

Before:

>>> def fn(x):
... ...
>>> def fn(x): ...

After:

>>> def create_fn():
... closure_var_1 = None
... def fn(x):
... ...
...
... def fn(x): ...
...
... return fn
...
...
... fn = create_fn()
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,7 @@ class Dispatcher:

Examples:

>>> def builtin_add(a: int, b: int) -> int:
... ...
...
>>> def builtin_add(a: int, b: int) -> int: ...
>>> Dispatcher.register(builtin_add, ("int", "int"), lambda a, b: a + b)
>>> handler = Dispatcher.dispatch(builtin_add, 1, 2)
>>> handler(1, 2)
Expand Down Expand Up @@ -250,13 +248,10 @@ def register_decorator(cls, fn: Callable[..., Any]):
fn: The function to be registered.

Examples:
>>> def builtin_add(a: int, b: int) -> int:
... ...
...
>>> def builtin_add(a: int, b: int) -> int: ...
>>> @Dispatcher.register_decorator(builtin_add)
... def builtin_add_dispatcher(a: int, b: int) -> int:
... return a + b
...
>>> handler = Dispatcher.dispatch(builtin_add, 1, 2)
>>> handler(1, 2)
3
Expand Down
5 changes: 4 additions & 1 deletion python/paddle/jit/sot/opcode_translator/executor/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ class DanglingTracker(Tracker):

Examples:
>>> import operator
>>> from sot.opcode_translator.executor.variables import BuiltinVariable, ConstantVariable
>>> from sot.opcode_translator.executor.variables import (
... BuiltinVariable,
... ConstantVariable,
... )
>>> a = ConstantVariable.wrap_literal(1, None)
>>> b = ConstantVariable.wrap_literal(2, None)
>>> c = BuiltinVariable(operator.add, None, DanglingTracker())(a, b)
Expand Down
31 changes: 16 additions & 15 deletions python/paddle/nn/layer/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ class TransformerEncoderLayer(Layer):

Examples:

.. code-block:: python
.. code-block:: pycon

>>> import paddle
>>> from paddle.nn import TransformerEncoderLayer
Expand All @@ -620,7 +620,7 @@ class TransformerEncoderLayer(Layer):
>>> encoder_layer = TransformerEncoderLayer(128, 2, 512)
>>> enc_output = encoder_layer(enc_input, attn_mask)
>>> print(enc_output.shape)
[2, 4, 128]
paddle.Size([2, 4, 128])
"""

activation: Layer
Expand Down Expand Up @@ -972,7 +972,7 @@ class TransformerDecoderLayer(Layer):

Examples:

.. code-block:: python
.. code-block:: pycon

>>> import paddle
>>> from paddle.nn import TransformerDecoderLayer
Expand All @@ -986,12 +986,11 @@ class TransformerDecoderLayer(Layer):
>>> # cross attention mask: [batch_size, n_head, tgt_len, src_len]
>>> cross_attn_mask = paddle.rand((2, 2, 4, 6))
>>> decoder_layer = TransformerDecoderLayer(128, 2, 512)
>>> output = decoder_layer(dec_input,
... enc_output,
... self_attn_mask,
... cross_attn_mask)
>>> output = decoder_layer(
... dec_input, enc_output, self_attn_mask, cross_attn_mask
... )
>>> print(output.shape)
[2, 4, 128]
paddle.Size([2, 4, 128])
"""

normalize_before: bool
Expand Down Expand Up @@ -1498,7 +1497,7 @@ class Transformer(Layer):

Examples:

.. code-block:: python
.. code-block:: pycon

>>> import paddle
>>> from paddle.nn import Transformer
Expand All @@ -1514,13 +1513,15 @@ class Transformer(Layer):
>>> # memory_mask: [batch_size, n_head, tgt_len, src_len]
>>> cross_attn_mask = paddle.rand((2, 2, 6, 4))
>>> transformer = Transformer(128, 2, 4, 4, 512)
>>> output = transformer(enc_input,
... dec_input,
... enc_self_attn_mask,
... dec_self_attn_mask,
... cross_attn_mask)
>>> output = transformer(
... enc_input,
... dec_input,
... enc_self_attn_mask,
... dec_self_attn_mask,
... cross_attn_mask,
... )
>>> print(output.shape)
[2, 6, 128]
paddle.Size([2, 6, 128])
"""

encoder: Layer
Expand Down
13 changes: 9 additions & 4 deletions python/paddle/tensor/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2035,18 +2035,23 @@ def flatten(

Examples:

.. code-block:: python
.. code-block:: pycon

>>> import paddle

>>> image_shape=(2, 3, 4, 4)
>>> image_shape = (2, 3, 4, 4)

>>> x = paddle.arange(end=image_shape[0] * image_shape[1] * image_shape[2] * image_shape[3])
>>> x = paddle.arange(
... end=image_shape[0]
... * image_shape[1]
... * image_shape[2]
... * image_shape[3]
... )
>>> img = paddle.reshape(x, image_shape)

>>> out = paddle.flatten(img, start_axis=1, stop_axis=2)
>>> print(out.shape)
[2, 12, 4]
paddle.Size([2, 12, 4])

>>> # out shares data with img in dygraph mode
>>> img[0, 0, 0, 0] = -1
Expand Down
20 changes: 14 additions & 6 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -5880,19 +5880,27 @@ def angle(x: Tensor, name: str | None = None) -> Tensor:
Tensor: An N-D Tensor of real data type with the same precision as that of x's data type.

Examples:
.. code-block:: python
.. code-block:: pycon

>>> import paddle

>>> x = paddle.to_tensor([-2, -1, 0, 1]).unsqueeze(-1).astype('float32')
>>> x = (
... paddle.to_tensor([-2, -1, 0, 1])
... .unsqueeze(-1)
... .astype('float32')
... )
>>> y = paddle.to_tensor([-2, -1, 0, 1]).astype('float32')
>>> z = x + 1j * y
>>> z
Tensor(shape=[4, 4], dtype=complex64, place=Place(cpu), stop_gradient=True,
[[(-2-2j), (-2-1j), (-2+0j), (-2+1j)],
[(-1-2j), (-1-1j), (-1+0j), (-1+1j)],
[-2j , -1j , 0j , 1j ],
[ (1-2j), (1-1j), (1+0j), (1+1j)]])
[[(-2.00000000-2.00000000j), (-2.00000000-1.00000000j),
(-2.00000000+0.00000000j), (-2.00000000+1.00000000j)],
[(-1.00000000-2.00000000j), (-1.00000000-1.00000000j),
(-1.00000000+0.00000000j), (-1.00000000+1.00000000j)],
[(0.00000000-2.00000000j) , (0.00000000-1.00000000j) ,
(0.00000000+0.00000000j), (0.00000000+1.00000000j)],
[ (1.00000000-2.00000000j), (1.00000000-1.00000000j),
(1.00000000+0.00000000j), (1.00000000+1.00000000j)]])

>>> theta = paddle.angle(z)
>>> theta
Expand Down
Loading