liangsu9988 commited on
Commit
34562e4
·
verified ·
1 Parent(s): b208e84

Uploaded using `kernel-builder`.

Browse files
Files changed (37) hide show
  1. benchmarks/benchmark.py +17 -5
  2. build/torch211-cxx11-cu128-x86_64-linux/__init__.py +8 -2
  3. build/torch211-cxx11-cu128-x86_64-linux/{_flashrt_fp8_ffn_cuda_5de4768.abi3.so → _flashrt_fp8_ffn_cuda_4a17df5.abi3.so} +2 -2
  4. build/torch211-cxx11-cu128-x86_64-linux/_ops.py +3 -3
  5. build/torch211-cxx11-cu128-x86_64-linux/metadata.json +1 -1
  6. build/torch211-cxx11-cu130-x86_64-linux/__init__.py +8 -2
  7. build/torch211-cxx11-cu130-x86_64-linux/{_flashrt_fp8_ffn_cuda_5de4768.abi3.so → _flashrt_fp8_ffn_cuda_4a17df5.abi3.so} +2 -2
  8. build/torch211-cxx11-cu130-x86_64-linux/_ops.py +3 -3
  9. build/torch211-cxx11-cu130-x86_64-linux/metadata.json +1 -1
  10. build/torch211-cxx11-rocm71-x86_64-linux/__init__.py +227 -0
  11. build/{torch212-cxx11-cu130-x86_64-linux/_flashrt_fp8_ffn_cuda_5de4768.abi3.so → torch211-cxx11-rocm71-x86_64-linux/_flashrt_fp8_ffn_rocm_4a17df5.abi3.so} +2 -2
  12. build/torch211-cxx11-rocm71-x86_64-linux/_ops.py +9 -0
  13. build/torch211-cxx11-rocm71-x86_64-linux/flashrt_fp8_ffn/__init__.py +26 -0
  14. build/torch211-cxx11-rocm71-x86_64-linux/metadata.json +13 -0
  15. build/torch211-cxx11-rocm72-x86_64-linux/__init__.py +227 -0
  16. build/{torch212-cxx11-cu132-x86_64-linux/_flashrt_fp8_ffn_cuda_5de4768.abi3.so → torch211-cxx11-rocm72-x86_64-linux/_flashrt_fp8_ffn_rocm_4a17df5.abi3.so} +2 -2
  17. build/torch211-cxx11-rocm72-x86_64-linux/_ops.py +9 -0
  18. build/torch211-cxx11-rocm72-x86_64-linux/flashrt_fp8_ffn/__init__.py +26 -0
  19. build/torch211-cxx11-rocm72-x86_64-linux/metadata.json +13 -0
  20. build/torch212-cxx11-cu130-x86_64-linux/__init__.py +8 -2
  21. build/torch212-cxx11-cu130-x86_64-linux/_flashrt_fp8_ffn_cuda_4a17df5.abi3.so +3 -0
  22. build/torch212-cxx11-cu130-x86_64-linux/_ops.py +3 -3
  23. build/torch212-cxx11-cu130-x86_64-linux/metadata.json +1 -1
  24. build/torch212-cxx11-cu132-x86_64-linux/__init__.py +8 -2
  25. build/torch212-cxx11-cu132-x86_64-linux/_flashrt_fp8_ffn_cuda_4a17df5.abi3.so +3 -0
  26. build/torch212-cxx11-cu132-x86_64-linux/_ops.py +3 -3
  27. build/torch212-cxx11-cu132-x86_64-linux/metadata.json +1 -1
  28. build/torch212-cxx11-rocm71-x86_64-linux/__init__.py +227 -0
  29. build/torch212-cxx11-rocm71-x86_64-linux/_flashrt_fp8_ffn_rocm_4a17df5.abi3.so +3 -0
  30. build/torch212-cxx11-rocm71-x86_64-linux/_ops.py +9 -0
  31. build/torch212-cxx11-rocm71-x86_64-linux/flashrt_fp8_ffn/__init__.py +26 -0
  32. build/torch212-cxx11-rocm71-x86_64-linux/metadata.json +13 -0
  33. build/torch212-cxx11-rocm72-x86_64-linux/__init__.py +227 -0
  34. build/torch212-cxx11-rocm72-x86_64-linux/_flashrt_fp8_ffn_rocm_4a17df5.abi3.so +3 -0
  35. build/torch212-cxx11-rocm72-x86_64-linux/_ops.py +9 -0
  36. build/torch212-cxx11-rocm72-x86_64-linux/flashrt_fp8_ffn/__init__.py +26 -0
  37. build/torch212-cxx11-rocm72-x86_64-linux/metadata.json +13 -0
benchmarks/benchmark.py CHANGED
@@ -133,7 +133,7 @@ class SourceOps:
133
  if hidden is None:
134
  hidden = torch.empty((x.shape[0], up_w.shape[0]), device=x.device, dtype=torch.bfloat16)
135
  if hidden_fp8 is None:
136
- hidden_fp8 = torch.empty_like(hidden, dtype=torch.float8_e4m3fn)
137
  if out is None:
138
  out = torch.empty((x.shape[0], down_w.shape[0]), device=x.device, dtype=torch.bfloat16)
139
  self._ops.fp8_gelu_mlp_bf16(
@@ -204,11 +204,22 @@ def load_installed_ops(artifact: str | None):
204
  def load_hub_ops(repo_id: str, version: int):
205
  from kernels import get_kernel
206
 
207
- return get_kernel(repo_id, version=version, trust_remote_code=True)
 
 
 
 
 
 
 
 
 
 
208
 
209
 
210
  def quantize_fp8(x: torch.Tensor, scale: torch.Tensor) -> torch.Tensor:
211
- return torch.clamp(x.float() / scale.float(), -448.0, 448.0).to(torch.float8_e4m3fn)
 
212
 
213
 
214
  def dequant_fp8(x: torch.Tensor, scale: torch.Tensor) -> torch.Tensor:
@@ -242,7 +253,8 @@ stable_bf16_bias_add = compiler_disable(bf16_bias_add_boundary)
242
  def torch_mlp(x, up_w, up_b, down_w, down_b, x_s, up_s, hid_s, dn_s):
243
  hidden = (dequant_fp8(x, x_s) @ dequant_fp8(up_w, up_s).T).to(torch.bfloat16)
244
  hidden = torch.nn.functional.gelu(hidden + up_b.float(), approximate="tanh")
245
- hidden_fp8 = torch.clamp(hidden / hid_s.float(), -448.0, 448.0).to(torch.float8_e4m3fn)
 
246
  out = (dequant_fp8(hidden_fp8, hid_s) @ dequant_fp8(down_w, dn_s).T).to(torch.bfloat16)
247
  return (out + down_b.float()).to(torch.bfloat16)
248
 
@@ -274,7 +286,7 @@ def make_inputs(M: int, K: int, H: int, N: int, layers: int):
274
  up_bs = [torch.randn((H,), device="cuda", dtype=torch.bfloat16) for _ in range(layers)]
275
  down_bs = [torch.randn((N,), device="cuda", dtype=torch.bfloat16) for _ in range(layers)]
276
  hidden = [torch.empty((M, H), device="cuda", dtype=torch.bfloat16) for _ in range(layers)]
277
- hidden_fp8 = [torch.empty((M, H), device="cuda", dtype=torch.float8_e4m3fn) for _ in range(layers)]
278
  outs = [torch.empty((M, N), device="cuda", dtype=torch.bfloat16) for _ in range(layers)]
279
  return xs, up_ws, up_bs, down_ws, down_bs, x_scale, up_scale, hidden_scale, down_scale, hidden, hidden_fp8, outs
280
 
 
133
  if hidden is None:
134
  hidden = torch.empty((x.shape[0], up_w.shape[0]), device=x.device, dtype=torch.bfloat16)
135
  if hidden_fp8 is None:
136
+ hidden_fp8 = torch.empty_like(hidden, dtype=fp8_dtype())
137
  if out is None:
138
  out = torch.empty((x.shape[0], down_w.shape[0]), device=x.device, dtype=torch.bfloat16)
139
  self._ops.fp8_gelu_mlp_bf16(
 
204
  def load_hub_ops(repo_id: str, version: int):
205
  from kernels import get_kernel
206
 
207
+ return get_kernel(repo_id, version=version)
208
+
209
+
210
+ def fp8_dtype() -> torch.dtype:
211
+ if torch.version.hip is not None and hasattr(torch, "float8_e4m3fnuz"):
212
+ return torch.float8_e4m3fnuz
213
+ return torch.float8_e4m3fn
214
+
215
+
216
+ def fp8_max() -> float:
217
+ return 240.0 if torch.version.hip is not None else 448.0
218
 
219
 
220
  def quantize_fp8(x: torch.Tensor, scale: torch.Tensor) -> torch.Tensor:
221
+ limit = fp8_max()
222
+ return torch.clamp(x.float() / scale.float(), -limit, limit).to(fp8_dtype())
223
 
224
 
225
  def dequant_fp8(x: torch.Tensor, scale: torch.Tensor) -> torch.Tensor:
 
253
  def torch_mlp(x, up_w, up_b, down_w, down_b, x_s, up_s, hid_s, dn_s):
254
  hidden = (dequant_fp8(x, x_s) @ dequant_fp8(up_w, up_s).T).to(torch.bfloat16)
255
  hidden = torch.nn.functional.gelu(hidden + up_b.float(), approximate="tanh")
256
+ limit = fp8_max()
257
+ hidden_fp8 = torch.clamp(hidden / hid_s.float(), -limit, limit).to(fp8_dtype())
258
  out = (dequant_fp8(hidden_fp8, hid_s) @ dequant_fp8(down_w, dn_s).T).to(torch.bfloat16)
259
  return (out + down_b.float()).to(torch.bfloat16)
260
 
 
286
  up_bs = [torch.randn((H,), device="cuda", dtype=torch.bfloat16) for _ in range(layers)]
287
  down_bs = [torch.randn((N,), device="cuda", dtype=torch.bfloat16) for _ in range(layers)]
288
  hidden = [torch.empty((M, H), device="cuda", dtype=torch.bfloat16) for _ in range(layers)]
289
+ hidden_fp8 = [torch.empty((M, H), device="cuda", dtype=fp8_dtype()) for _ in range(layers)]
290
  outs = [torch.empty((M, N), device="cuda", dtype=torch.bfloat16) for _ in range(layers)]
291
  return xs, up_ws, up_bs, down_ws, down_bs, x_scale, up_scale, hidden_scale, down_scale, hidden, hidden_fp8, outs
292
 
build/torch211-cxx11-cu128-x86_64-linux/__init__.py CHANGED
@@ -34,6 +34,12 @@ _preload_cublaslt()
34
  from ._ops import add_op_namespace_prefix, ops
35
 
36
 
 
 
 
 
 
 
37
  @torch.library.register_fake(add_op_namespace_prefix("fp8_gemm_bf16"))
38
  def _fp8_gemm_bf16_fake(
39
  input: torch.Tensor,
@@ -146,7 +152,7 @@ def fp8_linear_bias_gelu_quant_bf16(
146
  dtype=torch.bfloat16,
147
  )
148
  if out_fp8 is None:
149
- out_fp8 = torch.empty_like(hidden_bf16, dtype=torch.float8_e4m3fn)
150
  ops.fp8_linear_bias_gelu_quant_bf16(
151
  input,
152
  weight,
@@ -190,7 +196,7 @@ def fp8_gelu_mlp_bf16(
190
  dtype=torch.bfloat16,
191
  )
192
  if hidden_fp8 is None:
193
- hidden_fp8 = torch.empty_like(hidden_bf16, dtype=torch.float8_e4m3fn)
194
  if out is None:
195
  out = torch.empty(
196
  (input.shape[0], down_weight.shape[0]),
 
34
  from ._ops import add_op_namespace_prefix, ops
35
 
36
 
37
+ def _fp8_dtype() -> torch.dtype:
38
+ if torch.version.hip is not None and hasattr(torch, "float8_e4m3fnuz"):
39
+ return torch.float8_e4m3fnuz
40
+ return torch.float8_e4m3fn
41
+
42
+
43
  @torch.library.register_fake(add_op_namespace_prefix("fp8_gemm_bf16"))
44
  def _fp8_gemm_bf16_fake(
45
  input: torch.Tensor,
 
152
  dtype=torch.bfloat16,
153
  )
154
  if out_fp8 is None:
155
+ out_fp8 = torch.empty_like(hidden_bf16, dtype=_fp8_dtype())
156
  ops.fp8_linear_bias_gelu_quant_bf16(
157
  input,
158
  weight,
 
196
  dtype=torch.bfloat16,
197
  )
198
  if hidden_fp8 is None:
199
+ hidden_fp8 = torch.empty_like(hidden_bf16, dtype=_fp8_dtype())
200
  if out is None:
201
  out = torch.empty(
202
  (input.shape[0], down_weight.shape[0]),
build/torch211-cxx11-cu128-x86_64-linux/{_flashrt_fp8_ffn_cuda_5de4768.abi3.so → _flashrt_fp8_ffn_cuda_4a17df5.abi3.so} RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:cba29914868c02e1355bef5aab55ef03909936318d9e03747333ab0b3a8cd94c
3
- size 309712
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:49d584720e4739dbf34f58426b49bd60f9ce619de6956132177812c850af7479
3
+ size 300056
build/torch211-cxx11-cu128-x86_64-linux/_ops.py CHANGED
@@ -1,9 +1,9 @@
1
  import torch
2
- from . import _flashrt_fp8_ffn_cuda_5de4768
3
- ops = torch.ops._flashrt_fp8_ffn_cuda_5de4768
4
 
5
  def add_op_namespace_prefix(op_name: str):
6
  """
7
  Prefix op by namespace.
8
  """
9
- return f"_flashrt_fp8_ffn_cuda_5de4768::{op_name}"
 
1
  import torch
2
+ from . import _flashrt_fp8_ffn_cuda_4a17df5
3
+ ops = torch.ops._flashrt_fp8_ffn_cuda_4a17df5
4
 
5
  def add_op_namespace_prefix(op_name: str):
6
  """
7
  Prefix op by namespace.
8
  """
9
+ return f"_flashrt_fp8_ffn_cuda_4a17df5::{op_name}"
build/torch211-cxx11-cu128-x86_64-linux/metadata.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "flashrt-fp8-ffn",
3
- "id": "_flashrt_fp8_ffn_cuda_5de4768",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "python-depends": [],
 
1
  {
2
  "name": "flashrt-fp8-ffn",
3
+ "id": "_flashrt_fp8_ffn_cuda_4a17df5",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "python-depends": [],
build/torch211-cxx11-cu130-x86_64-linux/__init__.py CHANGED
@@ -34,6 +34,12 @@ _preload_cublaslt()
34
  from ._ops import add_op_namespace_prefix, ops
35
 
36
 
 
 
 
 
 
 
37
  @torch.library.register_fake(add_op_namespace_prefix("fp8_gemm_bf16"))
38
  def _fp8_gemm_bf16_fake(
39
  input: torch.Tensor,
@@ -146,7 +152,7 @@ def fp8_linear_bias_gelu_quant_bf16(
146
  dtype=torch.bfloat16,
147
  )
148
  if out_fp8 is None:
149
- out_fp8 = torch.empty_like(hidden_bf16, dtype=torch.float8_e4m3fn)
150
  ops.fp8_linear_bias_gelu_quant_bf16(
151
  input,
152
  weight,
@@ -190,7 +196,7 @@ def fp8_gelu_mlp_bf16(
190
  dtype=torch.bfloat16,
191
  )
192
  if hidden_fp8 is None:
193
- hidden_fp8 = torch.empty_like(hidden_bf16, dtype=torch.float8_e4m3fn)
194
  if out is None:
195
  out = torch.empty(
196
  (input.shape[0], down_weight.shape[0]),
 
34
  from ._ops import add_op_namespace_prefix, ops
35
 
36
 
37
+ def _fp8_dtype() -> torch.dtype:
38
+ if torch.version.hip is not None and hasattr(torch, "float8_e4m3fnuz"):
39
+ return torch.float8_e4m3fnuz
40
+ return torch.float8_e4m3fn
41
+
42
+
43
  @torch.library.register_fake(add_op_namespace_prefix("fp8_gemm_bf16"))
44
  def _fp8_gemm_bf16_fake(
45
  input: torch.Tensor,
 
152
  dtype=torch.bfloat16,
153
  )
154
  if out_fp8 is None:
155
+ out_fp8 = torch.empty_like(hidden_bf16, dtype=_fp8_dtype())
156
  ops.fp8_linear_bias_gelu_quant_bf16(
157
  input,
158
  weight,
 
196
  dtype=torch.bfloat16,
197
  )
198
  if hidden_fp8 is None:
199
+ hidden_fp8 = torch.empty_like(hidden_bf16, dtype=_fp8_dtype())
200
  if out is None:
201
  out = torch.empty(
202
  (input.shape[0], down_weight.shape[0]),
build/torch211-cxx11-cu130-x86_64-linux/{_flashrt_fp8_ffn_cuda_5de4768.abi3.so → _flashrt_fp8_ffn_cuda_4a17df5.abi3.so} RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:35453f166929a1191c848cadbb9a8a180692a585ea5a1c84ef42731af14ebbdf
3
- size 307832
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3113678b5434234f55790ee0a215dffc4da6936faa0af7f6e1822211a720372f
3
+ size 298176
build/torch211-cxx11-cu130-x86_64-linux/_ops.py CHANGED
@@ -1,9 +1,9 @@
1
  import torch
2
- from . import _flashrt_fp8_ffn_cuda_5de4768
3
- ops = torch.ops._flashrt_fp8_ffn_cuda_5de4768
4
 
5
  def add_op_namespace_prefix(op_name: str):
6
  """
7
  Prefix op by namespace.
8
  """
9
- return f"_flashrt_fp8_ffn_cuda_5de4768::{op_name}"
 
1
  import torch
2
+ from . import _flashrt_fp8_ffn_cuda_4a17df5
3
+ ops = torch.ops._flashrt_fp8_ffn_cuda_4a17df5
4
 
5
  def add_op_namespace_prefix(op_name: str):
6
  """
7
  Prefix op by namespace.
8
  """
9
+ return f"_flashrt_fp8_ffn_cuda_4a17df5::{op_name}"
build/torch211-cxx11-cu130-x86_64-linux/metadata.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "flashrt-fp8-ffn",
3
- "id": "_flashrt_fp8_ffn_cuda_5de4768",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "python-depends": [],
 
1
  {
2
  "name": "flashrt-fp8-ffn",
3
+ "id": "_flashrt_fp8_ffn_cuda_4a17df5",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "python-depends": [],
build/torch211-cxx11-rocm71-x86_64-linux/__init__.py ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """FlashRT FP8 FFN kernels."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import ctypes
6
+ import ctypes.util
7
+ from pathlib import Path
8
+ from typing import Optional
9
+
10
+ import torch
11
+
12
+
13
+ def _torch_bundled_cublaslt() -> Optional[Path]:
14
+ for parent in Path(torch.__file__).resolve().parents:
15
+ candidate = parent / "nvidia" / "cublas" / "lib" / "libcublasLt.so.12"
16
+ if candidate.exists():
17
+ return candidate
18
+ return None
19
+
20
+
21
+ def _preload_cublaslt() -> None:
22
+ bundled = _torch_bundled_cublaslt()
23
+ library = str(bundled) if bundled is not None else (
24
+ ctypes.util.find_library("cublasLt") or "libcublasLt.so"
25
+ )
26
+ try:
27
+ ctypes.CDLL(library, mode=ctypes.RTLD_GLOBAL)
28
+ except OSError:
29
+ pass
30
+
31
+
32
+ _preload_cublaslt()
33
+
34
+ from ._ops import add_op_namespace_prefix, ops
35
+
36
+
37
+ def _fp8_dtype() -> torch.dtype:
38
+ if torch.version.hip is not None and hasattr(torch, "float8_e4m3fnuz"):
39
+ return torch.float8_e4m3fnuz
40
+ return torch.float8_e4m3fn
41
+
42
+
43
+ @torch.library.register_fake(add_op_namespace_prefix("fp8_gemm_bf16"))
44
+ def _fp8_gemm_bf16_fake(
45
+ input: torch.Tensor,
46
+ weight: torch.Tensor,
47
+ input_scale: torch.Tensor,
48
+ weight_scale: torch.Tensor,
49
+ out: torch.Tensor,
50
+ ) -> None:
51
+ if input.dim() != 2 or weight.dim() != 2:
52
+ raise RuntimeError("input and weight must be rank-2 tensors")
53
+ if out.shape != (input.shape[0], weight.shape[0]):
54
+ raise RuntimeError("out shape must be (input.shape[0], weight.shape[0])")
55
+ return None
56
+
57
+
58
+ @torch.library.register_fake(add_op_namespace_prefix("fp8_linear_bias_gelu_quant_bf16"))
59
+ def _fp8_linear_bias_gelu_quant_bf16_fake(
60
+ input: torch.Tensor,
61
+ weight: torch.Tensor,
62
+ bias: torch.Tensor,
63
+ input_scale: torch.Tensor,
64
+ weight_scale: torch.Tensor,
65
+ output_scale: torch.Tensor,
66
+ hidden_bf16: torch.Tensor,
67
+ out_fp8: torch.Tensor,
68
+ ) -> None:
69
+ expected = (input.shape[0], weight.shape[0])
70
+ if hidden_bf16.shape != expected or out_fp8.shape != expected:
71
+ raise RuntimeError(
72
+ "hidden_bf16 and out_fp8 shapes must be "
73
+ "(input.shape[0], weight.shape[0])"
74
+ )
75
+ return None
76
+
77
+
78
+ @torch.library.register_fake(add_op_namespace_prefix("fp8_gelu_mlp_bf16"))
79
+ def _fp8_gelu_mlp_bf16_fake(
80
+ input: torch.Tensor,
81
+ up_weight: torch.Tensor,
82
+ up_bias: torch.Tensor,
83
+ down_weight: torch.Tensor,
84
+ down_bias: torch.Tensor,
85
+ input_scale: torch.Tensor,
86
+ up_weight_scale: torch.Tensor,
87
+ hidden_scale: torch.Tensor,
88
+ down_weight_scale: torch.Tensor,
89
+ hidden_bf16: torch.Tensor,
90
+ hidden_fp8: torch.Tensor,
91
+ out: torch.Tensor,
92
+ ) -> None:
93
+ hidden_shape = (input.shape[0], up_weight.shape[0])
94
+ out_shape = (input.shape[0], down_weight.shape[0])
95
+ if hidden_bf16.shape != hidden_shape or hidden_fp8.shape != hidden_shape:
96
+ raise RuntimeError(
97
+ "hidden buffers must be (input.shape[0], up_weight.shape[0])"
98
+ )
99
+ if out.shape != out_shape:
100
+ raise RuntimeError("out shape must be (input.shape[0], down_weight.shape[0])")
101
+ return None
102
+
103
+
104
+ def _scalar_scale_like(input: torch.Tensor, value: float = 1.0) -> torch.Tensor:
105
+ return torch.tensor([value], device=input.device, dtype=torch.float32)
106
+
107
+
108
+ def fp8_gemm_bf16(
109
+ input: torch.Tensor,
110
+ weight: torch.Tensor,
111
+ input_scale: torch.Tensor,
112
+ weight_scale: torch.Tensor,
113
+ out: torch.Tensor | None = None,
114
+ ) -> torch.Tensor:
115
+ """Compute ``(input * input_scale) @ (weight * weight_scale).T``.
116
+
117
+ ``input`` is FP8 E4M3 with shape ``(M, K)``. ``weight`` is FP8 E4M3 with
118
+ shape ``(N, K)``. ``input_scale`` and ``weight_scale`` are CUDA float32
119
+ scalar tensors. Output is BF16 with shape ``(M, N)``.
120
+ """
121
+
122
+ if out is None:
123
+ out = torch.empty(
124
+ (input.shape[0], weight.shape[0]),
125
+ device=input.device,
126
+ dtype=torch.bfloat16,
127
+ )
128
+ ops.fp8_gemm_bf16(input, weight, input_scale, weight_scale, out)
129
+ return out
130
+
131
+
132
+ def fp8_linear_bias_gelu_quant_bf16(
133
+ input: torch.Tensor,
134
+ weight: torch.Tensor,
135
+ bias: torch.Tensor,
136
+ input_scale: torch.Tensor,
137
+ weight_scale: torch.Tensor,
138
+ output_scale: torch.Tensor,
139
+ hidden_bf16: torch.Tensor | None = None,
140
+ out_fp8: torch.Tensor | None = None,
141
+ ) -> tuple[torch.Tensor, torch.Tensor]:
142
+ """FP8 linear + BF16 bias/GELU + FP8 quantized output.
143
+
144
+ Returns ``(hidden_bf16, out_fp8)``. ``hidden_bf16`` is the post-GEMM
145
+ pre-activation scratch; ``out_fp8`` is the quantized activation.
146
+ """
147
+
148
+ if hidden_bf16 is None:
149
+ hidden_bf16 = torch.empty(
150
+ (input.shape[0], weight.shape[0]),
151
+ device=input.device,
152
+ dtype=torch.bfloat16,
153
+ )
154
+ if out_fp8 is None:
155
+ out_fp8 = torch.empty_like(hidden_bf16, dtype=_fp8_dtype())
156
+ ops.fp8_linear_bias_gelu_quant_bf16(
157
+ input,
158
+ weight,
159
+ bias,
160
+ input_scale,
161
+ weight_scale,
162
+ output_scale,
163
+ hidden_bf16,
164
+ out_fp8,
165
+ )
166
+ return hidden_bf16, out_fp8
167
+
168
+
169
+ def fp8_gelu_mlp_bf16(
170
+ input: torch.Tensor,
171
+ up_weight: torch.Tensor,
172
+ up_bias: torch.Tensor,
173
+ down_weight: torch.Tensor,
174
+ down_bias: torch.Tensor,
175
+ input_scale: torch.Tensor,
176
+ up_weight_scale: torch.Tensor,
177
+ hidden_scale: torch.Tensor,
178
+ down_weight_scale: torch.Tensor,
179
+ hidden_bf16: torch.Tensor | None = None,
180
+ hidden_fp8: torch.Tensor | None = None,
181
+ out: torch.Tensor | None = None,
182
+ ) -> torch.Tensor:
183
+ """FP8 GELU MLP block with BF16 output.
184
+
185
+ Computes:
186
+
187
+ ``hidden = gelu(fp8_gemm(input, up_weight) + up_bias)``
188
+ ``hidden_fp8 = quantize_fp8(hidden, hidden_scale)``
189
+ ``out = fp8_gemm(hidden_fp8, down_weight) + down_bias``
190
+ """
191
+
192
+ if hidden_bf16 is None:
193
+ hidden_bf16 = torch.empty(
194
+ (input.shape[0], up_weight.shape[0]),
195
+ device=input.device,
196
+ dtype=torch.bfloat16,
197
+ )
198
+ if hidden_fp8 is None:
199
+ hidden_fp8 = torch.empty_like(hidden_bf16, dtype=_fp8_dtype())
200
+ if out is None:
201
+ out = torch.empty(
202
+ (input.shape[0], down_weight.shape[0]),
203
+ device=input.device,
204
+ dtype=torch.bfloat16,
205
+ )
206
+ ops.fp8_gelu_mlp_bf16(
207
+ input,
208
+ up_weight,
209
+ up_bias,
210
+ down_weight,
211
+ down_bias,
212
+ input_scale,
213
+ up_weight_scale,
214
+ hidden_scale,
215
+ down_weight_scale,
216
+ hidden_bf16,
217
+ hidden_fp8,
218
+ out,
219
+ )
220
+ return out
221
+
222
+
223
+ __all__ = [
224
+ "fp8_gemm_bf16",
225
+ "fp8_gelu_mlp_bf16",
226
+ "fp8_linear_bias_gelu_quant_bf16",
227
+ ]
build/{torch212-cxx11-cu130-x86_64-linux/_flashrt_fp8_ffn_cuda_5de4768.abi3.so → torch211-cxx11-rocm71-x86_64-linux/_flashrt_fp8_ffn_rocm_4a17df5.abi3.so} RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:f6c063d5fccd81e2b697f6b37feb6864e8a56478fb0b7efbc1a992d69244c581
3
- size 314080
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9d56b2ca87eae36419a8aeee3fe444a1894f7c1024c70eaabdea7848201374e3
3
+ size 145192
build/torch211-cxx11-rocm71-x86_64-linux/_ops.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from . import _flashrt_fp8_ffn_rocm_4a17df5
3
+ ops = torch.ops._flashrt_fp8_ffn_rocm_4a17df5
4
+
5
+ def add_op_namespace_prefix(op_name: str):
6
+ """
7
+ Prefix op by namespace.
8
+ """
9
+ return f"_flashrt_fp8_ffn_rocm_4a17df5::{op_name}"
build/torch211-cxx11-rocm71-x86_64-linux/flashrt_fp8_ffn/__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-rocm71-x86_64-linux/metadata.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "flashrt-fp8-ffn",
3
+ "id": "_flashrt_fp8_ffn_rocm_4a17df5",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "python-depends": [],
7
+ "backend": {
8
+ "type": "rocm",
9
+ "archs": [
10
+ "gfx942"
11
+ ]
12
+ }
13
+ }
build/torch211-cxx11-rocm72-x86_64-linux/__init__.py ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """FlashRT FP8 FFN kernels."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import ctypes
6
+ import ctypes.util
7
+ from pathlib import Path
8
+ from typing import Optional
9
+
10
+ import torch
11
+
12
+
13
+ def _torch_bundled_cublaslt() -> Optional[Path]:
14
+ for parent in Path(torch.__file__).resolve().parents:
15
+ candidate = parent / "nvidia" / "cublas" / "lib" / "libcublasLt.so.12"
16
+ if candidate.exists():
17
+ return candidate
18
+ return None
19
+
20
+
21
+ def _preload_cublaslt() -> None:
22
+ bundled = _torch_bundled_cublaslt()
23
+ library = str(bundled) if bundled is not None else (
24
+ ctypes.util.find_library("cublasLt") or "libcublasLt.so"
25
+ )
26
+ try:
27
+ ctypes.CDLL(library, mode=ctypes.RTLD_GLOBAL)
28
+ except OSError:
29
+ pass
30
+
31
+
32
+ _preload_cublaslt()
33
+
34
+ from ._ops import add_op_namespace_prefix, ops
35
+
36
+
37
+ def _fp8_dtype() -> torch.dtype:
38
+ if torch.version.hip is not None and hasattr(torch, "float8_e4m3fnuz"):
39
+ return torch.float8_e4m3fnuz
40
+ return torch.float8_e4m3fn
41
+
42
+
43
+ @torch.library.register_fake(add_op_namespace_prefix("fp8_gemm_bf16"))
44
+ def _fp8_gemm_bf16_fake(
45
+ input: torch.Tensor,
46
+ weight: torch.Tensor,
47
+ input_scale: torch.Tensor,
48
+ weight_scale: torch.Tensor,
49
+ out: torch.Tensor,
50
+ ) -> None:
51
+ if input.dim() != 2 or weight.dim() != 2:
52
+ raise RuntimeError("input and weight must be rank-2 tensors")
53
+ if out.shape != (input.shape[0], weight.shape[0]):
54
+ raise RuntimeError("out shape must be (input.shape[0], weight.shape[0])")
55
+ return None
56
+
57
+
58
+ @torch.library.register_fake(add_op_namespace_prefix("fp8_linear_bias_gelu_quant_bf16"))
59
+ def _fp8_linear_bias_gelu_quant_bf16_fake(
60
+ input: torch.Tensor,
61
+ weight: torch.Tensor,
62
+ bias: torch.Tensor,
63
+ input_scale: torch.Tensor,
64
+ weight_scale: torch.Tensor,
65
+ output_scale: torch.Tensor,
66
+ hidden_bf16: torch.Tensor,
67
+ out_fp8: torch.Tensor,
68
+ ) -> None:
69
+ expected = (input.shape[0], weight.shape[0])
70
+ if hidden_bf16.shape != expected or out_fp8.shape != expected:
71
+ raise RuntimeError(
72
+ "hidden_bf16 and out_fp8 shapes must be "
73
+ "(input.shape[0], weight.shape[0])"
74
+ )
75
+ return None
76
+
77
+
78
+ @torch.library.register_fake(add_op_namespace_prefix("fp8_gelu_mlp_bf16"))
79
+ def _fp8_gelu_mlp_bf16_fake(
80
+ input: torch.Tensor,
81
+ up_weight: torch.Tensor,
82
+ up_bias: torch.Tensor,
83
+ down_weight: torch.Tensor,
84
+ down_bias: torch.Tensor,
85
+ input_scale: torch.Tensor,
86
+ up_weight_scale: torch.Tensor,
87
+ hidden_scale: torch.Tensor,
88
+ down_weight_scale: torch.Tensor,
89
+ hidden_bf16: torch.Tensor,
90
+ hidden_fp8: torch.Tensor,
91
+ out: torch.Tensor,
92
+ ) -> None:
93
+ hidden_shape = (input.shape[0], up_weight.shape[0])
94
+ out_shape = (input.shape[0], down_weight.shape[0])
95
+ if hidden_bf16.shape != hidden_shape or hidden_fp8.shape != hidden_shape:
96
+ raise RuntimeError(
97
+ "hidden buffers must be (input.shape[0], up_weight.shape[0])"
98
+ )
99
+ if out.shape != out_shape:
100
+ raise RuntimeError("out shape must be (input.shape[0], down_weight.shape[0])")
101
+ return None
102
+
103
+
104
+ def _scalar_scale_like(input: torch.Tensor, value: float = 1.0) -> torch.Tensor:
105
+ return torch.tensor([value], device=input.device, dtype=torch.float32)
106
+
107
+
108
+ def fp8_gemm_bf16(
109
+ input: torch.Tensor,
110
+ weight: torch.Tensor,
111
+ input_scale: torch.Tensor,
112
+ weight_scale: torch.Tensor,
113
+ out: torch.Tensor | None = None,
114
+ ) -> torch.Tensor:
115
+ """Compute ``(input * input_scale) @ (weight * weight_scale).T``.
116
+
117
+ ``input`` is FP8 E4M3 with shape ``(M, K)``. ``weight`` is FP8 E4M3 with
118
+ shape ``(N, K)``. ``input_scale`` and ``weight_scale`` are CUDA float32
119
+ scalar tensors. Output is BF16 with shape ``(M, N)``.
120
+ """
121
+
122
+ if out is None:
123
+ out = torch.empty(
124
+ (input.shape[0], weight.shape[0]),
125
+ device=input.device,
126
+ dtype=torch.bfloat16,
127
+ )
128
+ ops.fp8_gemm_bf16(input, weight, input_scale, weight_scale, out)
129
+ return out
130
+
131
+
132
+ def fp8_linear_bias_gelu_quant_bf16(
133
+ input: torch.Tensor,
134
+ weight: torch.Tensor,
135
+ bias: torch.Tensor,
136
+ input_scale: torch.Tensor,
137
+ weight_scale: torch.Tensor,
138
+ output_scale: torch.Tensor,
139
+ hidden_bf16: torch.Tensor | None = None,
140
+ out_fp8: torch.Tensor | None = None,
141
+ ) -> tuple[torch.Tensor, torch.Tensor]:
142
+ """FP8 linear + BF16 bias/GELU + FP8 quantized output.
143
+
144
+ Returns ``(hidden_bf16, out_fp8)``. ``hidden_bf16`` is the post-GEMM
145
+ pre-activation scratch; ``out_fp8`` is the quantized activation.
146
+ """
147
+
148
+ if hidden_bf16 is None:
149
+ hidden_bf16 = torch.empty(
150
+ (input.shape[0], weight.shape[0]),
151
+ device=input.device,
152
+ dtype=torch.bfloat16,
153
+ )
154
+ if out_fp8 is None:
155
+ out_fp8 = torch.empty_like(hidden_bf16, dtype=_fp8_dtype())
156
+ ops.fp8_linear_bias_gelu_quant_bf16(
157
+ input,
158
+ weight,
159
+ bias,
160
+ input_scale,
161
+ weight_scale,
162
+ output_scale,
163
+ hidden_bf16,
164
+ out_fp8,
165
+ )
166
+ return hidden_bf16, out_fp8
167
+
168
+
169
+ def fp8_gelu_mlp_bf16(
170
+ input: torch.Tensor,
171
+ up_weight: torch.Tensor,
172
+ up_bias: torch.Tensor,
173
+ down_weight: torch.Tensor,
174
+ down_bias: torch.Tensor,
175
+ input_scale: torch.Tensor,
176
+ up_weight_scale: torch.Tensor,
177
+ hidden_scale: torch.Tensor,
178
+ down_weight_scale: torch.Tensor,
179
+ hidden_bf16: torch.Tensor | None = None,
180
+ hidden_fp8: torch.Tensor | None = None,
181
+ out: torch.Tensor | None = None,
182
+ ) -> torch.Tensor:
183
+ """FP8 GELU MLP block with BF16 output.
184
+
185
+ Computes:
186
+
187
+ ``hidden = gelu(fp8_gemm(input, up_weight) + up_bias)``
188
+ ``hidden_fp8 = quantize_fp8(hidden, hidden_scale)``
189
+ ``out = fp8_gemm(hidden_fp8, down_weight) + down_bias``
190
+ """
191
+
192
+ if hidden_bf16 is None:
193
+ hidden_bf16 = torch.empty(
194
+ (input.shape[0], up_weight.shape[0]),
195
+ device=input.device,
196
+ dtype=torch.bfloat16,
197
+ )
198
+ if hidden_fp8 is None:
199
+ hidden_fp8 = torch.empty_like(hidden_bf16, dtype=_fp8_dtype())
200
+ if out is None:
201
+ out = torch.empty(
202
+ (input.shape[0], down_weight.shape[0]),
203
+ device=input.device,
204
+ dtype=torch.bfloat16,
205
+ )
206
+ ops.fp8_gelu_mlp_bf16(
207
+ input,
208
+ up_weight,
209
+ up_bias,
210
+ down_weight,
211
+ down_bias,
212
+ input_scale,
213
+ up_weight_scale,
214
+ hidden_scale,
215
+ down_weight_scale,
216
+ hidden_bf16,
217
+ hidden_fp8,
218
+ out,
219
+ )
220
+ return out
221
+
222
+
223
+ __all__ = [
224
+ "fp8_gemm_bf16",
225
+ "fp8_gelu_mlp_bf16",
226
+ "fp8_linear_bias_gelu_quant_bf16",
227
+ ]
build/{torch212-cxx11-cu132-x86_64-linux/_flashrt_fp8_ffn_cuda_5de4768.abi3.so → torch211-cxx11-rocm72-x86_64-linux/_flashrt_fp8_ffn_rocm_4a17df5.abi3.so} RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:86bcfc15a0fb8f01e00b10ae650f19783ea298fd7ebda9dceef741ac7081610f
3
- size 314080
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:74b3c142dc669c0e4354b31e05cdf0b080617a74cdf451761aa4c5520d86f651
3
+ size 143848
build/torch211-cxx11-rocm72-x86_64-linux/_ops.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from . import _flashrt_fp8_ffn_rocm_4a17df5
3
+ ops = torch.ops._flashrt_fp8_ffn_rocm_4a17df5
4
+
5
+ def add_op_namespace_prefix(op_name: str):
6
+ """
7
+ Prefix op by namespace.
8
+ """
9
+ return f"_flashrt_fp8_ffn_rocm_4a17df5::{op_name}"
build/torch211-cxx11-rocm72-x86_64-linux/flashrt_fp8_ffn/__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-rocm72-x86_64-linux/metadata.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "flashrt-fp8-ffn",
3
+ "id": "_flashrt_fp8_ffn_rocm_4a17df5",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "python-depends": [],
7
+ "backend": {
8
+ "type": "rocm",
9
+ "archs": [
10
+ "gfx942"
11
+ ]
12
+ }
13
+ }
build/torch212-cxx11-cu130-x86_64-linux/__init__.py CHANGED
@@ -34,6 +34,12 @@ _preload_cublaslt()
34
  from ._ops import add_op_namespace_prefix, ops
35
 
36
 
 
 
 
 
 
 
37
  @torch.library.register_fake(add_op_namespace_prefix("fp8_gemm_bf16"))
38
  def _fp8_gemm_bf16_fake(
39
  input: torch.Tensor,
@@ -146,7 +152,7 @@ def fp8_linear_bias_gelu_quant_bf16(
146
  dtype=torch.bfloat16,
147
  )
148
  if out_fp8 is None:
149
- out_fp8 = torch.empty_like(hidden_bf16, dtype=torch.float8_e4m3fn)
150
  ops.fp8_linear_bias_gelu_quant_bf16(
151
  input,
152
  weight,
@@ -190,7 +196,7 @@ def fp8_gelu_mlp_bf16(
190
  dtype=torch.bfloat16,
191
  )
192
  if hidden_fp8 is None:
193
- hidden_fp8 = torch.empty_like(hidden_bf16, dtype=torch.float8_e4m3fn)
194
  if out is None:
195
  out = torch.empty(
196
  (input.shape[0], down_weight.shape[0]),
 
34
  from ._ops import add_op_namespace_prefix, ops
35
 
36
 
37
+ def _fp8_dtype() -> torch.dtype:
38
+ if torch.version.hip is not None and hasattr(torch, "float8_e4m3fnuz"):
39
+ return torch.float8_e4m3fnuz
40
+ return torch.float8_e4m3fn
41
+
42
+
43
  @torch.library.register_fake(add_op_namespace_prefix("fp8_gemm_bf16"))
44
  def _fp8_gemm_bf16_fake(
45
  input: torch.Tensor,
 
152
  dtype=torch.bfloat16,
153
  )
154
  if out_fp8 is None:
155
+ out_fp8 = torch.empty_like(hidden_bf16, dtype=_fp8_dtype())
156
  ops.fp8_linear_bias_gelu_quant_bf16(
157
  input,
158
  weight,
 
196
  dtype=torch.bfloat16,
197
  )
198
  if hidden_fp8 is None:
199
+ hidden_fp8 = torch.empty_like(hidden_bf16, dtype=_fp8_dtype())
200
  if out is None:
201
  out = torch.empty(
202
  (input.shape[0], down_weight.shape[0]),
build/torch212-cxx11-cu130-x86_64-linux/_flashrt_fp8_ffn_cuda_4a17df5.abi3.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f30f0cd5e5d36d0c5eb7ee59f26230eaa452d9eb4e346853a39965ad5ed5a7a6
3
+ size 298032
build/torch212-cxx11-cu130-x86_64-linux/_ops.py CHANGED
@@ -1,9 +1,9 @@
1
  import torch
2
- from . import _flashrt_fp8_ffn_cuda_5de4768
3
- ops = torch.ops._flashrt_fp8_ffn_cuda_5de4768
4
 
5
  def add_op_namespace_prefix(op_name: str):
6
  """
7
  Prefix op by namespace.
8
  """
9
- return f"_flashrt_fp8_ffn_cuda_5de4768::{op_name}"
 
1
  import torch
2
+ from . import _flashrt_fp8_ffn_cuda_4a17df5
3
+ ops = torch.ops._flashrt_fp8_ffn_cuda_4a17df5
4
 
5
  def add_op_namespace_prefix(op_name: str):
6
  """
7
  Prefix op by namespace.
8
  """
9
+ return f"_flashrt_fp8_ffn_cuda_4a17df5::{op_name}"
build/torch212-cxx11-cu130-x86_64-linux/metadata.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "flashrt-fp8-ffn",
3
- "id": "_flashrt_fp8_ffn_cuda_5de4768",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "python-depends": [],
 
1
  {
2
  "name": "flashrt-fp8-ffn",
3
+ "id": "_flashrt_fp8_ffn_cuda_4a17df5",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "python-depends": [],
build/torch212-cxx11-cu132-x86_64-linux/__init__.py CHANGED
@@ -34,6 +34,12 @@ _preload_cublaslt()
34
  from ._ops import add_op_namespace_prefix, ops
35
 
36
 
 
 
 
 
 
 
37
  @torch.library.register_fake(add_op_namespace_prefix("fp8_gemm_bf16"))
38
  def _fp8_gemm_bf16_fake(
39
  input: torch.Tensor,
@@ -146,7 +152,7 @@ def fp8_linear_bias_gelu_quant_bf16(
146
  dtype=torch.bfloat16,
147
  )
148
  if out_fp8 is None:
149
- out_fp8 = torch.empty_like(hidden_bf16, dtype=torch.float8_e4m3fn)
150
  ops.fp8_linear_bias_gelu_quant_bf16(
151
  input,
152
  weight,
@@ -190,7 +196,7 @@ def fp8_gelu_mlp_bf16(
190
  dtype=torch.bfloat16,
191
  )
192
  if hidden_fp8 is None:
193
- hidden_fp8 = torch.empty_like(hidden_bf16, dtype=torch.float8_e4m3fn)
194
  if out is None:
195
  out = torch.empty(
196
  (input.shape[0], down_weight.shape[0]),
 
34
  from ._ops import add_op_namespace_prefix, ops
35
 
36
 
37
+ def _fp8_dtype() -> torch.dtype:
38
+ if torch.version.hip is not None and hasattr(torch, "float8_e4m3fnuz"):
39
+ return torch.float8_e4m3fnuz
40
+ return torch.float8_e4m3fn
41
+
42
+
43
  @torch.library.register_fake(add_op_namespace_prefix("fp8_gemm_bf16"))
44
  def _fp8_gemm_bf16_fake(
45
  input: torch.Tensor,
 
152
  dtype=torch.bfloat16,
153
  )
154
  if out_fp8 is None:
155
+ out_fp8 = torch.empty_like(hidden_bf16, dtype=_fp8_dtype())
156
  ops.fp8_linear_bias_gelu_quant_bf16(
157
  input,
158
  weight,
 
196
  dtype=torch.bfloat16,
197
  )
198
  if hidden_fp8 is None:
199
+ hidden_fp8 = torch.empty_like(hidden_bf16, dtype=_fp8_dtype())
200
  if out is None:
201
  out = torch.empty(
202
  (input.shape[0], down_weight.shape[0]),
build/torch212-cxx11-cu132-x86_64-linux/_flashrt_fp8_ffn_cuda_4a17df5.abi3.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:14e82facb9b426efcf66b0855d0f281192c806d20d4e4bd00ef63c1a94ea4d48
3
+ size 298032
build/torch212-cxx11-cu132-x86_64-linux/_ops.py CHANGED
@@ -1,9 +1,9 @@
1
  import torch
2
- from . import _flashrt_fp8_ffn_cuda_5de4768
3
- ops = torch.ops._flashrt_fp8_ffn_cuda_5de4768
4
 
5
  def add_op_namespace_prefix(op_name: str):
6
  """
7
  Prefix op by namespace.
8
  """
9
- return f"_flashrt_fp8_ffn_cuda_5de4768::{op_name}"
 
1
  import torch
2
+ from . import _flashrt_fp8_ffn_cuda_4a17df5
3
+ ops = torch.ops._flashrt_fp8_ffn_cuda_4a17df5
4
 
5
  def add_op_namespace_prefix(op_name: str):
6
  """
7
  Prefix op by namespace.
8
  """
9
+ return f"_flashrt_fp8_ffn_cuda_4a17df5::{op_name}"
build/torch212-cxx11-cu132-x86_64-linux/metadata.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "flashrt-fp8-ffn",
3
- "id": "_flashrt_fp8_ffn_cuda_5de4768",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "python-depends": [],
 
1
  {
2
  "name": "flashrt-fp8-ffn",
3
+ "id": "_flashrt_fp8_ffn_cuda_4a17df5",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "python-depends": [],
build/torch212-cxx11-rocm71-x86_64-linux/__init__.py ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """FlashRT FP8 FFN kernels."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import ctypes
6
+ import ctypes.util
7
+ from pathlib import Path
8
+ from typing import Optional
9
+
10
+ import torch
11
+
12
+
13
+ def _torch_bundled_cublaslt() -> Optional[Path]:
14
+ for parent in Path(torch.__file__).resolve().parents:
15
+ candidate = parent / "nvidia" / "cublas" / "lib" / "libcublasLt.so.12"
16
+ if candidate.exists():
17
+ return candidate
18
+ return None
19
+
20
+
21
+ def _preload_cublaslt() -> None:
22
+ bundled = _torch_bundled_cublaslt()
23
+ library = str(bundled) if bundled is not None else (
24
+ ctypes.util.find_library("cublasLt") or "libcublasLt.so"
25
+ )
26
+ try:
27
+ ctypes.CDLL(library, mode=ctypes.RTLD_GLOBAL)
28
+ except OSError:
29
+ pass
30
+
31
+
32
+ _preload_cublaslt()
33
+
34
+ from ._ops import add_op_namespace_prefix, ops
35
+
36
+
37
+ def _fp8_dtype() -> torch.dtype:
38
+ if torch.version.hip is not None and hasattr(torch, "float8_e4m3fnuz"):
39
+ return torch.float8_e4m3fnuz
40
+ return torch.float8_e4m3fn
41
+
42
+
43
+ @torch.library.register_fake(add_op_namespace_prefix("fp8_gemm_bf16"))
44
+ def _fp8_gemm_bf16_fake(
45
+ input: torch.Tensor,
46
+ weight: torch.Tensor,
47
+ input_scale: torch.Tensor,
48
+ weight_scale: torch.Tensor,
49
+ out: torch.Tensor,
50
+ ) -> None:
51
+ if input.dim() != 2 or weight.dim() != 2:
52
+ raise RuntimeError("input and weight must be rank-2 tensors")
53
+ if out.shape != (input.shape[0], weight.shape[0]):
54
+ raise RuntimeError("out shape must be (input.shape[0], weight.shape[0])")
55
+ return None
56
+
57
+
58
+ @torch.library.register_fake(add_op_namespace_prefix("fp8_linear_bias_gelu_quant_bf16"))
59
+ def _fp8_linear_bias_gelu_quant_bf16_fake(
60
+ input: torch.Tensor,
61
+ weight: torch.Tensor,
62
+ bias: torch.Tensor,
63
+ input_scale: torch.Tensor,
64
+ weight_scale: torch.Tensor,
65
+ output_scale: torch.Tensor,
66
+ hidden_bf16: torch.Tensor,
67
+ out_fp8: torch.Tensor,
68
+ ) -> None:
69
+ expected = (input.shape[0], weight.shape[0])
70
+ if hidden_bf16.shape != expected or out_fp8.shape != expected:
71
+ raise RuntimeError(
72
+ "hidden_bf16 and out_fp8 shapes must be "
73
+ "(input.shape[0], weight.shape[0])"
74
+ )
75
+ return None
76
+
77
+
78
+ @torch.library.register_fake(add_op_namespace_prefix("fp8_gelu_mlp_bf16"))
79
+ def _fp8_gelu_mlp_bf16_fake(
80
+ input: torch.Tensor,
81
+ up_weight: torch.Tensor,
82
+ up_bias: torch.Tensor,
83
+ down_weight: torch.Tensor,
84
+ down_bias: torch.Tensor,
85
+ input_scale: torch.Tensor,
86
+ up_weight_scale: torch.Tensor,
87
+ hidden_scale: torch.Tensor,
88
+ down_weight_scale: torch.Tensor,
89
+ hidden_bf16: torch.Tensor,
90
+ hidden_fp8: torch.Tensor,
91
+ out: torch.Tensor,
92
+ ) -> None:
93
+ hidden_shape = (input.shape[0], up_weight.shape[0])
94
+ out_shape = (input.shape[0], down_weight.shape[0])
95
+ if hidden_bf16.shape != hidden_shape or hidden_fp8.shape != hidden_shape:
96
+ raise RuntimeError(
97
+ "hidden buffers must be (input.shape[0], up_weight.shape[0])"
98
+ )
99
+ if out.shape != out_shape:
100
+ raise RuntimeError("out shape must be (input.shape[0], down_weight.shape[0])")
101
+ return None
102
+
103
+
104
+ def _scalar_scale_like(input: torch.Tensor, value: float = 1.0) -> torch.Tensor:
105
+ return torch.tensor([value], device=input.device, dtype=torch.float32)
106
+
107
+
108
+ def fp8_gemm_bf16(
109
+ input: torch.Tensor,
110
+ weight: torch.Tensor,
111
+ input_scale: torch.Tensor,
112
+ weight_scale: torch.Tensor,
113
+ out: torch.Tensor | None = None,
114
+ ) -> torch.Tensor:
115
+ """Compute ``(input * input_scale) @ (weight * weight_scale).T``.
116
+
117
+ ``input`` is FP8 E4M3 with shape ``(M, K)``. ``weight`` is FP8 E4M3 with
118
+ shape ``(N, K)``. ``input_scale`` and ``weight_scale`` are CUDA float32
119
+ scalar tensors. Output is BF16 with shape ``(M, N)``.
120
+ """
121
+
122
+ if out is None:
123
+ out = torch.empty(
124
+ (input.shape[0], weight.shape[0]),
125
+ device=input.device,
126
+ dtype=torch.bfloat16,
127
+ )
128
+ ops.fp8_gemm_bf16(input, weight, input_scale, weight_scale, out)
129
+ return out
130
+
131
+
132
+ def fp8_linear_bias_gelu_quant_bf16(
133
+ input: torch.Tensor,
134
+ weight: torch.Tensor,
135
+ bias: torch.Tensor,
136
+ input_scale: torch.Tensor,
137
+ weight_scale: torch.Tensor,
138
+ output_scale: torch.Tensor,
139
+ hidden_bf16: torch.Tensor | None = None,
140
+ out_fp8: torch.Tensor | None = None,
141
+ ) -> tuple[torch.Tensor, torch.Tensor]:
142
+ """FP8 linear + BF16 bias/GELU + FP8 quantized output.
143
+
144
+ Returns ``(hidden_bf16, out_fp8)``. ``hidden_bf16`` is the post-GEMM
145
+ pre-activation scratch; ``out_fp8`` is the quantized activation.
146
+ """
147
+
148
+ if hidden_bf16 is None:
149
+ hidden_bf16 = torch.empty(
150
+ (input.shape[0], weight.shape[0]),
151
+ device=input.device,
152
+ dtype=torch.bfloat16,
153
+ )
154
+ if out_fp8 is None:
155
+ out_fp8 = torch.empty_like(hidden_bf16, dtype=_fp8_dtype())
156
+ ops.fp8_linear_bias_gelu_quant_bf16(
157
+ input,
158
+ weight,
159
+ bias,
160
+ input_scale,
161
+ weight_scale,
162
+ output_scale,
163
+ hidden_bf16,
164
+ out_fp8,
165
+ )
166
+ return hidden_bf16, out_fp8
167
+
168
+
169
+ def fp8_gelu_mlp_bf16(
170
+ input: torch.Tensor,
171
+ up_weight: torch.Tensor,
172
+ up_bias: torch.Tensor,
173
+ down_weight: torch.Tensor,
174
+ down_bias: torch.Tensor,
175
+ input_scale: torch.Tensor,
176
+ up_weight_scale: torch.Tensor,
177
+ hidden_scale: torch.Tensor,
178
+ down_weight_scale: torch.Tensor,
179
+ hidden_bf16: torch.Tensor | None = None,
180
+ hidden_fp8: torch.Tensor | None = None,
181
+ out: torch.Tensor | None = None,
182
+ ) -> torch.Tensor:
183
+ """FP8 GELU MLP block with BF16 output.
184
+
185
+ Computes:
186
+
187
+ ``hidden = gelu(fp8_gemm(input, up_weight) + up_bias)``
188
+ ``hidden_fp8 = quantize_fp8(hidden, hidden_scale)``
189
+ ``out = fp8_gemm(hidden_fp8, down_weight) + down_bias``
190
+ """
191
+
192
+ if hidden_bf16 is None:
193
+ hidden_bf16 = torch.empty(
194
+ (input.shape[0], up_weight.shape[0]),
195
+ device=input.device,
196
+ dtype=torch.bfloat16,
197
+ )
198
+ if hidden_fp8 is None:
199
+ hidden_fp8 = torch.empty_like(hidden_bf16, dtype=_fp8_dtype())
200
+ if out is None:
201
+ out = torch.empty(
202
+ (input.shape[0], down_weight.shape[0]),
203
+ device=input.device,
204
+ dtype=torch.bfloat16,
205
+ )
206
+ ops.fp8_gelu_mlp_bf16(
207
+ input,
208
+ up_weight,
209
+ up_bias,
210
+ down_weight,
211
+ down_bias,
212
+ input_scale,
213
+ up_weight_scale,
214
+ hidden_scale,
215
+ down_weight_scale,
216
+ hidden_bf16,
217
+ hidden_fp8,
218
+ out,
219
+ )
220
+ return out
221
+
222
+
223
+ __all__ = [
224
+ "fp8_gemm_bf16",
225
+ "fp8_gelu_mlp_bf16",
226
+ "fp8_linear_bias_gelu_quant_bf16",
227
+ ]
build/torch212-cxx11-rocm71-x86_64-linux/_flashrt_fp8_ffn_rocm_4a17df5.abi3.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3dc2093b25e985a0af258cd2702fb3e28ac98e4ff04a406c423ce48dd66cea11
3
+ size 146416
build/torch212-cxx11-rocm71-x86_64-linux/_ops.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from . import _flashrt_fp8_ffn_rocm_4a17df5
3
+ ops = torch.ops._flashrt_fp8_ffn_rocm_4a17df5
4
+
5
+ def add_op_namespace_prefix(op_name: str):
6
+ """
7
+ Prefix op by namespace.
8
+ """
9
+ return f"_flashrt_fp8_ffn_rocm_4a17df5::{op_name}"
build/torch212-cxx11-rocm71-x86_64-linux/flashrt_fp8_ffn/__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-rocm71-x86_64-linux/metadata.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "flashrt-fp8-ffn",
3
+ "id": "_flashrt_fp8_ffn_rocm_4a17df5",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "python-depends": [],
7
+ "backend": {
8
+ "type": "rocm",
9
+ "archs": [
10
+ "gfx942"
11
+ ]
12
+ }
13
+ }
build/torch212-cxx11-rocm72-x86_64-linux/__init__.py ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """FlashRT FP8 FFN kernels."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import ctypes
6
+ import ctypes.util
7
+ from pathlib import Path
8
+ from typing import Optional
9
+
10
+ import torch
11
+
12
+
13
+ def _torch_bundled_cublaslt() -> Optional[Path]:
14
+ for parent in Path(torch.__file__).resolve().parents:
15
+ candidate = parent / "nvidia" / "cublas" / "lib" / "libcublasLt.so.12"
16
+ if candidate.exists():
17
+ return candidate
18
+ return None
19
+
20
+
21
+ def _preload_cublaslt() -> None:
22
+ bundled = _torch_bundled_cublaslt()
23
+ library = str(bundled) if bundled is not None else (
24
+ ctypes.util.find_library("cublasLt") or "libcublasLt.so"
25
+ )
26
+ try:
27
+ ctypes.CDLL(library, mode=ctypes.RTLD_GLOBAL)
28
+ except OSError:
29
+ pass
30
+
31
+
32
+ _preload_cublaslt()
33
+
34
+ from ._ops import add_op_namespace_prefix, ops
35
+
36
+
37
+ def _fp8_dtype() -> torch.dtype:
38
+ if torch.version.hip is not None and hasattr(torch, "float8_e4m3fnuz"):
39
+ return torch.float8_e4m3fnuz
40
+ return torch.float8_e4m3fn
41
+
42
+
43
+ @torch.library.register_fake(add_op_namespace_prefix("fp8_gemm_bf16"))
44
+ def _fp8_gemm_bf16_fake(
45
+ input: torch.Tensor,
46
+ weight: torch.Tensor,
47
+ input_scale: torch.Tensor,
48
+ weight_scale: torch.Tensor,
49
+ out: torch.Tensor,
50
+ ) -> None:
51
+ if input.dim() != 2 or weight.dim() != 2:
52
+ raise RuntimeError("input and weight must be rank-2 tensors")
53
+ if out.shape != (input.shape[0], weight.shape[0]):
54
+ raise RuntimeError("out shape must be (input.shape[0], weight.shape[0])")
55
+ return None
56
+
57
+
58
+ @torch.library.register_fake(add_op_namespace_prefix("fp8_linear_bias_gelu_quant_bf16"))
59
+ def _fp8_linear_bias_gelu_quant_bf16_fake(
60
+ input: torch.Tensor,
61
+ weight: torch.Tensor,
62
+ bias: torch.Tensor,
63
+ input_scale: torch.Tensor,
64
+ weight_scale: torch.Tensor,
65
+ output_scale: torch.Tensor,
66
+ hidden_bf16: torch.Tensor,
67
+ out_fp8: torch.Tensor,
68
+ ) -> None:
69
+ expected = (input.shape[0], weight.shape[0])
70
+ if hidden_bf16.shape != expected or out_fp8.shape != expected:
71
+ raise RuntimeError(
72
+ "hidden_bf16 and out_fp8 shapes must be "
73
+ "(input.shape[0], weight.shape[0])"
74
+ )
75
+ return None
76
+
77
+
78
+ @torch.library.register_fake(add_op_namespace_prefix("fp8_gelu_mlp_bf16"))
79
+ def _fp8_gelu_mlp_bf16_fake(
80
+ input: torch.Tensor,
81
+ up_weight: torch.Tensor,
82
+ up_bias: torch.Tensor,
83
+ down_weight: torch.Tensor,
84
+ down_bias: torch.Tensor,
85
+ input_scale: torch.Tensor,
86
+ up_weight_scale: torch.Tensor,
87
+ hidden_scale: torch.Tensor,
88
+ down_weight_scale: torch.Tensor,
89
+ hidden_bf16: torch.Tensor,
90
+ hidden_fp8: torch.Tensor,
91
+ out: torch.Tensor,
92
+ ) -> None:
93
+ hidden_shape = (input.shape[0], up_weight.shape[0])
94
+ out_shape = (input.shape[0], down_weight.shape[0])
95
+ if hidden_bf16.shape != hidden_shape or hidden_fp8.shape != hidden_shape:
96
+ raise RuntimeError(
97
+ "hidden buffers must be (input.shape[0], up_weight.shape[0])"
98
+ )
99
+ if out.shape != out_shape:
100
+ raise RuntimeError("out shape must be (input.shape[0], down_weight.shape[0])")
101
+ return None
102
+
103
+
104
+ def _scalar_scale_like(input: torch.Tensor, value: float = 1.0) -> torch.Tensor:
105
+ return torch.tensor([value], device=input.device, dtype=torch.float32)
106
+
107
+
108
+ def fp8_gemm_bf16(
109
+ input: torch.Tensor,
110
+ weight: torch.Tensor,
111
+ input_scale: torch.Tensor,
112
+ weight_scale: torch.Tensor,
113
+ out: torch.Tensor | None = None,
114
+ ) -> torch.Tensor:
115
+ """Compute ``(input * input_scale) @ (weight * weight_scale).T``.
116
+
117
+ ``input`` is FP8 E4M3 with shape ``(M, K)``. ``weight`` is FP8 E4M3 with
118
+ shape ``(N, K)``. ``input_scale`` and ``weight_scale`` are CUDA float32
119
+ scalar tensors. Output is BF16 with shape ``(M, N)``.
120
+ """
121
+
122
+ if out is None:
123
+ out = torch.empty(
124
+ (input.shape[0], weight.shape[0]),
125
+ device=input.device,
126
+ dtype=torch.bfloat16,
127
+ )
128
+ ops.fp8_gemm_bf16(input, weight, input_scale, weight_scale, out)
129
+ return out
130
+
131
+
132
+ def fp8_linear_bias_gelu_quant_bf16(
133
+ input: torch.Tensor,
134
+ weight: torch.Tensor,
135
+ bias: torch.Tensor,
136
+ input_scale: torch.Tensor,
137
+ weight_scale: torch.Tensor,
138
+ output_scale: torch.Tensor,
139
+ hidden_bf16: torch.Tensor | None = None,
140
+ out_fp8: torch.Tensor | None = None,
141
+ ) -> tuple[torch.Tensor, torch.Tensor]:
142
+ """FP8 linear + BF16 bias/GELU + FP8 quantized output.
143
+
144
+ Returns ``(hidden_bf16, out_fp8)``. ``hidden_bf16`` is the post-GEMM
145
+ pre-activation scratch; ``out_fp8`` is the quantized activation.
146
+ """
147
+
148
+ if hidden_bf16 is None:
149
+ hidden_bf16 = torch.empty(
150
+ (input.shape[0], weight.shape[0]),
151
+ device=input.device,
152
+ dtype=torch.bfloat16,
153
+ )
154
+ if out_fp8 is None:
155
+ out_fp8 = torch.empty_like(hidden_bf16, dtype=_fp8_dtype())
156
+ ops.fp8_linear_bias_gelu_quant_bf16(
157
+ input,
158
+ weight,
159
+ bias,
160
+ input_scale,
161
+ weight_scale,
162
+ output_scale,
163
+ hidden_bf16,
164
+ out_fp8,
165
+ )
166
+ return hidden_bf16, out_fp8
167
+
168
+
169
+ def fp8_gelu_mlp_bf16(
170
+ input: torch.Tensor,
171
+ up_weight: torch.Tensor,
172
+ up_bias: torch.Tensor,
173
+ down_weight: torch.Tensor,
174
+ down_bias: torch.Tensor,
175
+ input_scale: torch.Tensor,
176
+ up_weight_scale: torch.Tensor,
177
+ hidden_scale: torch.Tensor,
178
+ down_weight_scale: torch.Tensor,
179
+ hidden_bf16: torch.Tensor | None = None,
180
+ hidden_fp8: torch.Tensor | None = None,
181
+ out: torch.Tensor | None = None,
182
+ ) -> torch.Tensor:
183
+ """FP8 GELU MLP block with BF16 output.
184
+
185
+ Computes:
186
+
187
+ ``hidden = gelu(fp8_gemm(input, up_weight) + up_bias)``
188
+ ``hidden_fp8 = quantize_fp8(hidden, hidden_scale)``
189
+ ``out = fp8_gemm(hidden_fp8, down_weight) + down_bias``
190
+ """
191
+
192
+ if hidden_bf16 is None:
193
+ hidden_bf16 = torch.empty(
194
+ (input.shape[0], up_weight.shape[0]),
195
+ device=input.device,
196
+ dtype=torch.bfloat16,
197
+ )
198
+ if hidden_fp8 is None:
199
+ hidden_fp8 = torch.empty_like(hidden_bf16, dtype=_fp8_dtype())
200
+ if out is None:
201
+ out = torch.empty(
202
+ (input.shape[0], down_weight.shape[0]),
203
+ device=input.device,
204
+ dtype=torch.bfloat16,
205
+ )
206
+ ops.fp8_gelu_mlp_bf16(
207
+ input,
208
+ up_weight,
209
+ up_bias,
210
+ down_weight,
211
+ down_bias,
212
+ input_scale,
213
+ up_weight_scale,
214
+ hidden_scale,
215
+ down_weight_scale,
216
+ hidden_bf16,
217
+ hidden_fp8,
218
+ out,
219
+ )
220
+ return out
221
+
222
+
223
+ __all__ = [
224
+ "fp8_gemm_bf16",
225
+ "fp8_gelu_mlp_bf16",
226
+ "fp8_linear_bias_gelu_quant_bf16",
227
+ ]
build/torch212-cxx11-rocm72-x86_64-linux/_flashrt_fp8_ffn_rocm_4a17df5.abi3.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:387239194294371025c6fa91fd16e3ac550052f1632b73ec37b7879371cc8b9d
3
+ size 145040
build/torch212-cxx11-rocm72-x86_64-linux/_ops.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from . import _flashrt_fp8_ffn_rocm_4a17df5
3
+ ops = torch.ops._flashrt_fp8_ffn_rocm_4a17df5
4
+
5
+ def add_op_namespace_prefix(op_name: str):
6
+ """
7
+ Prefix op by namespace.
8
+ """
9
+ return f"_flashrt_fp8_ffn_rocm_4a17df5::{op_name}"
build/torch212-cxx11-rocm72-x86_64-linux/flashrt_fp8_ffn/__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-rocm72-x86_64-linux/metadata.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "flashrt-fp8-ffn",
3
+ "id": "_flashrt_fp8_ffn_rocm_4a17df5",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "python-depends": [],
7
+ "backend": {
8
+ "type": "rocm",
9
+ "archs": [
10
+ "gfx942"
11
+ ]
12
+ }
13
+ }