This is just a result of a sweep for using strings where enums could be used. This is just to aid the discussion about whether they should be addressed.
Several places in the cuda.core public API accept or return string values chosen from a small, fixed vocabulary. These are candidates for replacement with proper Python enum types, which would improve discoverability, IDE autocompletion, isinstance checking, and catch typos at the call site rather than at runtime.
Findings
Compilation / Linking pipeline
| Location |
Kind |
Values |
Program.__init__(code_type) |
arg |
"c++", "ptx", "nvvm" |
Program.compile(target_type) |
arg |
"ptx", "cubin", "ltoir" |
Program.backend |
return |
"NVRTC", "NVVM", "nvJitLink", "driver" |
Program.pch_status |
return |
"created", "not_attempted", "failed" |
Linker.link(target_type) |
arg |
"cubin", "ptx" |
Linker.backend |
return |
"nvJitLink", "driver" |
ObjectCode.code_type |
return |
"cubin", "ptx", "ltoir", "fatbin", "object", "library" |
Memory
| Location |
Kind |
Values |
ManagedMemoryResourceOptions.preferred_location_type |
arg |
"device", "host", "host_numa" |
ManagedMemoryResource.preferred_location |
return (tuple[0]) |
"device", "host", "host_numa" |
VirtualMemoryResourceOptions.allocation_type |
arg |
"pinned", "managed" |
VirtualMemoryResourceOptions.location_type |
arg |
"device", "host", "host_numa", "host_numa_current" |
VirtualMemoryResourceOptions.handle_type |
arg |
"posix_fd", "generic", "win32_kmt", "fabric" |
VirtualMemoryResourceOptions.granularity |
arg |
"recommended", "minimum" |
VirtualMemoryResourceOptions.self_access / peer_access |
arg |
"rw", "r" |
Note: the VirtualMemoryResourceOptions fields already use Literal[...] type hints, which partially addresses the issue, but proper enums would still improve iteration, isinstance checks, and user discoverability.
Graph API
| Location |
Kind |
Values |
GraphAllocOptions.memory_type |
arg |
"device", "host", "managed" |
AllocNode.memory_type |
return |
"device", "host", "managed" |
ConditionalNode.cond_type |
return |
"if", "while", "switch" |
Proposed enum groupings
| Enum name |
Values |
Consumers |
CodeType |
cubin, ptx, ltoir, fatbin, object, library |
ObjectCode.code_type, Linker.link, Program.compile |
SourceType |
c++, ptx, nvvm |
Program.__init__ |
CompilerBackend |
NVRTC, NVVM, nvJitLink, driver |
Program.backend, Linker.backend |
PchStatus |
created, not_attempted, failed |
Program.pch_status |
MemoryType |
device, host, managed |
GraphAllocOptions, AllocNode, ManagedMemoryResource |
LocationType |
device, host, host_numa, host_numa_current |
VirtualMemoryResourceOptions, ManagedMemoryResourceOptions |
ConditionalType |
if_, while_, switch_ |
ConditionalNode.cond_type |
HandleType |
posix_fd, generic, win32_kmt, fabric |
VirtualMemoryResourceOptions |
Granularity |
recommended, minimum |
VirtualMemoryResourceOptions |
AccessMode |
rw, r |
VirtualMemoryResourceOptions |
Discussion points
- The compilation/linking pipeline (
Program, Linker, ObjectCode) is the highest-impact area since string values flow across multiple API boundaries.
- Switching from
str to enum would be a breaking change for existing callers. One migration path is to accept both str and the enum type during a deprecation window.
- Some values (
"c++", "if", "while") are not valid Python identifiers and would need renaming (e.g. SourceType.CPP, ConditionalType.IF).
Several places in the
cuda.corepublic API accept or return string values chosen from a small, fixed vocabulary. These are candidates for replacement with proper Pythonenumtypes, which would improve discoverability, IDE autocompletion,isinstancechecking, and catch typos at the call site rather than at runtime.Findings
Compilation / Linking pipeline
Program.__init__(code_type)"c++","ptx","nvvm"Program.compile(target_type)"ptx","cubin","ltoir"Program.backend"NVRTC","NVVM","nvJitLink","driver"Program.pch_status"created","not_attempted","failed"Linker.link(target_type)"cubin","ptx"Linker.backend"nvJitLink","driver"ObjectCode.code_type"cubin","ptx","ltoir","fatbin","object","library"Memory
ManagedMemoryResourceOptions.preferred_location_type"device","host","host_numa"ManagedMemoryResource.preferred_location"device","host","host_numa"VirtualMemoryResourceOptions.allocation_type"pinned","managed"VirtualMemoryResourceOptions.location_type"device","host","host_numa","host_numa_current"VirtualMemoryResourceOptions.handle_type"posix_fd","generic","win32_kmt","fabric"VirtualMemoryResourceOptions.granularity"recommended","minimum"VirtualMemoryResourceOptions.self_access/peer_access"rw","r"Note: the
VirtualMemoryResourceOptionsfields already useLiteral[...]type hints, which partially addresses the issue, but proper enums would still improve iteration,isinstancechecks, and user discoverability.Graph API
GraphAllocOptions.memory_type"device","host","managed"AllocNode.memory_type"device","host","managed"ConditionalNode.cond_type"if","while","switch"Proposed enum groupings
CodeTypecubin,ptx,ltoir,fatbin,object,libraryObjectCode.code_type,Linker.link,Program.compileSourceTypec++,ptx,nvvmProgram.__init__CompilerBackendNVRTC,NVVM,nvJitLink,driverProgram.backend,Linker.backendPchStatuscreated,not_attempted,failedProgram.pch_statusMemoryTypedevice,host,managedGraphAllocOptions,AllocNode,ManagedMemoryResourceLocationTypedevice,host,host_numa,host_numa_currentVirtualMemoryResourceOptions,ManagedMemoryResourceOptionsConditionalTypeif_,while_,switch_ConditionalNode.cond_typeHandleTypeposix_fd,generic,win32_kmt,fabricVirtualMemoryResourceOptionsGranularityrecommended,minimumVirtualMemoryResourceOptionsAccessModerw,rVirtualMemoryResourceOptionsDiscussion points
Program,Linker,ObjectCode) is the highest-impact area since string values flow across multiple API boundaries.strtoenumwould be a breaking change for existing callers. One migration path is to accept bothstrand the enum type during a deprecation window."c++","if","while") are not valid Python identifiers and would need renaming (e.g.SourceType.CPP,ConditionalType.IF).