Skip to content

Commit

Permalink
Revert "Revert D25003113: make validate debug-only in Device copy ctr" (
Browse files Browse the repository at this point in the history
#49123)

Summary:
Pull Request resolved: #49123

This reverts commit 7a4a2df.

Test Plan: Imported from OSS

Reviewed By: ezyang

Differential Revision: D25463531

Pulled By: bdhirsh

fbshipit-source-id: 7c7ecdc1d63ffd137b84a129887c424b2083a958
  • Loading branch information
bdhirsh authored and facebook-github-bot committed Dec 14, 2020
1 parent 94a3d4b commit f54ab8f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 16 deletions.
8 changes: 6 additions & 2 deletions c10/core/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ struct C10_API Device final {
DeviceType type_;
DeviceIndex index_ = -1;
void validate() {
TORCH_CHECK(index_ == -1 || index_ >= 0,
// Removing these checks in release builds noticeably improves
// performance in micro-benchmarks.
// This is safe to do, because backends that use the DeviceIndex
// have a later check when we actually try to switch to that device.
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(index_ == -1 || index_ >= 0,
"Device index must be -1 or non-negative, got ", (int)index_);
TORCH_CHECK(!is_cpu() || index_ <= 0,
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(!is_cpu() || index_ <= 0,
"CPU device index must be -1 or zero, got ", (int)index_);
}
};
Expand Down
4 changes: 0 additions & 4 deletions test/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,6 @@ def test_device(self):
self.assertEqual(90, cuda90.index)

self.assertRaises(RuntimeError, lambda: torch.device('cpu:-1'))
self.assertRaises(RuntimeError, lambda: torch.device('cpu:1'))
self.assertRaises(RuntimeError, lambda: torch.device('cpu', -1))
self.assertRaises(RuntimeError, lambda: torch.device('cpu', 1))
self.assertRaises(RuntimeError, lambda: torch.device('cuda:-1'))
self.assertRaises(RuntimeError, lambda: torch.device('cuda:2 '))
self.assertRaises(RuntimeError, lambda: torch.device('cuda: 2'))
Expand All @@ -356,7 +353,6 @@ def test_device(self):
self.assertRaises(RuntimeError, lambda: torch.device('cuda:2 cuda:3'))
self.assertRaises(RuntimeError, lambda: torch.device('cuda:2+cuda:3'))
self.assertRaises(RuntimeError, lambda: torch.device('cuda:2cuda:3'))
self.assertRaises(RuntimeError, lambda: torch.device('cuda', -1))
self.assertRaises(RuntimeError, lambda: torch.device(-1))

self.assertRaises(RuntimeError, lambda: torch.device('other'))
Expand Down
10 changes: 0 additions & 10 deletions torch/testing/_internal/distributed/nn/api/remote_module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,6 @@ def test_invalid_devices(self):
)
)

with self.assertRaisesRegex(
RuntimeError, r"CPU device index must be -1 or zero, got 2"
):
list(
self._create_remote_module_iter(
"{}/cpu:2".format(dst_worker_name),
modes=[ModuleCreationMode.MODULE_CTOR],
)
)

with self.assertRaisesRegex(RuntimeError, r"Device string must not be empty"):
list(
self._create_remote_module_iter(
Expand Down

0 comments on commit f54ab8f

Please sign in to comment.