liangsu9988 commited on
Commit
0111722
·
verified ·
1 Parent(s): 07d6b31

Add torch213-cxx11-cu130-aarch64-linux artifact

Browse files
build/torch213-cxx11-cu130-aarch64-linux/__init__.py ADDED
@@ -0,0 +1,261 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Weight-only BF16-activation linear and FFN regions."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Optional
6
+
7
+ import torch
8
+
9
+ from ._ops import add_op_namespace_prefix, ops
10
+
11
+
12
+ def w4_sfb_size_bytes(rows: int, cols: int) -> int:
13
+ if rows <= 0 or cols <= 0 or cols % 64:
14
+ raise ValueError("rows must be positive and cols must be divisible by 64")
15
+ return ((int(rows) + 127) // 128) * (((int(cols) // 16) + 3) // 4) * 512
16
+
17
+
18
+ @torch.library.register_fake(add_op_namespace_prefix("quantize_w4_weight_bf16"))
19
+ def _quantize_w4_fake(weight: torch.Tensor, packed: torch.Tensor, sfb: torch.Tensor) -> None:
20
+ return None
21
+
22
+
23
+ @torch.library.register_fake(add_op_namespace_prefix("dequantize_w4_weight_bf16"))
24
+ def _dequantize_w4_fake(packed: torch.Tensor, sfb: torch.Tensor, weight: torch.Tensor) -> None:
25
+ return None
26
+
27
+
28
+ @torch.library.register_fake(add_op_namespace_prefix("w4a16_linear_bf16"))
29
+ def _w4_linear_fake(
30
+ x: torch.Tensor,
31
+ packed: torch.Tensor,
32
+ sfb: torch.Tensor,
33
+ alpha: float,
34
+ variant: int,
35
+ out: torch.Tensor,
36
+ ) -> None:
37
+ return None
38
+
39
+
40
+ @torch.library.register_fake(add_op_namespace_prefix("quantize_w8_weight_bf16"))
41
+ def _quantize_w8_fake(weight: torch.Tensor, quantized: torch.Tensor, scales: torch.Tensor) -> None:
42
+ return None
43
+
44
+
45
+ @torch.library.register_fake(add_op_namespace_prefix("dequantize_w8_weight_bf16"))
46
+ def _dequantize_w8_fake(quantized: torch.Tensor, scales: torch.Tensor, weight: torch.Tensor) -> None:
47
+ return None
48
+
49
+
50
+ @torch.library.register_fake(add_op_namespace_prefix("w8a16_linear_bf16"))
51
+ def _w8_linear_fake(
52
+ x: torch.Tensor,
53
+ quantized: torch.Tensor,
54
+ scales: torch.Tensor,
55
+ variant: int,
56
+ out: torch.Tensor,
57
+ ) -> None:
58
+ return None
59
+
60
+
61
+ @torch.library.register_fake(add_op_namespace_prefix("w4a16_gated_ffn_bf16"))
62
+ def _w4_gated_fake(
63
+ x, gu_w, gu_s, dn_w, dn_s, gu_b, dn_b, gelu, gu_alpha, dn_alpha,
64
+ variant, gu, hidden, out,
65
+ ) -> None:
66
+ return None
67
+
68
+
69
+ @torch.library.register_fake(add_op_namespace_prefix("w8a16_gated_ffn_bf16"))
70
+ def _w8_gated_fake(
71
+ x, gu_w, gu_s, dn_w, dn_s, gu_b, dn_b, gelu, variant, gu, hidden, out,
72
+ ) -> None:
73
+ return None
74
+
75
+
76
+ @torch.library.register_fake(add_op_namespace_prefix("w4a16_gelu_ffn_bf16"))
77
+ def _w4_gelu_fake(
78
+ x, up_w, up_s, dn_w, dn_s, up_b, dn_b, up_alpha, dn_alpha,
79
+ variant, up, hidden, out,
80
+ ) -> None:
81
+ return None
82
+
83
+
84
+ @torch.library.register_fake(add_op_namespace_prefix("w8a16_gelu_ffn_bf16"))
85
+ def _w8_gelu_fake(
86
+ x, up_w, up_s, dn_w, dn_s, up_b, dn_b, variant, up, hidden, out,
87
+ ) -> None:
88
+ return None
89
+
90
+
91
+ def quantize_w4_weight_bf16(weight: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
92
+ """Pack a static BF16 weight matrix into NVFP4 data and SFB scales."""
93
+ n, k = weight.shape
94
+ packed = torch.empty((n, k // 2), device=weight.device, dtype=torch.uint8)
95
+ sfb = torch.empty((w4_sfb_size_bytes(n, k),), device=weight.device, dtype=torch.uint8)
96
+ ops.quantize_w4_weight_bf16(weight, packed, sfb)
97
+ return packed, sfb
98
+
99
+
100
+ def dequantize_w4_weight_bf16(
101
+ packed: torch.Tensor, sfb: torch.Tensor, *, cols: int
102
+ ) -> torch.Tensor:
103
+ weight = torch.empty((packed.shape[0], int(cols)), device=packed.device, dtype=torch.bfloat16)
104
+ ops.dequantize_w4_weight_bf16(packed, sfb, weight)
105
+ return weight
106
+
107
+
108
+ def quantize_w8_weight_bf16(weight: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
109
+ """Symmetrically quantize a static BF16 weight matrix per output channel."""
110
+ quantized = torch.empty_like(weight, dtype=torch.int8)
111
+ scales = torch.empty((weight.shape[0],), device=weight.device, dtype=torch.float32)
112
+ ops.quantize_w8_weight_bf16(weight, quantized, scales)
113
+ return quantized, scales
114
+
115
+
116
+ def dequantize_w8_weight_bf16(
117
+ quantized: torch.Tensor, scales: torch.Tensor
118
+ ) -> torch.Tensor:
119
+ weight = torch.empty_like(quantized, dtype=torch.bfloat16)
120
+ ops.dequantize_w8_weight_bf16(quantized, scales, weight)
121
+ return weight
122
+
123
+
124
+ def w4a16_linear_bf16(
125
+ x: torch.Tensor,
126
+ weight_packed: torch.Tensor,
127
+ weight_sfb: torch.Tensor,
128
+ *,
129
+ alpha: float = 1.0,
130
+ variant: int = 0,
131
+ out: Optional[torch.Tensor] = None,
132
+ ) -> torch.Tensor:
133
+ if out is None:
134
+ out = torch.empty((x.shape[0], weight_packed.shape[0]), device=x.device, dtype=torch.bfloat16)
135
+ ops.w4a16_linear_bf16(x, weight_packed, weight_sfb, float(alpha), int(variant), out)
136
+ return out
137
+
138
+
139
+ def w8a16_linear_bf16(
140
+ x: torch.Tensor,
141
+ weight_int8: torch.Tensor,
142
+ weight_scale: torch.Tensor,
143
+ *,
144
+ variant: int = 0,
145
+ out: Optional[torch.Tensor] = None,
146
+ ) -> torch.Tensor:
147
+ if out is None:
148
+ out = torch.empty((x.shape[0], weight_int8.shape[0]), device=x.device, dtype=torch.bfloat16)
149
+ ops.w8a16_linear_bf16(x, weight_int8, weight_scale, int(variant), out)
150
+ return out
151
+
152
+
153
+ def _gated_workspace(x: torch.Tensor, gate_up_rows: int, out_rows: int, workspace, out):
154
+ hidden_size = gate_up_rows // 2
155
+ if workspace is None:
156
+ gate_up = torch.empty((x.shape[0], gate_up_rows), device=x.device, dtype=torch.bfloat16)
157
+ hidden = torch.empty((x.shape[0], hidden_size), device=x.device, dtype=torch.bfloat16)
158
+ else:
159
+ gate_up, hidden = workspace
160
+ if out is None:
161
+ out = torch.empty((x.shape[0], out_rows), device=x.device, dtype=torch.bfloat16)
162
+ return gate_up, hidden, out
163
+
164
+
165
+ def _gelu_workspace(x: torch.Tensor, hidden_size: int, out_rows: int, workspace, out):
166
+ if workspace is None:
167
+ up = torch.empty((x.shape[0], hidden_size), device=x.device, dtype=torch.bfloat16)
168
+ hidden = torch.empty_like(up)
169
+ else:
170
+ up, hidden = workspace
171
+ if out is None:
172
+ out = torch.empty((x.shape[0], out_rows), device=x.device, dtype=torch.bfloat16)
173
+ return up, hidden, out
174
+
175
+
176
+ def _w4_gated(
177
+ x, gate_up_weight, gate_up_sfb, down_weight, down_sfb, *, gelu,
178
+ gate_up_bias=None, down_bias=None, gate_up_alpha=1.0, down_alpha=1.0,
179
+ variant=0, workspace=None, out=None,
180
+ ):
181
+ gate_up, hidden, out = _gated_workspace(
182
+ x, gate_up_weight.shape[0], down_weight.shape[0], workspace, out
183
+ )
184
+ ops.w4a16_gated_ffn_bf16(
185
+ x, gate_up_weight, gate_up_sfb, down_weight, down_sfb,
186
+ gate_up_bias, down_bias, bool(gelu), float(gate_up_alpha),
187
+ float(down_alpha), int(variant), gate_up, hidden, out,
188
+ )
189
+ return out
190
+
191
+
192
+ def w4a16_swiglu_ffn_bf16(x, gate_up_weight, gate_up_sfb, down_weight, down_sfb, **kwargs):
193
+ return _w4_gated(x, gate_up_weight, gate_up_sfb, down_weight, down_sfb, gelu=False, **kwargs)
194
+
195
+
196
+ def w4a16_geglu_ffn_bf16(x, gate_up_weight, gate_up_sfb, down_weight, down_sfb, **kwargs):
197
+ return _w4_gated(x, gate_up_weight, gate_up_sfb, down_weight, down_sfb, gelu=True, **kwargs)
198
+
199
+
200
+ def _w8_gated(
201
+ x, gate_up_weight, gate_up_scale, down_weight, down_scale, *, gelu,
202
+ gate_up_bias=None, down_bias=None, variant=0, workspace=None, out=None,
203
+ ):
204
+ gate_up, hidden, out = _gated_workspace(
205
+ x, gate_up_weight.shape[0], down_weight.shape[0], workspace, out
206
+ )
207
+ ops.w8a16_gated_ffn_bf16(
208
+ x, gate_up_weight, gate_up_scale, down_weight, down_scale,
209
+ gate_up_bias, down_bias, bool(gelu), int(variant), gate_up, hidden, out,
210
+ )
211
+ return out
212
+
213
+
214
+ def w8a16_swiglu_ffn_bf16(x, gate_up_weight, gate_up_scale, down_weight, down_scale, **kwargs):
215
+ return _w8_gated(x, gate_up_weight, gate_up_scale, down_weight, down_scale, gelu=False, **kwargs)
216
+
217
+
218
+ def w8a16_geglu_ffn_bf16(x, gate_up_weight, gate_up_scale, down_weight, down_scale, **kwargs):
219
+ return _w8_gated(x, gate_up_weight, gate_up_scale, down_weight, down_scale, gelu=True, **kwargs)
220
+
221
+
222
+ def w4a16_gelu_ffn_bf16(
223
+ x, up_weight, up_sfb, down_weight, down_sfb, *, up_bias=None,
224
+ down_bias=None, up_alpha=1.0, down_alpha=1.0, variant=0,
225
+ workspace=None, out=None,
226
+ ):
227
+ up, hidden, out = _gelu_workspace(x, up_weight.shape[0], down_weight.shape[0], workspace, out)
228
+ ops.w4a16_gelu_ffn_bf16(
229
+ x, up_weight, up_sfb, down_weight, down_sfb, up_bias, down_bias,
230
+ float(up_alpha), float(down_alpha), int(variant), up, hidden, out,
231
+ )
232
+ return out
233
+
234
+
235
+ def w8a16_gelu_ffn_bf16(
236
+ x, up_weight, up_scale, down_weight, down_scale, *, up_bias=None,
237
+ down_bias=None, variant=0, workspace=None, out=None,
238
+ ):
239
+ up, hidden, out = _gelu_workspace(x, up_weight.shape[0], down_weight.shape[0], workspace, out)
240
+ ops.w8a16_gelu_ffn_bf16(
241
+ x, up_weight, up_scale, down_weight, down_scale, up_bias, down_bias,
242
+ int(variant), up, hidden, out,
243
+ )
244
+ return out
245
+
246
+
247
+ __all__ = [
248
+ "dequantize_w4_weight_bf16",
249
+ "dequantize_w8_weight_bf16",
250
+ "quantize_w4_weight_bf16",
251
+ "quantize_w8_weight_bf16",
252
+ "w4_sfb_size_bytes",
253
+ "w4a16_geglu_ffn_bf16",
254
+ "w4a16_gelu_ffn_bf16",
255
+ "w4a16_linear_bf16",
256
+ "w4a16_swiglu_ffn_bf16",
257
+ "w8a16_geglu_ffn_bf16",
258
+ "w8a16_gelu_ffn_bf16",
259
+ "w8a16_linear_bf16",
260
+ "w8a16_swiglu_ffn_bf16",
261
+ ]
build/torch213-cxx11-cu130-aarch64-linux/_ops.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ import torch
2
+ from . import weight_only_ffn_source_test
3
+ ops = torch.ops.weight_only_ffn_source_test
4
+
5
+ def add_op_namespace_prefix(op_name: str):
6
+ return f"weight_only_ffn_source_test::{op_name}"
build/torch213-cxx11-cu130-aarch64-linux/metadata.json ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "weight-only-ffn",
3
+ "id": "weight_only_ffn_source_test",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "python-depends": [],
7
+ "backend": {
8
+ "type": "cuda",
9
+ "archs": [
10
+ "11.0a"
11
+ ]
12
+ },
13
+ "digest": {
14
+ "algorithm": "sha256",
15
+ "files": {
16
+ "__init__.py": "0q6TMj65una1LdyHLC291Dwy2VzrBbAD8Wm8MEdmqwA=",
17
+ "weight_only_ffn_source_test.abi3.so": "4bTmrdGoTStyhYzGGZQbYZmrMo/Hbq9EVXN9i//6Q3Q=",
18
+ "_ops.py": "sIcm8AtiXP5iJMFaDNUMdBLwzQULjS0N7fOxWIFF1DQ=",
19
+ "weight_only_ffn/__init__.py": "v6p5XMfQzddhi1fLSAw4HX9CyS0rQsidvu9VsT01xi4="
20
+ }
21
+ },
22
+ "provenance": {
23
+ "kernel": {
24
+ "sha": "456d297",
25
+ "dirty": false
26
+ },
27
+ "validation": {
28
+ "torch": "2.13.0+cu130",
29
+ "cuda": "13.0"
30
+ }
31
+ }
32
+ }
build/torch213-cxx11-cu130-aarch64-linux/weight_only_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/torch213-cxx11-cu130-aarch64-linux/weight_only_ffn_source_test.abi3.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e1b4e6add1a84d2b72858cc619941b6199ab328fc76eaf4455737d8bfffa4374
3
+ size 825064