|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import time |
| 6 | + |
| 7 | +from runner.runtime import alloc_persistent, ensure_context |
| 8 | + |
| 9 | +from cuda.bindings import driver as cuda |
| 10 | + |
| 11 | +ensure_context() |
| 12 | + |
| 13 | +PTR = alloc_persistent(1 << 20) |
| 14 | + |
| 15 | +cuuint32_t = cuda.cuuint32_t |
| 16 | +cuuint64_t = cuda.cuuint64_t |
| 17 | + |
| 18 | +# Tiled: rank-2 float32, 128x128, 64x64 tile. |
| 19 | +TILED_DTYPE = cuda.CUtensorMapDataType.CU_TENSOR_MAP_DATA_TYPE_FLOAT32 |
| 20 | +TILED_RANK = 2 |
| 21 | +TILED_GLOBAL_DIM = (cuuint64_t(128), cuuint64_t(128)) |
| 22 | +TILED_GLOBAL_STRIDES = (cuuint64_t(128 * 4),) |
| 23 | +TILED_BOX_DIM = (cuuint32_t(64), cuuint32_t(64)) |
| 24 | +TILED_ELEMENT_STRIDES = (cuuint32_t(1), cuuint32_t(1)) |
| 25 | +TILED_INTERLEAVE = cuda.CUtensorMapInterleave.CU_TENSOR_MAP_INTERLEAVE_NONE |
| 26 | +TILED_SWIZZLE = cuda.CUtensorMapSwizzle.CU_TENSOR_MAP_SWIZZLE_NONE |
| 27 | +TILED_L2 = cuda.CUtensorMapL2promotion.CU_TENSOR_MAP_L2_PROMOTION_NONE |
| 28 | +TILED_OOB = cuda.CUtensorMapFloatOOBfill.CU_TENSOR_MAP_FLOAT_OOB_FILL_NONE |
| 29 | + |
| 30 | +# Im2col: rank-3 float16, 32x64x64. |
| 31 | +IM2COL_DTYPE = cuda.CUtensorMapDataType.CU_TENSOR_MAP_DATA_TYPE_FLOAT16 |
| 32 | +IM2COL_RANK = 3 |
| 33 | +IM2COL_GLOBAL_DIM = (cuuint64_t(32), cuuint64_t(64), cuuint64_t(64)) |
| 34 | +IM2COL_GLOBAL_STRIDES = (cuuint64_t(32 * 2), cuuint64_t(32 * 64 * 2)) |
| 35 | +IM2COL_PIXEL_BOX_LOWER = (0,) |
| 36 | +IM2COL_PIXEL_BOX_UPPER = (0,) |
| 37 | +IM2COL_CHANNELS = 32 |
| 38 | +IM2COL_PIXELS = 32 |
| 39 | +IM2COL_ELEMENT_STRIDES = (cuuint32_t(1), cuuint32_t(1), cuuint32_t(1)) |
| 40 | +IM2COL_INTERLEAVE = cuda.CUtensorMapInterleave.CU_TENSOR_MAP_INTERLEAVE_NONE |
| 41 | +IM2COL_SWIZZLE = cuda.CUtensorMapSwizzle.CU_TENSOR_MAP_SWIZZLE_NONE |
| 42 | +IM2COL_L2 = cuda.CUtensorMapL2promotion.CU_TENSOR_MAP_L2_PROMOTION_NONE |
| 43 | +IM2COL_OOB = cuda.CUtensorMapFloatOOBfill.CU_TENSOR_MAP_FLOAT_OOB_FILL_NONE |
| 44 | + |
| 45 | +_SUCCESS = cuda.CUresult.CUDA_SUCCESS |
| 46 | + |
| 47 | +# Resolve bindings once at module load. A missing attribute (old binding that |
| 48 | +# predates a TMA API) is the only legitimate reason for a probe to skip — |
| 49 | +# everything else (signature mismatches, unexpected TypeError, etc.) should |
| 50 | +# surface loudly instead of being reclassified as "unsupported". |
| 51 | +_ENCODE_TILED = getattr(cuda, "cuTensorMapEncodeTiled", None) |
| 52 | +_ENCODE_IM2COL = getattr(cuda, "cuTensorMapEncodeIm2col", None) |
| 53 | +_ENCODE_IM2COL_WIDE = getattr(cuda, "cuTensorMapEncodeIm2colWide", None) |
| 54 | +_IM2COL_WIDE_MODE_CLS = getattr(cuda, "CUtensorMapIm2ColWideMode", None) |
| 55 | + |
| 56 | + |
| 57 | +def _probe_tiled() -> bool: |
| 58 | + if _ENCODE_TILED is None: |
| 59 | + return False |
| 60 | + err, _ = _ENCODE_TILED( |
| 61 | + TILED_DTYPE, |
| 62 | + TILED_RANK, |
| 63 | + PTR, |
| 64 | + TILED_GLOBAL_DIM, |
| 65 | + TILED_GLOBAL_STRIDES, |
| 66 | + TILED_BOX_DIM, |
| 67 | + TILED_ELEMENT_STRIDES, |
| 68 | + TILED_INTERLEAVE, |
| 69 | + TILED_SWIZZLE, |
| 70 | + TILED_L2, |
| 71 | + TILED_OOB, |
| 72 | + ) |
| 73 | + return err == _SUCCESS |
| 74 | + |
| 75 | + |
| 76 | +def _probe_im2col() -> bool: |
| 77 | + if _ENCODE_IM2COL is None: |
| 78 | + return False |
| 79 | + err, _ = _ENCODE_IM2COL( |
| 80 | + IM2COL_DTYPE, |
| 81 | + IM2COL_RANK, |
| 82 | + PTR, |
| 83 | + IM2COL_GLOBAL_DIM, |
| 84 | + IM2COL_GLOBAL_STRIDES, |
| 85 | + IM2COL_PIXEL_BOX_LOWER, |
| 86 | + IM2COL_PIXEL_BOX_UPPER, |
| 87 | + IM2COL_CHANNELS, |
| 88 | + IM2COL_PIXELS, |
| 89 | + IM2COL_ELEMENT_STRIDES, |
| 90 | + IM2COL_INTERLEAVE, |
| 91 | + IM2COL_SWIZZLE, |
| 92 | + IM2COL_L2, |
| 93 | + IM2COL_OOB, |
| 94 | + ) |
| 95 | + return err == _SUCCESS |
| 96 | + |
| 97 | + |
| 98 | +def _probe_im2col_wide() -> bool: |
| 99 | + if _ENCODE_IM2COL_WIDE is None or _IM2COL_WIDE_MODE_CLS is None: |
| 100 | + return False |
| 101 | + mode = _IM2COL_WIDE_MODE_CLS.CU_TENSOR_MAP_IM2COL_WIDE_MODE_W |
| 102 | + err, _ = _ENCODE_IM2COL_WIDE( |
| 103 | + IM2COL_DTYPE, |
| 104 | + IM2COL_RANK, |
| 105 | + PTR, |
| 106 | + IM2COL_GLOBAL_DIM, |
| 107 | + IM2COL_GLOBAL_STRIDES, |
| 108 | + 0, |
| 109 | + 0, |
| 110 | + IM2COL_CHANNELS, |
| 111 | + IM2COL_PIXELS, |
| 112 | + IM2COL_ELEMENT_STRIDES, |
| 113 | + IM2COL_INTERLEAVE, |
| 114 | + mode, |
| 115 | + cuda.CUtensorMapSwizzle.CU_TENSOR_MAP_SWIZZLE_128B, |
| 116 | + IM2COL_L2, |
| 117 | + IM2COL_OOB, |
| 118 | + ) |
| 119 | + return err == _SUCCESS |
| 120 | + |
| 121 | + |
| 122 | +_TILED_OK = _probe_tiled() |
| 123 | +_IM2COL_OK = _probe_im2col() |
| 124 | +_IM2COL_WIDE_OK = _probe_im2col_wide() |
| 125 | + |
| 126 | +if _IM2COL_WIDE_OK: |
| 127 | + _IM2COL_WIDE_MODE_W = _IM2COL_WIDE_MODE_CLS.CU_TENSOR_MAP_IM2COL_WIDE_MODE_W |
| 128 | + _IM2COL_WIDE_SWIZZLE = cuda.CUtensorMapSwizzle.CU_TENSOR_MAP_SWIZZLE_128B |
| 129 | + |
| 130 | +SKIPPED_BENCHMARKS: set[str] = set() |
| 131 | +if not _TILED_OK: |
| 132 | + SKIPPED_BENCHMARKS.add("bench_tensor_map_encode_tiled") |
| 133 | +if not _IM2COL_OK: |
| 134 | + SKIPPED_BENCHMARKS.add("bench_tensor_map_encode_im2col") |
| 135 | +if not _IM2COL_WIDE_OK: |
| 136 | + SKIPPED_BENCHMARKS.add("bench_tensor_map_encode_im2col_wide") |
| 137 | + |
| 138 | + |
| 139 | +def bench_tensor_map_encode_tiled(loops: int) -> float: |
| 140 | + _fn = cuda.cuTensorMapEncodeTiled |
| 141 | + _dt = TILED_DTYPE |
| 142 | + _rank = TILED_RANK |
| 143 | + _addr = PTR |
| 144 | + _gdim = TILED_GLOBAL_DIM |
| 145 | + _gstr = TILED_GLOBAL_STRIDES |
| 146 | + _bdim = TILED_BOX_DIM |
| 147 | + _estr = TILED_ELEMENT_STRIDES |
| 148 | + _inter = TILED_INTERLEAVE |
| 149 | + _swz = TILED_SWIZZLE |
| 150 | + _l2 = TILED_L2 |
| 151 | + _oob = TILED_OOB |
| 152 | + |
| 153 | + t0 = time.perf_counter() |
| 154 | + for _ in range(loops): |
| 155 | + _fn(_dt, _rank, _addr, _gdim, _gstr, _bdim, _estr, _inter, _swz, _l2, _oob) |
| 156 | + return time.perf_counter() - t0 |
| 157 | + |
| 158 | + |
| 159 | +def bench_tensor_map_encode_im2col(loops: int) -> float: |
| 160 | + _fn = cuda.cuTensorMapEncodeIm2col |
| 161 | + _dt = IM2COL_DTYPE |
| 162 | + _rank = IM2COL_RANK |
| 163 | + _addr = PTR |
| 164 | + _gdim = IM2COL_GLOBAL_DIM |
| 165 | + _gstr = IM2COL_GLOBAL_STRIDES |
| 166 | + _lower = IM2COL_PIXEL_BOX_LOWER |
| 167 | + _upper = IM2COL_PIXEL_BOX_UPPER |
| 168 | + _ch = IM2COL_CHANNELS |
| 169 | + _px = IM2COL_PIXELS |
| 170 | + _estr = IM2COL_ELEMENT_STRIDES |
| 171 | + _inter = IM2COL_INTERLEAVE |
| 172 | + _swz = IM2COL_SWIZZLE |
| 173 | + _l2 = IM2COL_L2 |
| 174 | + _oob = IM2COL_OOB |
| 175 | + |
| 176 | + t0 = time.perf_counter() |
| 177 | + for _ in range(loops): |
| 178 | + _fn(_dt, _rank, _addr, _gdim, _gstr, _lower, _upper, _ch, _px, _estr, _inter, _swz, _l2, _oob) |
| 179 | + return time.perf_counter() - t0 |
| 180 | + |
| 181 | + |
| 182 | +def bench_tensor_map_encode_im2col_wide(loops: int) -> float: |
| 183 | + _fn = _ENCODE_IM2COL_WIDE |
| 184 | + _dt = IM2COL_DTYPE |
| 185 | + _rank = IM2COL_RANK |
| 186 | + _addr = PTR |
| 187 | + _gdim = IM2COL_GLOBAL_DIM |
| 188 | + _gstr = IM2COL_GLOBAL_STRIDES |
| 189 | + _lower_w = 0 |
| 190 | + _upper_w = 0 |
| 191 | + _ch = IM2COL_CHANNELS |
| 192 | + _px = IM2COL_PIXELS |
| 193 | + _estr = IM2COL_ELEMENT_STRIDES |
| 194 | + _inter = IM2COL_INTERLEAVE |
| 195 | + _mode = _IM2COL_WIDE_MODE_W |
| 196 | + _swz = _IM2COL_WIDE_SWIZZLE |
| 197 | + _l2 = IM2COL_L2 |
| 198 | + _oob = IM2COL_OOB |
| 199 | + |
| 200 | + t0 = time.perf_counter() |
| 201 | + for _ in range(loops): |
| 202 | + _fn(_dt, _rank, _addr, _gdim, _gstr, _lower_w, _upper_w, _ch, _px, _estr, _inter, _mode, _swz, _l2, _oob) |
| 203 | + return time.perf_counter() - t0 |
0 commit comments