Skip to content
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

fix behavior of device_id=None in Tensor.cuda #44515

Merged
merged 3 commits into from
Jul 26, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void FusedAttentionCsrGradKernel(const Context& dev_ctx,
#if CUDA_VERSION >= 11070
/* Step1: Forward: softmax{CSR} * value{Dense} -> out{Dense}, reuse */
SparseCsrTensor dsoftmax;
CsrDenseMatmulGradKernel<T, Context>(
MatmulCsrDenseGradKernel<T, Context>(
dev_ctx, softmax, value, dout, &dsoftmax, dvalue);

/* Step2: Calculate grad of sdd_result, manualy not reuse */
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/sparse/gpu/fused_attention_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void FusedAttentionCsrKernel(

/* Step3: DSD Matmul, reuse */
softmax->set_dims(phi::make_ddim({q_dim[0], q_dim[1], q_dim[2], q_dim[2]}));
CsrDenseMatmulKernel<T, Context>(dev_ctx, *softmax, value, out);
MatmulCsrDenseKernel<T, Context>(dev_ctx, *softmax, value, out);
#else
PADDLE_THROW(
phi::errors::Unimplemented("forward of 'sparse.nn.functional.attention' "
Expand Down
17 changes: 11 additions & 6 deletions python/paddle/fluid/dygraph/varbase_patch_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,15 +866,20 @@ def cpu(self):
return res

@framework.dygraph_only
def cuda(self, device_id=0, blocking=True):
def cuda(self, device_id=None, blocking=True):
if device_id is None:
device_id = 0
if not isinstance(device_id, int):
raise ValueError("\'device_id\' must be a positive integer")
if self.place.is_gpu_place():
res_place = framework._current_expected_place()
if not isinstance(res_place, core.CUDAPlace):
res_place = core.CUDAPlace(0)
elif isinstance(device_id, int):
res_place = core.CUDAPlace(device_id)
else:
Copy link
Contributor

Choose a reason for hiding this comment

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

如果 device_id=0 也会走入这个分支吧?所以是不是改为

if device_id: -> if device_id is not None:

Copy link
Contributor Author

@zhwesky2010 zhwesky2010 Jul 21, 2022

Choose a reason for hiding this comment

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

Done, thx

Copy link
Contributor

Choose a reason for hiding this comment

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

除了 int 类型 和 None 以外是不是也可以支持 core.CUDAPlace 类?这样就不用走 res_place = core.CUDAPlace(device_id) 语句了,同时用户在外面可以直接通过某个 tensor 直接get 到 CUDAPlace 传进来。

这是一个讨论问题。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

raise ValueError("device_id must be int|None")

if self.place._equals(res_place):
return self
else:
res = self._copy_to(core.CUDAPlace(device_id), True)
res = self._copy_to(res_place, True)
res.stop_gradient = self.stop_gradient
res.persistable = self.persistable
return res
Expand Down
19 changes: 10 additions & 9 deletions python/paddle/fluid/tests/unittests/test_var_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def setUp(self):

def func_test_to_tensor(self):

def _test_place(place):
def check_with_place(place):
with fluid.dygraph.guard():
paddle.set_default_dtype('float32')
# set_default_dtype should not take effect on int
Expand Down Expand Up @@ -79,6 +79,7 @@ def _test_place(place):
y = x.pin_memory()
self.assertEqual(y.place.__repr__(), "Place(gpu_pinned)")
y = x.cuda()
self.assertEqual(y.place.__repr__(), "Place(gpu:0)")
y = x.cuda(None)
self.assertEqual(y.place.__repr__(), "Place(gpu:0)")
y = x.cuda(device_id=0)
Expand Down Expand Up @@ -266,16 +267,16 @@ def _test_place(place):
with self.assertRaises(ValueError):
paddle.to_tensor([[1], [2, 3]], place=1)

_test_place(core.CPUPlace())
_test_place("cpu")
check_with_place(core.CPUPlace())
check_with_place("cpu")
if core.is_compiled_with_cuda():
_test_place(core.CUDAPinnedPlace())
_test_place("gpu_pinned")
_test_place(core.CUDAPlace(0))
_test_place("gpu:0")
check_with_place(core.CUDAPinnedPlace())
check_with_place("gpu_pinned")
check_with_place(core.CUDAPlace(0))
check_with_place("gpu:0")
if core.is_compiled_with_npu():
_test_place(core.NPUPlace(0))
_test_place("npu:0")
check_with_place(core.NPUPlace(0))
check_with_place("npu:0")

def test_to_tensor(self):
with _test_eager_guard():
Expand Down