sglang 0.5.0rc1__py3-none-any.whl → 0.5.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (203) hide show
  1. sglang/bench_one_batch.py +0 -7
  2. sglang/bench_one_batch_server.py +7 -2
  3. sglang/bench_serving.py +3 -3
  4. sglang/eval/llama3_eval.py +0 -1
  5. sglang/srt/configs/model_config.py +25 -9
  6. sglang/srt/configs/update_config.py +40 -5
  7. sglang/srt/constrained/xgrammar_backend.py +23 -11
  8. sglang/srt/conversation.py +2 -15
  9. sglang/srt/disaggregation/ascend/conn.py +1 -3
  10. sglang/srt/disaggregation/base/conn.py +1 -0
  11. sglang/srt/disaggregation/decode.py +1 -2
  12. sglang/srt/disaggregation/launch_lb.py +7 -1
  13. sglang/srt/disaggregation/mini_lb.py +11 -5
  14. sglang/srt/disaggregation/mooncake/conn.py +141 -47
  15. sglang/srt/disaggregation/prefill.py +261 -5
  16. sglang/srt/disaggregation/utils.py +2 -1
  17. sglang/srt/distributed/device_communicators/custom_all_reduce.py +1 -1
  18. sglang/srt/distributed/device_communicators/pynccl.py +68 -18
  19. sglang/srt/distributed/device_communicators/pynccl_wrapper.py +52 -0
  20. sglang/srt/distributed/naive_distributed.py +112 -0
  21. sglang/srt/distributed/parallel_state.py +90 -4
  22. sglang/srt/entrypoints/context.py +20 -1
  23. sglang/srt/entrypoints/engine.py +29 -4
  24. sglang/srt/entrypoints/http_server.py +76 -0
  25. sglang/srt/entrypoints/openai/protocol.py +4 -2
  26. sglang/srt/entrypoints/openai/serving_chat.py +23 -6
  27. sglang/srt/entrypoints/openai/serving_completions.py +10 -1
  28. sglang/srt/entrypoints/openai/serving_responses.py +2 -2
  29. sglang/srt/eplb/expert_distribution.py +2 -3
  30. sglang/srt/function_call/deepseekv3_detector.py +1 -1
  31. sglang/srt/hf_transformers_utils.py +24 -0
  32. sglang/srt/host_shared_memory.py +83 -0
  33. sglang/srt/layers/attention/ascend_backend.py +132 -22
  34. sglang/srt/layers/attention/flashattention_backend.py +24 -17
  35. sglang/srt/layers/attention/flashinfer_backend.py +14 -3
  36. sglang/srt/layers/attention/flashinfer_mla_backend.py +227 -76
  37. sglang/srt/layers/attention/triton_backend.py +109 -73
  38. sglang/srt/layers/attention/triton_ops/decode_attention.py +33 -2
  39. sglang/srt/layers/attention/triton_ops/extend_attention.py +32 -2
  40. sglang/srt/layers/attention/trtllm_mha_backend.py +398 -36
  41. sglang/srt/layers/attention/trtllm_mla_backend.py +49 -19
  42. sglang/srt/layers/attention/utils.py +94 -15
  43. sglang/srt/layers/attention/vision.py +40 -13
  44. sglang/srt/layers/attention/vision_utils.py +65 -0
  45. sglang/srt/layers/communicator.py +58 -10
  46. sglang/srt/layers/dp_attention.py +137 -27
  47. sglang/srt/layers/elementwise.py +94 -0
  48. sglang/srt/layers/flashinfer_comm_fusion.py +29 -1
  49. sglang/srt/layers/layernorm.py +8 -1
  50. sglang/srt/layers/linear.py +24 -0
  51. sglang/srt/layers/logits_processor.py +16 -18
  52. sglang/srt/layers/moe/__init__.py +31 -0
  53. sglang/srt/layers/moe/ep_moe/layer.py +37 -33
  54. sglang/srt/layers/moe/fused_moe_native.py +14 -25
  55. sglang/srt/layers/moe/fused_moe_triton/configs/triton_3_2_0/E=129,N=352,device_name=NVIDIA_A800-SXM4-80GB,dtype=int8_w8a8.json +146 -0
  56. sglang/srt/layers/moe/fused_moe_triton/configs/triton_3_2_0/E=161,N=192,device_name=NVIDIA_A800-SXM4-80GB,dtype=int8_w8a8.json +146 -0
  57. sglang/srt/layers/moe/fused_moe_triton/configs/triton_3_3_0/E=16,N=1024,device_name=NVIDIA_B200.json +146 -0
  58. sglang/srt/layers/moe/fused_moe_triton/configs/triton_3_3_1/E=128,N=384,device_name=NVIDIA_H20,dtype=fp8_w8a8,block_shape=[128, 128].json +146 -0
  59. sglang/srt/layers/moe/fused_moe_triton/configs/triton_3_3_1/E=128,N=768,device_name=NVIDIA_H20.json +146 -0
  60. sglang/srt/layers/moe/fused_moe_triton/configs/triton_3_3_1/E=160,N=640,device_name=NVIDIA_H200,dtype=fp8_w8a8,block_shape=[128, 128].json +146 -0
  61. sglang/srt/layers/moe/fused_moe_triton/configs/triton_3_3_1/E=257,N=128,device_name=NVIDIA_H20,dtype=fp8_w8a8,block_shape=[128, 128].json +146 -0
  62. sglang/srt/layers/moe/fused_moe_triton/configs/triton_3_3_1/E=257,N=128,device_name=NVIDIA_H200,dtype=fp8_w8a8,block_shape=[128, 128].json +146 -0
  63. sglang/srt/layers/moe/fused_moe_triton/configs/triton_3_3_1/E=257,N=256,device_name=NVIDIA_H20,dtype=fp8_w8a8,block_shape=[128, 128].json +146 -0
  64. sglang/srt/layers/moe/fused_moe_triton/configs/triton_3_3_1/E=257,N=256,device_name=NVIDIA_H20-3e,dtype=fp8_w8a8,block_shape=[128, 128].json +146 -0
  65. sglang/srt/layers/moe/fused_moe_triton/configs/triton_3_3_1/E=257,N=256,device_name=NVIDIA_H200,dtype=fp8_w8a8,block_shape=[128, 128].json +146 -0
  66. sglang/srt/layers/moe/fused_moe_triton/configs/triton_3_4_0/E=128,N=384,device_name=NVIDIA_B200,dtype=fp8_w8a8,block_shape=[128, 128].json +146 -0
  67. sglang/srt/layers/moe/fused_moe_triton/configs/triton_3_4_0/E=129,N=352,device_name=NVIDIA_RTX_PRO_6000_Blackwell_Max-Q_Workstation_Edition,dtype=fp8_w8a8.json +146 -0
  68. sglang/srt/layers/moe/fused_moe_triton/configs/triton_3_4_0/E=129,N=704,device_name=NVIDIA_B200,dtype=fp8_w8a8.json +146 -0
  69. sglang/srt/layers/moe/fused_moe_triton/configs/triton_3_4_0/E=161,N=384,device_name=NVIDIA_RTX_PRO_6000_Blackwell_Max-Q_Workstation_Edition,dtype=fp8_w8a8.json +146 -0
  70. sglang/srt/layers/moe/fused_moe_triton/fused_moe.py +69 -76
  71. sglang/srt/layers/moe/fused_moe_triton/layer.py +66 -123
  72. sglang/srt/layers/moe/fused_moe_triton/triton_kernels_moe.py +20 -18
  73. sglang/srt/layers/moe/moe_runner/__init__.py +3 -0
  74. sglang/srt/layers/moe/moe_runner/base.py +13 -0
  75. sglang/srt/layers/moe/rocm_moe_utils.py +141 -0
  76. sglang/srt/layers/moe/router.py +15 -9
  77. sglang/srt/layers/moe/token_dispatcher/__init__.py +6 -0
  78. sglang/srt/layers/moe/token_dispatcher/base_dispatcher.py +55 -14
  79. sglang/srt/layers/moe/token_dispatcher/deepep.py +11 -21
  80. sglang/srt/layers/moe/token_dispatcher/standard.py +1 -1
  81. sglang/srt/layers/moe/topk.py +167 -83
  82. sglang/srt/layers/moe/utils.py +159 -18
  83. sglang/srt/layers/multimodal.py +156 -40
  84. sglang/srt/layers/quantization/__init__.py +18 -46
  85. sglang/srt/layers/quantization/awq.py +22 -23
  86. sglang/srt/layers/quantization/base_config.py +2 -6
  87. sglang/srt/layers/quantization/blockwise_int8.py +4 -12
  88. sglang/srt/layers/quantization/compressed_tensors/compressed_tensors_moe.py +72 -29
  89. sglang/srt/layers/quantization/deep_gemm_wrapper/configurer.py +2 -1
  90. sglang/srt/layers/quantization/fp8.py +127 -119
  91. sglang/srt/layers/quantization/fp8_kernel.py +195 -24
  92. sglang/srt/layers/quantization/fp8_utils.py +34 -9
  93. sglang/srt/layers/quantization/fpgemm_fp8.py +203 -0
  94. sglang/srt/layers/quantization/gptq.py +17 -21
  95. sglang/srt/layers/quantization/marlin_utils.py +26 -8
  96. sglang/srt/layers/quantization/marlin_utils_fp8.py +352 -0
  97. sglang/srt/layers/quantization/modelopt_quant.py +217 -98
  98. sglang/srt/layers/quantization/moe_wna16.py +10 -15
  99. sglang/srt/layers/quantization/mxfp4.py +222 -39
  100. sglang/srt/layers/quantization/quark/quark.py +390 -0
  101. sglang/srt/layers/quantization/quark/quark_moe.py +197 -0
  102. sglang/srt/layers/quantization/unquant.py +34 -70
  103. sglang/srt/layers/quantization/utils.py +77 -2
  104. sglang/srt/layers/quantization/w4afp8.py +7 -8
  105. sglang/srt/layers/quantization/w8a8_fp8.py +5 -13
  106. sglang/srt/layers/quantization/w8a8_int8.py +5 -13
  107. sglang/srt/layers/radix_attention.py +6 -0
  108. sglang/srt/layers/rotary_embedding.py +1 -0
  109. sglang/srt/layers/sampler.py +5 -2
  110. sglang/srt/lora/layers.py +6 -2
  111. sglang/srt/lora/lora_manager.py +21 -22
  112. sglang/srt/lora/lora_registry.py +3 -3
  113. sglang/srt/lora/mem_pool.py +26 -24
  114. sglang/srt/lora/utils.py +10 -12
  115. sglang/srt/managers/cache_controller.py +80 -19
  116. sglang/srt/managers/detokenizer_manager.py +10 -2
  117. sglang/srt/managers/io_struct.py +23 -0
  118. sglang/srt/managers/mm_utils.py +1 -1
  119. sglang/srt/managers/schedule_batch.py +22 -48
  120. sglang/srt/managers/scheduler.py +28 -20
  121. sglang/srt/managers/session_controller.py +1 -1
  122. sglang/srt/managers/template_manager.py +7 -5
  123. sglang/srt/managers/tokenizer_manager.py +88 -39
  124. sglang/srt/managers/tp_worker.py +1 -0
  125. sglang/srt/managers/utils.py +59 -1
  126. sglang/srt/mem_cache/allocator.py +10 -157
  127. sglang/srt/mem_cache/allocator_ascend.py +147 -0
  128. sglang/srt/mem_cache/chunk_cache.py +1 -1
  129. sglang/srt/mem_cache/hicache_storage.py +14 -4
  130. sglang/srt/mem_cache/memory_pool.py +3 -3
  131. sglang/srt/mem_cache/memory_pool_host.py +35 -2
  132. sglang/srt/mem_cache/storage/hf3fs/storage_hf3fs.py +56 -12
  133. sglang/srt/mem_cache/storage/mooncake_store/mooncake_store.py +8 -4
  134. sglang/srt/mem_cache/storage/nixl/hicache_nixl.py +153 -59
  135. sglang/srt/mem_cache/storage/nixl/nixl_utils.py +19 -53
  136. sglang/srt/mem_cache/storage/nixl/test_hicache_nixl_storage.py +46 -7
  137. sglang/srt/model_executor/cuda_graph_runner.py +33 -33
  138. sglang/srt/model_executor/forward_batch_info.py +11 -10
  139. sglang/srt/model_executor/model_runner.py +93 -78
  140. sglang/srt/model_executor/npu_graph_runner.py +94 -0
  141. sglang/srt/model_loader/loader.py +24 -6
  142. sglang/srt/models/dbrx.py +12 -6
  143. sglang/srt/models/deepseek.py +2 -1
  144. sglang/srt/models/deepseek_nextn.py +5 -2
  145. sglang/srt/models/deepseek_v2.py +226 -223
  146. sglang/srt/models/ernie4.py +2 -2
  147. sglang/srt/models/glm4_moe.py +27 -65
  148. sglang/srt/models/glm4_moe_nextn.py +2 -1
  149. sglang/srt/models/glm4v.py +52 -1
  150. sglang/srt/models/glm4v_moe.py +8 -11
  151. sglang/srt/models/gpt_oss.py +41 -76
  152. sglang/srt/models/granitemoe.py +0 -1
  153. sglang/srt/models/grok.py +376 -48
  154. sglang/srt/models/interns1.py +12 -47
  155. sglang/srt/models/internvl.py +6 -51
  156. sglang/srt/models/llama.py +10 -2
  157. sglang/srt/models/llama4.py +18 -7
  158. sglang/srt/models/minicpm3.py +0 -1
  159. sglang/srt/models/mixtral.py +0 -2
  160. sglang/srt/models/nemotron_nas.py +435 -0
  161. sglang/srt/models/olmoe.py +0 -1
  162. sglang/srt/models/phi4mm.py +3 -21
  163. sglang/srt/models/qwen2.py +2 -2
  164. sglang/srt/models/qwen2_5_vl.py +2 -0
  165. sglang/srt/models/qwen2_moe.py +23 -23
  166. sglang/srt/models/qwen3.py +2 -2
  167. sglang/srt/models/qwen3_classification.py +84 -0
  168. sglang/srt/models/qwen3_moe.py +27 -43
  169. sglang/srt/models/step3_vl.py +8 -3
  170. sglang/srt/models/xverse_moe.py +11 -5
  171. sglang/srt/multimodal/processors/base_processor.py +3 -3
  172. sglang/srt/multimodal/processors/internvl.py +7 -2
  173. sglang/srt/multimodal/processors/llava.py +11 -7
  174. sglang/srt/offloader.py +433 -0
  175. sglang/srt/operations.py +22 -2
  176. sglang/srt/reasoning_parser.py +4 -3
  177. sglang/srt/sampling/sampling_batch_info.py +7 -4
  178. sglang/srt/server_args.py +264 -105
  179. sglang/srt/speculative/eagle_draft_cuda_graph_runner.py +8 -21
  180. sglang/srt/speculative/eagle_draft_extend_cuda_graph_runner.py +7 -21
  181. sglang/srt/speculative/eagle_utils.py +36 -13
  182. sglang/srt/speculative/eagle_worker.py +56 -3
  183. sglang/srt/tokenizer/tiktoken_tokenizer.py +161 -0
  184. sglang/srt/two_batch_overlap.py +20 -19
  185. sglang/srt/utils.py +68 -70
  186. sglang/test/runners.py +8 -5
  187. sglang/test/test_block_fp8.py +5 -6
  188. sglang/test/test_block_fp8_ep.py +13 -19
  189. sglang/test/test_cutlass_moe.py +4 -6
  190. sglang/test/test_cutlass_w4a8_moe.py +4 -3
  191. sglang/test/test_fp4_moe.py +4 -3
  192. sglang/test/test_marlin_moe.py +1 -1
  193. sglang/test/test_marlin_utils.py +1 -1
  194. sglang/test/test_utils.py +7 -0
  195. sglang/utils.py +0 -1
  196. sglang/version.py +1 -1
  197. {sglang-0.5.0rc1.dist-info → sglang-0.5.1.dist-info}/METADATA +11 -11
  198. {sglang-0.5.0rc1.dist-info → sglang-0.5.1.dist-info}/RECORD +201 -171
  199. sglang/srt/layers/quantization/fp4.py +0 -557
  200. sglang/srt/layers/quantization/scalar_type.py +0 -352
  201. {sglang-0.5.0rc1.dist-info → sglang-0.5.1.dist-info}/WHEEL +0 -0
  202. {sglang-0.5.0rc1.dist-info → sglang-0.5.1.dist-info}/licenses/LICENSE +0 -0
  203. {sglang-0.5.0rc1.dist-info → sglang-0.5.1.dist-info}/top_level.txt +0 -0
@@ -1,352 +0,0 @@
1
- # SPDX-License-Identifier: Apache-2.0
2
- # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
-
4
- import functools
5
- import struct
6
- from dataclasses import dataclass
7
- from enum import Enum
8
- from typing import Optional, Union
9
-
10
- _SCALAR_TYPES_ID_MAP = {}
11
-
12
-
13
- # Mirrors enum in `core/scalar_type.hpp`
14
- class NanRepr(Enum):
15
- NONE = 0 # nans are not supported
16
- IEEE_754 = 1 # nans are: Exp all 1s, mantissa not all 0s
17
- EXTD_RANGE_MAX_MIN = 2 # nans are: Exp all 1s, mantissa all 1s
18
-
19
-
20
- # This ScalarType class is a parallel implementation of the C++ ScalarType
21
- # class found in csrc/core/scalar_type.hpp. These two classes should be kept
22
- # in sync until the inductor fully supports custom C++ classes.
23
- @dataclass(frozen=True)
24
- class ScalarType:
25
- """
26
- ScalarType can represent a wide range of floating point and integer
27
- types, in particular it can be used to represent sub-byte data types
28
- (something that torch.dtype currently does not support). It is also
29
- capable of representing types with a bias, i.e.:
30
- `stored_value = value + bias`,
31
- this is useful for quantized types (e.g. standard GPTQ 4bit uses a bias
32
- of 8). The implementation for this class can be found in
33
- csrc/core/scalar_type.hpp, these type signatures should be kept in sync
34
- with that file.
35
- """
36
-
37
- exponent: int
38
- """
39
- Number of bits in the exponent if this is a floating point type
40
- (zero if this an integer type)
41
- """
42
-
43
- mantissa: int
44
- """
45
- Number of bits in the mantissa if this is a floating point type,
46
- or the number bits representing an integer excluding the sign bit if
47
- this an integer type.
48
- """
49
-
50
- signed: bool
51
- "If the type is signed (i.e. has a sign bit)"
52
-
53
- bias: int
54
- """
55
- bias used to encode the values in this scalar type
56
- (value = stored_value - bias, default 0) for example if we store the
57
- type as an unsigned integer with a bias of 128 then the value 0 will be
58
- stored as 128 and -1 will be stored as 127 and 1 will be stored as 129.
59
- """
60
-
61
- _finite_values_only: bool = False
62
- """
63
- Private: if infs are supported, used `has_infs()` instead.
64
- """
65
-
66
- nan_repr: NanRepr = NanRepr.IEEE_754
67
- """
68
- How NaNs are represent in this scalar type, returns NanRepr value.
69
- (not applicable for integer types)
70
- """
71
-
72
- def _floating_point_max_int(self) -> int:
73
- assert (
74
- self.mantissa <= 52 and self.exponent <= 11
75
- ), f"Cannot represent max/min as a double for type {self.__str__()}"
76
-
77
- max_mantissa = (1 << self.mantissa) - 1
78
- if self.nan_repr == NanRepr.EXTD_RANGE_MAX_MIN:
79
- max_mantissa = max_mantissa - 1
80
-
81
- max_exponent = (1 << self.exponent) - 2
82
- if self.nan_repr == NanRepr.EXTD_RANGE_MAX_MIN or self.nan_repr == NanRepr.NONE:
83
- assert (
84
- self.exponent < 11
85
- ), f"Cannot represent max/min as a double for type {self.__str__()}"
86
- max_exponent = max_exponent + 1
87
-
88
- # adjust the exponent to match that of a double
89
- # for now we assume the exponent bias is the standard 2^(e-1) -1, (where
90
- # e is the exponent bits), there is some precedent for non-standard
91
- # biases, example `float8_e4m3b11fnuz` here:
92
- # https://github.com/jax-ml/ml_dtypes but to avoid premature over
93
- # complication we are just assuming the standard exponent bias until
94
- # there is a need to support non-standard biases
95
- exponent_bias = (1 << (self.exponent - 1)) - 1
96
- exponent_bias_double = (1 << 10) - 1 # double e = 11
97
-
98
- max_exponent_double = max_exponent - exponent_bias + exponent_bias_double
99
-
100
- # shift the mantissa and exponent into the proper positions for an
101
- # IEEE double and bitwise-or them together.
102
- return (max_mantissa << (52 - self.mantissa)) | (max_exponent_double << 52)
103
-
104
- def _floating_point_max(self) -> float:
105
- double_raw = self._floating_point_max_int()
106
- return struct.unpack("!d", struct.pack("!Q", double_raw))[0]
107
-
108
- def _raw_max(self) -> Union[int, float]:
109
- if self.is_floating_point():
110
- return self._floating_point_max()
111
- else:
112
- assert (
113
- self.size_bits < 64 or self.size_bits == 64 and self.is_signed()
114
- ), "Cannot represent max as an int"
115
- return (1 << self.mantissa) - 1
116
-
117
- def _raw_min(self) -> Union[int, float]:
118
- if self.is_floating_point():
119
- assert (
120
- self.is_signed()
121
- ), "We currently assume all floating point types are signed"
122
- sign_bit_double = 1 << 63
123
-
124
- max_raw = self._floating_point_max_int()
125
- min_raw = max_raw | sign_bit_double
126
- return struct.unpack("!d", struct.pack("!Q", min_raw))[0]
127
- else:
128
- assert (
129
- not self.is_signed() or self.size_bits <= 64
130
- ), "Cannot represent min as a int64_t"
131
-
132
- if self.is_signed():
133
- return -(1 << (self.size_bits - 1))
134
- else:
135
- return 0
136
-
137
- @functools.cached_property
138
- def id(self) -> int:
139
- """
140
- Convert the ScalarType to an int which can be passed to pytorch custom
141
- ops. This layout of the int must be kept in sync with the C++
142
- ScalarType's from_id method.
143
- """
144
- val = 0
145
- offset = 0
146
-
147
- def or_and_advance(member, bit_width):
148
- nonlocal val
149
- nonlocal offset
150
- bit_mask = (1 << bit_width) - 1
151
- val = val | (int(member) & bit_mask) << offset
152
- offset = offset + bit_width
153
-
154
- or_and_advance(self.exponent, 8)
155
- or_and_advance(self.mantissa, 8)
156
- or_and_advance(self.signed, 1)
157
- or_and_advance(self.bias, 32)
158
- or_and_advance(self._finite_values_only, 1)
159
- or_and_advance(self.nan_repr.value, 8)
160
-
161
- assert offset <= 64, f"ScalarType fields too big {offset} to fit into an int64"
162
-
163
- _SCALAR_TYPES_ID_MAP[val] = self
164
-
165
- return val
166
-
167
- @property
168
- def size_bits(self) -> int:
169
- return self.exponent + self.mantissa + int(self.signed)
170
-
171
- def min(self) -> Union[int, float]:
172
- """
173
- Min representable value for this scalar type.
174
- (accounting for bias if there is one)
175
- """
176
- return self._raw_min() - self.bias
177
-
178
- def max(self) -> Union[int, float]:
179
- """
180
- Max representable value for this scalar type.
181
- (accounting for bias if there is one)
182
- """
183
- return self._raw_max() - self.bias
184
-
185
- def is_signed(self) -> bool:
186
- """
187
- If the type is signed (i.e. has a sign bit), same as `signed`
188
- added for consistency with:
189
- https://pytorch.org/docs/stable/generated/torch.Tensor.is_signed.html
190
- """
191
- return self.signed
192
-
193
- def is_floating_point(self) -> bool:
194
- "If the type is a floating point type"
195
- return self.exponent != 0
196
-
197
- def is_integer(self) -> bool:
198
- "If the type is an integer type"
199
- return self.exponent == 0
200
-
201
- def has_bias(self) -> bool:
202
- "If the type has a non-zero bias"
203
- return self.bias != 0
204
-
205
- def has_infs(self) -> bool:
206
- "If the type is floating point and supports infinity"
207
- return not self._finite_values_only
208
-
209
- def has_nans(self) -> bool:
210
- return self.nan_repr != NanRepr.NONE.value
211
-
212
- def is_ieee_754(self) -> bool:
213
- """
214
- If the type is a floating point type that follows IEEE 754
215
- conventions
216
- """
217
- return self.nan_repr == NanRepr.IEEE_754.value and not self._finite_values_only
218
-
219
- def __str__(self) -> str:
220
- """
221
- naming generally follows: https://github.com/jax-ml/ml_dtypes
222
- for floating point types (leading f) the scheme is:
223
- `float<size_bits>_e<exponent_bits>m<mantissa_bits>[flags]`
224
- flags:
225
- - no-flags: means it follows IEEE 754 conventions
226
- - f: means finite values only (no infinities)
227
- - n: means nans are supported (non-standard encoding)
228
- for integer types the scheme is:
229
- `[u]int<size_bits>[b<bias>]`
230
- - if bias is not present it means its zero
231
- """
232
- if self.is_floating_point():
233
- ret = (
234
- "float"
235
- + str(self.size_bits)
236
- + "_e"
237
- + str(self.exponent)
238
- + "m"
239
- + str(self.mantissa)
240
- )
241
-
242
- if not self.is_ieee_754():
243
- if self._finite_values_only:
244
- ret = ret + "f"
245
- if self.nan_repr != NanRepr.NONE:
246
- ret = ret + "n"
247
-
248
- return ret
249
- else:
250
- ret = ("int" if self.is_signed() else "uint") + str(self.size_bits)
251
- if self.has_bias():
252
- ret = ret + "b" + str(self.bias)
253
- return ret
254
-
255
- def __repr__(self) -> str:
256
- return "ScalarType." + self.__str__()
257
-
258
- # __len__ needs to be defined (and has to throw TypeError) for pytorch's
259
- # opcheck to work.
260
- def __len__(self) -> int:
261
- raise TypeError
262
-
263
- #
264
- # Convenience Constructors
265
- #
266
-
267
- @classmethod
268
- def int_(cls, size_bits: int, bias: Optional[int]) -> "ScalarType":
269
- "Create a signed integer scalar type (size_bits includes sign-bit)."
270
- ret = cls(0, size_bits - 1, True, bias if bias else 0)
271
- ret.id # noqa B018: make sure the id is cached
272
- return ret
273
-
274
- @classmethod
275
- def uint(cls, size_bits: int, bias: Optional[int]) -> "ScalarType":
276
- """Create a unsigned integer scalar type."""
277
- ret = cls(0, size_bits, False, bias if bias else 0)
278
- ret.id # noqa B018: make sure the id is cached
279
- return ret
280
-
281
- @classmethod
282
- def float_IEEE754(cls, exponent: int, mantissa: int) -> "ScalarType":
283
- """
284
- Create a standard floating point type
285
- (i.e. follows IEEE 754 conventions).
286
- """
287
- assert mantissa > 0 and exponent > 0
288
- ret = cls(exponent, mantissa, True, 0)
289
- ret.id # noqa B018: make sure the id is cached
290
- return ret
291
-
292
- @classmethod
293
- def float_(
294
- cls, exponent: int, mantissa: int, finite_values_only: bool, nan_repr: NanRepr
295
- ) -> "ScalarType":
296
- """
297
- Create a non-standard floating point type
298
- (i.e. does not follow IEEE 754 conventions).
299
- """
300
- assert mantissa > 0 and exponent > 0
301
- assert nan_repr != NanRepr.IEEE_754, (
302
- "use `float_IEEE754` constructor for floating point types that "
303
- "follow IEEE 754 conventions"
304
- )
305
- ret = cls(exponent, mantissa, True, 0, finite_values_only, nan_repr)
306
- ret.id # noqa B018: make sure the id is cached
307
- return ret
308
-
309
- @classmethod
310
- def from_id(cls, scalar_type_id: int):
311
- if scalar_type_id not in _SCALAR_TYPES_ID_MAP:
312
- raise ValueError(f"scalar_type_id {scalar_type_id} doesn't exists.")
313
- return _SCALAR_TYPES_ID_MAP[scalar_type_id]
314
-
315
-
316
- # naming generally follows: https://github.com/jax-ml/ml_dtypes
317
- # for floating point types (leading f) the scheme is:
318
- # `float<size_bits>_e<exponent_bits>m<mantissa_bits>[flags]`
319
- # flags:
320
- # - no-flags: means it follows IEEE 754 conventions
321
- # - f: means finite values only (no infinities)
322
- # - n: means nans are supported (non-standard encoding)
323
- # for integer types the scheme is:
324
- # `[u]int<size_bits>[b<bias>]`
325
- # - if bias is not present it means its zero
326
-
327
-
328
- class scalar_types:
329
- int4 = ScalarType.int_(4, None)
330
- uint4 = ScalarType.uint(4, None)
331
- int8 = ScalarType.int_(8, None)
332
- uint8 = ScalarType.uint(8, None)
333
- float8_e4m3fn = ScalarType.float_(4, 3, True, NanRepr.EXTD_RANGE_MAX_MIN)
334
- float8_e5m2 = ScalarType.float_IEEE754(5, 2)
335
- float16_e8m7 = ScalarType.float_IEEE754(8, 7)
336
- float16_e5m10 = ScalarType.float_IEEE754(5, 10)
337
-
338
- # fp6, https://github.com/usyd-fsalab/fp6_llm/tree/main
339
- float6_e3m2f = ScalarType.float_(3, 2, True, NanRepr.NONE)
340
-
341
- # fp4, https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
342
- float4_e2m1f = ScalarType.float_(2, 1, True, NanRepr.NONE)
343
-
344
- # "gptq" types
345
- uint2b2 = ScalarType.uint(2, 2)
346
- uint3b4 = ScalarType.uint(3, 4)
347
- uint4b8 = ScalarType.uint(4, 8)
348
- uint8b128 = ScalarType.uint(8, 128)
349
-
350
- # colloquial names
351
- bfloat16 = float16_e8m7
352
- float16 = float16_e5m10