liangsu9988 commited on
Commit
cc23714
·
verified ·
1 Parent(s): e8ac26e

Promote latest kernel artifacts to main

Browse files
README.md CHANGED
@@ -1,9 +1,116 @@
1
- # flashrt/fp4-gemm
2
 
3
- This repository is a compatibility mirror for older `kernels` clients
4
- that resolve repositories through the default Hugging Face model repo API.
5
 
6
- Canonical Kernel Hub repo: https://huggingface.co/kernels/flashrt/fp4-gemm
 
 
 
7
 
8
- Do not edit this mirror by hand. It is generated from the Kernel Hub
9
- `vN` branches and contains the same `build/**` artifacts.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # fp4-gemm
2
 
3
+ FlashRT native Blackwell NVFP4 A4W4 GEMM kernels.
 
4
 
5
+ This package consumes packed FP4 E2M1 tensors plus CUTLASS Sm1xx SFA/SFB scale
6
+ buffers and produces BF16 output. It is designed to pair with
7
+ `flashrt/fp4-fused-ops` and other static low-bit transformer/diffuser runtime
8
+ paths.
9
 
10
+ ## Available Functions
11
+
12
+ - `sfa_size_bytes(rows, dim)`
13
+ - `quantize_fp4_sfa_fp16(x, packed=None, sfa=None, is_sfb=False)`
14
+ - `quantize_fp4_sfa_bf16(x, packed=None, sfa=None, is_sfb=False)`
15
+ - `dequantize_fp4_sfa_fp16(packed, sfa, out=None, is_sfb=False)`
16
+ - `nvfp4_gemm_bf16(a_packed, b_packed, sfa, sfb, alpha=1.0, out=None, variant=-1)`
17
+ - `nvfp4_gemm_bias_bf16(a_packed, b_packed, sfa, sfb, bias, out=None)`
18
+ - `nvfp4_gemm_bias_residual_bf16(a_packed, b_packed, sfa, sfb, bias, residual, out=None)`
19
+ - `nvfp4_gemm_residual_bf16(a_packed, b_packed, sfa, sfb, residual, alpha=1.0, out=None)`
20
+ - `nvfp4_gemm_bias_gelu_bf16(a_packed, b_packed, sfa, sfb, bias, alpha=1.0, out=None)`
21
+ - `nvfp4_gemm_bias_gelu_nvfp4(a_packed, b_packed, sfa, sfb, bias, alpha=1.0, out_packed=None, out_sfa=None)`
22
+ - `nvfp4_gemm_streamk_bf16(a_packed, b_packed, sfa, sfb, alpha=1.0, out=None)`
23
+ - `nvfp4_gemm_streamk_bias_bf16(a_packed, b_packed, sfa, sfb, bias, alpha=1.0, out=None)`
24
+ - `fp4_w4a16_linear_bf16(...)` is retained as a compatibility alias
25
+
26
+ ## Tensor Contract
27
+
28
+ - `a_packed`: `torch.uint8`, shape `(M, K / 2)`.
29
+ - `b_packed`: `torch.uint8`, shape `(N, K / 2)`.
30
+ - `sfa`: `torch.uint8`, CUTLASS SFA layout for `(M, K)`.
31
+ - `sfb`: `torch.uint8`, CUTLASS SFB layout for `(N, K)`.
32
+ - output: `torch.bfloat16`, shape `(M, N)`.
33
+ - `K` must be divisible by 16.
34
+ - Targets: Blackwell `sm_110a` (Jetson AGX Thor, CUDA 13+) and `sm_120a`
35
+ (RTX Blackwell, CUDA 12.8+).
36
+
37
+ `variant` selects the CUTLASS schedule:
38
+
39
+ - `-1`: architecture-aware auto-dispatch (public default).
40
+ - `0`: default `<128,128,256>` cooperative schedule.
41
+ - `1`: widen `<128,256,128>` schedule, intended for very large `N`.
42
+ - `2`: pingpong schedule for A/B testing shape-specific wins.
43
+
44
+ The canonical linear API and FP4/SFA quantize/dequantize helpers are available
45
+ on both SM110 and SM120. SM110 additionally provides the GROOT N1.7 production
46
+ epilogues `nvfp4_gemm_bias_bf16`, `nvfp4_gemm_bias_residual_bf16`, and
47
+ `nvfp4_gemm_bias_gelu_nvfp4`. The latter emits packed FP4 plus CUTLASS SFA so
48
+ the following projection can consume it without a BF16 materialization and a
49
+ standalone quantization launch. Stream-K and the older BF16 GELU epilogue keep
50
+ their existing SM120 dispatch and reject unsupported architectures explicitly.
51
+
52
+ The SM110 release gate includes the production `(M,N,K)` shapes
53
+ `(41,4608,1536)`, `(41,6144,1536)`, and `(41,1536,6144)`, plus the legacy
54
+ `M=51` compatibility row. The kernels are the native sources used by FlashRT's
55
+ GROOT N1.7 Thor NVFP4 pipeline.
56
+
57
+ ## Minimal Usage
58
+
59
+ ```python
60
+ from kernels import get_kernel
61
+ import torch
62
+
63
+ ops = get_kernel("flashrt/fp4-gemm", version=1, trust_remote_code=True)
64
+
65
+ x = torch.randn((32, 256), device="cuda", dtype=torch.float16)
66
+ w = torch.randn((512, 256), device="cuda", dtype=torch.float16)
67
+
68
+ a_packed, sfa = ops.quantize_fp4_sfa_fp16(x, is_sfb=False)
69
+ b_packed, sfb = ops.quantize_fp4_sfa_fp16(w, is_sfb=True)
70
+
71
+ y = ops.nvfp4_gemm_bf16(a_packed, b_packed, sfa, sfb, alpha=1.0)
72
+ ```
73
+
74
+ For BF16 model activations, use the direct producer so the hot path does not
75
+ materialize an intermediate FP16 tensor:
76
+
77
+ ```python
78
+ x_bf16 = torch.randn((1, 5120), device="cuda", dtype=torch.bfloat16)
79
+ a_packed, sfa = ops.quantize_fp4_sfa_bf16(x_bf16)
80
+ ```
81
+
82
+ The BF16 entry writes the same E2M1 bytes and CUTLASS SFA/SFB layout as
83
+ `quantize_fp4_sfa_fp16(x_bf16.to(torch.float16))` for finite FP16-range
84
+ inputs. It is an additive API; the existing FP16 producer remains unchanged.
85
+
86
+ The quantize/dequantize helpers are included for examples and validation. A
87
+ production runtime should keep weights prepacked and should avoid quantizing in
88
+ the hot path unless that producer kernel is part of the intended low-bit block.
89
+
90
+ Use the bias/GELU and residual variants to avoid returning to BF16
91
+ elementwise code between low-bit GEMMs. Stream-K variants are selected only
92
+ for the validated large down-projection shapes; unsupported shapes reject
93
+ rather than silently selecting a losing schedule.
94
+
95
+ ## Validation
96
+
97
+ ```bash
98
+ python fp4-gemm/tests/test_fp4_gemm.py --backend source --mode full
99
+ python fp4-gemm/tests/test_fp4_gemm.py --backend installed --mode full \
100
+ --artifact fp4-gemm/build/torch211-cxx11-cu128-x86_64-linux
101
+ python fp4-gemm/benchmarks/benchmark.py --backend installed --mode headline \
102
+ --artifact fp4-gemm/build/torch211-cxx11-cu128-x86_64-linux
103
+
104
+ # Thor model-shape gate
105
+ python fp4-gemm/tests/test_fp4_gemm.py --backend installed \
106
+ --mode thor-models \
107
+ --artifact fp4-gemm/build/torch211-cxx11-cu130-aarch64-linux
108
+ ```
109
+
110
+ The correctness reference dequantizes the same FP4/SFA and FP4/SFB inputs used
111
+ by the kernel, then computes the PyTorch GEMM reference from those dequantized
112
+ low-bit values.
113
+
114
+ The producer gate also checks the BF16 direct entry byte-for-byte against the
115
+ established FP16 compatibility chain at decode widths 5120, 6144 and 17408,
116
+ plus multi-row activation and SFB layouts.
build/torch212-cxx11-cu130-x86_64-linux/__init__.py CHANGED
@@ -109,6 +109,11 @@ def _quant_mse_fake(x, packed, sfa, is_sfb: bool = False) -> None:
109
  return None
110
 
111
 
 
 
 
 
 
112
  @torch.library.register_fake(add_op_namespace_prefix("quantize_e0m3_sfa_fp16"))
113
  def _quant_e0m3_fake(x, packed, sfa, is_sfb: bool = False) -> None:
114
  return None
@@ -186,6 +191,91 @@ def quantize_fp4_sfa_mse_fp16(
186
  return packed, sfa
187
 
188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  def quantize_e0m3_sfa_fp16(
190
  x: torch.Tensor,
191
  packed: torch.Tensor | None = None,
@@ -381,6 +471,29 @@ def nvfp4_gemm_geglu_nvfp4_fp16(
381
  return out_packed, out_sfa
382
 
383
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
384
  def nvfp4_gemm_bias_gelu_nvfp4_fp16(
385
  a_packed: torch.Tensor,
386
  b_packed: torch.Tensor,
@@ -601,6 +714,8 @@ def nvfp4_gemm_streamk_bias_bf16(
601
 
602
 
603
  __all__ = [
 
 
604
  "dequantize_fp4_sfa_fp16",
605
  "e0m3_weight_gemm_fp16",
606
  "fp4_w4a16_linear_bf16",
@@ -622,6 +737,9 @@ __all__ = [
622
  "nvfp4_gemm_streamk_bias_bf16",
623
  "quantize_fp4_sfa_fp16",
624
  "quantize_fp4_sfa_mse_fp16",
 
 
 
625
  "quantize_e0m3_sfa_fp16",
626
  "quantize_fp4_sfa_bf16",
627
  "sfa_size_bytes",
 
109
  return None
110
 
111
 
112
+ @torch.library.register_fake(add_op_namespace_prefix("quantize_fp4_sfa_mse_bf16"))
113
+ def _quant_mse_bf16_fake(x, packed, sfa, is_sfb: bool = False) -> None:
114
+ return None
115
+
116
+
117
  @torch.library.register_fake(add_op_namespace_prefix("quantize_e0m3_sfa_fp16"))
118
  def _quant_e0m3_fake(x, packed, sfa, is_sfb: bool = False) -> None:
119
  return None
 
191
  return packed, sfa
192
 
193
 
194
+ def quantize_fp4_sfa_mse_bf16(
195
+ x: torch.Tensor,
196
+ packed: torch.Tensor | None = None,
197
+ sfa: torch.Tensor | None = None,
198
+ is_sfb: bool = False,
199
+ ):
200
+ """Pack BF16 weights using per-block reconstruction-MSE scale search."""
201
+ if packed is None or sfa is None:
202
+ packed, sfa = _alloc_fp4(x.shape[0], x.shape[1], x.device)
203
+ ops.quantize_fp4_sfa_mse_bf16(x, packed, sfa, bool(is_sfb))
204
+ return packed, sfa
205
+
206
+
207
+ def aligned_fp4_dim(dim: int, alignment: int = 32) -> int:
208
+ """Return the physical FP4 GEMM dimension for a logical dimension."""
209
+ if dim <= 0 or alignment <= 0 or alignment % 16 != 0:
210
+ raise ValueError("dim must be positive and alignment divisible by 16")
211
+ return ((dim + alignment - 1) // alignment) * alignment
212
+
213
+
214
+ def quantize_fp4_sfa_padded_bf16(
215
+ x: torch.Tensor,
216
+ *,
217
+ alignment: int = 32,
218
+ is_sfb: bool = False,
219
+ ):
220
+ """Bind-time BF16 pack with zero padding on the final dimension.
221
+
222
+ The returned packed tensor uses the physical aligned width. Keep the
223
+ logical width returned as the third item for slicing final model outputs.
224
+ This helper allocates and is intentionally not a runtime hot-path op.
225
+ """
226
+ if x.ndim != 2 or x.dtype != torch.bfloat16:
227
+ raise ValueError("x must be a 2-D BF16 tensor")
228
+ logical_dim = int(x.shape[1])
229
+ physical_dim = aligned_fp4_dim(logical_dim, alignment)
230
+ if physical_dim == logical_dim:
231
+ padded = x.contiguous()
232
+ else:
233
+ padded = torch.zeros(
234
+ (x.shape[0], physical_dim), device=x.device, dtype=x.dtype
235
+ )
236
+ padded[:, :logical_dim].copy_(x)
237
+ packed, sfa = quantize_fp4_sfa_bf16(padded, is_sfb=is_sfb)
238
+ return packed, sfa, logical_dim
239
+
240
+
241
+ def pack_nvfp4_weight_bf16(
242
+ weight: torch.Tensor,
243
+ bias: torch.Tensor | None = None,
244
+ *,
245
+ alignment: int = 32,
246
+ mse: bool = False,
247
+ ):
248
+ """Zero-pad and pack a logical ``(N, K)`` BF16 weight at bind time.
249
+
250
+ Both physical ``N`` and ``K`` are aligned. This is the model-neutral
251
+ contract used by widths such as SigLIP's logical 4304, which becomes 4320
252
+ for Blackwell NVFP4 TMA. The hot path consumes the returned static tensors
253
+ directly and performs no padding or allocation.
254
+ """
255
+ if weight.ndim != 2 or weight.dtype != torch.bfloat16:
256
+ raise ValueError("weight must be a 2-D BF16 tensor")
257
+ logical_n, logical_k = map(int, weight.shape)
258
+ physical_n = aligned_fp4_dim(logical_n, alignment)
259
+ physical_k = aligned_fp4_dim(logical_k, alignment)
260
+ padded_weight = torch.zeros(
261
+ (physical_n, physical_k), device=weight.device, dtype=weight.dtype
262
+ )
263
+ padded_weight[:logical_n, :logical_k].copy_(weight)
264
+ quantize = quantize_fp4_sfa_mse_bf16 if mse else quantize_fp4_sfa_bf16
265
+ packed, sfb = quantize(padded_weight, is_sfb=True)
266
+ padded_bias = None
267
+ if bias is not None:
268
+ if bias.ndim != 1 or bias.numel() != logical_n:
269
+ raise ValueError("bias must have shape (N,)")
270
+ if bias.dtype != torch.bfloat16 or bias.device != weight.device:
271
+ raise ValueError("bias must match the weight dtype and device")
272
+ padded_bias = torch.zeros(
273
+ (physical_n,), device=bias.device, dtype=bias.dtype
274
+ )
275
+ padded_bias[:logical_n].copy_(bias)
276
+ return packed, sfb, padded_bias, (logical_n, logical_k)
277
+
278
+
279
  def quantize_e0m3_sfa_fp16(
280
  x: torch.Tensor,
281
  packed: torch.Tensor | None = None,
 
471
  return out_packed, out_sfa
472
 
473
 
474
+ def cutlass_fp4_gemm_geglu_il_hw_v10(
475
+ a_packed: torch.Tensor,
476
+ b_interleaved_packed: torch.Tensor,
477
+ sfa: torch.Tensor,
478
+ sfb: torch.Tensor,
479
+ *,
480
+ scratch: torch.Tensor | None = None,
481
+ out_packed: torch.Tensor | None = None,
482
+ out_sfa: torch.Tensor | None = None,
483
+ ) -> tuple[torch.Tensor, torch.Tensor]:
484
+ """Explicit public alias for the native SM110 v10 GeGLU schedule."""
485
+ return nvfp4_gemm_geglu_nvfp4_fp16(
486
+ a_packed,
487
+ b_interleaved_packed,
488
+ sfa,
489
+ sfb,
490
+ skinny=True,
491
+ scratch=scratch,
492
+ out_packed=out_packed,
493
+ out_sfa=out_sfa,
494
+ )
495
+
496
+
497
  def nvfp4_gemm_bias_gelu_nvfp4_fp16(
498
  a_packed: torch.Tensor,
499
  b_packed: torch.Tensor,
 
714
 
715
 
716
  __all__ = [
717
+ "aligned_fp4_dim",
718
+ "cutlass_fp4_gemm_geglu_il_hw_v10",
719
  "dequantize_fp4_sfa_fp16",
720
  "e0m3_weight_gemm_fp16",
721
  "fp4_w4a16_linear_bf16",
 
737
  "nvfp4_gemm_streamk_bias_bf16",
738
  "quantize_fp4_sfa_fp16",
739
  "quantize_fp4_sfa_mse_fp16",
740
+ "quantize_fp4_sfa_mse_bf16",
741
+ "quantize_fp4_sfa_padded_bf16",
742
+ "pack_nvfp4_weight_bf16",
743
  "quantize_e0m3_sfa_fp16",
744
  "quantize_fp4_sfa_bf16",
745
  "sfa_size_bytes",
build/torch212-cxx11-cu130-x86_64-linux/{_fp4_gemm_cuda_b6588aa.abi3.so → _fp4_gemm_cuda_92c4b99.abi3.so} RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:2d9be54b8589e5131efa5a4ff9ddd2699519e41aa5603d54b5b0a8410c7c2a07
3
- size 7718880
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fe2db33c4dc640281bcfccce7ae0c98a53a42efeef388079b65149bb7e3031a5
3
+ size 7895896
build/torch212-cxx11-cu130-x86_64-linux/_ops.py CHANGED
@@ -1,9 +1,9 @@
1
  import torch
2
- from . import _fp4_gemm_cuda_b6588aa
3
- ops = torch.ops._fp4_gemm_cuda_b6588aa
4
 
5
  def add_op_namespace_prefix(op_name: str):
6
  """
7
  Prefix op by namespace.
8
  """
9
- return f"_fp4_gemm_cuda_b6588aa::{op_name}"
 
1
  import torch
2
+ from . import _fp4_gemm_cuda_92c4b99
3
+ ops = torch.ops._fp4_gemm_cuda_92c4b99
4
 
5
  def add_op_namespace_prefix(op_name: str):
6
  """
7
  Prefix op by namespace.
8
  """
9
+ return f"_fp4_gemm_cuda_92c4b99::{op_name}"
build/torch212-cxx11-cu130-x86_64-linux/metadata.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "fp4-gemm",
3
- "id": "_fp4_gemm_cuda_b6588aa",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "python-depends": [],
@@ -14,9 +14,9 @@
14
  "digest": {
15
  "algorithm": "sha256",
16
  "files": {
17
- "__init__.py": "Fb/FDYOpDw+YGlevblUsv4AkILDQCJdB807qdDUDI1c=",
18
- "_fp4_gemm_cuda_b6588aa.abi3.so": "LZvlS4WJ5RMe+lpP+d3SaZUZ5BqlYD1UtbCoQQx8Kgc=",
19
- "_ops.py": "KKClchvuzBz2g1A148Gi2R6QIx0Ipj0CKlTT511GrUY="
20
  }
21
  },
22
  "provenance": {
@@ -26,7 +26,7 @@
26
  "dirty": false
27
  },
28
  "kernel": {
29
- "sha": "b6588aa59dd4f8c646f90b58312a0a66886e061f",
30
  "dirty": false
31
  }
32
  }
 
1
  {
2
  "name": "fp4-gemm",
3
+ "id": "_fp4_gemm_cuda_92c4b99",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "python-depends": [],
 
14
  "digest": {
15
  "algorithm": "sha256",
16
  "files": {
17
+ "__init__.py": "Mr1/bAUzs+af68D+bRXX6iONDof//dzKgn6QU5pKdu8=",
18
+ "_fp4_gemm_cuda_92c4b99.abi3.so": "/i2zPE3GQCgbz8zOeuDJilOkLv7vOIB5tlFJu34wMaU=",
19
+ "_ops.py": "c5GHeHUOPDgpedue7MgdtRjCwHOmUsBo4OfqoFVegq0="
20
  }
21
  },
22
  "provenance": {
 
26
  "dirty": false
27
  },
28
  "kernel": {
29
+ "sha": "92c4b991b6f1fbd89744aa6f46f62b1bfac7ca7e",
30
  "dirty": false
31
  }
32
  }
build/torch212-cxx11-cu132-x86_64-linux/__init__.py CHANGED
@@ -109,6 +109,11 @@ def _quant_mse_fake(x, packed, sfa, is_sfb: bool = False) -> None:
109
  return None
110
 
111
 
 
 
 
 
 
112
  @torch.library.register_fake(add_op_namespace_prefix("quantize_e0m3_sfa_fp16"))
113
  def _quant_e0m3_fake(x, packed, sfa, is_sfb: bool = False) -> None:
114
  return None
@@ -186,6 +191,91 @@ def quantize_fp4_sfa_mse_fp16(
186
  return packed, sfa
187
 
188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  def quantize_e0m3_sfa_fp16(
190
  x: torch.Tensor,
191
  packed: torch.Tensor | None = None,
@@ -381,6 +471,29 @@ def nvfp4_gemm_geglu_nvfp4_fp16(
381
  return out_packed, out_sfa
382
 
383
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
384
  def nvfp4_gemm_bias_gelu_nvfp4_fp16(
385
  a_packed: torch.Tensor,
386
  b_packed: torch.Tensor,
@@ -601,6 +714,8 @@ def nvfp4_gemm_streamk_bias_bf16(
601
 
602
 
603
  __all__ = [
 
 
604
  "dequantize_fp4_sfa_fp16",
605
  "e0m3_weight_gemm_fp16",
606
  "fp4_w4a16_linear_bf16",
@@ -622,6 +737,9 @@ __all__ = [
622
  "nvfp4_gemm_streamk_bias_bf16",
623
  "quantize_fp4_sfa_fp16",
624
  "quantize_fp4_sfa_mse_fp16",
 
 
 
625
  "quantize_e0m3_sfa_fp16",
626
  "quantize_fp4_sfa_bf16",
627
  "sfa_size_bytes",
 
109
  return None
110
 
111
 
112
+ @torch.library.register_fake(add_op_namespace_prefix("quantize_fp4_sfa_mse_bf16"))
113
+ def _quant_mse_bf16_fake(x, packed, sfa, is_sfb: bool = False) -> None:
114
+ return None
115
+
116
+
117
  @torch.library.register_fake(add_op_namespace_prefix("quantize_e0m3_sfa_fp16"))
118
  def _quant_e0m3_fake(x, packed, sfa, is_sfb: bool = False) -> None:
119
  return None
 
191
  return packed, sfa
192
 
193
 
194
+ def quantize_fp4_sfa_mse_bf16(
195
+ x: torch.Tensor,
196
+ packed: torch.Tensor | None = None,
197
+ sfa: torch.Tensor | None = None,
198
+ is_sfb: bool = False,
199
+ ):
200
+ """Pack BF16 weights using per-block reconstruction-MSE scale search."""
201
+ if packed is None or sfa is None:
202
+ packed, sfa = _alloc_fp4(x.shape[0], x.shape[1], x.device)
203
+ ops.quantize_fp4_sfa_mse_bf16(x, packed, sfa, bool(is_sfb))
204
+ return packed, sfa
205
+
206
+
207
+ def aligned_fp4_dim(dim: int, alignment: int = 32) -> int:
208
+ """Return the physical FP4 GEMM dimension for a logical dimension."""
209
+ if dim <= 0 or alignment <= 0 or alignment % 16 != 0:
210
+ raise ValueError("dim must be positive and alignment divisible by 16")
211
+ return ((dim + alignment - 1) // alignment) * alignment
212
+
213
+
214
+ def quantize_fp4_sfa_padded_bf16(
215
+ x: torch.Tensor,
216
+ *,
217
+ alignment: int = 32,
218
+ is_sfb: bool = False,
219
+ ):
220
+ """Bind-time BF16 pack with zero padding on the final dimension.
221
+
222
+ The returned packed tensor uses the physical aligned width. Keep the
223
+ logical width returned as the third item for slicing final model outputs.
224
+ This helper allocates and is intentionally not a runtime hot-path op.
225
+ """
226
+ if x.ndim != 2 or x.dtype != torch.bfloat16:
227
+ raise ValueError("x must be a 2-D BF16 tensor")
228
+ logical_dim = int(x.shape[1])
229
+ physical_dim = aligned_fp4_dim(logical_dim, alignment)
230
+ if physical_dim == logical_dim:
231
+ padded = x.contiguous()
232
+ else:
233
+ padded = torch.zeros(
234
+ (x.shape[0], physical_dim), device=x.device, dtype=x.dtype
235
+ )
236
+ padded[:, :logical_dim].copy_(x)
237
+ packed, sfa = quantize_fp4_sfa_bf16(padded, is_sfb=is_sfb)
238
+ return packed, sfa, logical_dim
239
+
240
+
241
+ def pack_nvfp4_weight_bf16(
242
+ weight: torch.Tensor,
243
+ bias: torch.Tensor | None = None,
244
+ *,
245
+ alignment: int = 32,
246
+ mse: bool = False,
247
+ ):
248
+ """Zero-pad and pack a logical ``(N, K)`` BF16 weight at bind time.
249
+
250
+ Both physical ``N`` and ``K`` are aligned. This is the model-neutral
251
+ contract used by widths such as SigLIP's logical 4304, which becomes 4320
252
+ for Blackwell NVFP4 TMA. The hot path consumes the returned static tensors
253
+ directly and performs no padding or allocation.
254
+ """
255
+ if weight.ndim != 2 or weight.dtype != torch.bfloat16:
256
+ raise ValueError("weight must be a 2-D BF16 tensor")
257
+ logical_n, logical_k = map(int, weight.shape)
258
+ physical_n = aligned_fp4_dim(logical_n, alignment)
259
+ physical_k = aligned_fp4_dim(logical_k, alignment)
260
+ padded_weight = torch.zeros(
261
+ (physical_n, physical_k), device=weight.device, dtype=weight.dtype
262
+ )
263
+ padded_weight[:logical_n, :logical_k].copy_(weight)
264
+ quantize = quantize_fp4_sfa_mse_bf16 if mse else quantize_fp4_sfa_bf16
265
+ packed, sfb = quantize(padded_weight, is_sfb=True)
266
+ padded_bias = None
267
+ if bias is not None:
268
+ if bias.ndim != 1 or bias.numel() != logical_n:
269
+ raise ValueError("bias must have shape (N,)")
270
+ if bias.dtype != torch.bfloat16 or bias.device != weight.device:
271
+ raise ValueError("bias must match the weight dtype and device")
272
+ padded_bias = torch.zeros(
273
+ (physical_n,), device=bias.device, dtype=bias.dtype
274
+ )
275
+ padded_bias[:logical_n].copy_(bias)
276
+ return packed, sfb, padded_bias, (logical_n, logical_k)
277
+
278
+
279
  def quantize_e0m3_sfa_fp16(
280
  x: torch.Tensor,
281
  packed: torch.Tensor | None = None,
 
471
  return out_packed, out_sfa
472
 
473
 
474
+ def cutlass_fp4_gemm_geglu_il_hw_v10(
475
+ a_packed: torch.Tensor,
476
+ b_interleaved_packed: torch.Tensor,
477
+ sfa: torch.Tensor,
478
+ sfb: torch.Tensor,
479
+ *,
480
+ scratch: torch.Tensor | None = None,
481
+ out_packed: torch.Tensor | None = None,
482
+ out_sfa: torch.Tensor | None = None,
483
+ ) -> tuple[torch.Tensor, torch.Tensor]:
484
+ """Explicit public alias for the native SM110 v10 GeGLU schedule."""
485
+ return nvfp4_gemm_geglu_nvfp4_fp16(
486
+ a_packed,
487
+ b_interleaved_packed,
488
+ sfa,
489
+ sfb,
490
+ skinny=True,
491
+ scratch=scratch,
492
+ out_packed=out_packed,
493
+ out_sfa=out_sfa,
494
+ )
495
+
496
+
497
  def nvfp4_gemm_bias_gelu_nvfp4_fp16(
498
  a_packed: torch.Tensor,
499
  b_packed: torch.Tensor,
 
714
 
715
 
716
  __all__ = [
717
+ "aligned_fp4_dim",
718
+ "cutlass_fp4_gemm_geglu_il_hw_v10",
719
  "dequantize_fp4_sfa_fp16",
720
  "e0m3_weight_gemm_fp16",
721
  "fp4_w4a16_linear_bf16",
 
737
  "nvfp4_gemm_streamk_bias_bf16",
738
  "quantize_fp4_sfa_fp16",
739
  "quantize_fp4_sfa_mse_fp16",
740
+ "quantize_fp4_sfa_mse_bf16",
741
+ "quantize_fp4_sfa_padded_bf16",
742
+ "pack_nvfp4_weight_bf16",
743
  "quantize_e0m3_sfa_fp16",
744
  "quantize_fp4_sfa_bf16",
745
  "sfa_size_bytes",
build/torch212-cxx11-cu132-x86_64-linux/{_fp4_gemm_cuda_b6588aa.abi3.so → _fp4_gemm_cuda_92c4b99.abi3.so} RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:308bf04520e36c5f5cbdcb45644cc86b01449729e246a10f87cc835b2935be25
3
- size 7706544
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:68ba4bc31b2467a824e3e0a2daeb3ddacac43f4e16b5ec0779b90c283194f0cb
3
+ size 7887648
build/torch212-cxx11-cu132-x86_64-linux/_ops.py CHANGED
@@ -1,9 +1,9 @@
1
  import torch
2
- from . import _fp4_gemm_cuda_b6588aa
3
- ops = torch.ops._fp4_gemm_cuda_b6588aa
4
 
5
  def add_op_namespace_prefix(op_name: str):
6
  """
7
  Prefix op by namespace.
8
  """
9
- return f"_fp4_gemm_cuda_b6588aa::{op_name}"
 
1
  import torch
2
+ from . import _fp4_gemm_cuda_92c4b99
3
+ ops = torch.ops._fp4_gemm_cuda_92c4b99
4
 
5
  def add_op_namespace_prefix(op_name: str):
6
  """
7
  Prefix op by namespace.
8
  """
9
+ return f"_fp4_gemm_cuda_92c4b99::{op_name}"
build/torch212-cxx11-cu132-x86_64-linux/metadata.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "fp4-gemm",
3
- "id": "_fp4_gemm_cuda_b6588aa",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "python-depends": [],
@@ -14,9 +14,9 @@
14
  "digest": {
15
  "algorithm": "sha256",
16
  "files": {
17
- "__init__.py": "Fb/FDYOpDw+YGlevblUsv4AkILDQCJdB807qdDUDI1c=",
18
- "_fp4_gemm_cuda_b6588aa.abi3.so": "MIvwRSDjbF9cvctFZEzIawFElyniRqEPh8yDWyk1viU=",
19
- "_ops.py": "KKClchvuzBz2g1A148Gi2R6QIx0Ipj0CKlTT511GrUY="
20
  }
21
  },
22
  "provenance": {
@@ -26,7 +26,7 @@
26
  "dirty": false
27
  },
28
  "kernel": {
29
- "sha": "b6588aa59dd4f8c646f90b58312a0a66886e061f",
30
  "dirty": false
31
  }
32
  }
 
1
  {
2
  "name": "fp4-gemm",
3
+ "id": "_fp4_gemm_cuda_92c4b99",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "python-depends": [],
 
14
  "digest": {
15
  "algorithm": "sha256",
16
  "files": {
17
+ "__init__.py": "Mr1/bAUzs+af68D+bRXX6iONDof//dzKgn6QU5pKdu8=",
18
+ "_fp4_gemm_cuda_92c4b99.abi3.so": "aLpLwxskZ6gk4+Ci2us92srEP04WtewHebkMKDGU8Ms=",
19
+ "_ops.py": "c5GHeHUOPDgpedue7MgdtRjCwHOmUsBo4OfqoFVegq0="
20
  }
21
  },
22
  "provenance": {
 
26
  "dirty": false
27
  },
28
  "kernel": {
29
+ "sha": "92c4b991b6f1fbd89744aa6f46f62b1bfac7ca7e",
30
  "dirty": false
31
  }
32
  }
build/torch213-cxx11-cu130-x86_64-linux/__init__.py CHANGED
@@ -109,6 +109,11 @@ def _quant_mse_fake(x, packed, sfa, is_sfb: bool = False) -> None:
109
  return None
110
 
111
 
 
 
 
 
 
112
  @torch.library.register_fake(add_op_namespace_prefix("quantize_e0m3_sfa_fp16"))
113
  def _quant_e0m3_fake(x, packed, sfa, is_sfb: bool = False) -> None:
114
  return None
@@ -186,6 +191,91 @@ def quantize_fp4_sfa_mse_fp16(
186
  return packed, sfa
187
 
188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  def quantize_e0m3_sfa_fp16(
190
  x: torch.Tensor,
191
  packed: torch.Tensor | None = None,
@@ -381,6 +471,29 @@ def nvfp4_gemm_geglu_nvfp4_fp16(
381
  return out_packed, out_sfa
382
 
383
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
384
  def nvfp4_gemm_bias_gelu_nvfp4_fp16(
385
  a_packed: torch.Tensor,
386
  b_packed: torch.Tensor,
@@ -601,6 +714,8 @@ def nvfp4_gemm_streamk_bias_bf16(
601
 
602
 
603
  __all__ = [
 
 
604
  "dequantize_fp4_sfa_fp16",
605
  "e0m3_weight_gemm_fp16",
606
  "fp4_w4a16_linear_bf16",
@@ -622,6 +737,9 @@ __all__ = [
622
  "nvfp4_gemm_streamk_bias_bf16",
623
  "quantize_fp4_sfa_fp16",
624
  "quantize_fp4_sfa_mse_fp16",
 
 
 
625
  "quantize_e0m3_sfa_fp16",
626
  "quantize_fp4_sfa_bf16",
627
  "sfa_size_bytes",
 
109
  return None
110
 
111
 
112
+ @torch.library.register_fake(add_op_namespace_prefix("quantize_fp4_sfa_mse_bf16"))
113
+ def _quant_mse_bf16_fake(x, packed, sfa, is_sfb: bool = False) -> None:
114
+ return None
115
+
116
+
117
  @torch.library.register_fake(add_op_namespace_prefix("quantize_e0m3_sfa_fp16"))
118
  def _quant_e0m3_fake(x, packed, sfa, is_sfb: bool = False) -> None:
119
  return None
 
191
  return packed, sfa
192
 
193
 
194
+ def quantize_fp4_sfa_mse_bf16(
195
+ x: torch.Tensor,
196
+ packed: torch.Tensor | None = None,
197
+ sfa: torch.Tensor | None = None,
198
+ is_sfb: bool = False,
199
+ ):
200
+ """Pack BF16 weights using per-block reconstruction-MSE scale search."""
201
+ if packed is None or sfa is None:
202
+ packed, sfa = _alloc_fp4(x.shape[0], x.shape[1], x.device)
203
+ ops.quantize_fp4_sfa_mse_bf16(x, packed, sfa, bool(is_sfb))
204
+ return packed, sfa
205
+
206
+
207
+ def aligned_fp4_dim(dim: int, alignment: int = 32) -> int:
208
+ """Return the physical FP4 GEMM dimension for a logical dimension."""
209
+ if dim <= 0 or alignment <= 0 or alignment % 16 != 0:
210
+ raise ValueError("dim must be positive and alignment divisible by 16")
211
+ return ((dim + alignment - 1) // alignment) * alignment
212
+
213
+
214
+ def quantize_fp4_sfa_padded_bf16(
215
+ x: torch.Tensor,
216
+ *,
217
+ alignment: int = 32,
218
+ is_sfb: bool = False,
219
+ ):
220
+ """Bind-time BF16 pack with zero padding on the final dimension.
221
+
222
+ The returned packed tensor uses the physical aligned width. Keep the
223
+ logical width returned as the third item for slicing final model outputs.
224
+ This helper allocates and is intentionally not a runtime hot-path op.
225
+ """
226
+ if x.ndim != 2 or x.dtype != torch.bfloat16:
227
+ raise ValueError("x must be a 2-D BF16 tensor")
228
+ logical_dim = int(x.shape[1])
229
+ physical_dim = aligned_fp4_dim(logical_dim, alignment)
230
+ if physical_dim == logical_dim:
231
+ padded = x.contiguous()
232
+ else:
233
+ padded = torch.zeros(
234
+ (x.shape[0], physical_dim), device=x.device, dtype=x.dtype
235
+ )
236
+ padded[:, :logical_dim].copy_(x)
237
+ packed, sfa = quantize_fp4_sfa_bf16(padded, is_sfb=is_sfb)
238
+ return packed, sfa, logical_dim
239
+
240
+
241
+ def pack_nvfp4_weight_bf16(
242
+ weight: torch.Tensor,
243
+ bias: torch.Tensor | None = None,
244
+ *,
245
+ alignment: int = 32,
246
+ mse: bool = False,
247
+ ):
248
+ """Zero-pad and pack a logical ``(N, K)`` BF16 weight at bind time.
249
+
250
+ Both physical ``N`` and ``K`` are aligned. This is the model-neutral
251
+ contract used by widths such as SigLIP's logical 4304, which becomes 4320
252
+ for Blackwell NVFP4 TMA. The hot path consumes the returned static tensors
253
+ directly and performs no padding or allocation.
254
+ """
255
+ if weight.ndim != 2 or weight.dtype != torch.bfloat16:
256
+ raise ValueError("weight must be a 2-D BF16 tensor")
257
+ logical_n, logical_k = map(int, weight.shape)
258
+ physical_n = aligned_fp4_dim(logical_n, alignment)
259
+ physical_k = aligned_fp4_dim(logical_k, alignment)
260
+ padded_weight = torch.zeros(
261
+ (physical_n, physical_k), device=weight.device, dtype=weight.dtype
262
+ )
263
+ padded_weight[:logical_n, :logical_k].copy_(weight)
264
+ quantize = quantize_fp4_sfa_mse_bf16 if mse else quantize_fp4_sfa_bf16
265
+ packed, sfb = quantize(padded_weight, is_sfb=True)
266
+ padded_bias = None
267
+ if bias is not None:
268
+ if bias.ndim != 1 or bias.numel() != logical_n:
269
+ raise ValueError("bias must have shape (N,)")
270
+ if bias.dtype != torch.bfloat16 or bias.device != weight.device:
271
+ raise ValueError("bias must match the weight dtype and device")
272
+ padded_bias = torch.zeros(
273
+ (physical_n,), device=bias.device, dtype=bias.dtype
274
+ )
275
+ padded_bias[:logical_n].copy_(bias)
276
+ return packed, sfb, padded_bias, (logical_n, logical_k)
277
+
278
+
279
  def quantize_e0m3_sfa_fp16(
280
  x: torch.Tensor,
281
  packed: torch.Tensor | None = None,
 
471
  return out_packed, out_sfa
472
 
473
 
474
+ def cutlass_fp4_gemm_geglu_il_hw_v10(
475
+ a_packed: torch.Tensor,
476
+ b_interleaved_packed: torch.Tensor,
477
+ sfa: torch.Tensor,
478
+ sfb: torch.Tensor,
479
+ *,
480
+ scratch: torch.Tensor | None = None,
481
+ out_packed: torch.Tensor | None = None,
482
+ out_sfa: torch.Tensor | None = None,
483
+ ) -> tuple[torch.Tensor, torch.Tensor]:
484
+ """Explicit public alias for the native SM110 v10 GeGLU schedule."""
485
+ return nvfp4_gemm_geglu_nvfp4_fp16(
486
+ a_packed,
487
+ b_interleaved_packed,
488
+ sfa,
489
+ sfb,
490
+ skinny=True,
491
+ scratch=scratch,
492
+ out_packed=out_packed,
493
+ out_sfa=out_sfa,
494
+ )
495
+
496
+
497
  def nvfp4_gemm_bias_gelu_nvfp4_fp16(
498
  a_packed: torch.Tensor,
499
  b_packed: torch.Tensor,
 
714
 
715
 
716
  __all__ = [
717
+ "aligned_fp4_dim",
718
+ "cutlass_fp4_gemm_geglu_il_hw_v10",
719
  "dequantize_fp4_sfa_fp16",
720
  "e0m3_weight_gemm_fp16",
721
  "fp4_w4a16_linear_bf16",
 
737
  "nvfp4_gemm_streamk_bias_bf16",
738
  "quantize_fp4_sfa_fp16",
739
  "quantize_fp4_sfa_mse_fp16",
740
+ "quantize_fp4_sfa_mse_bf16",
741
+ "quantize_fp4_sfa_padded_bf16",
742
+ "pack_nvfp4_weight_bf16",
743
  "quantize_e0m3_sfa_fp16",
744
  "quantize_fp4_sfa_bf16",
745
  "sfa_size_bytes",
build/torch213-cxx11-cu130-x86_64-linux/{_fp4_gemm_cuda_b6588aa.abi3.so → _fp4_gemm_cuda_92c4b99.abi3.so} RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:67ad5a71cafc0766b46303485eb638fcd03d048a6b48e97f3a0a90e17585ee2a
3
- size 7714632
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6f97f9a442fba5511c0bcded0b9d388f387b9fd26fe56951698a79ded706ca52
3
+ size 7895736
build/torch213-cxx11-cu130-x86_64-linux/_ops.py CHANGED
@@ -1,9 +1,9 @@
1
  import torch
2
- from . import _fp4_gemm_cuda_b6588aa
3
- ops = torch.ops._fp4_gemm_cuda_b6588aa
4
 
5
  def add_op_namespace_prefix(op_name: str):
6
  """
7
  Prefix op by namespace.
8
  """
9
- return f"_fp4_gemm_cuda_b6588aa::{op_name}"
 
1
  import torch
2
+ from . import _fp4_gemm_cuda_92c4b99
3
+ ops = torch.ops._fp4_gemm_cuda_92c4b99
4
 
5
  def add_op_namespace_prefix(op_name: str):
6
  """
7
  Prefix op by namespace.
8
  """
9
+ return f"_fp4_gemm_cuda_92c4b99::{op_name}"
build/torch213-cxx11-cu130-x86_64-linux/metadata.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "fp4-gemm",
3
- "id": "_fp4_gemm_cuda_b6588aa",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "python-depends": [],
@@ -14,9 +14,9 @@
14
  "digest": {
15
  "algorithm": "sha256",
16
  "files": {
17
- "__init__.py": "Fb/FDYOpDw+YGlevblUsv4AkILDQCJdB807qdDUDI1c=",
18
- "_fp4_gemm_cuda_b6588aa.abi3.so": "Z61accr8B2a0YwNIXrY4/NA9BIprSOl/OgqQ4XWF7io=",
19
- "_ops.py": "KKClchvuzBz2g1A148Gi2R6QIx0Ipj0CKlTT511GrUY="
20
  }
21
  },
22
  "provenance": {
@@ -26,7 +26,7 @@
26
  "dirty": false
27
  },
28
  "kernel": {
29
- "sha": "b6588aa59dd4f8c646f90b58312a0a66886e061f",
30
  "dirty": false
31
  }
32
  }
 
1
  {
2
  "name": "fp4-gemm",
3
+ "id": "_fp4_gemm_cuda_92c4b99",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "python-depends": [],
 
14
  "digest": {
15
  "algorithm": "sha256",
16
  "files": {
17
+ "__init__.py": "Mr1/bAUzs+af68D+bRXX6iONDof//dzKgn6QU5pKdu8=",
18
+ "_fp4_gemm_cuda_92c4b99.abi3.so": "b5f5pEL7pVEcC83tC504jzh7n9Jv5WlRaYp53tcGylI=",
19
+ "_ops.py": "c5GHeHUOPDgpedue7MgdtRjCwHOmUsBo4OfqoFVegq0="
20
  }
21
  },
22
  "provenance": {
 
26
  "dirty": false
27
  },
28
  "kernel": {
29
+ "sha": "92c4b991b6f1fbd89744aa6f46f62b1bfac7ca7e",
30
  "dirty": false
31
  }
32
  }
build/torch213-cxx11-cu132-x86_64-linux/__init__.py CHANGED
@@ -109,6 +109,11 @@ def _quant_mse_fake(x, packed, sfa, is_sfb: bool = False) -> None:
109
  return None
110
 
111
 
 
 
 
 
 
112
  @torch.library.register_fake(add_op_namespace_prefix("quantize_e0m3_sfa_fp16"))
113
  def _quant_e0m3_fake(x, packed, sfa, is_sfb: bool = False) -> None:
114
  return None
@@ -186,6 +191,91 @@ def quantize_fp4_sfa_mse_fp16(
186
  return packed, sfa
187
 
188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  def quantize_e0m3_sfa_fp16(
190
  x: torch.Tensor,
191
  packed: torch.Tensor | None = None,
@@ -381,6 +471,29 @@ def nvfp4_gemm_geglu_nvfp4_fp16(
381
  return out_packed, out_sfa
382
 
383
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
384
  def nvfp4_gemm_bias_gelu_nvfp4_fp16(
385
  a_packed: torch.Tensor,
386
  b_packed: torch.Tensor,
@@ -601,6 +714,8 @@ def nvfp4_gemm_streamk_bias_bf16(
601
 
602
 
603
  __all__ = [
 
 
604
  "dequantize_fp4_sfa_fp16",
605
  "e0m3_weight_gemm_fp16",
606
  "fp4_w4a16_linear_bf16",
@@ -622,6 +737,9 @@ __all__ = [
622
  "nvfp4_gemm_streamk_bias_bf16",
623
  "quantize_fp4_sfa_fp16",
624
  "quantize_fp4_sfa_mse_fp16",
 
 
 
625
  "quantize_e0m3_sfa_fp16",
626
  "quantize_fp4_sfa_bf16",
627
  "sfa_size_bytes",
 
109
  return None
110
 
111
 
112
+ @torch.library.register_fake(add_op_namespace_prefix("quantize_fp4_sfa_mse_bf16"))
113
+ def _quant_mse_bf16_fake(x, packed, sfa, is_sfb: bool = False) -> None:
114
+ return None
115
+
116
+
117
  @torch.library.register_fake(add_op_namespace_prefix("quantize_e0m3_sfa_fp16"))
118
  def _quant_e0m3_fake(x, packed, sfa, is_sfb: bool = False) -> None:
119
  return None
 
191
  return packed, sfa
192
 
193
 
194
+ def quantize_fp4_sfa_mse_bf16(
195
+ x: torch.Tensor,
196
+ packed: torch.Tensor | None = None,
197
+ sfa: torch.Tensor | None = None,
198
+ is_sfb: bool = False,
199
+ ):
200
+ """Pack BF16 weights using per-block reconstruction-MSE scale search."""
201
+ if packed is None or sfa is None:
202
+ packed, sfa = _alloc_fp4(x.shape[0], x.shape[1], x.device)
203
+ ops.quantize_fp4_sfa_mse_bf16(x, packed, sfa, bool(is_sfb))
204
+ return packed, sfa
205
+
206
+
207
+ def aligned_fp4_dim(dim: int, alignment: int = 32) -> int:
208
+ """Return the physical FP4 GEMM dimension for a logical dimension."""
209
+ if dim <= 0 or alignment <= 0 or alignment % 16 != 0:
210
+ raise ValueError("dim must be positive and alignment divisible by 16")
211
+ return ((dim + alignment - 1) // alignment) * alignment
212
+
213
+
214
+ def quantize_fp4_sfa_padded_bf16(
215
+ x: torch.Tensor,
216
+ *,
217
+ alignment: int = 32,
218
+ is_sfb: bool = False,
219
+ ):
220
+ """Bind-time BF16 pack with zero padding on the final dimension.
221
+
222
+ The returned packed tensor uses the physical aligned width. Keep the
223
+ logical width returned as the third item for slicing final model outputs.
224
+ This helper allocates and is intentionally not a runtime hot-path op.
225
+ """
226
+ if x.ndim != 2 or x.dtype != torch.bfloat16:
227
+ raise ValueError("x must be a 2-D BF16 tensor")
228
+ logical_dim = int(x.shape[1])
229
+ physical_dim = aligned_fp4_dim(logical_dim, alignment)
230
+ if physical_dim == logical_dim:
231
+ padded = x.contiguous()
232
+ else:
233
+ padded = torch.zeros(
234
+ (x.shape[0], physical_dim), device=x.device, dtype=x.dtype
235
+ )
236
+ padded[:, :logical_dim].copy_(x)
237
+ packed, sfa = quantize_fp4_sfa_bf16(padded, is_sfb=is_sfb)
238
+ return packed, sfa, logical_dim
239
+
240
+
241
+ def pack_nvfp4_weight_bf16(
242
+ weight: torch.Tensor,
243
+ bias: torch.Tensor | None = None,
244
+ *,
245
+ alignment: int = 32,
246
+ mse: bool = False,
247
+ ):
248
+ """Zero-pad and pack a logical ``(N, K)`` BF16 weight at bind time.
249
+
250
+ Both physical ``N`` and ``K`` are aligned. This is the model-neutral
251
+ contract used by widths such as SigLIP's logical 4304, which becomes 4320
252
+ for Blackwell NVFP4 TMA. The hot path consumes the returned static tensors
253
+ directly and performs no padding or allocation.
254
+ """
255
+ if weight.ndim != 2 or weight.dtype != torch.bfloat16:
256
+ raise ValueError("weight must be a 2-D BF16 tensor")
257
+ logical_n, logical_k = map(int, weight.shape)
258
+ physical_n = aligned_fp4_dim(logical_n, alignment)
259
+ physical_k = aligned_fp4_dim(logical_k, alignment)
260
+ padded_weight = torch.zeros(
261
+ (physical_n, physical_k), device=weight.device, dtype=weight.dtype
262
+ )
263
+ padded_weight[:logical_n, :logical_k].copy_(weight)
264
+ quantize = quantize_fp4_sfa_mse_bf16 if mse else quantize_fp4_sfa_bf16
265
+ packed, sfb = quantize(padded_weight, is_sfb=True)
266
+ padded_bias = None
267
+ if bias is not None:
268
+ if bias.ndim != 1 or bias.numel() != logical_n:
269
+ raise ValueError("bias must have shape (N,)")
270
+ if bias.dtype != torch.bfloat16 or bias.device != weight.device:
271
+ raise ValueError("bias must match the weight dtype and device")
272
+ padded_bias = torch.zeros(
273
+ (physical_n,), device=bias.device, dtype=bias.dtype
274
+ )
275
+ padded_bias[:logical_n].copy_(bias)
276
+ return packed, sfb, padded_bias, (logical_n, logical_k)
277
+
278
+
279
  def quantize_e0m3_sfa_fp16(
280
  x: torch.Tensor,
281
  packed: torch.Tensor | None = None,
 
471
  return out_packed, out_sfa
472
 
473
 
474
+ def cutlass_fp4_gemm_geglu_il_hw_v10(
475
+ a_packed: torch.Tensor,
476
+ b_interleaved_packed: torch.Tensor,
477
+ sfa: torch.Tensor,
478
+ sfb: torch.Tensor,
479
+ *,
480
+ scratch: torch.Tensor | None = None,
481
+ out_packed: torch.Tensor | None = None,
482
+ out_sfa: torch.Tensor | None = None,
483
+ ) -> tuple[torch.Tensor, torch.Tensor]:
484
+ """Explicit public alias for the native SM110 v10 GeGLU schedule."""
485
+ return nvfp4_gemm_geglu_nvfp4_fp16(
486
+ a_packed,
487
+ b_interleaved_packed,
488
+ sfa,
489
+ sfb,
490
+ skinny=True,
491
+ scratch=scratch,
492
+ out_packed=out_packed,
493
+ out_sfa=out_sfa,
494
+ )
495
+
496
+
497
  def nvfp4_gemm_bias_gelu_nvfp4_fp16(
498
  a_packed: torch.Tensor,
499
  b_packed: torch.Tensor,
 
714
 
715
 
716
  __all__ = [
717
+ "aligned_fp4_dim",
718
+ "cutlass_fp4_gemm_geglu_il_hw_v10",
719
  "dequantize_fp4_sfa_fp16",
720
  "e0m3_weight_gemm_fp16",
721
  "fp4_w4a16_linear_bf16",
 
737
  "nvfp4_gemm_streamk_bias_bf16",
738
  "quantize_fp4_sfa_fp16",
739
  "quantize_fp4_sfa_mse_fp16",
740
+ "quantize_fp4_sfa_mse_bf16",
741
+ "quantize_fp4_sfa_padded_bf16",
742
+ "pack_nvfp4_weight_bf16",
743
  "quantize_e0m3_sfa_fp16",
744
  "quantize_fp4_sfa_bf16",
745
  "sfa_size_bytes",
build/torch213-cxx11-cu132-x86_64-linux/{_fp4_gemm_cuda_b6588aa.abi3.so → _fp4_gemm_cuda_92c4b99.abi3.so} RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:4b27d4b88246c79fd017c8845f6f99cd1c9e7946039fda8f6abec2635a4edd65
3
- size 7706384
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d1cfd40cecbd45d8a9e0958a5f9af7508f4b8deb0910eda2ad078c5e82690c55
3
+ size 7887496
build/torch213-cxx11-cu132-x86_64-linux/_ops.py CHANGED
@@ -1,9 +1,9 @@
1
  import torch
2
- from . import _fp4_gemm_cuda_b6588aa
3
- ops = torch.ops._fp4_gemm_cuda_b6588aa
4
 
5
  def add_op_namespace_prefix(op_name: str):
6
  """
7
  Prefix op by namespace.
8
  """
9
- return f"_fp4_gemm_cuda_b6588aa::{op_name}"
 
1
  import torch
2
+ from . import _fp4_gemm_cuda_92c4b99
3
+ ops = torch.ops._fp4_gemm_cuda_92c4b99
4
 
5
  def add_op_namespace_prefix(op_name: str):
6
  """
7
  Prefix op by namespace.
8
  """
9
+ return f"_fp4_gemm_cuda_92c4b99::{op_name}"
build/torch213-cxx11-cu132-x86_64-linux/metadata.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "fp4-gemm",
3
- "id": "_fp4_gemm_cuda_b6588aa",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "python-depends": [],
@@ -14,9 +14,9 @@
14
  "digest": {
15
  "algorithm": "sha256",
16
  "files": {
17
- "__init__.py": "Fb/FDYOpDw+YGlevblUsv4AkILDQCJdB807qdDUDI1c=",
18
- "_fp4_gemm_cuda_b6588aa.abi3.so": "SyfUuIJGx5/QF8iEX2+ZzRyeeUYDn9qPar7CY1pO3WU=",
19
- "_ops.py": "KKClchvuzBz2g1A148Gi2R6QIx0Ipj0CKlTT511GrUY="
20
  }
21
  },
22
  "provenance": {
@@ -26,7 +26,7 @@
26
  "dirty": false
27
  },
28
  "kernel": {
29
- "sha": "b6588aa59dd4f8c646f90b58312a0a66886e061f",
30
  "dirty": false
31
  }
32
  }
 
1
  {
2
  "name": "fp4-gemm",
3
+ "id": "_fp4_gemm_cuda_92c4b99",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "python-depends": [],
 
14
  "digest": {
15
  "algorithm": "sha256",
16
  "files": {
17
+ "__init__.py": "Mr1/bAUzs+af68D+bRXX6iONDof//dzKgn6QU5pKdu8=",
18
+ "_fp4_gemm_cuda_92c4b99.abi3.so": "0c/UDOy9Rdip4JWKX5r3UI9LjesJEO2irQeMXoJpDFU=",
19
+ "_ops.py": "c5GHeHUOPDgpedue7MgdtRjCwHOmUsBo4OfqoFVegq0="
20
  }
21
  },
22
  "provenance": {
 
26
  "dirty": false
27
  },
28
  "kernel": {
29
+ "sha": "92c4b991b6f1fbd89744aa6f46f62b1bfac7ca7e",
30
  "dirty": false
31
  }
32
  }