Skip to content

[Typing] update pybind11 stub #72553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions python/paddle/base/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -1969,7 +1969,7 @@ def backward(self, retain_graph=False):
>>> import paddle
>>> paddle.disable_static()

>>> x = np.ones([2, 2], np.float32) # type: ignore[var-annotated]
>>> x = np.ones([2, 2], np.float32)
>>> inputs = []
>>> for _ in range(10):
... tmp = paddle.to_tensor(x)
Expand Down Expand Up @@ -2013,7 +2013,7 @@ def gradient(self):
>>> import numpy as np

>>> # example1: return ndarray
>>> x = np.ones([2, 2], np.float32) # type: ignore[var-annotated]
>>> x = np.ones([2, 2], np.float32)
>>> with base.dygraph.guard():
... inputs2 = []
... for _ in range(10):
Expand Down Expand Up @@ -2062,7 +2062,7 @@ def clear_gradient(self):
>>> import paddle.base as base
>>> import numpy as np

>>> x = np.ones([2, 2], np.float32) # type: ignore[var-annotated]
>>> x = np.ones([2, 2], np.float32)
>>> inputs2 = []
>>> for _ in range(10):
>>> tmp = paddle.to_tensor(x)
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/base/layers/math_op_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def astype(self, dtype):
>>> import paddle
>>> import numpy as np

>>> x = np.ones([2, 2], np.float32) # type: ignore[var-annotated]
>>> x = np.ones([2, 2], np.float32)
>>> with base.dygraph.guard():
... original_variable = paddle.to_tensor(x)
... print("original var's dtype is: {}, numpy dtype is {}".format(original_variable.dtype, original_variable.numpy().dtype))
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/distributed/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ class DataParallel(Layer):
... model = paddle.DataParallel(model)
... opt = paddle.optimizer.SGD(learning_rate=0.01, parameters=model.parameters())
... for step in range(10):
... x_data = numpy.random.randn(2, 2).astype(numpy.float32) # type: ignore[var-annotated]
... x_data = numpy.random.randn(2, 2).astype(numpy.float32)
... x = paddle.to_tensor(x_data)
... x.stop_gradient = False
... # step 1 : skip gradient synchronization by 'no_sync'
Expand Down
6 changes: 3 additions & 3 deletions python/paddle/incubate/optimizer/lbfgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ class LBFGS(Optimizer):

>>> paddle.disable_static()
>>> np.random.seed(0)
>>> np_w = np.random.rand(1).astype(np.float32) # type: ignore[var-annotated]
>>> np_x = np.random.rand(1).astype(np.float32) # type: ignore[var-annotated]
>>> np_w = np.random.rand(1).astype(np.float32)
>>> np_x = np.random.rand(1).astype(np.float32)

>>> inputs = [np.random.rand(1).astype(np.float32) for i in range(10)] # type: ignore[var-annotated]
>>> inputs = [np.random.rand(1).astype(np.float32) for i in range(10)]
>>> # y = 2x
>>> targets = [2 * x for x in inputs]

Expand Down
6 changes: 3 additions & 3 deletions python/paddle/optimizer/lbfgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,10 @@ class LBFGS(Optimizer):

>>> paddle.disable_static()
>>> np.random.seed(0)
>>> np_w = np.random.rand(1).astype(np.float32) # type: ignore[var-annotated]
>>> np_x = np.random.rand(1).astype(np.float32) # type: ignore[var-annotated]
>>> np_w = np.random.rand(1).astype(np.float32)
>>> np_x = np.random.rand(1).astype(np.float32)

>>> inputs = [np.random.rand(1).astype(np.float32) for i in range(10)] # type: ignore[var-annotated]
>>> inputs = [np.random.rand(1).astype(np.float32) for i in range(10)]
>>> # y = 2x
>>> targets = [2 * x for x in inputs]

Expand Down
4 changes: 2 additions & 2 deletions python/paddle/static/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def data(

# In this example, we will feed x and y with np-ndarray "1"
# and fetch z, like implementing "1 + 1 = 2" in PaddlePaddle
>>> feed_data = np.ones(shape=[3, 2, 1], dtype=np.float32) # type: ignore[var-annotated]
>>> feed_data = np.ones(shape=[3, 2, 1], dtype=np.float32)

>>> exe = paddle.static.Executor(paddle.framework.CPUPlace())
>>> out = exe.run(paddle.static.default_main_program(),
Expand Down Expand Up @@ -308,7 +308,7 @@ def from_numpy(
>>> import numpy as np
>>> from paddle.static import InputSpec

>>> x = np.ones([2, 2], np.float32) # type: ignore[var-annotated]
>>> x = np.ones([2, 2], np.float32)
>>> x_spec = InputSpec.from_numpy(x, name='x')
>>> print(x_spec)
InputSpec(shape=(2, 2), dtype=paddle.float32, name=x, stop_gradient=False)
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/static/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ def load_inference_model(

>>> [inference_program, feed_target_names, fetch_targets] = (
... paddle.static.load_inference_model(path_prefix, exe))
>>> tensor_img = np.array(np.random.random((64, 784)), dtype=np.float32) # type: ignore[var-annotated]
>>> tensor_img = np.array(np.random.random((64, 784)), dtype=np.float32)
>>> results = exe.run(inference_program,
... feed={feed_target_names[0]: tensor_img},
... fetch_list=fetch_targets)
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/static/nn/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3578,7 +3578,7 @@ def embedding(
>>> exe = paddle.static.Executor(place)
>>> exe.run(paddle.static.default_startup_program())

>>> x = np.array([[7, 2, 4, 5],[4, 3, 2, 9]], dtype=np.int64) # type: ignore[var-annotated]
>>> x = np.array([[7, 2, 4, 5],[4, 3, 2, 9]], dtype=np.int64)
>>> out, = exe.run(paddle.static.default_main_program(), feed={'x':x}, fetch_list=[output])
>>> print(out)
[[[1. 1. 1.]
Expand Down
76 changes: 36 additions & 40 deletions python/paddle/static/nn/static_pylayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,46 +312,42 @@ def static_pylayer(forward_fn, inputs, backward_fn=None, name=None):
Examples:
.. code-block:: python

>>> import paddle
>>> import numpy as np

>>> paddle.enable_static()

>>> def forward_fn(x):
... return paddle.exp(x)

>>> def backward_fn(dy):
... return 2 * paddle.exp(dy)

>>> main_program = paddle.static.Program()
>>> start_program = paddle.static.Program()

>>> place = paddle.CPUPlace()
>>> exe = paddle.static.Executor(place)
>>> with paddle.static.program_guard(main_program, start_program):
... data = paddle.static.data(name="X", shape=[None, 5], dtype="float32")
... data.stop_gradient = False
... ret = paddle.static.nn.static_pylayer(forward_fn, [data], backward_fn)
... data_grad = paddle.static.gradients([ret], data)[0]

>>> exe.run(start_program)
>>> x = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32) # type: ignore[var-annotated]
>>> x, x_grad, y = exe.run(
... main_program,
... feed={"X": x},
... fetch_list=[
... data.name,
... data_grad.name,
... ret.name
... ],
... )

>>> print(x)
[[1. 2. 3. 4. 5.]]
>>> print(x_grad)
[[5.4365635 5.4365635 5.4365635 5.4365635 5.4365635]]
>>> print(y)
[[ 2.7182817 7.389056 20.085537 54.59815 148.41316 ]]
>>> import paddle
>>> import numpy as np

>>> paddle.enable_static()

>>> def forward_fn(x):
... return paddle.exp(x)

>>> def backward_fn(dy):
... return 2 * paddle.exp(dy)

>>> main_program = paddle.static.Program()
>>> start_program = paddle.static.Program()

>>> place = paddle.CPUPlace()
>>> exe = paddle.static.Executor(place)
>>> with paddle.static.program_guard(main_program, start_program):
... data = paddle.static.data(name="X", shape=[None, 5], dtype="float32")
... data.stop_gradient = False
... ret = paddle.static.nn.static_pylayer(forward_fn, [data], backward_fn)
... data_grad = paddle.static.gradients([ret], data)[0]

>>> exe.run(start_program)
>>> x = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)
>>> x, x_grad, y = exe.run(
... main_program,
... feed={"X": x},
... fetch_list=[data, data_grad, ret],
... )

>>> print(x)
[[1. 2. 3. 4. 5.]]
>>> print(x_grad)
[[5.4365635 5.4365635 5.4365635 5.4365635 5.4365635]]
>>> print(y)
[[ 2.7182817 7.389056 20.085537 54.59815 148.41316 ]]
"""
assert (
in_dygraph_mode() is False
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/static/pir_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ def load_inference_model_pir(path_prefix, executor, **kwargs):

>>> [inference_program, feed_target_names, fetch_targets] = (
... paddle.static.load_inference_model(path_prefix, exe))
>>> tensor_img = np.array(np.random.random((64, 784)), dtype=np.float32) # type: ignore[var-annotated]
>>> tensor_img = np.array(np.random.random((64, 784)), dtype=np.float32)
>>> results = exe.run(inference_program,
... feed={feed_target_names[0]: tensor_img},
... fetch_list=fetch_targets)
Expand Down
10 changes: 5 additions & 5 deletions python/paddle/tensor/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ def shape(input: Tensor) -> Tensor:
input.shape = [3, 2]

Args:
input (Variable): The input can be N-D Tensor or SelectedRows with data type bool, bfloat16, float16, float32, float64, int32, int64.
input (Tensor): The input can be N-D Tensor or SelectedRows with data type bool, bfloat16, float16, float32, float64, int32, int64.
If input variable is type of SelectedRows, returns the shape of it's inner tensor.

Returns:
Variable (Tensor): The shape of the input variable.
Tensor: The shape of the input variable.

Examples:
.. code-block:: python
Expand All @@ -102,11 +102,11 @@ def shape(input: Tensor) -> Tensor:
>>> exe = paddle.static.Executor(paddle.CPUPlace())
>>> exe.run(paddle.static.default_startup_program())

>>> img = np.ones((3, 100, 100)).astype(np.float32) # type: ignore[var-annotated]
>>> img = np.ones((3, 100, 100)).astype(np.float32)

>>> res = exe.run(paddle.static.default_main_program(), feed={'x':img}, fetch_list=[output])
>>> res = exe.run(paddle.static.default_main_program(), feed={'x': img}, fetch_list=[output])
>>> print(res)
[array([ 3, 100, 100], dtype=int32)]
[array([ 3, 100, 100], dtype=int64)]
"""
if in_dynamic_or_pir_mode():
out = _C_ops.shape64(input) # type: ignore
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/tensor/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2645,7 +2645,7 @@ def assign(x: TensorLike, output: paddle.Tensor | None = None) -> paddle.Tensor:
[2.5 2.5]]
>>> array = np.array([[1, 1], [3, 4], [1, 3]]).astype(
... np.int64
... ) # type: ignore[var-annotated]
... )
>>> result1 = paddle.zeros(shape=[3, 3], dtype='float32')
>>> paddle.assign(array, result1)
>>> print(result1.numpy())
Expand Down
9 changes: 2 additions & 7 deletions python/paddle/vision/transforms/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,18 +437,13 @@ def adjust_brightness(
>>> fake_img = Image.fromarray(fake_img)
>>> print(fake_img.size)
(300, 256)
>>> print(fake_img.load()[1,1]) # type: ignore[index]
>>> print(fake_img.load()[1, 1]) # type: ignore[index]
(61, 155, 171)
>>> converted_img = F.adjust_brightness(fake_img, 0.5)
>>> print(converted_img.size)
(300, 256)
>>> print(converted_img.load()[1,1]) # type: ignore[index]
>>> print(converted_img.load()[1, 1])
(30, 77, 85)





"""
if not (
_is_pil_image(img) or _is_numpy_image(img) or _is_tensor_image(img)
Expand Down
30 changes: 15 additions & 15 deletions python/paddle/vision/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ class ToTensor(BaseTransform[_InputT, "Tensor"]):
>>> img_arr = ((paddle.rand((4, 5, 3)) * 255.).astype('uint8')).numpy()
>>> fake_img = Image.fromarray(img_arr)
>>> transform = T.ToTensor()
>>> tensor = transform(fake_img) # type: ignore[call-overload]
>>> tensor = transform(fake_img)
>>> print(tensor.shape)
[3, 4, 5]
>>> print(tensor.dtype)
Expand Down Expand Up @@ -457,11 +457,11 @@ class Resize(BaseTransform[_InputT, _RetT]):

>>> fake_img = Image.fromarray((np.random.rand(256, 300, 3) * 255.).astype(np.uint8))
>>> transform = Resize(size=224)
>>> converted_img = transform(fake_img) # type: ignore[call-overload]
>>> converted_img = transform(fake_img)
>>> print(converted_img.size)
(262, 224)
>>> transform = Resize(size=(200,150))
>>> converted_img = transform(fake_img) # type: ignore[call-overload]
>>> converted_img = transform(fake_img)
>>> print(converted_img.size)
(150, 200)
"""
Expand Down Expand Up @@ -530,7 +530,7 @@ class RandomResizedCrop(BaseTransform[_InputT, _RetT]):

>>> transform = RandomResizedCrop(224)
>>> fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img) # type: ignore[call-overload]
>>> fake_img = transform(fake_img)
>>> print(fake_img.size)
(224, 224)

Expand Down Expand Up @@ -736,7 +736,7 @@ class CenterCrop(BaseTransform[_InputT, _RetT]):

>>> transform = CenterCrop(224)
>>> fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img) # type: ignore[call-overload]
>>> fake_img = transform(fake_img)
>>> print(fake_img.size)
(224, 224)

Expand Down Expand Up @@ -918,7 +918,7 @@ class Normalize(BaseTransform[_InputT, _RetT]):
... data_format='HWC')
...
>>> fake_img = paddle.rand([300,320,3]).numpy() * 255.
>>> fake_img = normalize(fake_img) # type: ignore[call-overload]
>>> fake_img = normalize(fake_img)
>>> print(fake_img.shape)
(300, 320, 3)
>>> print(fake_img.max(), fake_img.min())
Expand Down Expand Up @@ -985,7 +985,7 @@ class Transpose(BaseTransform[_InputT, _RetT]):

>>> transform = Transpose()
>>> fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img) # type: ignore[call-overload]
>>> fake_img = transform(fake_img)
>>> print(fake_img.shape)
(3, 300, 320)

Expand Down Expand Up @@ -1042,7 +1042,7 @@ class BrightnessTransform(BaseTransform[_InputT, _RetT]):
>>> print(fake_img.load()[1,1]) # type: ignore[index]
(60, 169, 34)
>>> # doctest: +SKIP('random sample in Brightness function')
>>> fake_img = transform(fake_img) # type: ignore[call-overload]
>>> fake_img = transform(fake_img)
>>> print(fake_img.load()[1,1])
(68, 192, 38)

Expand Down Expand Up @@ -1089,7 +1089,7 @@ class ContrastTransform(BaseTransform[_InputT, _RetT]):

>>> transform = ContrastTransform(0.4)
>>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img) # type: ignore[call-overload]
>>> fake_img = transform(fake_img)
>>> print(fake_img.size)
(224, 224)

Expand Down Expand Up @@ -1138,7 +1138,7 @@ class SaturationTransform(BaseTransform[_InputT, _RetT]):

>>> transform = SaturationTransform(0.4)
>>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img) # type: ignore[call-overload]
>>> fake_img = transform(fake_img)
>>> print(fake_img.size)
(224, 224)
"""
Expand Down Expand Up @@ -1184,7 +1184,7 @@ class HueTransform(BaseTransform[_InputT, _RetT]):

>>> transform = HueTransform(0.4)
>>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img) # type: ignore[call-overload]
>>> fake_img = transform(fake_img)
>>> print(fake_img.size)
(224, 224)

Expand Down Expand Up @@ -1239,7 +1239,7 @@ class ColorJitter(BaseTransform[_InputT, _RetT]):

>>> transform = ColorJitter(0.4, 0.4, 0.4, 0.4)
>>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img) # type: ignore[call-overload]
>>> fake_img = transform(fake_img)
>>> print(fake_img.size)
(224, 224)

Expand Down Expand Up @@ -1480,7 +1480,7 @@ class Pad(BaseTransform[_InputT, _RetT]):

>>> transform = Pad(2)
>>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img) # type: ignore[call-overload]
>>> fake_img = transform(fake_img)
>>> print(fake_img.size)
(228, 228)
"""
Expand Down Expand Up @@ -1778,7 +1778,7 @@ class RandomRotation(BaseTransform[_InputT, _RetT]):

>>> transform = RandomRotation(90)
>>> fake_img = Image.fromarray((np.random.rand(200, 150, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img) # type: ignore[call-overload]
>>> fake_img = transform(fake_img)
>>> print(fake_img.size)
(150, 200)
"""
Expand Down Expand Up @@ -2012,7 +2012,7 @@ class Grayscale(BaseTransform[_InputT, _RetT]):

>>> transform = Grayscale()
>>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img) # type: ignore[call-overload]
>>> fake_img = transform(fake_img)
>>> print(np.array(fake_img).shape)
(224, 224)
"""
Expand Down
Loading
Loading