Skip to content

Commit ee16510

Browse files
Copilotleofangkkraus14
authored
Add Device.arch property for convenient compute capability string access (#877)
* Initial plan * Add Device.arch property and update examples to use it Co-authored-by: leofang <5534781+leofang@users.noreply.github.com> * Inline dev.arch calls in f-strings per PR feedback Co-authored-by: leofang <5534781+leofang@users.noreply.github.com> * Add release note for Device.arch property Co-authored-by: leofang <5534781+leofang@users.noreply.github.com> * Use f-string instead of "".join for Device.arch property Co-authored-by: kkraus14 <3665167+kkraus14@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: leofang <5534781+leofang@users.noreply.github.com> Co-authored-by: kkraus14 <3665167+kkraus14@users.noreply.github.com>
1 parent bb1fe80 commit ee16510

File tree

10 files changed

+26
-17
lines changed

10 files changed

+26
-17
lines changed

cuda_core/cuda/core/experimental/_device.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,11 @@ def compute_capability(self) -> ComputeCapability:
11121112
self.properties._cache["compute_capability"] = cc
11131113
return cc
11141114

1115+
@property
1116+
def arch(self) -> str:
1117+
"""Return compute capability as a string (e.g., '75' for CC 7.5)."""
1118+
return f"{self.compute_capability.major}{self.compute_capability.minor}"
1119+
11151120
@property
11161121
def context(self) -> Context:
11171122
"""Return the current :obj:`~_context.Context` associated with this device.

cuda_core/docs/source/release/0.X.Y-notes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Breaking Changes
2424
New features
2525
------------
2626

27-
None.
27+
- Added :attr:`Device.arch` property that returns the compute capability as a string (e.g., '75' for CC 7.5), providing a convenient alternative to manually concatenating the compute capability tuple.
2828

2929

3030
New examples

cuda_core/examples/cuda_graphs.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ def main():
5353
cp.cuda.ExternalStream(int(stream.handle)).use()
5454

5555
# Compile the program
56-
arch = "".join(f"{i}" for i in dev.compute_capability)
57-
program_options = ProgramOptions(std="c++17", arch=f"sm_{arch}")
56+
program_options = ProgramOptions(std="c++17", arch=f"sm_{dev.arch}")
5857
prog = Program(code, code_type="c++", options=program_options)
5958
mod = prog.compile(
6059
"cubin", name_expressions=("vector_add<float>", "vector_multiply<float>", "vector_subtract<float>")

cuda_core/examples/memory_ops.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@
5454
cp.cuda.ExternalStream(int(stream.handle)).use()
5555

5656
# Compile kernel
57-
arch = "".join(f"{i}" for i in dev.compute_capability)
58-
program_options = ProgramOptions(std="c++17", arch=f"sm_{arch}")
57+
program_options = ProgramOptions(std="c++17", arch=f"sm_{dev.arch}")
5958
prog = Program(code, code_type="c++", options=program_options)
6059
mod = prog.compile("cubin")
6160
kernel = mod.get_kernel("memory_ops")

cuda_core/examples/pytorch_example.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def __cuda_stream__(self):
5151
s = dev.create_stream(PyTorchStreamWrapper(pt_stream))
5252

5353
# prepare program
54-
arch = "".join(f"{i}" for i in dev.compute_capability)
55-
program_options = ProgramOptions(std="c++11", arch=f"sm_{arch}")
54+
program_options = ProgramOptions(std="c++11", arch=f"sm_{dev.arch}")
5655
prog = Program(code, code_type="c++", options=program_options)
5756
mod = prog.compile(
5857
"cubin",

cuda_core/examples/saxpy.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
s = dev.create_stream()
3939

4040
# prepare program
41-
arch = "".join(f"{i}" for i in dev.compute_capability)
42-
program_options = ProgramOptions(std="c++11", arch=f"sm_{arch}")
41+
program_options = ProgramOptions(std="c++11", arch=f"sm_{dev.arch}")
4342
prog = Program(code, code_type="c++", options=program_options)
4443

4544
# Note the use of the `name_expressions` argument to specify the template

cuda_core/examples/simple_multi_gpu_example.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
}
4141
}
4242
"""
43-
arch0 = "".join(f"{i}" for i in dev0.compute_capability)
44-
prog_add = Program(code_add, code_type="c++", options={"std": "c++17", "arch": f"sm_{arch0}"})
43+
prog_add = Program(code_add, code_type="c++", options={"std": "c++17", "arch": f"sm_{dev0.arch}"})
4544
mod_add = prog_add.compile("cubin")
4645
ker_add = mod_add.get_kernel("vector_add")
4746

@@ -63,8 +62,7 @@
6362
}
6463
}
6564
"""
66-
arch1 = "".join(f"{i}" for i in dev1.compute_capability)
67-
prog_sub = Program(code_sub, code_type="c++", options={"std": "c++17", "arch": f"sm_{arch1}"})
65+
prog_sub = Program(code_sub, code_type="c++", options={"std": "c++17", "arch": f"sm_{dev1.arch}"})
6866
mod_sub = prog_sub.compile("cubin")
6967
ker_sub = mod_sub.get_kernel("vector_sub")
7068

cuda_core/examples/strided_memory_view_gpu.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ def run():
103103
# To know the GPU's compute capability, we need to identify which GPU to use.
104104
dev = Device(0)
105105
dev.set_current()
106-
arch = "".join(f"{i}" for i in dev.compute_capability)
107-
gpu_prog = Program(gpu_code, code_type="c++", options=ProgramOptions(arch=f"sm_{arch}", std="c++11"))
106+
gpu_prog = Program(gpu_code, code_type="c++", options=ProgramOptions(arch=f"sm_{dev.arch}", std="c++11"))
108107
mod = gpu_prog.compile(target_type="cubin")
109108
gpu_ker = mod.get_kernel(func_name)
110109

cuda_core/examples/vector_add.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
s = dev.create_stream()
3434

3535
# prepare program
36-
arch = "".join(f"{i}" for i in dev.compute_capability)
37-
program_options = ProgramOptions(std="c++17", arch=f"sm_{arch}")
36+
program_options = ProgramOptions(std="c++17", arch=f"sm_{dev.arch}")
3837
prog = Program(code, code_type="c++", options=program_options)
3938
mod = prog.compile("cubin", name_expressions=("vector_add<float>",))
4039

cuda_core/tests/test_device.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ def test_compute_capability():
105105
assert device.compute_capability == expected_cc
106106

107107

108+
def test_arch():
109+
device = Device()
110+
# Test that arch returns the same as the old pattern
111+
expected_arch = "".join(f"{i}" for i in device.compute_capability)
112+
assert device.arch == expected_arch
113+
# Test that it's a string
114+
assert isinstance(device.arch, str)
115+
# Test that it matches the expected format (e.g., "75" for CC 7.5)
116+
cc = device.compute_capability
117+
assert device.arch == f"{cc.major}{cc.minor}"
118+
119+
108120
cuda_base_properties = [
109121
("max_threads_per_block", int),
110122
("max_block_dim_x", int),

0 commit comments

Comments
 (0)