|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# MIT License |
| 4 | +# |
| 5 | +# Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | +# |
| 25 | +import time |
| 26 | +from functools import wraps |
| 27 | + |
| 28 | +import cupy |
| 29 | +import numpy as np |
| 30 | + |
| 31 | +from ucm.shared.trans import ucmtrans |
| 32 | + |
| 33 | + |
| 34 | +def test_wrap(func): |
| 35 | + @wraps(func) |
| 36 | + def wrapper(*args, **kwargs): |
| 37 | + print(f"========>> Running in {func.__name__}:") |
| 38 | + result = func(*args, **kwargs) |
| 39 | + print() |
| 40 | + return result |
| 41 | + |
| 42 | + return wrapper |
| 43 | + |
| 44 | + |
| 45 | +def make_host_memory(size, number, dtype, fill=False): |
| 46 | + host = cupy.cuda.alloc_pinned_memory(size * number) |
| 47 | + host_np = np.frombuffer(host, dtype=dtype) |
| 48 | + if fill: |
| 49 | + fixed_len = min(1024, number) |
| 50 | + host_np[:fixed_len] = np.arange(fixed_len, dtype=dtype) |
| 51 | + print("make:", host_np.shape, host_np.itemsize, host_np) |
| 52 | + return host |
| 53 | + |
| 54 | + |
| 55 | +def compare(host1, host2, dtype): |
| 56 | + host1_np = np.frombuffer(host1, dtype=dtype) |
| 57 | + host2_np = np.frombuffer(host2, dtype=dtype) |
| 58 | + print("compare[1]:", host1_np.shape, host1_np.itemsize, host1_np) |
| 59 | + print("compare[2]:", host2_np.shape, host2_np.itemsize, host2_np) |
| 60 | + return np.array_equal(host1_np, host2_np) |
| 61 | + |
| 62 | + |
| 63 | +@test_wrap |
| 64 | +def trans_with_ce(d, size, number, dtype): |
| 65 | + s = d.MakeStream() |
| 66 | + host1 = make_host_memory(size, number, dtype, True) |
| 67 | + device = [cupy.empty(size, dtype=np.uint8) for _ in range(number)] |
| 68 | + device_ptr = np.array([d.data.ptr for d in device], dtype=np.uint64) |
| 69 | + host2 = make_host_memory(size, number, dtype) |
| 70 | + tp = time.perf_counter() |
| 71 | + s.HostToDeviceScatter(host1.ptr, device_ptr, size, number) |
| 72 | + s.DeviceToHostGather(device_ptr, host2.ptr, size, number) |
| 73 | + cost = time.perf_counter() - tp |
| 74 | + print(f"cost: {cost}s") |
| 75 | + print(f"bandwidth: {size * number / cost / 1e9}GB/s") |
| 76 | + assert compare(host1, host2, dtype) |
| 77 | + |
| 78 | + |
| 79 | +@test_wrap |
| 80 | +def trans_with_sm(d, size, number, dtype): |
| 81 | + s = d.MakeSMStream() |
| 82 | + host1 = make_host_memory(size, number, dtype, True) |
| 83 | + device = [cupy.empty(size, dtype=np.uint8) for _ in range(number)] |
| 84 | + device_ptr = np.array([d.data.ptr for d in device], dtype=np.uint64) |
| 85 | + device_ptr_cupy = cupy.empty(number, dtype=np.uint64) |
| 86 | + device_ptr_cupy.set(device_ptr) |
| 87 | + host2 = make_host_memory(size, number, dtype) |
| 88 | + tp = time.perf_counter() |
| 89 | + s.HostToDeviceScatter(host1.ptr, device_ptr_cupy.data.ptr, size, number) |
| 90 | + s.DeviceToHostGather(device_ptr_cupy.data.ptr, host2.ptr, size, number) |
| 91 | + cost = time.perf_counter() - tp |
| 92 | + print(f"cost: {cost}s") |
| 93 | + print(f"bandwidth: {size * number / cost / 1e9}GB/s") |
| 94 | + assert compare(host1, host2, dtype) |
| 95 | + |
| 96 | + |
| 97 | +@test_wrap |
| 98 | +def trans_with_ce_async(d, size, number, dtype): |
| 99 | + s = d.MakeStream() |
| 100 | + host1 = make_host_memory(size, number, dtype, True) |
| 101 | + device = [cupy.empty(size, dtype=np.uint8) for _ in range(number)] |
| 102 | + device_ptr = np.array([d.data.ptr for d in device], dtype=np.uint64) |
| 103 | + host2 = make_host_memory(size, number, dtype) |
| 104 | + tp = time.perf_counter() |
| 105 | + s.HostToDeviceScatterAsync(host1.ptr, device_ptr, size, number) |
| 106 | + s.DeviceToHostGatherAsync(device_ptr, host2.ptr, size, number) |
| 107 | + s.Synchronized() |
| 108 | + cost = time.perf_counter() - tp |
| 109 | + print(f"cost: {cost}s") |
| 110 | + print(f"bandwidth: {size * number / cost / 1e9}GB/s") |
| 111 | + assert compare(host1, host2, dtype) |
| 112 | + |
| 113 | + |
| 114 | +@test_wrap |
| 115 | +def trans_with_sm_async(d, size, number, dtype): |
| 116 | + s = d.MakeSMStream() |
| 117 | + host1 = make_host_memory(size, number, dtype, True) |
| 118 | + device = [cupy.empty(size, dtype=np.uint8) for _ in range(number)] |
| 119 | + device_ptr = np.array([d.data.ptr for d in device], dtype=np.uint64) |
| 120 | + device_ptr_cupy = cupy.empty(number, dtype=np.uint64) |
| 121 | + device_ptr_cupy.set(device_ptr) |
| 122 | + host2 = make_host_memory(size, number, dtype) |
| 123 | + tp = time.perf_counter() |
| 124 | + s.HostToDeviceScatterAsync(host1.ptr, device_ptr_cupy.data.ptr, size, number) |
| 125 | + s.DeviceToHostGatherAsync(device_ptr_cupy.data.ptr, host2.ptr, size, number) |
| 126 | + s.Synchronized() |
| 127 | + cost = time.perf_counter() - tp |
| 128 | + print(f"cost: {cost}s") |
| 129 | + print(f"bandwidth: {size * number / cost / 1e9}GB/s") |
| 130 | + assert compare(host1, host2, dtype) |
| 131 | + |
| 132 | + |
| 133 | +def main(): |
| 134 | + device_id = 0 |
| 135 | + size = 36 * 1024 |
| 136 | + number = 61 * 64 |
| 137 | + dtype = np.float16 |
| 138 | + print(f"ucmtrans: {ucmtrans.commit_id}-{ucmtrans.build_type}") |
| 139 | + cupy.cuda.Device(device_id).use() |
| 140 | + d = ucmtrans.Device() |
| 141 | + d.Setup(device_id) |
| 142 | + trans_with_ce(d, size, number, dtype) |
| 143 | + trans_with_sm(d, size, number, dtype) |
| 144 | + trans_with_ce_async(d, size, number, dtype) |
| 145 | + trans_with_sm_async(d, size, number, dtype) |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + main() |
0 commit comments