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 copy bug #7159

Merged
merged 7 commits into from
Dec 31, 2021
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
4 changes: 2 additions & 2 deletions oneflow/user/ops/copy_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ namespace {
Maybe<Symbol<Device>> MakeOpDevice(const Symbol<Device>& in_device,
const Symbol<Device>& out_device) {
if (JUST(in_device->of_type()) == "gpu" && JUST(out_device->of_type()) == "cpu") {
return Device::New("cuda_d2h");
return Device::New("cuda_d2h", in_device->device_id());
} else if (JUST(in_device->of_type()) == "cpu" && JUST(out_device->of_type()) == "gpu") {
return Device::New("cuda_h2d");
return Device::New("cuda_h2d", out_device->device_id());
Copy link
Contributor

Choose a reason for hiding this comment

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

这里是修复了copy op实现上的一个bug:在new Device时如果不带上输出的device id,会走到

/* static */ Maybe<Symbol<Device>> Device::New(const std::string& type) {
  return New(type, GlobalProcessCtx::LocalRank());
}

仅通过type构造的Device,其device id默认为本进程所在rank,而不是to.("cuda:x")的目标device x

} else {
return Device::New(out_device->type(), out_device->device_id());
}
Expand Down
9 changes: 9 additions & 0 deletions python/oneflow/test/modules/test_tensor_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def test_consistent_tensor_to(test_case):
test_case.assertEqual(cloned_local[0].numpy().item(), 0)
test_case.assertEqual(x.to_local()[0].numpy().item(), 1)

def test_tensor_to_h2d1(test_case):
input = flow.tensor(np.random.randn(2, 3, 4, 5), dtype=flow.int64)
output = input.to(device=flow.device("cuda:1"), dtype=flow.int32)
test_case.assertEqual(output.device, flow.device("cuda:1"))
test_case.assertEqual(output.dtype, flow.int32)
test_case.assertTrue(
np.allclose(input.numpy(), output.numpy(), rtol=0.0001, atol=0.0001)
)


@flow.unittest.skip_unless_1n1d()
@unittest.skipIf(os.getenv("ONEFLOW_TEST_CPU_ONLY"), "only test cpu cases")
Expand Down