Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
15399e4
Add English explanation
SCUcookie Nov 14, 2024
8986f04
Merge branch 'PaddlePaddle:develop' into develop
SCUcookie Nov 15, 2024
17bbaeb
Merge branch 'PaddlePaddle:develop' into develop
SCUcookie Nov 15, 2024
773e954
Merge branch 'PaddlePaddle:develop' into develop
SCUcookie Nov 18, 2024
09fe571
Merge branch 'PaddlePaddle:develop' into develop
SCUcookie Nov 18, 2024
d20ae97
Merge branch 'PaddlePaddle:develop' into develop
SCUcookie Nov 18, 2024
18349c5
modify code
SCUcookie Nov 18, 2024
34d57d5
Merge branch 'PaddlePaddle:develop' into develop
SCUcookie Nov 18, 2024
2461584
add __rand__
SCUcookie Nov 18, 2024
72fc12d
cancel update
SCUcookie Nov 18, 2024
6d4addc
add __rand__
SCUcookie Nov 18, 2024
eeb07ec
delete superfluous code
SCUcookie Nov 18, 2024
48e14ac
recover code
SCUcookie Nov 18, 2024
ae0a24b
Merge branch 'PaddlePaddle:develop' into Origin
SCUcookie Nov 18, 2024
82c622c
develop
SCUcookie Nov 18, 2024
73bb823
recover
SCUcookie Nov 18, 2024
cf81a6d
Merge branch 'PaddlePaddle:develop' into develop
SCUcookie Nov 18, 2024
376e452
add __rand__
SCUcookie Nov 18, 2024
777324f
Merge branch 'develop' of github.com:SCUcookie/Paddle into MyTensor
SCUcookie Nov 18, 2024
43a4d5c
Merge branch 'Origin' of github.com:SCUcookie/Paddle into MyTensor
SCUcookie Nov 18, 2024
214b64c
finish
SCUcookie Nov 18, 2024
b1a12a2
modify v1
SCUcookie Nov 19, 2024
3bb3cdd
question
SCUcookie Nov 21, 2024
c99d929
try
SCUcookie Nov 21, 2024
a456dbe
precommit
SCUcookie Nov 21, 2024
fddc3c2
modifying
SCUcookie Nov 21, 2024
c8e6a5f
upstream
SCUcookie Nov 25, 2024
024397d
try
SCUcookie Nov 25, 2024
9348366
modify
SCUcookie Nov 25, 2024
a79bad9
modify
SCUcookie Nov 26, 2024
0b65ceb
modify
SCUcookie Nov 26, 2024
6976b37
remove prototype
SCUcookie Nov 26, 2024
4e6dc05
finish
SCUcookie Nov 27, 2024
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
48 changes: 48 additions & 0 deletions python/paddle/base/dygraph/tensor_patch_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def _to_static_var(self, to_parameter=False, **kwargs):
'strides',
'offset',
'__cuda_array_interface__',
'__dlpack__',
]
param_keys = ['stop_gradient', 'trainable']
if isinstance(self, EagerParamBase):
Expand Down Expand Up @@ -1330,6 +1331,52 @@ def __cuda_array_interface__(self):
"version": 2,
}

def __dlpack__(self, stream=None):
"""
Creates a DLPack capsule of the current tensor to be exported to other libraries.

Args:
stream (int | None): An optional Python integer representing a pointer
to a CUDA stream. Synchronizes the tensor with this
stream before exporting.
If None or -1, no synchronization is performed.
If 0, the default stream is used.
"""
if "gpu" not in str(self.place):
raise AttributeError(
"Can't get __dlpack__ on non-CUDA tensor. "
"Use tensor.cuda() to move the tensor to device memory."
)

if self.is_sparse():
raise AttributeError(
"Can't get __dlpack__ on sparse tensor. "
"Use Tensor.to_dense() to convert to a dense tensor first."
)

if not self.stop_gradient:
raise RuntimeError(
"Can't get __dlpack__ on Tensor that requires gradients. "
"If gradients aren't required, use tensor.detach() to get a tensor without gradient."
)

if stream is not None and not isinstance(stream, int):
raise TypeError("stream must be an integer or None")

if stream is not None and stream != -1:
if self.place.is_gpu_place():
if stream == 0:
stream = paddle.device.cuda.default_stream()
else:
stream = paddle.device.cuda.ExternalStream(stream)
current_stream = paddle.device.cuda.current_stream()
if stream != current_stream:
event = paddle.device.cuda.Event()
event.record(current_stream)
stream.wait_event(event)

return paddle.utils.dlpack.to_dlpack(self)

if not hasattr(core, "eager"):
return

Expand Down Expand Up @@ -1374,6 +1421,7 @@ def __cuda_array_interface__(self):
("_use_gpudnn", _use_gpudnn),
("_md5sum", _md5sum),
("__cuda_array_interface__", __cuda_array_interface__),
("__dlpack__", __dlpack__),
):
setattr(core.eager.Tensor, method_name, method)

Expand Down
1 change: 1 addition & 0 deletions test/dygraph_to_static/test_tensor_attr_consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
'value',
'zero_',
"__cuda_array_interface__",
"__dlpack__",
]
)
STATIC_ONLY_TENSOR_ATTRS_ALLOW_LIST = OrderedSet(
Expand Down
9 changes: 9 additions & 0 deletions test/legacy_test/test_dlpack.py
Copy link
Contributor

@HydrogenSulfate HydrogenSulfate Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__dlpack__作为Tensor的类方法,和__cuda_array_interface__是类似的:

def __cuda_array_interface__(self):
参考这个PR写一下就行:#68192

Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,15 @@ def test_to_dlpack_from_zero_size(self):
np.testing.assert_array_equal(x.numpy(), y1.numpy())
np.testing.assert_array_equal(x.numpy(), y2.numpy())

def test_dlpack_basic(self):
tensor = paddle.to_tensor([1.0, 2.0, 3.0])
dlpack_capsule = tensor.__dlpack__()
self.assertIsNotNone(dlpack_capsule)
Comment on lines +328 to +331
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只判断是不是None是不够的,需要将dlpack_capsule用paddle.from_dlpack重新转回Tensor,检测转换前的两个tensor里的值,以及.data_ptr()是不是一样的

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的老师


converted_tensor = paddle.from_dlpack(dlpack_capsule)
self.assertTrue(paddle.equal_all(tensor, converted_tensor))
self.assertEqual(tensor.data_ptr(), converted_tensor.data_ptr())
Comment on lines +328 to +335
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个单测有两个问题:

  1. 设备类型没有测试全,只测了当前默认设备。
  2. 没有测试stream这个参数非None的情况



class TestRaiseError(unittest.TestCase):
def test_to_dlpack_raise_type_error(self):
Expand Down
2 changes: 1 addition & 1 deletion third_party/pybind
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个文件记得还原回去

Submodule pybind updated 178 files
6 changes: 3 additions & 3 deletions tools/enforce/grep_invalid_enforce.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
# This script is used to grep invalid PADDLE checks by directory or file in the paddle/fluid/,
# the result show all invalid PADDLE checks in specified directory or file.

# Usage:
# Usage:
# - bash grep_invalid_enforce.sh [target directory or file] (run in tools directory)
# - The default check path is paddle/fluid/operators

# Result Examples:
# 1. grep invalid PADDLE checks in directory

# - Command: /work/paddle/tools {develop} bash grep_invalid_enforce.sh ../paddle/fluid/imperative
# - Command: /work/paddle/tools {develop} bash grep_invalid_enforce.sh ../paddle/fluid/imperative
# - Results:
# - paddle/fluid/imperative/gradient_accumulator.cc
# PADDLE_ENFORCE_EQ(dst_tensor->numel() == numel, true,
Expand Down Expand Up @@ -60,7 +60,7 @@
# "Place cannot be CUDAPlace when use_double_buffer is False");
# PADDLE_ENFORCE_NOT_NULL(exceptions_[i]);
# PADDLE_ENFORCE_EQ(status, Status::kException);
# PADDLE_ENFORCE_EQ(status, Status::kSuccess);
# PADDLE_ENFORCE_EQ(status, Status::kSuccess);

. ./count_enforce_by_file.sh --source-only

Expand Down