liangsu9988 commited on
Commit
4fb53e5
·
verified ·
1 Parent(s): 755a1f2

Uploaded using `kernel-builder`.

Browse files
benchmarks/benchmark.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Benchmark fp4-gemm."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import argparse
7
+ import importlib.util
8
+ import json
9
+ import sys
10
+ from dataclasses import asdict, dataclass
11
+ from pathlib import Path
12
+
13
+ import torch
14
+
15
+
16
+ ROOT = Path(__file__).resolve().parents[2]
17
+ TEST_FILE = ROOT / "fp4-gemm" / "tests" / "test_fp4_gemm.py"
18
+
19
+
20
+ @dataclass
21
+ class BenchResult:
22
+ shape: str
23
+ M: int
24
+ N: int
25
+ K: int
26
+ variant: int
27
+ flashrt_us: float
28
+ torch_reference_us: float
29
+ speedup_vs_reference: float
30
+ max_abs: float
31
+ mean_abs: float
32
+ p99_abs: float
33
+ cosine: float
34
+ status: str
35
+
36
+
37
+ def load_helpers():
38
+ spec = importlib.util.spec_from_file_location("fp4_gemm_test_helpers", TEST_FILE)
39
+ if spec is None or spec.loader is None:
40
+ raise RuntimeError(f"cannot load helpers from {TEST_FILE}")
41
+ module = importlib.util.module_from_spec(spec)
42
+ sys.modules["fp4_gemm_test_helpers"] = module
43
+ spec.loader.exec_module(module)
44
+ return module
45
+
46
+
47
+ def measure(fn, warmup: int, iters: int) -> float:
48
+ for _ in range(warmup):
49
+ fn()
50
+ torch.cuda.synchronize()
51
+ start = torch.cuda.Event(enable_timing=True)
52
+ end = torch.cuda.Event(enable_timing=True)
53
+ start.record()
54
+ for _ in range(iters):
55
+ fn()
56
+ end.record()
57
+ torch.cuda.synchronize()
58
+ return float(start.elapsed_time(end) * 1000.0 / iters)
59
+
60
+
61
+ def bench_case(helpers, ops, name: str, shape: tuple[int, int, int], warmup: int, iters: int) -> list[BenchResult]:
62
+ m, n, k = shape
63
+ a_packed, b_packed, sfa, sfb, expected = helpers.prepare_quantized(ops, m, n, k)
64
+ a_deq = torch.empty((m, k), device="cuda", dtype=torch.float16)
65
+ b_deq = torch.empty((n, k), device="cuda", dtype=torch.float16)
66
+ ops.dequantize_fp4_sfa_fp16(a_packed, sfa, a_deq, False)
67
+ ops.dequantize_fp4_sfa_fp16(b_packed, sfb, b_deq, True)
68
+ torch.cuda.synchronize()
69
+
70
+ def torch_ref():
71
+ return (a_deq.float() @ b_deq.float().T).to(torch.bfloat16)
72
+
73
+ torch_us = measure(torch_ref, warmup, iters)
74
+ results: list[BenchResult] = []
75
+ for variant in (0, 1, 2):
76
+ out = torch.empty((m, n), device="cuda", dtype=torch.bfloat16)
77
+ ops.fp4_w4a16_linear_bf16(a_packed, b_packed, sfa, sfb, out, 1.0, variant)
78
+ torch.cuda.synchronize()
79
+ max_abs, mean_abs, p99_abs, cosine = helpers.metrics(out, expected)
80
+ flashrt_us = measure(
81
+ lambda: ops.fp4_w4a16_linear_bf16(a_packed, b_packed, sfa, sfb, out, 1.0, variant),
82
+ warmup,
83
+ iters,
84
+ )
85
+ results.append(
86
+ BenchResult(
87
+ shape=name,
88
+ M=m,
89
+ N=n,
90
+ K=k,
91
+ variant=variant,
92
+ flashrt_us=flashrt_us,
93
+ torch_reference_us=torch_us,
94
+ speedup_vs_reference=torch_us / flashrt_us,
95
+ max_abs=max_abs,
96
+ mean_abs=mean_abs,
97
+ p99_abs=p99_abs,
98
+ cosine=cosine,
99
+ status="ok",
100
+ )
101
+ )
102
+ return results
103
+
104
+
105
+ def main() -> int:
106
+ parser = argparse.ArgumentParser()
107
+ parser.add_argument("--mode", choices=["smoke", "headline"], default="headline")
108
+ parser.add_argument("--warmup", type=int, default=20)
109
+ parser.add_argument("--iterations", type=int, default=100)
110
+ parser.add_argument("--json-out", default=None)
111
+ args = parser.parse_args()
112
+
113
+ helpers = load_helpers()
114
+ ops = helpers.load_source_ops()
115
+ shapes = {
116
+ "small_m16_n128_k128": (16, 128, 128),
117
+ "small_m32_n256_k256": (32, 256, 256),
118
+ "mlp_tile_m64_n512_k512": (64, 512, 512),
119
+ }
120
+ if args.mode == "smoke":
121
+ shapes = {"small_m16_n128_k128": shapes["small_m16_n128_k128"]}
122
+ results: list[BenchResult] = []
123
+ for name, shape in shapes.items():
124
+ results.extend(bench_case(helpers, ops, name, shape, args.warmup, args.iterations))
125
+ payload = {
126
+ "mode": args.mode,
127
+ "device": torch.cuda.get_device_name(),
128
+ "torch": torch.__version__,
129
+ "results": [asdict(item) for item in results],
130
+ }
131
+ print(json.dumps(payload, indent=2))
132
+ if args.json_out:
133
+ out = Path(args.json_out)
134
+ out.parent.mkdir(parents=True, exist_ok=True)
135
+ out.write_text(json.dumps(payload, indent=2) + "\n")
136
+ return 0
137
+
138
+
139
+ if __name__ == "__main__":
140
+ raise SystemExit(main())
build/torch211-cxx11-cu128-x86_64-linux/__init__.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """FlashRT FP4 GEMM kernels."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import torch
6
+
7
+ from ._ops import add_op_namespace_prefix, ops
8
+
9
+
10
+ def sfa_size_bytes(rows: int, dim: int) -> int:
11
+ if rows <= 0 or dim <= 0 or dim % 16 != 0:
12
+ raise ValueError("rows must be positive and dim must be positive/divisible by 16")
13
+ n_blocks = dim // 16
14
+ n_row_super = (rows + 127) // 128
15
+ n_col_super = (n_blocks + 3) // 4
16
+ return n_row_super * n_col_super * 512
17
+
18
+
19
+ def _alloc_fp4(rows: int, dim: int, device: torch.device | str):
20
+ return (
21
+ torch.empty((rows, dim // 2), device=device, dtype=torch.uint8),
22
+ torch.empty((sfa_size_bytes(rows, dim),), device=device, dtype=torch.uint8),
23
+ )
24
+
25
+
26
+ @torch.library.register_fake(add_op_namespace_prefix("fp4_w4a16_linear_bf16"))
27
+ def _linear_fake(
28
+ a_packed: torch.Tensor,
29
+ b_packed: torch.Tensor,
30
+ sfa: torch.Tensor,
31
+ sfb: torch.Tensor,
32
+ out: torch.Tensor,
33
+ alpha: float = 1.0,
34
+ variant: int = 0,
35
+ ) -> None:
36
+ return None
37
+
38
+
39
+ @torch.library.register_fake(add_op_namespace_prefix("quantize_fp4_sfa_fp16"))
40
+ def _quant_fake(x: torch.Tensor, packed: torch.Tensor, sfa: torch.Tensor, is_sfb: bool = False) -> None:
41
+ return None
42
+
43
+
44
+ @torch.library.register_fake(add_op_namespace_prefix("dequantize_fp4_sfa_fp16"))
45
+ def _dequant_fake(packed: torch.Tensor, sfa: torch.Tensor, out: torch.Tensor, is_sfb: bool = False) -> None:
46
+ return None
47
+
48
+
49
+ def quantize_fp4_sfa_fp16(
50
+ x: torch.Tensor,
51
+ packed: torch.Tensor | None = None,
52
+ sfa: torch.Tensor | None = None,
53
+ is_sfb: bool = False,
54
+ ):
55
+ if packed is None or sfa is None:
56
+ packed, sfa = _alloc_fp4(x.shape[0], x.shape[1], x.device)
57
+ ops.quantize_fp4_sfa_fp16(x, packed, sfa, bool(is_sfb))
58
+ return packed, sfa
59
+
60
+
61
+ def dequantize_fp4_sfa_fp16(
62
+ packed: torch.Tensor,
63
+ sfa: torch.Tensor,
64
+ out: torch.Tensor | None = None,
65
+ is_sfb: bool = False,
66
+ ) -> torch.Tensor:
67
+ if out is None:
68
+ out = torch.empty((packed.shape[0], packed.shape[1] * 2), device=packed.device, dtype=torch.float16)
69
+ ops.dequantize_fp4_sfa_fp16(packed, sfa, out, bool(is_sfb))
70
+ return out
71
+
72
+
73
+ def fp4_w4a16_linear_bf16(
74
+ a_packed: torch.Tensor,
75
+ b_packed: torch.Tensor,
76
+ sfa: torch.Tensor,
77
+ sfb: torch.Tensor,
78
+ alpha: float = 1.0,
79
+ out: torch.Tensor | None = None,
80
+ variant: int = 0,
81
+ ) -> torch.Tensor:
82
+ if out is None:
83
+ out = torch.empty((a_packed.shape[0], b_packed.shape[0]), device=a_packed.device, dtype=torch.bfloat16)
84
+ ops.fp4_w4a16_linear_bf16(a_packed, b_packed, sfa, sfb, out, float(alpha), int(variant))
85
+ return out
86
+
build/torch211-cxx11-cu128-x86_64-linux/_fp4_gemm_cuda_d8a589a.abi3.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5c5bb3e58d2e4dcdee5807ca455fafeb1a1e72515cbdd0bd9c130ea3bfacb9d1
3
+ size 671608
build/torch211-cxx11-cu128-x86_64-linux/_ops.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from . import _fp4_gemm_cuda_d8a589a
3
+ ops = torch.ops._fp4_gemm_cuda_d8a589a
4
+
5
+ def add_op_namespace_prefix(op_name: str):
6
+ """
7
+ Prefix op by namespace.
8
+ """
9
+ return f"_fp4_gemm_cuda_d8a589a::{op_name}"
build/torch211-cxx11-cu128-x86_64-linux/fp4_gemm/__init__.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ctypes
2
+ import importlib.util
3
+ import sys
4
+ from pathlib import Path
5
+ from types import ModuleType
6
+
7
+
8
+ def _import_from_path(file_path: Path) -> ModuleType:
9
+ # We cannot use the module name as-is, after adding it to `sys.modules`,
10
+ # it would also be used for other imports. So, we make a module name that
11
+ # depends on the path for it to be unique using the hex-encoded hash of
12
+ # the path.
13
+ path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value)
14
+ module_name = path_hash
15
+ spec = importlib.util.spec_from_file_location(module_name, file_path)
16
+ if spec is None:
17
+ raise ImportError(f"Cannot load spec for {module_name} from {file_path}")
18
+ module = importlib.util.module_from_spec(spec)
19
+ if module is None:
20
+ raise ImportError(f"Cannot load module {module_name} from spec")
21
+ sys.modules[module_name] = module
22
+ spec.loader.exec_module(module) # type: ignore
23
+ return module
24
+
25
+
26
+ globals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))
build/torch211-cxx11-cu128-x86_64-linux/metadata.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "fp4-gemm",
3
+ "id": "_fp4_gemm_cuda_d8a589a",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "python-depends": [],
7
+ "backend": {
8
+ "type": "cuda",
9
+ "archs": [
10
+ "12.0a"
11
+ ]
12
+ },
13
+ "digest": {
14
+ "algorithm": "sha256",
15
+ "files": {
16
+ "__init__.py": "WSLdLQD92ulRtOhSLoAhWgFYN6oucbbkTg19GAaltBs=",
17
+ "_fp4_gemm_cuda_d8a589a.abi3.so": "XFuz5Y0uTc3uWAfKRV+v6xoeclFcvdC9nBMOo7+sudE=",
18
+ "_ops.py": "JCogli/U0X3ICMajcGwPdPXOfLjDX5Izclp70jtHdJM=",
19
+ "fp4_gemm/__init__.py": "DFYPlrhXwYjEqCl/8n0SmWGZV8NFml5DPhMjKfv98GY="
20
+ }
21
+ }
22
+ }
build/torch211-cxx11-cu130-x86_64-linux/__init__.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """FlashRT FP4 GEMM kernels."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import torch
6
+
7
+ from ._ops import add_op_namespace_prefix, ops
8
+
9
+
10
+ def sfa_size_bytes(rows: int, dim: int) -> int:
11
+ if rows <= 0 or dim <= 0 or dim % 16 != 0:
12
+ raise ValueError("rows must be positive and dim must be positive/divisible by 16")
13
+ n_blocks = dim // 16
14
+ n_row_super = (rows + 127) // 128
15
+ n_col_super = (n_blocks + 3) // 4
16
+ return n_row_super * n_col_super * 512
17
+
18
+
19
+ def _alloc_fp4(rows: int, dim: int, device: torch.device | str):
20
+ return (
21
+ torch.empty((rows, dim // 2), device=device, dtype=torch.uint8),
22
+ torch.empty((sfa_size_bytes(rows, dim),), device=device, dtype=torch.uint8),
23
+ )
24
+
25
+
26
+ @torch.library.register_fake(add_op_namespace_prefix("fp4_w4a16_linear_bf16"))
27
+ def _linear_fake(
28
+ a_packed: torch.Tensor,
29
+ b_packed: torch.Tensor,
30
+ sfa: torch.Tensor,
31
+ sfb: torch.Tensor,
32
+ out: torch.Tensor,
33
+ alpha: float = 1.0,
34
+ variant: int = 0,
35
+ ) -> None:
36
+ return None
37
+
38
+
39
+ @torch.library.register_fake(add_op_namespace_prefix("quantize_fp4_sfa_fp16"))
40
+ def _quant_fake(x: torch.Tensor, packed: torch.Tensor, sfa: torch.Tensor, is_sfb: bool = False) -> None:
41
+ return None
42
+
43
+
44
+ @torch.library.register_fake(add_op_namespace_prefix("dequantize_fp4_sfa_fp16"))
45
+ def _dequant_fake(packed: torch.Tensor, sfa: torch.Tensor, out: torch.Tensor, is_sfb: bool = False) -> None:
46
+ return None
47
+
48
+
49
+ def quantize_fp4_sfa_fp16(
50
+ x: torch.Tensor,
51
+ packed: torch.Tensor | None = None,
52
+ sfa: torch.Tensor | None = None,
53
+ is_sfb: bool = False,
54
+ ):
55
+ if packed is None or sfa is None:
56
+ packed, sfa = _alloc_fp4(x.shape[0], x.shape[1], x.device)
57
+ ops.quantize_fp4_sfa_fp16(x, packed, sfa, bool(is_sfb))
58
+ return packed, sfa
59
+
60
+
61
+ def dequantize_fp4_sfa_fp16(
62
+ packed: torch.Tensor,
63
+ sfa: torch.Tensor,
64
+ out: torch.Tensor | None = None,
65
+ is_sfb: bool = False,
66
+ ) -> torch.Tensor:
67
+ if out is None:
68
+ out = torch.empty((packed.shape[0], packed.shape[1] * 2), device=packed.device, dtype=torch.float16)
69
+ ops.dequantize_fp4_sfa_fp16(packed, sfa, out, bool(is_sfb))
70
+ return out
71
+
72
+
73
+ def fp4_w4a16_linear_bf16(
74
+ a_packed: torch.Tensor,
75
+ b_packed: torch.Tensor,
76
+ sfa: torch.Tensor,
77
+ sfb: torch.Tensor,
78
+ alpha: float = 1.0,
79
+ out: torch.Tensor | None = None,
80
+ variant: int = 0,
81
+ ) -> torch.Tensor:
82
+ if out is None:
83
+ out = torch.empty((a_packed.shape[0], b_packed.shape[0]), device=a_packed.device, dtype=torch.bfloat16)
84
+ ops.fp4_w4a16_linear_bf16(a_packed, b_packed, sfa, sfb, out, float(alpha), int(variant))
85
+ return out
86
+
build/torch211-cxx11-cu130-x86_64-linux/_fp4_gemm_cuda_d8a589a.abi3.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5fc0542c6250a3290db008f65939284a1fc789c05ff972945905d647673f3b76
3
+ size 714176
build/torch211-cxx11-cu130-x86_64-linux/_ops.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from . import _fp4_gemm_cuda_d8a589a
3
+ ops = torch.ops._fp4_gemm_cuda_d8a589a
4
+
5
+ def add_op_namespace_prefix(op_name: str):
6
+ """
7
+ Prefix op by namespace.
8
+ """
9
+ return f"_fp4_gemm_cuda_d8a589a::{op_name}"
build/torch211-cxx11-cu130-x86_64-linux/fp4_gemm/__init__.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ctypes
2
+ import importlib.util
3
+ import sys
4
+ from pathlib import Path
5
+ from types import ModuleType
6
+
7
+
8
+ def _import_from_path(file_path: Path) -> ModuleType:
9
+ # We cannot use the module name as-is, after adding it to `sys.modules`,
10
+ # it would also be used for other imports. So, we make a module name that
11
+ # depends on the path for it to be unique using the hex-encoded hash of
12
+ # the path.
13
+ path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value)
14
+ module_name = path_hash
15
+ spec = importlib.util.spec_from_file_location(module_name, file_path)
16
+ if spec is None:
17
+ raise ImportError(f"Cannot load spec for {module_name} from {file_path}")
18
+ module = importlib.util.module_from_spec(spec)
19
+ if module is None:
20
+ raise ImportError(f"Cannot load module {module_name} from spec")
21
+ sys.modules[module_name] = module
22
+ spec.loader.exec_module(module) # type: ignore
23
+ return module
24
+
25
+
26
+ globals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))
build/torch211-cxx11-cu130-x86_64-linux/metadata.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "fp4-gemm",
3
+ "id": "_fp4_gemm_cuda_d8a589a",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "python-depends": [],
7
+ "backend": {
8
+ "type": "cuda",
9
+ "archs": [
10
+ "12.0a"
11
+ ]
12
+ },
13
+ "digest": {
14
+ "algorithm": "sha256",
15
+ "files": {
16
+ "__init__.py": "WSLdLQD92ulRtOhSLoAhWgFYN6oucbbkTg19GAaltBs=",
17
+ "_fp4_gemm_cuda_d8a589a.abi3.so": "X8BULGJQoykNsAj2WTkoSh/HicBf+XKUWQXWR2c/O3Y=",
18
+ "_ops.py": "JCogli/U0X3ICMajcGwPdPXOfLjDX5Izclp70jtHdJM=",
19
+ "fp4_gemm/__init__.py": "DFYPlrhXwYjEqCl/8n0SmWGZV8NFml5DPhMjKfv98GY="
20
+ }
21
+ }
22
+ }
build/torch212-cxx11-cu130-x86_64-linux/__init__.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """FlashRT FP4 GEMM kernels."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import torch
6
+
7
+ from ._ops import add_op_namespace_prefix, ops
8
+
9
+
10
+ def sfa_size_bytes(rows: int, dim: int) -> int:
11
+ if rows <= 0 or dim <= 0 or dim % 16 != 0:
12
+ raise ValueError("rows must be positive and dim must be positive/divisible by 16")
13
+ n_blocks = dim // 16
14
+ n_row_super = (rows + 127) // 128
15
+ n_col_super = (n_blocks + 3) // 4
16
+ return n_row_super * n_col_super * 512
17
+
18
+
19
+ def _alloc_fp4(rows: int, dim: int, device: torch.device | str):
20
+ return (
21
+ torch.empty((rows, dim // 2), device=device, dtype=torch.uint8),
22
+ torch.empty((sfa_size_bytes(rows, dim),), device=device, dtype=torch.uint8),
23
+ )
24
+
25
+
26
+ @torch.library.register_fake(add_op_namespace_prefix("fp4_w4a16_linear_bf16"))
27
+ def _linear_fake(
28
+ a_packed: torch.Tensor,
29
+ b_packed: torch.Tensor,
30
+ sfa: torch.Tensor,
31
+ sfb: torch.Tensor,
32
+ out: torch.Tensor,
33
+ alpha: float = 1.0,
34
+ variant: int = 0,
35
+ ) -> None:
36
+ return None
37
+
38
+
39
+ @torch.library.register_fake(add_op_namespace_prefix("quantize_fp4_sfa_fp16"))
40
+ def _quant_fake(x: torch.Tensor, packed: torch.Tensor, sfa: torch.Tensor, is_sfb: bool = False) -> None:
41
+ return None
42
+
43
+
44
+ @torch.library.register_fake(add_op_namespace_prefix("dequantize_fp4_sfa_fp16"))
45
+ def _dequant_fake(packed: torch.Tensor, sfa: torch.Tensor, out: torch.Tensor, is_sfb: bool = False) -> None:
46
+ return None
47
+
48
+
49
+ def quantize_fp4_sfa_fp16(
50
+ x: torch.Tensor,
51
+ packed: torch.Tensor | None = None,
52
+ sfa: torch.Tensor | None = None,
53
+ is_sfb: bool = False,
54
+ ):
55
+ if packed is None or sfa is None:
56
+ packed, sfa = _alloc_fp4(x.shape[0], x.shape[1], x.device)
57
+ ops.quantize_fp4_sfa_fp16(x, packed, sfa, bool(is_sfb))
58
+ return packed, sfa
59
+
60
+
61
+ def dequantize_fp4_sfa_fp16(
62
+ packed: torch.Tensor,
63
+ sfa: torch.Tensor,
64
+ out: torch.Tensor | None = None,
65
+ is_sfb: bool = False,
66
+ ) -> torch.Tensor:
67
+ if out is None:
68
+ out = torch.empty((packed.shape[0], packed.shape[1] * 2), device=packed.device, dtype=torch.float16)
69
+ ops.dequantize_fp4_sfa_fp16(packed, sfa, out, bool(is_sfb))
70
+ return out
71
+
72
+
73
+ def fp4_w4a16_linear_bf16(
74
+ a_packed: torch.Tensor,
75
+ b_packed: torch.Tensor,
76
+ sfa: torch.Tensor,
77
+ sfb: torch.Tensor,
78
+ alpha: float = 1.0,
79
+ out: torch.Tensor | None = None,
80
+ variant: int = 0,
81
+ ) -> torch.Tensor:
82
+ if out is None:
83
+ out = torch.empty((a_packed.shape[0], b_packed.shape[0]), device=a_packed.device, dtype=torch.bfloat16)
84
+ ops.fp4_w4a16_linear_bf16(a_packed, b_packed, sfa, sfb, out, float(alpha), int(variant))
85
+ return out
86
+
build/torch212-cxx11-cu130-x86_64-linux/_fp4_gemm_cuda_d8a589a.abi3.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d6175b6e0d9b1f3380c4f73cc5ee29236f24178fab167647e2fe0201a4ce0a1e
3
+ size 724584
build/torch212-cxx11-cu130-x86_64-linux/_ops.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from . import _fp4_gemm_cuda_d8a589a
3
+ ops = torch.ops._fp4_gemm_cuda_d8a589a
4
+
5
+ def add_op_namespace_prefix(op_name: str):
6
+ """
7
+ Prefix op by namespace.
8
+ """
9
+ return f"_fp4_gemm_cuda_d8a589a::{op_name}"
build/torch212-cxx11-cu130-x86_64-linux/fp4_gemm/__init__.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ctypes
2
+ import importlib.util
3
+ import sys
4
+ from pathlib import Path
5
+ from types import ModuleType
6
+
7
+
8
+ def _import_from_path(file_path: Path) -> ModuleType:
9
+ # We cannot use the module name as-is, after adding it to `sys.modules`,
10
+ # it would also be used for other imports. So, we make a module name that
11
+ # depends on the path for it to be unique using the hex-encoded hash of
12
+ # the path.
13
+ path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value)
14
+ module_name = path_hash
15
+ spec = importlib.util.spec_from_file_location(module_name, file_path)
16
+ if spec is None:
17
+ raise ImportError(f"Cannot load spec for {module_name} from {file_path}")
18
+ module = importlib.util.module_from_spec(spec)
19
+ if module is None:
20
+ raise ImportError(f"Cannot load module {module_name} from spec")
21
+ sys.modules[module_name] = module
22
+ spec.loader.exec_module(module) # type: ignore
23
+ return module
24
+
25
+
26
+ globals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))
build/torch212-cxx11-cu130-x86_64-linux/metadata.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "fp4-gemm",
3
+ "id": "_fp4_gemm_cuda_d8a589a",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "python-depends": [],
7
+ "backend": {
8
+ "type": "cuda",
9
+ "archs": [
10
+ "12.0a"
11
+ ]
12
+ },
13
+ "digest": {
14
+ "algorithm": "sha256",
15
+ "files": {
16
+ "__init__.py": "WSLdLQD92ulRtOhSLoAhWgFYN6oucbbkTg19GAaltBs=",
17
+ "_fp4_gemm_cuda_d8a589a.abi3.so": "1hdbbg2bHzOAxPc8xe4pI28kF4+rFnZH4v4CAaTOCh4=",
18
+ "_ops.py": "JCogli/U0X3ICMajcGwPdPXOfLjDX5Izclp70jtHdJM=",
19
+ "fp4_gemm/__init__.py": "DFYPlrhXwYjEqCl/8n0SmWGZV8NFml5DPhMjKfv98GY="
20
+ }
21
+ }
22
+ }
build/torch212-cxx11-cu132-x86_64-linux/__init__.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """FlashRT FP4 GEMM kernels."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import torch
6
+
7
+ from ._ops import add_op_namespace_prefix, ops
8
+
9
+
10
+ def sfa_size_bytes(rows: int, dim: int) -> int:
11
+ if rows <= 0 or dim <= 0 or dim % 16 != 0:
12
+ raise ValueError("rows must be positive and dim must be positive/divisible by 16")
13
+ n_blocks = dim // 16
14
+ n_row_super = (rows + 127) // 128
15
+ n_col_super = (n_blocks + 3) // 4
16
+ return n_row_super * n_col_super * 512
17
+
18
+
19
+ def _alloc_fp4(rows: int, dim: int, device: torch.device | str):
20
+ return (
21
+ torch.empty((rows, dim // 2), device=device, dtype=torch.uint8),
22
+ torch.empty((sfa_size_bytes(rows, dim),), device=device, dtype=torch.uint8),
23
+ )
24
+
25
+
26
+ @torch.library.register_fake(add_op_namespace_prefix("fp4_w4a16_linear_bf16"))
27
+ def _linear_fake(
28
+ a_packed: torch.Tensor,
29
+ b_packed: torch.Tensor,
30
+ sfa: torch.Tensor,
31
+ sfb: torch.Tensor,
32
+ out: torch.Tensor,
33
+ alpha: float = 1.0,
34
+ variant: int = 0,
35
+ ) -> None:
36
+ return None
37
+
38
+
39
+ @torch.library.register_fake(add_op_namespace_prefix("quantize_fp4_sfa_fp16"))
40
+ def _quant_fake(x: torch.Tensor, packed: torch.Tensor, sfa: torch.Tensor, is_sfb: bool = False) -> None:
41
+ return None
42
+
43
+
44
+ @torch.library.register_fake(add_op_namespace_prefix("dequantize_fp4_sfa_fp16"))
45
+ def _dequant_fake(packed: torch.Tensor, sfa: torch.Tensor, out: torch.Tensor, is_sfb: bool = False) -> None:
46
+ return None
47
+
48
+
49
+ def quantize_fp4_sfa_fp16(
50
+ x: torch.Tensor,
51
+ packed: torch.Tensor | None = None,
52
+ sfa: torch.Tensor | None = None,
53
+ is_sfb: bool = False,
54
+ ):
55
+ if packed is None or sfa is None:
56
+ packed, sfa = _alloc_fp4(x.shape[0], x.shape[1], x.device)
57
+ ops.quantize_fp4_sfa_fp16(x, packed, sfa, bool(is_sfb))
58
+ return packed, sfa
59
+
60
+
61
+ def dequantize_fp4_sfa_fp16(
62
+ packed: torch.Tensor,
63
+ sfa: torch.Tensor,
64
+ out: torch.Tensor | None = None,
65
+ is_sfb: bool = False,
66
+ ) -> torch.Tensor:
67
+ if out is None:
68
+ out = torch.empty((packed.shape[0], packed.shape[1] * 2), device=packed.device, dtype=torch.float16)
69
+ ops.dequantize_fp4_sfa_fp16(packed, sfa, out, bool(is_sfb))
70
+ return out
71
+
72
+
73
+ def fp4_w4a16_linear_bf16(
74
+ a_packed: torch.Tensor,
75
+ b_packed: torch.Tensor,
76
+ sfa: torch.Tensor,
77
+ sfb: torch.Tensor,
78
+ alpha: float = 1.0,
79
+ out: torch.Tensor | None = None,
80
+ variant: int = 0,
81
+ ) -> torch.Tensor:
82
+ if out is None:
83
+ out = torch.empty((a_packed.shape[0], b_packed.shape[0]), device=a_packed.device, dtype=torch.bfloat16)
84
+ ops.fp4_w4a16_linear_bf16(a_packed, b_packed, sfa, sfb, out, float(alpha), int(variant))
85
+ return out
86
+
build/torch212-cxx11-cu132-x86_64-linux/_fp4_gemm_cuda_d8a589a.abi3.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9358a9bee95a43f46c5ba7716c25513c5c01d11ba020ebcca042bcc5daff807e
3
+ size 724632
build/torch212-cxx11-cu132-x86_64-linux/_ops.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from . import _fp4_gemm_cuda_d8a589a
3
+ ops = torch.ops._fp4_gemm_cuda_d8a589a
4
+
5
+ def add_op_namespace_prefix(op_name: str):
6
+ """
7
+ Prefix op by namespace.
8
+ """
9
+ return f"_fp4_gemm_cuda_d8a589a::{op_name}"
build/torch212-cxx11-cu132-x86_64-linux/fp4_gemm/__init__.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ctypes
2
+ import importlib.util
3
+ import sys
4
+ from pathlib import Path
5
+ from types import ModuleType
6
+
7
+
8
+ def _import_from_path(file_path: Path) -> ModuleType:
9
+ # We cannot use the module name as-is, after adding it to `sys.modules`,
10
+ # it would also be used for other imports. So, we make a module name that
11
+ # depends on the path for it to be unique using the hex-encoded hash of
12
+ # the path.
13
+ path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value)
14
+ module_name = path_hash
15
+ spec = importlib.util.spec_from_file_location(module_name, file_path)
16
+ if spec is None:
17
+ raise ImportError(f"Cannot load spec for {module_name} from {file_path}")
18
+ module = importlib.util.module_from_spec(spec)
19
+ if module is None:
20
+ raise ImportError(f"Cannot load module {module_name} from spec")
21
+ sys.modules[module_name] = module
22
+ spec.loader.exec_module(module) # type: ignore
23
+ return module
24
+
25
+
26
+ globals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))
build/torch212-cxx11-cu132-x86_64-linux/metadata.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "fp4-gemm",
3
+ "id": "_fp4_gemm_cuda_d8a589a",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "python-depends": [],
7
+ "backend": {
8
+ "type": "cuda",
9
+ "archs": [
10
+ "12.0a"
11
+ ]
12
+ },
13
+ "digest": {
14
+ "algorithm": "sha256",
15
+ "files": {
16
+ "__init__.py": "WSLdLQD92ulRtOhSLoAhWgFYN6oucbbkTg19GAaltBs=",
17
+ "_fp4_gemm_cuda_d8a589a.abi3.so": "k1ipvulaQ/RsW6dxbCVRPFwB0RugIOvMoEK8xdr/gH4=",
18
+ "_ops.py": "JCogli/U0X3ICMajcGwPdPXOfLjDX5Izclp70jtHdJM=",
19
+ "fp4_gemm/__init__.py": "DFYPlrhXwYjEqCl/8n0SmWGZV8NFml5DPhMjKfv98GY="
20
+ }
21
+ }
22
+ }