1- # SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
1+ # SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22#
33# SPDX-License-Identifier: Apache-2.0
44
55from __future__ import annotations
66
77from dataclasses import dataclass , field
8- from typing import TYPE_CHECKING , Iterable , Literal
8+ from typing import TYPE_CHECKING , Iterable
99
1010if TYPE_CHECKING :
1111 from cuda .core ._stream import Stream
2121 _check_driver_error as raise_if_driver_error ,
2222)
2323from cuda .core ._utils .version import binding_version
24+ from cuda .core .typing import (
25+ VirtualMemoryAccessType ,
26+ VirtualMemoryAllocationType ,
27+ VirtualMemoryGranularityType ,
28+ VirtualMemoryHandleType ,
29+ VirtualMemoryLocationType ,
30+ )
2431
2532__all__ = ["VirtualMemoryResource" , "VirtualMemoryResourceOptions" ]
2633
27- VirtualMemoryHandleTypeT = Literal ["posix_fd" , "generic" , "win32_kmt" , "fabric" ] | None
28- VirtualMemoryLocationTypeT = Literal ["device" , "host" , "host_numa" , "host_numa_current" ]
29- VirtualMemoryGranularityT = Literal ["minimum" , "recommended" ]
30- VirtualMemoryAccessTypeT = Literal ["rw" , "r" ] | None
31- VirtualMemoryAllocationTypeT = Literal ["pinned" , "managed" ]
32-
3334
3435@dataclass
3536class VirtualMemoryResourceOptions :
@@ -38,69 +39,72 @@ class VirtualMemoryResourceOptions:
3839
3940 Attributes
4041 ----------
41- allocation_type: :obj:`~_memory.VirtualMemoryAllocationTypeT`
42+ allocation_type: :obj:`~_memory.VirtualMemoryAllocationType` | str
4243 Controls the type of allocation.
43- location_type: :obj:`~_memory.VirtualMemoryLocationTypeT`
44+ location_type: :obj:`~_memory.VirtualMemoryLocationType` | str
4445 Controls the location of the allocation.
45- handle_type: :obj:`~_memory.VirtualMemoryHandleTypeT`
46+ handle_type: :obj:`~_memory.VirtualMemoryHandleType` | str
4647 Export handle type for the physical allocation. Use
4748 ``"posix_fd"`` on Linux if you plan to
4849 import/export the allocation (required for cuMemRetainAllocationHandle).
4950 Use `None` if you don't need an exportable handle.
5051 gpu_direct_rdma: bool
5152 Hint that the allocation should be GDR-capable (if supported).
52- granularity: :obj:`~_memory.VirtualMemoryGranularityT`
53+ granularity: :obj:`~_memory.VirtualMemoryGranularityType` | str
5354 Controls granularity query and size rounding.
5455 addr_hint: int
5556 A (optional) virtual address hint to try to reserve at. Setting it to 0 lets the CUDA driver decide.
5657 addr_align: int
5758 Alignment for the VA reservation. If `None`, use the queried granularity.
5859 peers: Iterable[int]
5960 Extra device IDs that should be granted access in addition to ``device``.
60- self_access: :obj:`~_memory.VirtualMemoryAccessTypeT`
61+ self_access: :obj:`~_memory.VirtualMemoryAccessType` | None | str
6162 Access flags for the owning device.
62- peer_access: :obj:`~_memory.VirtualMemoryAccessTypeT`
63+ peer_access: :obj:`~_memory.VirtualMemoryAccessType` | None | str
6364 Access flags for peers.
6465 """
6566
66- # Human-friendly strings; normalized in __post_init__
67- allocation_type : VirtualMemoryAllocationTypeT = "pinned"
68- location_type : VirtualMemoryLocationTypeT = "device"
69- handle_type : VirtualMemoryHandleTypeT = "posix_fd"
70- granularity : VirtualMemoryGranularityT = "recommended"
67+ allocation_type : VirtualMemoryAllocationType = VirtualMemoryAllocationType .PINNED
68+ location_type : VirtualMemoryLocationType = VirtualMemoryLocationType .DEVICE
69+ handle_type : VirtualMemoryHandleType = VirtualMemoryHandleType .POSIX_FD
70+ granularity : VirtualMemoryGranularityType = VirtualMemoryGranularityType .RECOMMENDED
7171 gpu_direct_rdma : bool = False
7272 addr_hint : int | None = 0
7373 addr_align : int | None = None
7474 peers : Iterable [int ] = field (default_factory = tuple )
75- self_access : VirtualMemoryAccessTypeT = "rw"
76- peer_access : VirtualMemoryAccessTypeT = "rw"
75+ self_access : VirtualMemoryAccessType = VirtualMemoryAccessType . READ_WRITE
76+ peer_access : VirtualMemoryAccessType = VirtualMemoryAccessType . READ_WRITE
7777
7878 _a = driver .CUmemAccess_flags
79- _access_flags = {"rw" : _a .CU_MEM_ACCESS_FLAGS_PROT_READWRITE , "r" : _a .CU_MEM_ACCESS_FLAGS_PROT_READ , None : 0 } # noqa: RUF012
79+ _access_flags = { # noqa: RUF012
80+ VirtualMemoryAccessType .READ_WRITE : _a .CU_MEM_ACCESS_FLAGS_PROT_READWRITE ,
81+ VirtualMemoryAccessType .READ : _a .CU_MEM_ACCESS_FLAGS_PROT_READ ,
82+ None : 0 ,
83+ }
8084 _h = driver .CUmemAllocationHandleType
8185 _handle_types = { # noqa: RUF012
8286 None : _h .CU_MEM_HANDLE_TYPE_NONE ,
83- "posix_fd" : _h .CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR ,
84- "win32_kmt" : _h .CU_MEM_HANDLE_TYPE_WIN32_KMT ,
85- "fabric" : _h .CU_MEM_HANDLE_TYPE_FABRIC ,
87+ VirtualMemoryHandleType . POSIX_FD : _h .CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR ,
88+ VirtualMemoryHandleType . WIN32_KMT : _h .CU_MEM_HANDLE_TYPE_WIN32_KMT ,
89+ VirtualMemoryHandleType . FABRIC : _h .CU_MEM_HANDLE_TYPE_FABRIC ,
8690 }
8791 _g = driver .CUmemAllocationGranularity_flags
8892 _granularity = { # noqa: RUF012
89- "recommended" : _g .CU_MEM_ALLOC_GRANULARITY_RECOMMENDED ,
90- "minimum" : _g .CU_MEM_ALLOC_GRANULARITY_MINIMUM ,
93+ VirtualMemoryGranularityType . RECOMMENDED : _g .CU_MEM_ALLOC_GRANULARITY_RECOMMENDED ,
94+ VirtualMemoryGranularityType . MINIMUM : _g .CU_MEM_ALLOC_GRANULARITY_MINIMUM ,
9195 }
9296 _l = driver .CUmemLocationType
9397 _location_type = { # noqa: RUF012
94- "device" : _l .CU_MEM_LOCATION_TYPE_DEVICE ,
95- "host" : _l .CU_MEM_LOCATION_TYPE_HOST ,
96- "host_numa" : _l .CU_MEM_LOCATION_TYPE_HOST_NUMA ,
97- "host_numa_current" : _l .CU_MEM_LOCATION_TYPE_HOST_NUMA_CURRENT ,
98+ VirtualMemoryLocationType . DEVICE : _l .CU_MEM_LOCATION_TYPE_DEVICE ,
99+ VirtualMemoryLocationType . HOST : _l .CU_MEM_LOCATION_TYPE_HOST ,
100+ VirtualMemoryLocationType . HOST_NUMA : _l .CU_MEM_LOCATION_TYPE_HOST_NUMA ,
101+ VirtualMemoryLocationType . HOST_NUMA_CURRENT : _l .CU_MEM_LOCATION_TYPE_HOST_NUMA_CURRENT ,
98102 }
99103 _t = driver .CUmemAllocationType
100104 # CUDA 13+ exposes MANAGED in CUmemAllocationType; older 12.x does not
101- _allocation_type = {"pinned" : _t .CU_MEM_ALLOCATION_TYPE_PINNED } # noqa: RUF012
105+ _allocation_type = {VirtualMemoryAllocationType . PINNED : _t .CU_MEM_ALLOCATION_TYPE_PINNED } # noqa: RUF012
102106 if binding_version () >= (13 , 0 , 0 ):
103- _allocation_type ["managed" ] = _t .CU_MEM_ALLOCATION_TYPE_MANAGED
107+ _allocation_type [VirtualMemoryAllocationType . MANAGED ] = _t .CU_MEM_ALLOCATION_TYPE_MANAGED
104108
105109 @staticmethod
106110 def _access_to_flags (spec : str ):
0 commit comments