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

Add torch211-cxx11-cu130-aarch64-linux SM121 artifact

Browse files
build/torch211-cxx11-cu130-aarch64-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/torch211-cxx11-cu130-aarch64-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:cdae9ec3dd812aebf9b4afa228aa65299f5c650b5f6842aa0cd42130c2d3c5c7
3
+ size 176472
build/torch211-cxx11-cu130-aarch64-linux/_ops.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
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
+ return f"_flashrt_fp8_ffn_cuda_4a17df5::{op_name}"
build/torch211-cxx11-cu130-aarch64-linux/flashrt_fp8_ffn/__init__.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ctypes
2
+ import importlib.util
3
+ import sys
4
+ from pathlib import Path
5
+
6
+ def _import_from_path(file_path: Path):
7
+ path_hash = '{:x}'.format(ctypes.c_size_t(hash(file_path.absolute())).value)
8
+ spec = importlib.util.spec_from_file_location(path_hash, file_path)
9
+ module = importlib.util.module_from_spec(spec)
10
+ sys.modules[path_hash] = module
11
+ spec.loader.exec_module(module)
12
+ return module
13
+
14
+ globals().update(vars(_import_from_path(Path(__file__).parent.parent / '__init__.py')))
build/torch211-cxx11-cu130-aarch64-linux/metadata.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "flashrt-fp8-ffn",
3
+ "id": "_flashrt_fp8_ffn_cuda_4a17df5",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "python-depends": [],
7
+ "backend": {
8
+ "type": "cuda",
9
+ "archs": [
10
+ "12.1+PTX"
11
+ ]
12
+ }
13
+ }