|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +# pyre-ignore-all-errors[56] |
| 9 | + |
| 10 | +import unittest |
| 11 | +from typing import List |
| 12 | + |
| 13 | +import fbgemm_gpu |
| 14 | +import hypothesis.strategies as st |
| 15 | +import torch |
| 16 | +from hypothesis import given, settings, Verbosity |
| 17 | + |
| 18 | +# pyre-fixme[16]: Module `fbgemm_gpu` has no attribute `open_source`. |
| 19 | +open_source: bool = getattr(fbgemm_gpu, "open_source", False) |
| 20 | + |
| 21 | +if open_source: |
| 22 | + # pyre-ignore[21] |
| 23 | + from test_utils import gpu_available, gpu_unavailable, skipIfRocm |
| 24 | +else: |
| 25 | + from fbgemm_gpu.test.test_utils import gpu_available, gpu_unavailable, skipIfRocm |
| 26 | + |
| 27 | +if gpu_available: |
| 28 | + # pyre-ignore[21] |
| 29 | + from fbgemm_gpu.uvm import cudaMemAdvise, cudaMemoryAdvise, cudaMemPrefetchAsync |
| 30 | + |
| 31 | + |
| 32 | +MAX_EXAMPLES = 40 |
| 33 | + |
| 34 | + |
| 35 | +class CopyTest(unittest.TestCase): |
| 36 | + @unittest.skipIf(*gpu_unavailable) |
| 37 | + @given( |
| 38 | + sizes=st.lists(st.integers(min_value=1, max_value=8), min_size=1, max_size=4), |
| 39 | + uvm_op=st.sampled_from( |
| 40 | + [ |
| 41 | + torch.ops.fbgemm.new_unified_tensor, |
| 42 | + torch.ops.fbgemm.new_managed_tensor, |
| 43 | + torch.ops.fbgemm.new_vanilla_managed_tensor, |
| 44 | + ] |
| 45 | + ), |
| 46 | + ) |
| 47 | + @settings(verbosity=Verbosity.verbose, max_examples=MAX_EXAMPLES, deadline=None) |
| 48 | + # pyre-fixme[2]: Parameter must be annotated. |
| 49 | + def test_uvm_to_cpu(self, sizes: List[int], uvm_op) -> None: |
| 50 | + if uvm_op is torch.ops.fbgemm.new_unified_tensor: |
| 51 | + is_host_mapped = False |
| 52 | + uvm_t = uvm_op( |
| 53 | + torch.empty(0, device="cuda:0", dtype=torch.float), |
| 54 | + sizes, |
| 55 | + is_host_mapped, |
| 56 | + ) |
| 57 | + else: |
| 58 | + uvm_t = uvm_op(torch.empty(0, device="cuda:0", dtype=torch.float), sizes) |
| 59 | + |
| 60 | + cpu_t = torch.ops.fbgemm.uvm_to_cpu(uvm_t) |
| 61 | + assert not torch.ops.fbgemm.is_uvm_tensor(cpu_t) |
| 62 | + assert torch.ops.fbgemm.uvm_storage(cpu_t) |
| 63 | + |
| 64 | + uvm_t.copy_(cpu_t) |
| 65 | + assert torch.ops.fbgemm.is_uvm_tensor(uvm_t) |
| 66 | + assert torch.ops.fbgemm.uvm_storage(uvm_t) |
| 67 | + |
| 68 | + # Test use of cpu tensor after freeing the uvm tensor |
| 69 | + del uvm_t |
| 70 | + cpu_t.mul_(42) |
| 71 | + |
| 72 | + @skipIfRocm() |
| 73 | + @unittest.skipIf( |
| 74 | + not torch.cuda.is_available() or torch.cuda.device_count() < 2, |
| 75 | + "Skip unless two CUDA devices are detected", |
| 76 | + ) |
| 77 | + @given( |
| 78 | + sizes=st.lists( |
| 79 | + st.integers(min_value=1, max_value=(1024)), min_size=1, max_size=4 |
| 80 | + ), |
| 81 | + uvm_op=st.sampled_from( |
| 82 | + [ |
| 83 | + torch.ops.fbgemm.new_unified_tensor, |
| 84 | + torch.ops.fbgemm.new_managed_tensor, |
| 85 | + torch.ops.fbgemm.new_vanilla_managed_tensor, |
| 86 | + ] |
| 87 | + ), |
| 88 | + ) |
| 89 | + @settings(verbosity=Verbosity.verbose, max_examples=MAX_EXAMPLES, deadline=None) |
| 90 | + # pyre-fixme[2]: Parameter must be annotated. |
| 91 | + def test_uvm_to_device(self, sizes: List[int], uvm_op) -> None: |
| 92 | + if uvm_op is torch.ops.fbgemm.new_unified_tensor: |
| 93 | + is_host_mapped = False |
| 94 | + uvm_t = uvm_op( |
| 95 | + torch.empty(0, device="cuda:0", dtype=torch.float), |
| 96 | + sizes, |
| 97 | + is_host_mapped, |
| 98 | + ) |
| 99 | + else: |
| 100 | + uvm_t = uvm_op(torch.empty(0, device="cuda:0", dtype=torch.float), sizes) |
| 101 | + |
| 102 | + assert torch.ops.fbgemm.is_uvm_tensor(uvm_t) |
| 103 | + assert torch.ops.fbgemm.uvm_storage(uvm_t) |
| 104 | + |
| 105 | + # Reference uvm tensor from second cuda device |
| 106 | + try: |
| 107 | + device_prototype = torch.empty(0, device="cuda:1") |
| 108 | + except RuntimeError: |
| 109 | + # Skip the tests if there is no "cuda:1" device |
| 110 | + return |
| 111 | + |
| 112 | + second_t = torch.ops.fbgemm.uvm_to_device(uvm_t, device_prototype) |
| 113 | + |
| 114 | + assert torch.ops.fbgemm.is_uvm_tensor(second_t) |
| 115 | + assert torch.ops.fbgemm.uvm_storage(second_t) |
| 116 | + assert second_t.device == device_prototype.device |
| 117 | + |
| 118 | + @unittest.skipIf(*gpu_unavailable) |
| 119 | + @given( |
| 120 | + sizes=st.lists( |
| 121 | + st.integers(min_value=1, max_value=(512)), min_size=1, max_size=3 |
| 122 | + ), |
| 123 | + uvm_op=st.sampled_from( |
| 124 | + [ |
| 125 | + torch.ops.fbgemm.new_unified_tensor, |
| 126 | + torch.ops.fbgemm.new_managed_tensor, |
| 127 | + torch.ops.fbgemm.new_vanilla_managed_tensor, |
| 128 | + ] |
| 129 | + ), |
| 130 | + ) |
| 131 | + @settings(verbosity=Verbosity.verbose, max_examples=MAX_EXAMPLES, deadline=None) |
| 132 | + # pyre-fixme[2]: Parameter must be annotated. |
| 133 | + def test_uvm_to_cpu_clone(self, sizes: List[int], uvm_op) -> None: |
| 134 | + if uvm_op is torch.ops.fbgemm.new_unified_tensor: |
| 135 | + is_host_mapped = False |
| 136 | + uvm_t = uvm_op( |
| 137 | + torch.empty(0, device="cuda:0", dtype=torch.float), |
| 138 | + sizes, |
| 139 | + is_host_mapped, |
| 140 | + ) |
| 141 | + else: |
| 142 | + uvm_t = uvm_op(torch.empty(0, device="cuda:0", dtype=torch.float), sizes) |
| 143 | + |
| 144 | + assert torch.ops.fbgemm.is_uvm_tensor(uvm_t) |
| 145 | + assert torch.ops.fbgemm.uvm_storage(uvm_t) |
| 146 | + |
| 147 | + cpu_clone = torch.ops.fbgemm.uvm_to_cpu_clone(uvm_t) |
| 148 | + |
| 149 | + assert not torch.ops.fbgemm.is_uvm_tensor(cpu_clone) |
| 150 | + assert not torch.ops.fbgemm.uvm_storage(cpu_clone) |
| 151 | + |
| 152 | + |
| 153 | +if __name__ == "__main__": |
| 154 | + unittest.main() |
0 commit comments