Files
nvbench/python/examples/cupy_extract.py
Oleksandr Pavlyk a69a3647b2 CUTLASS example added, license headers added, fixes
- Add license header to each example file.
- Fixed broken runs caused by type declarations.
- Fixed hang in throughput.py when --run-once by doing a
  manual warm-up step, like in auto_throughput.py
2025-07-28 15:37:05 -05:00

62 lines
1.9 KiB
Python

# Copyright 2025 NVIDIA Corporation
#
# Licensed under the Apache License, Version 2.0 with the LLVM exception
# (the "License"); you may not use this file except in compliance with
# the License.
#
# You may obtain a copy of the License at
#
# http://llvm.org/foundation/relicensing/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import cuda.nvbench as nvbench
import cupy as cp
def as_cp_ExternalStream(
cs: nvbench.CudaStream, dev_id: int | None = -1
) -> cp.cuda.ExternalStream:
h = cs.addressof()
return cp.cuda.ExternalStream(h, dev_id)
def cupy_extract_by_mask(state: nvbench.State):
n_cols = state.get_int64("numCols")
n_rows = state.get_int64("numRows")
dev_id = state.get_device()
cp_s = as_cp_ExternalStream(state.get_stream(), dev_id)
state.collect_cupti_metrics()
state.add_element_count(n_rows * n_cols, "# Elements")
state.add_global_memory_reads(
n_rows * n_cols * (cp.dtype(cp.int32).itemsize + cp.dtype("?").itemsize)
)
state.add_global_memory_writes(n_rows * n_cols * (cp.dtype(cp.int32).itemsize))
with cp_s:
X = cp.full((n_cols, n_rows), fill_value=3, dtype=cp.int32)
mask = cp.ones((n_cols, n_rows), dtype="?")
_ = X[mask]
def launcher(launch: nvbench.Launch):
with as_cp_ExternalStream(launch.get_stream(), dev_id):
_ = X[mask]
state.exec(launcher, sync=True)
if __name__ == "__main__":
b = nvbench.register(cupy_extract_by_mask)
b.add_int64_axis("numCols", [1024, 2048, 4096, 2 * 4096])
b.add_int64_axis("numRows", [1024, 2048, 4096, 2 * 4096])
nvbench.run_all_benchmarks(sys.argv)