Skip to content

Commit

Permalink
[xdoctest][task 326-329] reformat example code with google style in …
Browse files Browse the repository at this point in the history
…paddle/fluid/pybind/* (PaddlePaddle#57155)

* [Doctest]fix No.326-329, test=docs_preview

* replace base
  • Loading branch information
Difers committed Sep 12, 2023
1 parent 1c0f8a3 commit 84be35a
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 266 deletions.
33 changes: 18 additions & 15 deletions paddle/fluid/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1239,11 +1239,15 @@ All parameter, weight, gradient are variables in Paddle.
Examples:
.. code-block:: python
# create tensor from a scope and set value to it.
param = scope.var('Param').get_tensor()
param_array = np.full((height, row_numel), 5.0).astype("float32")
param.set(param_array, place)
>>> import paddle
>>> import numpy as np
>>> scope = paddle.static.global_scope()
>>> place = paddle.CPUPlace()
>>> # create tensor from a scope and set value to it.
>>> param = scope.var('Param').get_tensor()
>>> param_array = np.full((10, 12), 5.0).astype("float32")
>>> param.set(param_array, place)
)DOC");
g_framework_scope_pytype = reinterpret_cast<PyTypeObject *>(_Scope.ptr());
_Scope
Expand Down Expand Up @@ -2148,9 +2152,8 @@ All parameter, weight, gradient are variables in Paddle.
Examples:
.. code-block:: python
import paddle.base as base
arr = base.LoDTensorArray()
>>> import paddle
>>> arr = paddle.framework.core.LoDTensorArray()
)DOC");
g_framework_lodtensorarray_pytype =
reinterpret_cast<PyTypeObject *>(pylodtensorarray.ptr());
Expand Down Expand Up @@ -2190,15 +2193,15 @@ All parameter, weight, gradient are variables in Paddle.
None.
Examples:
.. code-block:: python
.. code-block:: python
import paddle.base as base
import numpy as np
>>> import paddle
>>> import numpy as np
arr = base.LoDTensorArray()
t = base.LoDTensor()
t.set(np.ndarray([5, 30]), base.CPUPlace())
arr.append(t)
>>> arr = paddle.framework.core.LoDTensorArray()
>>> t = paddle.framework.core.LoDTensor()
>>> t.set(np.ndarray([5, 30]), paddle.CPUPlace())
>>> arr.append(t)
)DOC")
.def(
"_move_to_list",
Expand Down
139 changes: 73 additions & 66 deletions paddle/fluid/pybind/tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,11 @@ void BindTensor(pybind11::module &m) { // NOLINT
Examples:
.. code-block:: python
import paddle.base as base
import numpy as np
>>> import paddle
>>> import numpy as np
t = base.Tensor()
t.set(np.ndarray([5, 30]), base.CPUPlace())
>>> t = paddle.framework.core.Tensor()
>>> t.set(np.ndarray([5, 30]), paddle.CPUPlace())
)DOC")

.def(
Expand All @@ -411,14 +411,15 @@ void BindTensor(pybind11::module &m) { // NOLINT
Examples:
.. code-block:: python
.. code-block:: python
import paddle.base as base
import numpy as np
>>> import paddle
>>> import numpy as np
t = base.Tensor()
t.set(np.ndarray([5, 30]), base.CPUPlace())
print(t.shape()) # [5, 30]
>>> t = paddle.framework.core.Tensor()
>>> t.set(np.ndarray([5, 30]), paddle.CPUPlace())
>>> print(t.shape())
[5, 30]
)DOC")
.def("_to_dlpack",
[](phi::DenseTensor &self) {
Expand Down Expand Up @@ -515,15 +516,16 @@ void BindTensor(pybind11::module &m) { // NOLINT
None.
Examples:
.. code-block:: python
.. code-block:: python
import paddle.base as base
import numpy as np
>>> import paddle
>>> import numpy as np
t = base.Tensor()
t.set(np.ndarray([5, 30]), base.CPUPlace())
t.set_lod([[0, 2, 5]])
print(t.lod()) # [[0, 2, 5]]
>>> t = paddle.framework.core.Tensor()
>>> t.set(np.ndarray([5, 30]), paddle.CPUPlace())
>>> t.set_lod([[0, 2, 5]])
>>> print(t.lod())
[[0, 2, 5]]
)DOC")
.def(
"set_recursive_sequence_lengths",
Expand Down Expand Up @@ -564,16 +566,18 @@ void BindTensor(pybind11::module &m) { // NOLINT
None.
Examples:
.. code-block:: python
import paddle.base as base
import numpy as np
t = base.Tensor()
t.set(np.ndarray([5, 30]), base.CPUPlace())
t.set_recursive_sequence_lengths([[2, 3]])
print(t.recursive_sequence_lengths()) # [[2, 3]]
print(t.lod()) # [[0, 2, 5]]
.. code-block:: python
>>> import paddle
>>> import numpy as np
>>> t = paddle.framework.core.Tensor()
>>> t.set(np.ndarray([5, 30]), paddle.CPUPlace())
>>> t.set_recursive_sequence_lengths([[2, 3]])
>>> print(t.recursive_sequence_lengths())
[[2, 3]]
>>> print(t.lod())
[[0, 2, 5]]
)DOC")
.def(
"lod",
Expand All @@ -592,15 +596,16 @@ void BindTensor(pybind11::module &m) { // NOLINT
list[list[int]]: The lod of the Tensor.
Examples:
.. code-block:: python
.. code-block:: python
import paddle.base as base
import numpy as np
>>> import paddle
>>> import numpy as np
t = base.Tensor()
t.set(np.ndarray([5, 30]), base.CPUPlace())
t.set_lod([[0, 2, 5]])
print(t.lod()) # [[0, 2, 5]]
>>> t = paddle.framework.core.Tensor()
>>> t.set(np.ndarray([5, 30]), paddle.CPUPlace())
>>> t.set_lod([[0, 2, 5]])
>>> print(t.lod())
[[0, 2, 5]]
)DOC")
// Set above comments of set_lod.
.def(
Expand All @@ -621,15 +626,16 @@ void BindTensor(pybind11::module &m) { // NOLINT
list[list[int]]: The recursive sequence lengths.
Examples:
.. code-block:: python
.. code-block:: python
import paddle.base as base
import numpy as np
>>> import paddle
>>> import numpy as np
t = base.Tensor()
t.set(np.ndarray([5, 30]), base.CPUPlace())
t.set_recursive_sequence_lengths([[2, 3]])
print(t.recursive_sequence_lengths()) # [[2, 3]]
>>> t = paddle.framework.core.Tensor()
>>> t.set(np.ndarray([5, 30]), paddle.CPUPlace())
>>> t.set_recursive_sequence_lengths([[2, 3]])
>>> print(t.recursive_sequence_lengths())
[[2, 3]]
)DOC")
.def(
"has_valid_recursive_sequence_lengths",
Expand All @@ -645,15 +651,16 @@ void BindTensor(pybind11::module &m) { // NOLINT
bool: Whether the LoD is valid.
Examples:
.. code-block:: python
.. code-block:: python
import paddle.base as base
import numpy as np
>>> import paddle
>>> import numpy as np
t = base.Tensor()
t.set(np.ndarray([5, 30]), base.CPUPlace())
t.set_recursive_sequence_lengths([[2, 3]])
print(t.has_valid_recursive_sequence_lengths()) # True
>>> t = paddle.framework.core.Tensor()
>>> t.set(np.ndarray([5, 30]), paddle.CPUPlace())
>>> t.set_recursive_sequence_lengths([[2, 3]])
>>> print(t.has_valid_recursive_sequence_lengths())
True
)DOC")
.def("_as_type",
[](const phi::DenseTensor &self,
Expand Down Expand Up @@ -773,12 +780,12 @@ void BindTensor(pybind11::module &m) { // NOLINT
tensor dims, lod information, device index.
Examples:
.. code-block:: python
.. code-block:: python
import paddle
tensor = paddle.ones([3,3])
metainfo = tensor.value().get_tensor()._share_cuda()
>>> import paddle
>>> tensor = paddle.ones([3,3])
>>> metainfo = tensor.value().get_tensor()._share_cuda()
)DOC")
.def("_new_shared_cuda",
[](py::tuple t) {
Expand Down Expand Up @@ -819,13 +826,13 @@ void BindTensor(pybind11::module &m) { // NOLINT
tensor dims, lod information, device index.
Examples:
.. code-block:: python
.. code-block:: python
import paddle
tensor = paddle.ones([3,3])
metainfo = tensor.value().get_tensor()._share_cuda()
tensor_from_shared = paddle.to_tensor(paddle.base.core.LoDTensor._new_shared_cuda(metainfo))
>>> import paddle
>>> tensor = paddle.ones([3,3])
>>> metainfo = tensor.value().get_tensor()._share_cuda()
>>> tensor_from_shared = paddle.to_tensor(paddle.base.core.LoDTensor._new_shared_cuda(metainfo))
)DOC")
#endif
.def("_share_filename",
Expand Down Expand Up @@ -896,12 +903,12 @@ void BindTensor(pybind11::module &m) { // NOLINT
tensor dims and lod imformation.
Examples:
.. code-block:: python
.. code-block:: python
import paddle
tensor = paddle.ones([3,3])
metainfo = tensor.value().get_tensor()._share_filename()
>>> import paddle
>>> tensor = paddle.ones([3,3])
>>> metainfo = tensor.value().get_tensor()._share_filename()
)DOC")
.def("_new_shared_filename",
[](py::tuple t) { // __setstate__
Expand Down Expand Up @@ -940,13 +947,13 @@ void BindTensor(pybind11::module &m) { // NOLINT
tensor dims and lod information.
Examples:
.. code-block:: python
.. code-block:: python
import paddle
tensor = paddle.ones([3,3])
metainfo = tensor.value().get_tensor()._share_filename()
tensor_from_shared = paddle.to_tensor(paddle.base.core.LoDTensor._new_shared_filename(metainfo))
>>> import paddle
>>> tensor = paddle.ones([3,3])
>>> metainfo = tensor.value().get_tensor()._share_filename()
>>> tensor_from_shared = paddle.to_tensor(paddle.base.core.LoDTensor._new_shared_filename(metainfo))
)DOC")
.def("_shared_incref",
[](phi::DenseTensor &self) {
Expand Down
Loading

0 comments on commit 84be35a

Please sign in to comment.