Coverage for cuda/bindings/utils/_ptx_utils.py: 78.57%

14 statements  

« prev     ^ index     » next       coverage.py v7.18.0, created at 2026-12-03 01:06 +0000

1# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 

2# SPDX-License-Identifier: Apache-2.0 

3 

4import re 

5 

6# Mapping based on the official PTX ISA <-> CUDA Release table 

7# https://docs.nvidia.com/cuda/parallel-thread-execution/#release-notes-ptx-release-history 

8_ptx_to_cuda = { 

9 "1.0": (1, 0), 

10 "1.1": (1, 1), 

11 "1.2": (2, 0), 

12 "1.3": (2, 1), 

13 "1.4": (2, 2), 

14 "2.0": (3, 0), 

15 "2.1": (3, 1), 

16 "2.2": (3, 2), 

17 "2.3": (4, 0), 

18 "3.0": (4, 1), 

19 "3.1": (5, 0), 

20 "3.2": (5, 5), 

21 "4.0": (6, 0), 

22 "4.1": (6, 5), 

23 "4.2": (7, 0), 

24 "4.3": (7, 5), 

25 "5.0": (8, 0), 

26 "6.0": (9, 0), 

27 "6.1": (9, 1), 

28 "6.2": (9, 2), 

29 "6.3": (10, 0), 

30 "6.4": (10, 1), 

31 "6.5": (10, 2), 

32 "7.0": (11, 0), 

33 "7.1": (11, 1), 

34 "7.2": (11, 2), 

35 "7.3": (11, 3), 

36 "7.4": (11, 4), 

37 "7.5": (11, 5), 

38 "7.6": (11, 6), 

39 "7.7": (11, 7), 

40 "7.8": (11, 8), 

41 "8.0": (12, 0), 

42 "8.1": (12, 1), 

43 "8.2": (12, 2), 

44 "8.3": (12, 3), 

45 "8.4": (12, 4), 

46 "8.5": (12, 5), 

47 "8.6": (12, 7), 

48 "8.7": (12, 8), 

49 "8.8": (12, 9), 

50 "9.0": (13, 0), 

51 "9.1": (13, 1), 

52 "9.2": (13, 2), 

53 "9.3": (13, 3), 

54 "9.4": (13, 4), 

55 "9.5": (13, 5), 

56} 

57 

58 

59def get_minimal_required_cuda_ver_from_ptx_ver(ptx_version: str) -> int: 

60 """ 

61 Maps the PTX ISA version to the minimal CUDA driver, nvPTXCompiler, or nvJitLink version 

62 that is needed to load a PTX of the given ISA version. 

63 

64 Parameters 

65 ---------- 

66 ptx_version : str 

67 PTX ISA version as a string, e.g. "8.8" for PTX ISA 8.8. This is the ``.version`` 

68 directive in the PTX header. 

69 

70 Returns 

71 ------- 

72 int 

73 Minimal CUDA version as 1000 * major + 10 * minor, e.g. 12090 for CUDA 12.9. 

74 

75 Raises 

76 ------ 

77 ValueError 

78 If the PTX version is unknown. 

79 

80 Examples 

81 -------- 

82 >>> get_minimal_required_driver_ver_from_ptx_ver("8.8") 

83 12090 

84 >>> get_minimal_required_driver_ver_from_ptx_ver("7.0") 

85 11000 

86 """ 

87 try: 1ab

88 major, minor = _ptx_to_cuda[ptx_version] 1ab

89 return 1000 * major + 10 * minor 1ab

90 except KeyError: 

91 raise ValueError(f"Unknown or unsupported PTX ISA version: {ptx_version}") from None 

92 

93 

94# Regex pattern to match .version directive and capture the version number 

95# TODO: if import speed is a concern, consider lazy-initializing it. 

96_ptx_ver_pattern = re.compile(r"\.version\s+([0-9]+\.[0-9]+)") 

97 

98 

99def get_ptx_ver(ptx: str) -> str: 

100 """ 

101 Extract the PTX ISA version string from PTX source code. 

102 

103 Parameters 

104 ---------- 

105 ptx : str 

106 The PTX assembly source code as a string. 

107 

108 Returns 

109 ------- 

110 str 

111 The PTX ISA version string, e.g., "8.8". 

112 

113 Raises 

114 ------ 

115 ValueError 

116 If the .version directive is not found in the PTX source. 

117 

118 Examples 

119 -------- 

120 >>> ptx = r''' 

121 ... .version 8.8 

122 ... .target sm_86 

123 ... .address_size 64 

124 ... 

125 ... .visible .entry test_kernel() 

126 ... { 

127 ... ret; 

128 ... } 

129 ... ''' 

130 >>> get_ptx_ver(ptx) 

131 '8.8' 

132 """ 

133 m = _ptx_ver_pattern.search(ptx) 1ab

134 if m: 1ab

135 return m.group(1) 1ab

136 else: 

137 raise ValueError("No .version directive found in PTX source. Is it a valid PTX?")