Uploaded using `kernel-builder`.
Browse files- benchmarks/benchmark.py +120 -0
- build/torch211-cxx11-cu128-x86_64-linux/__init__.py +271 -0
- build/torch211-cxx11-cu128-x86_64-linux/_adaptive_layernorm_producers_cuda_1798e7f.abi3.so +3 -0
- build/torch211-cxx11-cu128-x86_64-linux/_ops.py +9 -0
- build/torch211-cxx11-cu128-x86_64-linux/adaptive_layernorm_producers/__init__.py +26 -0
- build/torch211-cxx11-cu128-x86_64-linux/metadata.json +23 -0
- build/torch211-cxx11-cu130-x86_64-linux/__init__.py +271 -0
- build/torch211-cxx11-cu130-x86_64-linux/_adaptive_layernorm_producers_cuda_1798e7f.abi3.so +3 -0
- build/torch211-cxx11-cu130-x86_64-linux/_ops.py +9 -0
- build/torch211-cxx11-cu130-x86_64-linux/adaptive_layernorm_producers/__init__.py +26 -0
- build/torch211-cxx11-cu130-x86_64-linux/metadata.json +22 -0
- build/torch212-cxx11-cu130-x86_64-linux/__init__.py +271 -0
- build/torch212-cxx11-cu130-x86_64-linux/_adaptive_layernorm_producers_cuda_1798e7f.abi3.so +3 -0
- build/torch212-cxx11-cu130-x86_64-linux/_ops.py +9 -0
- build/torch212-cxx11-cu130-x86_64-linux/adaptive_layernorm_producers/__init__.py +26 -0
- build/torch212-cxx11-cu130-x86_64-linux/metadata.json +22 -0
- build/torch212-cxx11-cu132-x86_64-linux/__init__.py +271 -0
- build/torch212-cxx11-cu132-x86_64-linux/_adaptive_layernorm_producers_cuda_1798e7f.abi3.so +3 -0
- build/torch212-cxx11-cu132-x86_64-linux/_ops.py +9 -0
- build/torch212-cxx11-cu132-x86_64-linux/adaptive_layernorm_producers/__init__.py +26 -0
- build/torch212-cxx11-cu132-x86_64-linux/metadata.json +22 -0
benchmarks/benchmark.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Benchmark adaptive-layernorm-producers against eager producer chains."""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import argparse
|
| 7 |
+
import importlib
|
| 8 |
+
import sys
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
|
| 11 |
+
import torch
|
| 12 |
+
|
| 13 |
+
ROOT = Path(__file__).resolve().parents[2]
|
| 14 |
+
sys.path.insert(0, str(ROOT / "adaptive-layernorm-producers" / "tests"))
|
| 15 |
+
from test_adaptive_layernorm_producers import ( # noqa: E402
|
| 16 |
+
load_source_ops,
|
| 17 |
+
make_case,
|
| 18 |
+
quant_fp8,
|
| 19 |
+
ref_adaln,
|
| 20 |
+
ref_layer_norm_no_affine,
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def load_installed_ops(artifact: str | None):
|
| 25 |
+
if artifact:
|
| 26 |
+
sys.path.insert(0, artifact)
|
| 27 |
+
try:
|
| 28 |
+
return importlib.import_module("adaptive_layernorm_producers")
|
| 29 |
+
finally:
|
| 30 |
+
if artifact:
|
| 31 |
+
sys.path.remove(artifact)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def time_cuda(fn, iters: int = 200, warmup: int = 50) -> float:
|
| 35 |
+
for _ in range(warmup):
|
| 36 |
+
fn()
|
| 37 |
+
torch.cuda.synchronize()
|
| 38 |
+
start = torch.cuda.Event(enable_timing=True)
|
| 39 |
+
end = torch.cuda.Event(enable_timing=True)
|
| 40 |
+
start.record()
|
| 41 |
+
for _ in range(iters):
|
| 42 |
+
fn()
|
| 43 |
+
end.record()
|
| 44 |
+
torch.cuda.synchronize()
|
| 45 |
+
return start.elapsed_time(end) * 1000.0 / iters
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def run_case(ops, name: str, rows: int, dim: int, eps: float, iters: int) -> dict[str, float | str | int]:
|
| 49 |
+
x, scale, shift, _inv_s, act_scale, _scale_fp8, _shift_fp8, _scale_deq, _shift_deq = make_case(rows, dim)
|
| 50 |
+
out = torch.empty_like(x, dtype=torch.float8_e4m3fn)
|
| 51 |
+
|
| 52 |
+
def fused():
|
| 53 |
+
ops.ada_layer_norm_quant_fp8_bf16(x, scale, shift, act_scale, eps, out=out)
|
| 54 |
+
|
| 55 |
+
def eager():
|
| 56 |
+
quant_fp8(ref_adaln(x, scale, shift, eps), act_scale)
|
| 57 |
+
|
| 58 |
+
no_affine_out = torch.empty_like(x, dtype=torch.float8_e4m3fn)
|
| 59 |
+
|
| 60 |
+
def fused_no_affine():
|
| 61 |
+
ops.layer_norm_no_affine_quant_fp8_static_bf16(x, act_scale, eps, out=no_affine_out)
|
| 62 |
+
|
| 63 |
+
def eager_no_affine():
|
| 64 |
+
quant_fp8(ref_layer_norm_no_affine(x, eps), act_scale)
|
| 65 |
+
|
| 66 |
+
fused_us = time_cuda(fused, iters=iters)
|
| 67 |
+
eager_us = time_cuda(eager, iters=iters)
|
| 68 |
+
fused_no_affine_us = time_cuda(fused_no_affine, iters=iters)
|
| 69 |
+
eager_no_affine_us = time_cuda(eager_no_affine, iters=iters)
|
| 70 |
+
return {
|
| 71 |
+
"shape": name,
|
| 72 |
+
"rows": rows,
|
| 73 |
+
"dim": dim,
|
| 74 |
+
"ada_fp8_us": fused_us,
|
| 75 |
+
"ada_eager_us": eager_us,
|
| 76 |
+
"ada_speedup": eager_us / fused_us,
|
| 77 |
+
"no_affine_fp8_us": fused_no_affine_us,
|
| 78 |
+
"no_affine_eager_us": eager_no_affine_us,
|
| 79 |
+
"no_affine_speedup": eager_no_affine_us / fused_no_affine_us,
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def main() -> None:
|
| 84 |
+
parser = argparse.ArgumentParser()
|
| 85 |
+
parser.add_argument("--backend", choices=["source", "installed"], default="source")
|
| 86 |
+
parser.add_argument("--artifact", default=None)
|
| 87 |
+
parser.add_argument("--iters", type=int, default=200)
|
| 88 |
+
parser.add_argument("--markdown", default=None)
|
| 89 |
+
args = parser.parse_args()
|
| 90 |
+
|
| 91 |
+
if not torch.cuda.is_available():
|
| 92 |
+
raise SystemExit("CUDA is required")
|
| 93 |
+
torch.manual_seed(2026)
|
| 94 |
+
ops = load_source_ops() if args.backend == "source" else load_installed_ops(args.artifact)
|
| 95 |
+
shapes = [
|
| 96 |
+
("decode_action", 16, 2048),
|
| 97 |
+
("wan_video_short", 64, 3072),
|
| 98 |
+
("wan_video_ctx", 256, 3072),
|
| 99 |
+
("wan_video_2k", 2520, 3072),
|
| 100 |
+
("wan_video_4k", 4096, 3072),
|
| 101 |
+
]
|
| 102 |
+
rows = [run_case(ops, name, r, d, 1e-5, args.iters) for name, r, d in shapes]
|
| 103 |
+
lines = [
|
| 104 |
+
"| Shape | Rows | Dim | AdaLN->FP8 us | Eager chain us | Speedup | LN->FP8 us | Eager LN chain us | Speedup |",
|
| 105 |
+
"|---|---:|---:|---:|---:|---:|---:|---:|---:|",
|
| 106 |
+
]
|
| 107 |
+
for row in rows:
|
| 108 |
+
line = (
|
| 109 |
+
f"| {row['shape']} | {row['rows']} | {row['dim']} | "
|
| 110 |
+
f"{row['ada_fp8_us']:.3f} | {row['ada_eager_us']:.3f} | {row['ada_speedup']:.2f}x | "
|
| 111 |
+
f"{row['no_affine_fp8_us']:.3f} | {row['no_affine_eager_us']:.3f} | {row['no_affine_speedup']:.2f}x |"
|
| 112 |
+
)
|
| 113 |
+
lines.append(line)
|
| 114 |
+
print(line)
|
| 115 |
+
if args.markdown:
|
| 116 |
+
Path(args.markdown).write_text("\n".join(lines) + "\n", encoding="utf-8")
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
if __name__ == "__main__":
|
| 120 |
+
main()
|
build/torch211-cxx11-cu128-x86_64-linux/__init__.py
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""FlashRT adaptive LayerNorm producer kernels for DiT/Wan-style blocks."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
from ._ops import add_op_namespace_prefix, ops
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _check_x(x: torch.Tensor) -> None:
|
| 11 |
+
if x.dim() != 2:
|
| 12 |
+
raise RuntimeError("x must have shape (rows, dim)")
|
| 13 |
+
if x.shape[1] % 2 != 0:
|
| 14 |
+
raise RuntimeError("x.shape[1] must be even")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def _check_mod(x: torch.Tensor, scale: torch.Tensor, shift: torch.Tensor) -> None:
|
| 18 |
+
_check_x(x)
|
| 19 |
+
dim = x.shape[1]
|
| 20 |
+
if scale.shape != (dim,) or shift.shape != (dim,):
|
| 21 |
+
raise RuntimeError("scale and shift must have shape (dim,)")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _check_scalar(scale: torch.Tensor, name: str) -> None:
|
| 25 |
+
if scale.numel() != 1:
|
| 26 |
+
raise RuntimeError(f"{name} must be a scalar tensor")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def swizzled_sf_size(rows: int, dim: int) -> int:
|
| 30 |
+
"""Return bytes required by the FlashRT/CUTLASS NVFP4 swizzled scale layout."""
|
| 31 |
+
|
| 32 |
+
if dim % 16 != 0:
|
| 33 |
+
raise RuntimeError("dim must be divisible by 16 for NVFP4 swizzled output")
|
| 34 |
+
n_blocks = dim // 16
|
| 35 |
+
n_row_super = (rows + 127) // 128
|
| 36 |
+
n_col_super = (n_blocks + 3) // 4
|
| 37 |
+
return n_row_super * n_col_super * 128 * 64
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def _alloc_fp8(x: torch.Tensor) -> torch.Tensor:
|
| 41 |
+
return torch.empty_like(x, dtype=torch.float8_e4m3fn)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def _alloc_nvfp4(x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
|
| 45 |
+
rows, dim = x.shape
|
| 46 |
+
packed = torch.empty((rows, dim // 2), device=x.device, dtype=torch.uint8)
|
| 47 |
+
sf = torch.zeros((swizzled_sf_size(rows, dim),), device=x.device, dtype=torch.uint8)
|
| 48 |
+
return packed, sf
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@torch.library.register_fake(add_op_namespace_prefix("ada_layer_norm_quant_fp8_bf16"))
|
| 52 |
+
def _ada_layer_norm_quant_fp8_bf16_fake(
|
| 53 |
+
x: torch.Tensor,
|
| 54 |
+
scale: torch.Tensor,
|
| 55 |
+
shift: torch.Tensor,
|
| 56 |
+
act_scale: torch.Tensor,
|
| 57 |
+
eps: float,
|
| 58 |
+
out: torch.Tensor,
|
| 59 |
+
) -> None:
|
| 60 |
+
_check_mod(x, scale, shift)
|
| 61 |
+
_check_scalar(act_scale, "act_scale")
|
| 62 |
+
if out.shape != x.shape:
|
| 63 |
+
raise RuntimeError("out must have the same shape as x")
|
| 64 |
+
return None
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
@torch.library.register_fake(add_op_namespace_prefix("ada_layer_norm_quant_fp8_modfp8_bf16"))
|
| 68 |
+
def _ada_layer_norm_quant_fp8_modfp8_bf16_fake(
|
| 69 |
+
x: torch.Tensor,
|
| 70 |
+
scale_fp8: torch.Tensor,
|
| 71 |
+
shift_fp8: torch.Tensor,
|
| 72 |
+
scale_deq: torch.Tensor,
|
| 73 |
+
shift_deq: torch.Tensor,
|
| 74 |
+
act_scale: torch.Tensor,
|
| 75 |
+
eps: float,
|
| 76 |
+
out: torch.Tensor,
|
| 77 |
+
) -> None:
|
| 78 |
+
_check_mod(x, scale_fp8, shift_fp8)
|
| 79 |
+
_check_scalar(scale_deq, "scale_deq")
|
| 80 |
+
_check_scalar(shift_deq, "shift_deq")
|
| 81 |
+
_check_scalar(act_scale, "act_scale")
|
| 82 |
+
if out.shape != x.shape:
|
| 83 |
+
raise RuntimeError("out must have the same shape as x")
|
| 84 |
+
return None
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@torch.library.register_fake(add_op_namespace_prefix("awq_ada_layer_norm_quant_fp8_bf16"))
|
| 88 |
+
def _awq_ada_layer_norm_quant_fp8_bf16_fake(
|
| 89 |
+
x: torch.Tensor,
|
| 90 |
+
scale: torch.Tensor,
|
| 91 |
+
shift: torch.Tensor,
|
| 92 |
+
inv_s: torch.Tensor,
|
| 93 |
+
act_scale: torch.Tensor,
|
| 94 |
+
eps: float,
|
| 95 |
+
out: torch.Tensor,
|
| 96 |
+
) -> None:
|
| 97 |
+
_check_mod(x, scale, shift)
|
| 98 |
+
if inv_s.shape != (x.shape[1],):
|
| 99 |
+
raise RuntimeError("inv_s must have shape (dim,)")
|
| 100 |
+
_check_scalar(act_scale, "act_scale")
|
| 101 |
+
if out.shape != x.shape:
|
| 102 |
+
raise RuntimeError("out must have the same shape as x")
|
| 103 |
+
return None
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
@torch.library.register_fake(add_op_namespace_prefix("ada_layer_norm_quant_nvfp4_swizzled_bf16"))
|
| 107 |
+
def _ada_layer_norm_quant_nvfp4_swizzled_bf16_fake(
|
| 108 |
+
x: torch.Tensor,
|
| 109 |
+
scale: torch.Tensor,
|
| 110 |
+
shift: torch.Tensor,
|
| 111 |
+
eps: float,
|
| 112 |
+
packed: torch.Tensor,
|
| 113 |
+
sf_swizzled: torch.Tensor,
|
| 114 |
+
) -> None:
|
| 115 |
+
_check_mod(x, scale, shift)
|
| 116 |
+
rows, dim = x.shape
|
| 117 |
+
if packed.shape != (rows, dim // 2):
|
| 118 |
+
raise RuntimeError("packed must have shape (rows, dim // 2)")
|
| 119 |
+
if sf_swizzled.numel() < swizzled_sf_size(rows, dim):
|
| 120 |
+
raise RuntimeError("sf_swizzled is too small")
|
| 121 |
+
return None
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
@torch.library.register_fake(add_op_namespace_prefix("ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16"))
|
| 125 |
+
def _ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16_fake(
|
| 126 |
+
x: torch.Tensor,
|
| 127 |
+
scale_fp8: torch.Tensor,
|
| 128 |
+
shift_fp8: torch.Tensor,
|
| 129 |
+
scale_deq: torch.Tensor,
|
| 130 |
+
shift_deq: torch.Tensor,
|
| 131 |
+
eps: float,
|
| 132 |
+
packed: torch.Tensor,
|
| 133 |
+
sf_swizzled: torch.Tensor,
|
| 134 |
+
) -> None:
|
| 135 |
+
_check_mod(x, scale_fp8, shift_fp8)
|
| 136 |
+
_check_scalar(scale_deq, "scale_deq")
|
| 137 |
+
_check_scalar(shift_deq, "shift_deq")
|
| 138 |
+
rows, dim = x.shape
|
| 139 |
+
if packed.shape != (rows, dim // 2):
|
| 140 |
+
raise RuntimeError("packed must have shape (rows, dim // 2)")
|
| 141 |
+
if sf_swizzled.numel() < swizzled_sf_size(rows, dim):
|
| 142 |
+
raise RuntimeError("sf_swizzled is too small")
|
| 143 |
+
return None
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
@torch.library.register_fake(add_op_namespace_prefix("layer_norm_no_affine_quant_fp8_static_bf16"))
|
| 147 |
+
def _layer_norm_no_affine_quant_fp8_static_bf16_fake(
|
| 148 |
+
x: torch.Tensor,
|
| 149 |
+
act_scale: torch.Tensor,
|
| 150 |
+
eps: float,
|
| 151 |
+
out: torch.Tensor,
|
| 152 |
+
) -> None:
|
| 153 |
+
_check_x(x)
|
| 154 |
+
_check_scalar(act_scale, "act_scale")
|
| 155 |
+
if out.shape != x.shape:
|
| 156 |
+
raise RuntimeError("out must have the same shape as x")
|
| 157 |
+
return None
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def ada_layer_norm_quant_fp8_bf16(
|
| 161 |
+
x: torch.Tensor,
|
| 162 |
+
scale: torch.Tensor,
|
| 163 |
+
shift: torch.Tensor,
|
| 164 |
+
act_scale: torch.Tensor,
|
| 165 |
+
eps: float = 1e-5,
|
| 166 |
+
out: torch.Tensor | None = None,
|
| 167 |
+
) -> torch.Tensor:
|
| 168 |
+
"""Fused no-affine LayerNorm + AdaLN scale/shift + static FP8 quantize."""
|
| 169 |
+
|
| 170 |
+
if out is None:
|
| 171 |
+
out = _alloc_fp8(x)
|
| 172 |
+
ops.ada_layer_norm_quant_fp8_bf16(x, scale, shift, act_scale, float(eps), out)
|
| 173 |
+
return out
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def ada_layer_norm_quant_fp8_modfp8_bf16(
|
| 177 |
+
x: torch.Tensor,
|
| 178 |
+
scale_fp8: torch.Tensor,
|
| 179 |
+
shift_fp8: torch.Tensor,
|
| 180 |
+
scale_deq: torch.Tensor,
|
| 181 |
+
shift_deq: torch.Tensor,
|
| 182 |
+
act_scale: torch.Tensor,
|
| 183 |
+
eps: float = 1e-5,
|
| 184 |
+
out: torch.Tensor | None = None,
|
| 185 |
+
) -> torch.Tensor:
|
| 186 |
+
"""Fused AdaLN producer when modulation vectors are stored in FP8."""
|
| 187 |
+
|
| 188 |
+
if out is None:
|
| 189 |
+
out = _alloc_fp8(x)
|
| 190 |
+
ops.ada_layer_norm_quant_fp8_modfp8_bf16(
|
| 191 |
+
x, scale_fp8, shift_fp8, scale_deq, shift_deq, act_scale, float(eps), out
|
| 192 |
+
)
|
| 193 |
+
return out
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
def awq_ada_layer_norm_quant_fp8_bf16(
|
| 197 |
+
x: torch.Tensor,
|
| 198 |
+
scale: torch.Tensor,
|
| 199 |
+
shift: torch.Tensor,
|
| 200 |
+
inv_s: torch.Tensor,
|
| 201 |
+
act_scale: torch.Tensor,
|
| 202 |
+
eps: float = 1e-5,
|
| 203 |
+
out: torch.Tensor | None = None,
|
| 204 |
+
) -> torch.Tensor:
|
| 205 |
+
"""Fused AdaLN producer with AWQ/SmoothQuant per-channel activation scale."""
|
| 206 |
+
|
| 207 |
+
if out is None:
|
| 208 |
+
out = _alloc_fp8(x)
|
| 209 |
+
ops.awq_ada_layer_norm_quant_fp8_bf16(x, scale, shift, inv_s, act_scale, float(eps), out)
|
| 210 |
+
return out
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def ada_layer_norm_quant_nvfp4_swizzled_bf16(
|
| 214 |
+
x: torch.Tensor,
|
| 215 |
+
scale: torch.Tensor,
|
| 216 |
+
shift: torch.Tensor,
|
| 217 |
+
eps: float = 1e-5,
|
| 218 |
+
packed: torch.Tensor | None = None,
|
| 219 |
+
sf_swizzled: torch.Tensor | None = None,
|
| 220 |
+
) -> tuple[torch.Tensor, torch.Tensor]:
|
| 221 |
+
"""Fused AdaLN producer to NVFP4 packed activations and swizzled scale factors."""
|
| 222 |
+
|
| 223 |
+
if packed is None or sf_swizzled is None:
|
| 224 |
+
packed, sf_swizzled = _alloc_nvfp4(x)
|
| 225 |
+
ops.ada_layer_norm_quant_nvfp4_swizzled_bf16(x, scale, shift, float(eps), packed, sf_swizzled)
|
| 226 |
+
return packed, sf_swizzled
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16(
|
| 230 |
+
x: torch.Tensor,
|
| 231 |
+
scale_fp8: torch.Tensor,
|
| 232 |
+
shift_fp8: torch.Tensor,
|
| 233 |
+
scale_deq: torch.Tensor,
|
| 234 |
+
shift_deq: torch.Tensor,
|
| 235 |
+
eps: float = 1e-5,
|
| 236 |
+
packed: torch.Tensor | None = None,
|
| 237 |
+
sf_swizzled: torch.Tensor | None = None,
|
| 238 |
+
) -> tuple[torch.Tensor, torch.Tensor]:
|
| 239 |
+
"""Fused FP8-modulated AdaLN producer to NVFP4 packed activations."""
|
| 240 |
+
|
| 241 |
+
if packed is None or sf_swizzled is None:
|
| 242 |
+
packed, sf_swizzled = _alloc_nvfp4(x)
|
| 243 |
+
ops.ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16(
|
| 244 |
+
x, scale_fp8, shift_fp8, scale_deq, shift_deq, float(eps), packed, sf_swizzled
|
| 245 |
+
)
|
| 246 |
+
return packed, sf_swizzled
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
def layer_norm_no_affine_quant_fp8_static_bf16(
|
| 250 |
+
x: torch.Tensor,
|
| 251 |
+
act_scale: torch.Tensor,
|
| 252 |
+
eps: float = 1e-5,
|
| 253 |
+
out: torch.Tensor | None = None,
|
| 254 |
+
) -> torch.Tensor:
|
| 255 |
+
"""Fused no-affine LayerNorm + static FP8 quantize."""
|
| 256 |
+
|
| 257 |
+
if out is None:
|
| 258 |
+
out = _alloc_fp8(x)
|
| 259 |
+
ops.layer_norm_no_affine_quant_fp8_static_bf16(x, act_scale, float(eps), out)
|
| 260 |
+
return out
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
__all__ = [
|
| 264 |
+
"ada_layer_norm_quant_fp8_bf16",
|
| 265 |
+
"ada_layer_norm_quant_fp8_modfp8_bf16",
|
| 266 |
+
"awq_ada_layer_norm_quant_fp8_bf16",
|
| 267 |
+
"ada_layer_norm_quant_nvfp4_swizzled_bf16",
|
| 268 |
+
"ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16",
|
| 269 |
+
"layer_norm_no_affine_quant_fp8_static_bf16",
|
| 270 |
+
"swizzled_sf_size",
|
| 271 |
+
]
|
build/torch211-cxx11-cu128-x86_64-linux/_adaptive_layernorm_producers_cuda_1798e7f.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a5e1f13479337ecabb51d7dcf8570714af33478d3df6b3394a43f0b560783ff0
|
| 3 |
+
size 1379872
|
build/torch211-cxx11-cu128-x86_64-linux/_ops.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from . import _adaptive_layernorm_producers_cuda_1798e7f
|
| 3 |
+
ops = torch.ops._adaptive_layernorm_producers_cuda_1798e7f
|
| 4 |
+
|
| 5 |
+
def add_op_namespace_prefix(op_name: str):
|
| 6 |
+
"""
|
| 7 |
+
Prefix op by namespace.
|
| 8 |
+
"""
|
| 9 |
+
return f"_adaptive_layernorm_producers_cuda_1798e7f::{op_name}"
|
build/torch211-cxx11-cu128-x86_64-linux/adaptive_layernorm_producers/__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,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "adaptive-layernorm-producers",
|
| 3 |
+
"id": "_adaptive_layernorm_producers_cuda_1798e7f",
|
| 4 |
+
"version": 1,
|
| 5 |
+
"license": "Apache-2.0",
|
| 6 |
+
"python-depends": [],
|
| 7 |
+
"backend": {
|
| 8 |
+
"type": "cuda",
|
| 9 |
+
"archs": [
|
| 10 |
+
"10.0",
|
| 11 |
+
"10.1",
|
| 12 |
+
"12.0+PTX",
|
| 13 |
+
"7.0",
|
| 14 |
+
"7.2",
|
| 15 |
+
"7.5",
|
| 16 |
+
"8.0",
|
| 17 |
+
"8.6",
|
| 18 |
+
"8.7",
|
| 19 |
+
"8.9",
|
| 20 |
+
"9.0"
|
| 21 |
+
]
|
| 22 |
+
}
|
| 23 |
+
}
|
build/torch211-cxx11-cu130-x86_64-linux/__init__.py
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""FlashRT adaptive LayerNorm producer kernels for DiT/Wan-style blocks."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
from ._ops import add_op_namespace_prefix, ops
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _check_x(x: torch.Tensor) -> None:
|
| 11 |
+
if x.dim() != 2:
|
| 12 |
+
raise RuntimeError("x must have shape (rows, dim)")
|
| 13 |
+
if x.shape[1] % 2 != 0:
|
| 14 |
+
raise RuntimeError("x.shape[1] must be even")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def _check_mod(x: torch.Tensor, scale: torch.Tensor, shift: torch.Tensor) -> None:
|
| 18 |
+
_check_x(x)
|
| 19 |
+
dim = x.shape[1]
|
| 20 |
+
if scale.shape != (dim,) or shift.shape != (dim,):
|
| 21 |
+
raise RuntimeError("scale and shift must have shape (dim,)")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _check_scalar(scale: torch.Tensor, name: str) -> None:
|
| 25 |
+
if scale.numel() != 1:
|
| 26 |
+
raise RuntimeError(f"{name} must be a scalar tensor")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def swizzled_sf_size(rows: int, dim: int) -> int:
|
| 30 |
+
"""Return bytes required by the FlashRT/CUTLASS NVFP4 swizzled scale layout."""
|
| 31 |
+
|
| 32 |
+
if dim % 16 != 0:
|
| 33 |
+
raise RuntimeError("dim must be divisible by 16 for NVFP4 swizzled output")
|
| 34 |
+
n_blocks = dim // 16
|
| 35 |
+
n_row_super = (rows + 127) // 128
|
| 36 |
+
n_col_super = (n_blocks + 3) // 4
|
| 37 |
+
return n_row_super * n_col_super * 128 * 64
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def _alloc_fp8(x: torch.Tensor) -> torch.Tensor:
|
| 41 |
+
return torch.empty_like(x, dtype=torch.float8_e4m3fn)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def _alloc_nvfp4(x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
|
| 45 |
+
rows, dim = x.shape
|
| 46 |
+
packed = torch.empty((rows, dim // 2), device=x.device, dtype=torch.uint8)
|
| 47 |
+
sf = torch.zeros((swizzled_sf_size(rows, dim),), device=x.device, dtype=torch.uint8)
|
| 48 |
+
return packed, sf
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@torch.library.register_fake(add_op_namespace_prefix("ada_layer_norm_quant_fp8_bf16"))
|
| 52 |
+
def _ada_layer_norm_quant_fp8_bf16_fake(
|
| 53 |
+
x: torch.Tensor,
|
| 54 |
+
scale: torch.Tensor,
|
| 55 |
+
shift: torch.Tensor,
|
| 56 |
+
act_scale: torch.Tensor,
|
| 57 |
+
eps: float,
|
| 58 |
+
out: torch.Tensor,
|
| 59 |
+
) -> None:
|
| 60 |
+
_check_mod(x, scale, shift)
|
| 61 |
+
_check_scalar(act_scale, "act_scale")
|
| 62 |
+
if out.shape != x.shape:
|
| 63 |
+
raise RuntimeError("out must have the same shape as x")
|
| 64 |
+
return None
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
@torch.library.register_fake(add_op_namespace_prefix("ada_layer_norm_quant_fp8_modfp8_bf16"))
|
| 68 |
+
def _ada_layer_norm_quant_fp8_modfp8_bf16_fake(
|
| 69 |
+
x: torch.Tensor,
|
| 70 |
+
scale_fp8: torch.Tensor,
|
| 71 |
+
shift_fp8: torch.Tensor,
|
| 72 |
+
scale_deq: torch.Tensor,
|
| 73 |
+
shift_deq: torch.Tensor,
|
| 74 |
+
act_scale: torch.Tensor,
|
| 75 |
+
eps: float,
|
| 76 |
+
out: torch.Tensor,
|
| 77 |
+
) -> None:
|
| 78 |
+
_check_mod(x, scale_fp8, shift_fp8)
|
| 79 |
+
_check_scalar(scale_deq, "scale_deq")
|
| 80 |
+
_check_scalar(shift_deq, "shift_deq")
|
| 81 |
+
_check_scalar(act_scale, "act_scale")
|
| 82 |
+
if out.shape != x.shape:
|
| 83 |
+
raise RuntimeError("out must have the same shape as x")
|
| 84 |
+
return None
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@torch.library.register_fake(add_op_namespace_prefix("awq_ada_layer_norm_quant_fp8_bf16"))
|
| 88 |
+
def _awq_ada_layer_norm_quant_fp8_bf16_fake(
|
| 89 |
+
x: torch.Tensor,
|
| 90 |
+
scale: torch.Tensor,
|
| 91 |
+
shift: torch.Tensor,
|
| 92 |
+
inv_s: torch.Tensor,
|
| 93 |
+
act_scale: torch.Tensor,
|
| 94 |
+
eps: float,
|
| 95 |
+
out: torch.Tensor,
|
| 96 |
+
) -> None:
|
| 97 |
+
_check_mod(x, scale, shift)
|
| 98 |
+
if inv_s.shape != (x.shape[1],):
|
| 99 |
+
raise RuntimeError("inv_s must have shape (dim,)")
|
| 100 |
+
_check_scalar(act_scale, "act_scale")
|
| 101 |
+
if out.shape != x.shape:
|
| 102 |
+
raise RuntimeError("out must have the same shape as x")
|
| 103 |
+
return None
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
@torch.library.register_fake(add_op_namespace_prefix("ada_layer_norm_quant_nvfp4_swizzled_bf16"))
|
| 107 |
+
def _ada_layer_norm_quant_nvfp4_swizzled_bf16_fake(
|
| 108 |
+
x: torch.Tensor,
|
| 109 |
+
scale: torch.Tensor,
|
| 110 |
+
shift: torch.Tensor,
|
| 111 |
+
eps: float,
|
| 112 |
+
packed: torch.Tensor,
|
| 113 |
+
sf_swizzled: torch.Tensor,
|
| 114 |
+
) -> None:
|
| 115 |
+
_check_mod(x, scale, shift)
|
| 116 |
+
rows, dim = x.shape
|
| 117 |
+
if packed.shape != (rows, dim // 2):
|
| 118 |
+
raise RuntimeError("packed must have shape (rows, dim // 2)")
|
| 119 |
+
if sf_swizzled.numel() < swizzled_sf_size(rows, dim):
|
| 120 |
+
raise RuntimeError("sf_swizzled is too small")
|
| 121 |
+
return None
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
@torch.library.register_fake(add_op_namespace_prefix("ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16"))
|
| 125 |
+
def _ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16_fake(
|
| 126 |
+
x: torch.Tensor,
|
| 127 |
+
scale_fp8: torch.Tensor,
|
| 128 |
+
shift_fp8: torch.Tensor,
|
| 129 |
+
scale_deq: torch.Tensor,
|
| 130 |
+
shift_deq: torch.Tensor,
|
| 131 |
+
eps: float,
|
| 132 |
+
packed: torch.Tensor,
|
| 133 |
+
sf_swizzled: torch.Tensor,
|
| 134 |
+
) -> None:
|
| 135 |
+
_check_mod(x, scale_fp8, shift_fp8)
|
| 136 |
+
_check_scalar(scale_deq, "scale_deq")
|
| 137 |
+
_check_scalar(shift_deq, "shift_deq")
|
| 138 |
+
rows, dim = x.shape
|
| 139 |
+
if packed.shape != (rows, dim // 2):
|
| 140 |
+
raise RuntimeError("packed must have shape (rows, dim // 2)")
|
| 141 |
+
if sf_swizzled.numel() < swizzled_sf_size(rows, dim):
|
| 142 |
+
raise RuntimeError("sf_swizzled is too small")
|
| 143 |
+
return None
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
@torch.library.register_fake(add_op_namespace_prefix("layer_norm_no_affine_quant_fp8_static_bf16"))
|
| 147 |
+
def _layer_norm_no_affine_quant_fp8_static_bf16_fake(
|
| 148 |
+
x: torch.Tensor,
|
| 149 |
+
act_scale: torch.Tensor,
|
| 150 |
+
eps: float,
|
| 151 |
+
out: torch.Tensor,
|
| 152 |
+
) -> None:
|
| 153 |
+
_check_x(x)
|
| 154 |
+
_check_scalar(act_scale, "act_scale")
|
| 155 |
+
if out.shape != x.shape:
|
| 156 |
+
raise RuntimeError("out must have the same shape as x")
|
| 157 |
+
return None
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def ada_layer_norm_quant_fp8_bf16(
|
| 161 |
+
x: torch.Tensor,
|
| 162 |
+
scale: torch.Tensor,
|
| 163 |
+
shift: torch.Tensor,
|
| 164 |
+
act_scale: torch.Tensor,
|
| 165 |
+
eps: float = 1e-5,
|
| 166 |
+
out: torch.Tensor | None = None,
|
| 167 |
+
) -> torch.Tensor:
|
| 168 |
+
"""Fused no-affine LayerNorm + AdaLN scale/shift + static FP8 quantize."""
|
| 169 |
+
|
| 170 |
+
if out is None:
|
| 171 |
+
out = _alloc_fp8(x)
|
| 172 |
+
ops.ada_layer_norm_quant_fp8_bf16(x, scale, shift, act_scale, float(eps), out)
|
| 173 |
+
return out
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def ada_layer_norm_quant_fp8_modfp8_bf16(
|
| 177 |
+
x: torch.Tensor,
|
| 178 |
+
scale_fp8: torch.Tensor,
|
| 179 |
+
shift_fp8: torch.Tensor,
|
| 180 |
+
scale_deq: torch.Tensor,
|
| 181 |
+
shift_deq: torch.Tensor,
|
| 182 |
+
act_scale: torch.Tensor,
|
| 183 |
+
eps: float = 1e-5,
|
| 184 |
+
out: torch.Tensor | None = None,
|
| 185 |
+
) -> torch.Tensor:
|
| 186 |
+
"""Fused AdaLN producer when modulation vectors are stored in FP8."""
|
| 187 |
+
|
| 188 |
+
if out is None:
|
| 189 |
+
out = _alloc_fp8(x)
|
| 190 |
+
ops.ada_layer_norm_quant_fp8_modfp8_bf16(
|
| 191 |
+
x, scale_fp8, shift_fp8, scale_deq, shift_deq, act_scale, float(eps), out
|
| 192 |
+
)
|
| 193 |
+
return out
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
def awq_ada_layer_norm_quant_fp8_bf16(
|
| 197 |
+
x: torch.Tensor,
|
| 198 |
+
scale: torch.Tensor,
|
| 199 |
+
shift: torch.Tensor,
|
| 200 |
+
inv_s: torch.Tensor,
|
| 201 |
+
act_scale: torch.Tensor,
|
| 202 |
+
eps: float = 1e-5,
|
| 203 |
+
out: torch.Tensor | None = None,
|
| 204 |
+
) -> torch.Tensor:
|
| 205 |
+
"""Fused AdaLN producer with AWQ/SmoothQuant per-channel activation scale."""
|
| 206 |
+
|
| 207 |
+
if out is None:
|
| 208 |
+
out = _alloc_fp8(x)
|
| 209 |
+
ops.awq_ada_layer_norm_quant_fp8_bf16(x, scale, shift, inv_s, act_scale, float(eps), out)
|
| 210 |
+
return out
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def ada_layer_norm_quant_nvfp4_swizzled_bf16(
|
| 214 |
+
x: torch.Tensor,
|
| 215 |
+
scale: torch.Tensor,
|
| 216 |
+
shift: torch.Tensor,
|
| 217 |
+
eps: float = 1e-5,
|
| 218 |
+
packed: torch.Tensor | None = None,
|
| 219 |
+
sf_swizzled: torch.Tensor | None = None,
|
| 220 |
+
) -> tuple[torch.Tensor, torch.Tensor]:
|
| 221 |
+
"""Fused AdaLN producer to NVFP4 packed activations and swizzled scale factors."""
|
| 222 |
+
|
| 223 |
+
if packed is None or sf_swizzled is None:
|
| 224 |
+
packed, sf_swizzled = _alloc_nvfp4(x)
|
| 225 |
+
ops.ada_layer_norm_quant_nvfp4_swizzled_bf16(x, scale, shift, float(eps), packed, sf_swizzled)
|
| 226 |
+
return packed, sf_swizzled
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16(
|
| 230 |
+
x: torch.Tensor,
|
| 231 |
+
scale_fp8: torch.Tensor,
|
| 232 |
+
shift_fp8: torch.Tensor,
|
| 233 |
+
scale_deq: torch.Tensor,
|
| 234 |
+
shift_deq: torch.Tensor,
|
| 235 |
+
eps: float = 1e-5,
|
| 236 |
+
packed: torch.Tensor | None = None,
|
| 237 |
+
sf_swizzled: torch.Tensor | None = None,
|
| 238 |
+
) -> tuple[torch.Tensor, torch.Tensor]:
|
| 239 |
+
"""Fused FP8-modulated AdaLN producer to NVFP4 packed activations."""
|
| 240 |
+
|
| 241 |
+
if packed is None or sf_swizzled is None:
|
| 242 |
+
packed, sf_swizzled = _alloc_nvfp4(x)
|
| 243 |
+
ops.ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16(
|
| 244 |
+
x, scale_fp8, shift_fp8, scale_deq, shift_deq, float(eps), packed, sf_swizzled
|
| 245 |
+
)
|
| 246 |
+
return packed, sf_swizzled
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
def layer_norm_no_affine_quant_fp8_static_bf16(
|
| 250 |
+
x: torch.Tensor,
|
| 251 |
+
act_scale: torch.Tensor,
|
| 252 |
+
eps: float = 1e-5,
|
| 253 |
+
out: torch.Tensor | None = None,
|
| 254 |
+
) -> torch.Tensor:
|
| 255 |
+
"""Fused no-affine LayerNorm + static FP8 quantize."""
|
| 256 |
+
|
| 257 |
+
if out is None:
|
| 258 |
+
out = _alloc_fp8(x)
|
| 259 |
+
ops.layer_norm_no_affine_quant_fp8_static_bf16(x, act_scale, float(eps), out)
|
| 260 |
+
return out
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
__all__ = [
|
| 264 |
+
"ada_layer_norm_quant_fp8_bf16",
|
| 265 |
+
"ada_layer_norm_quant_fp8_modfp8_bf16",
|
| 266 |
+
"awq_ada_layer_norm_quant_fp8_bf16",
|
| 267 |
+
"ada_layer_norm_quant_nvfp4_swizzled_bf16",
|
| 268 |
+
"ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16",
|
| 269 |
+
"layer_norm_no_affine_quant_fp8_static_bf16",
|
| 270 |
+
"swizzled_sf_size",
|
| 271 |
+
]
|
build/torch211-cxx11-cu130-x86_64-linux/_adaptive_layernorm_producers_cuda_1798e7f.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:66d3a7d56691415cd91a7b2b8694e10eb13bce362bb450e1599c047978be1971
|
| 3 |
+
size 1313616
|
build/torch211-cxx11-cu130-x86_64-linux/_ops.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from . import _adaptive_layernorm_producers_cuda_1798e7f
|
| 3 |
+
ops = torch.ops._adaptive_layernorm_producers_cuda_1798e7f
|
| 4 |
+
|
| 5 |
+
def add_op_namespace_prefix(op_name: str):
|
| 6 |
+
"""
|
| 7 |
+
Prefix op by namespace.
|
| 8 |
+
"""
|
| 9 |
+
return f"_adaptive_layernorm_producers_cuda_1798e7f::{op_name}"
|
build/torch211-cxx11-cu130-x86_64-linux/adaptive_layernorm_producers/__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": "adaptive-layernorm-producers",
|
| 3 |
+
"id": "_adaptive_layernorm_producers_cuda_1798e7f",
|
| 4 |
+
"version": 1,
|
| 5 |
+
"license": "Apache-2.0",
|
| 6 |
+
"python-depends": [],
|
| 7 |
+
"backend": {
|
| 8 |
+
"type": "cuda",
|
| 9 |
+
"archs": [
|
| 10 |
+
"10.0",
|
| 11 |
+
"11.0",
|
| 12 |
+
"12.0",
|
| 13 |
+
"12.1+PTX",
|
| 14 |
+
"7.5",
|
| 15 |
+
"8.0",
|
| 16 |
+
"8.6",
|
| 17 |
+
"8.7",
|
| 18 |
+
"8.9",
|
| 19 |
+
"9.0"
|
| 20 |
+
]
|
| 21 |
+
}
|
| 22 |
+
}
|
build/torch212-cxx11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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""FlashRT adaptive LayerNorm producer kernels for DiT/Wan-style blocks."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
from ._ops import add_op_namespace_prefix, ops
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _check_x(x: torch.Tensor) -> None:
|
| 11 |
+
if x.dim() != 2:
|
| 12 |
+
raise RuntimeError("x must have shape (rows, dim)")
|
| 13 |
+
if x.shape[1] % 2 != 0:
|
| 14 |
+
raise RuntimeError("x.shape[1] must be even")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def _check_mod(x: torch.Tensor, scale: torch.Tensor, shift: torch.Tensor) -> None:
|
| 18 |
+
_check_x(x)
|
| 19 |
+
dim = x.shape[1]
|
| 20 |
+
if scale.shape != (dim,) or shift.shape != (dim,):
|
| 21 |
+
raise RuntimeError("scale and shift must have shape (dim,)")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _check_scalar(scale: torch.Tensor, name: str) -> None:
|
| 25 |
+
if scale.numel() != 1:
|
| 26 |
+
raise RuntimeError(f"{name} must be a scalar tensor")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def swizzled_sf_size(rows: int, dim: int) -> int:
|
| 30 |
+
"""Return bytes required by the FlashRT/CUTLASS NVFP4 swizzled scale layout."""
|
| 31 |
+
|
| 32 |
+
if dim % 16 != 0:
|
| 33 |
+
raise RuntimeError("dim must be divisible by 16 for NVFP4 swizzled output")
|
| 34 |
+
n_blocks = dim // 16
|
| 35 |
+
n_row_super = (rows + 127) // 128
|
| 36 |
+
n_col_super = (n_blocks + 3) // 4
|
| 37 |
+
return n_row_super * n_col_super * 128 * 64
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def _alloc_fp8(x: torch.Tensor) -> torch.Tensor:
|
| 41 |
+
return torch.empty_like(x, dtype=torch.float8_e4m3fn)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def _alloc_nvfp4(x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
|
| 45 |
+
rows, dim = x.shape
|
| 46 |
+
packed = torch.empty((rows, dim // 2), device=x.device, dtype=torch.uint8)
|
| 47 |
+
sf = torch.zeros((swizzled_sf_size(rows, dim),), device=x.device, dtype=torch.uint8)
|
| 48 |
+
return packed, sf
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@torch.library.register_fake(add_op_namespace_prefix("ada_layer_norm_quant_fp8_bf16"))
|
| 52 |
+
def _ada_layer_norm_quant_fp8_bf16_fake(
|
| 53 |
+
x: torch.Tensor,
|
| 54 |
+
scale: torch.Tensor,
|
| 55 |
+
shift: torch.Tensor,
|
| 56 |
+
act_scale: torch.Tensor,
|
| 57 |
+
eps: float,
|
| 58 |
+
out: torch.Tensor,
|
| 59 |
+
) -> None:
|
| 60 |
+
_check_mod(x, scale, shift)
|
| 61 |
+
_check_scalar(act_scale, "act_scale")
|
| 62 |
+
if out.shape != x.shape:
|
| 63 |
+
raise RuntimeError("out must have the same shape as x")
|
| 64 |
+
return None
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
@torch.library.register_fake(add_op_namespace_prefix("ada_layer_norm_quant_fp8_modfp8_bf16"))
|
| 68 |
+
def _ada_layer_norm_quant_fp8_modfp8_bf16_fake(
|
| 69 |
+
x: torch.Tensor,
|
| 70 |
+
scale_fp8: torch.Tensor,
|
| 71 |
+
shift_fp8: torch.Tensor,
|
| 72 |
+
scale_deq: torch.Tensor,
|
| 73 |
+
shift_deq: torch.Tensor,
|
| 74 |
+
act_scale: torch.Tensor,
|
| 75 |
+
eps: float,
|
| 76 |
+
out: torch.Tensor,
|
| 77 |
+
) -> None:
|
| 78 |
+
_check_mod(x, scale_fp8, shift_fp8)
|
| 79 |
+
_check_scalar(scale_deq, "scale_deq")
|
| 80 |
+
_check_scalar(shift_deq, "shift_deq")
|
| 81 |
+
_check_scalar(act_scale, "act_scale")
|
| 82 |
+
if out.shape != x.shape:
|
| 83 |
+
raise RuntimeError("out must have the same shape as x")
|
| 84 |
+
return None
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@torch.library.register_fake(add_op_namespace_prefix("awq_ada_layer_norm_quant_fp8_bf16"))
|
| 88 |
+
def _awq_ada_layer_norm_quant_fp8_bf16_fake(
|
| 89 |
+
x: torch.Tensor,
|
| 90 |
+
scale: torch.Tensor,
|
| 91 |
+
shift: torch.Tensor,
|
| 92 |
+
inv_s: torch.Tensor,
|
| 93 |
+
act_scale: torch.Tensor,
|
| 94 |
+
eps: float,
|
| 95 |
+
out: torch.Tensor,
|
| 96 |
+
) -> None:
|
| 97 |
+
_check_mod(x, scale, shift)
|
| 98 |
+
if inv_s.shape != (x.shape[1],):
|
| 99 |
+
raise RuntimeError("inv_s must have shape (dim,)")
|
| 100 |
+
_check_scalar(act_scale, "act_scale")
|
| 101 |
+
if out.shape != x.shape:
|
| 102 |
+
raise RuntimeError("out must have the same shape as x")
|
| 103 |
+
return None
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
@torch.library.register_fake(add_op_namespace_prefix("ada_layer_norm_quant_nvfp4_swizzled_bf16"))
|
| 107 |
+
def _ada_layer_norm_quant_nvfp4_swizzled_bf16_fake(
|
| 108 |
+
x: torch.Tensor,
|
| 109 |
+
scale: torch.Tensor,
|
| 110 |
+
shift: torch.Tensor,
|
| 111 |
+
eps: float,
|
| 112 |
+
packed: torch.Tensor,
|
| 113 |
+
sf_swizzled: torch.Tensor,
|
| 114 |
+
) -> None:
|
| 115 |
+
_check_mod(x, scale, shift)
|
| 116 |
+
rows, dim = x.shape
|
| 117 |
+
if packed.shape != (rows, dim // 2):
|
| 118 |
+
raise RuntimeError("packed must have shape (rows, dim // 2)")
|
| 119 |
+
if sf_swizzled.numel() < swizzled_sf_size(rows, dim):
|
| 120 |
+
raise RuntimeError("sf_swizzled is too small")
|
| 121 |
+
return None
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
@torch.library.register_fake(add_op_namespace_prefix("ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16"))
|
| 125 |
+
def _ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16_fake(
|
| 126 |
+
x: torch.Tensor,
|
| 127 |
+
scale_fp8: torch.Tensor,
|
| 128 |
+
shift_fp8: torch.Tensor,
|
| 129 |
+
scale_deq: torch.Tensor,
|
| 130 |
+
shift_deq: torch.Tensor,
|
| 131 |
+
eps: float,
|
| 132 |
+
packed: torch.Tensor,
|
| 133 |
+
sf_swizzled: torch.Tensor,
|
| 134 |
+
) -> None:
|
| 135 |
+
_check_mod(x, scale_fp8, shift_fp8)
|
| 136 |
+
_check_scalar(scale_deq, "scale_deq")
|
| 137 |
+
_check_scalar(shift_deq, "shift_deq")
|
| 138 |
+
rows, dim = x.shape
|
| 139 |
+
if packed.shape != (rows, dim // 2):
|
| 140 |
+
raise RuntimeError("packed must have shape (rows, dim // 2)")
|
| 141 |
+
if sf_swizzled.numel() < swizzled_sf_size(rows, dim):
|
| 142 |
+
raise RuntimeError("sf_swizzled is too small")
|
| 143 |
+
return None
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
@torch.library.register_fake(add_op_namespace_prefix("layer_norm_no_affine_quant_fp8_static_bf16"))
|
| 147 |
+
def _layer_norm_no_affine_quant_fp8_static_bf16_fake(
|
| 148 |
+
x: torch.Tensor,
|
| 149 |
+
act_scale: torch.Tensor,
|
| 150 |
+
eps: float,
|
| 151 |
+
out: torch.Tensor,
|
| 152 |
+
) -> None:
|
| 153 |
+
_check_x(x)
|
| 154 |
+
_check_scalar(act_scale, "act_scale")
|
| 155 |
+
if out.shape != x.shape:
|
| 156 |
+
raise RuntimeError("out must have the same shape as x")
|
| 157 |
+
return None
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def ada_layer_norm_quant_fp8_bf16(
|
| 161 |
+
x: torch.Tensor,
|
| 162 |
+
scale: torch.Tensor,
|
| 163 |
+
shift: torch.Tensor,
|
| 164 |
+
act_scale: torch.Tensor,
|
| 165 |
+
eps: float = 1e-5,
|
| 166 |
+
out: torch.Tensor | None = None,
|
| 167 |
+
) -> torch.Tensor:
|
| 168 |
+
"""Fused no-affine LayerNorm + AdaLN scale/shift + static FP8 quantize."""
|
| 169 |
+
|
| 170 |
+
if out is None:
|
| 171 |
+
out = _alloc_fp8(x)
|
| 172 |
+
ops.ada_layer_norm_quant_fp8_bf16(x, scale, shift, act_scale, float(eps), out)
|
| 173 |
+
return out
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def ada_layer_norm_quant_fp8_modfp8_bf16(
|
| 177 |
+
x: torch.Tensor,
|
| 178 |
+
scale_fp8: torch.Tensor,
|
| 179 |
+
shift_fp8: torch.Tensor,
|
| 180 |
+
scale_deq: torch.Tensor,
|
| 181 |
+
shift_deq: torch.Tensor,
|
| 182 |
+
act_scale: torch.Tensor,
|
| 183 |
+
eps: float = 1e-5,
|
| 184 |
+
out: torch.Tensor | None = None,
|
| 185 |
+
) -> torch.Tensor:
|
| 186 |
+
"""Fused AdaLN producer when modulation vectors are stored in FP8."""
|
| 187 |
+
|
| 188 |
+
if out is None:
|
| 189 |
+
out = _alloc_fp8(x)
|
| 190 |
+
ops.ada_layer_norm_quant_fp8_modfp8_bf16(
|
| 191 |
+
x, scale_fp8, shift_fp8, scale_deq, shift_deq, act_scale, float(eps), out
|
| 192 |
+
)
|
| 193 |
+
return out
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
def awq_ada_layer_norm_quant_fp8_bf16(
|
| 197 |
+
x: torch.Tensor,
|
| 198 |
+
scale: torch.Tensor,
|
| 199 |
+
shift: torch.Tensor,
|
| 200 |
+
inv_s: torch.Tensor,
|
| 201 |
+
act_scale: torch.Tensor,
|
| 202 |
+
eps: float = 1e-5,
|
| 203 |
+
out: torch.Tensor | None = None,
|
| 204 |
+
) -> torch.Tensor:
|
| 205 |
+
"""Fused AdaLN producer with AWQ/SmoothQuant per-channel activation scale."""
|
| 206 |
+
|
| 207 |
+
if out is None:
|
| 208 |
+
out = _alloc_fp8(x)
|
| 209 |
+
ops.awq_ada_layer_norm_quant_fp8_bf16(x, scale, shift, inv_s, act_scale, float(eps), out)
|
| 210 |
+
return out
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def ada_layer_norm_quant_nvfp4_swizzled_bf16(
|
| 214 |
+
x: torch.Tensor,
|
| 215 |
+
scale: torch.Tensor,
|
| 216 |
+
shift: torch.Tensor,
|
| 217 |
+
eps: float = 1e-5,
|
| 218 |
+
packed: torch.Tensor | None = None,
|
| 219 |
+
sf_swizzled: torch.Tensor | None = None,
|
| 220 |
+
) -> tuple[torch.Tensor, torch.Tensor]:
|
| 221 |
+
"""Fused AdaLN producer to NVFP4 packed activations and swizzled scale factors."""
|
| 222 |
+
|
| 223 |
+
if packed is None or sf_swizzled is None:
|
| 224 |
+
packed, sf_swizzled = _alloc_nvfp4(x)
|
| 225 |
+
ops.ada_layer_norm_quant_nvfp4_swizzled_bf16(x, scale, shift, float(eps), packed, sf_swizzled)
|
| 226 |
+
return packed, sf_swizzled
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16(
|
| 230 |
+
x: torch.Tensor,
|
| 231 |
+
scale_fp8: torch.Tensor,
|
| 232 |
+
shift_fp8: torch.Tensor,
|
| 233 |
+
scale_deq: torch.Tensor,
|
| 234 |
+
shift_deq: torch.Tensor,
|
| 235 |
+
eps: float = 1e-5,
|
| 236 |
+
packed: torch.Tensor | None = None,
|
| 237 |
+
sf_swizzled: torch.Tensor | None = None,
|
| 238 |
+
) -> tuple[torch.Tensor, torch.Tensor]:
|
| 239 |
+
"""Fused FP8-modulated AdaLN producer to NVFP4 packed activations."""
|
| 240 |
+
|
| 241 |
+
if packed is None or sf_swizzled is None:
|
| 242 |
+
packed, sf_swizzled = _alloc_nvfp4(x)
|
| 243 |
+
ops.ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16(
|
| 244 |
+
x, scale_fp8, shift_fp8, scale_deq, shift_deq, float(eps), packed, sf_swizzled
|
| 245 |
+
)
|
| 246 |
+
return packed, sf_swizzled
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
def layer_norm_no_affine_quant_fp8_static_bf16(
|
| 250 |
+
x: torch.Tensor,
|
| 251 |
+
act_scale: torch.Tensor,
|
| 252 |
+
eps: float = 1e-5,
|
| 253 |
+
out: torch.Tensor | None = None,
|
| 254 |
+
) -> torch.Tensor:
|
| 255 |
+
"""Fused no-affine LayerNorm + static FP8 quantize."""
|
| 256 |
+
|
| 257 |
+
if out is None:
|
| 258 |
+
out = _alloc_fp8(x)
|
| 259 |
+
ops.layer_norm_no_affine_quant_fp8_static_bf16(x, act_scale, float(eps), out)
|
| 260 |
+
return out
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
__all__ = [
|
| 264 |
+
"ada_layer_norm_quant_fp8_bf16",
|
| 265 |
+
"ada_layer_norm_quant_fp8_modfp8_bf16",
|
| 266 |
+
"awq_ada_layer_norm_quant_fp8_bf16",
|
| 267 |
+
"ada_layer_norm_quant_nvfp4_swizzled_bf16",
|
| 268 |
+
"ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16",
|
| 269 |
+
"layer_norm_no_affine_quant_fp8_static_bf16",
|
| 270 |
+
"swizzled_sf_size",
|
| 271 |
+
]
|
build/torch212-cxx11-cu130-x86_64-linux/_adaptive_layernorm_producers_cuda_1798e7f.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7e0327c2ffbc171f1b2b70447f0076a4d0acd80bb1eb716045ee973e867a030e
|
| 3 |
+
size 1320088
|
build/torch212-cxx11-cu130-x86_64-linux/_ops.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from . import _adaptive_layernorm_producers_cuda_1798e7f
|
| 3 |
+
ops = torch.ops._adaptive_layernorm_producers_cuda_1798e7f
|
| 4 |
+
|
| 5 |
+
def add_op_namespace_prefix(op_name: str):
|
| 6 |
+
"""
|
| 7 |
+
Prefix op by namespace.
|
| 8 |
+
"""
|
| 9 |
+
return f"_adaptive_layernorm_producers_cuda_1798e7f::{op_name}"
|
build/torch212-cxx11-cu130-x86_64-linux/adaptive_layernorm_producers/__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": "adaptive-layernorm-producers",
|
| 3 |
+
"id": "_adaptive_layernorm_producers_cuda_1798e7f",
|
| 4 |
+
"version": 1,
|
| 5 |
+
"license": "Apache-2.0",
|
| 6 |
+
"python-depends": [],
|
| 7 |
+
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
from ._ops import add_op_namespace_prefix, ops
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _check_x(x: torch.Tensor) -> None:
|
| 11 |
+
if x.dim() != 2:
|
| 12 |
+
raise RuntimeError("x must have shape (rows, dim)")
|
| 13 |
+
if x.shape[1] % 2 != 0:
|
| 14 |
+
raise RuntimeError("x.shape[1] must be even")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def _check_mod(x: torch.Tensor, scale: torch.Tensor, shift: torch.Tensor) -> None:
|
| 18 |
+
_check_x(x)
|
| 19 |
+
dim = x.shape[1]
|
| 20 |
+
if scale.shape != (dim,) or shift.shape != (dim,):
|
| 21 |
+
raise RuntimeError("scale and shift must have shape (dim,)")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _check_scalar(scale: torch.Tensor, name: str) -> None:
|
| 25 |
+
if scale.numel() != 1:
|
| 26 |
+
raise RuntimeError(f"{name} must be a scalar tensor")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def swizzled_sf_size(rows: int, dim: int) -> int:
|
| 30 |
+
"""Return bytes required by the FlashRT/CUTLASS NVFP4 swizzled scale layout."""
|
| 31 |
+
|
| 32 |
+
if dim % 16 != 0:
|
| 33 |
+
raise RuntimeError("dim must be divisible by 16 for NVFP4 swizzled output")
|
| 34 |
+
n_blocks = dim // 16
|
| 35 |
+
n_row_super = (rows + 127) // 128
|
| 36 |
+
n_col_super = (n_blocks + 3) // 4
|
| 37 |
+
return n_row_super * n_col_super * 128 * 64
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def _alloc_fp8(x: torch.Tensor) -> torch.Tensor:
|
| 41 |
+
return torch.empty_like(x, dtype=torch.float8_e4m3fn)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def _alloc_nvfp4(x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
|
| 45 |
+
rows, dim = x.shape
|
| 46 |
+
packed = torch.empty((rows, dim // 2), device=x.device, dtype=torch.uint8)
|
| 47 |
+
sf = torch.zeros((swizzled_sf_size(rows, dim),), device=x.device, dtype=torch.uint8)
|
| 48 |
+
return packed, sf
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@torch.library.register_fake(add_op_namespace_prefix("ada_layer_norm_quant_fp8_bf16"))
|
| 52 |
+
def _ada_layer_norm_quant_fp8_bf16_fake(
|
| 53 |
+
x: torch.Tensor,
|
| 54 |
+
scale: torch.Tensor,
|
| 55 |
+
shift: torch.Tensor,
|
| 56 |
+
act_scale: torch.Tensor,
|
| 57 |
+
eps: float,
|
| 58 |
+
out: torch.Tensor,
|
| 59 |
+
) -> None:
|
| 60 |
+
_check_mod(x, scale, shift)
|
| 61 |
+
_check_scalar(act_scale, "act_scale")
|
| 62 |
+
if out.shape != x.shape:
|
| 63 |
+
raise RuntimeError("out must have the same shape as x")
|
| 64 |
+
return None
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
@torch.library.register_fake(add_op_namespace_prefix("ada_layer_norm_quant_fp8_modfp8_bf16"))
|
| 68 |
+
def _ada_layer_norm_quant_fp8_modfp8_bf16_fake(
|
| 69 |
+
x: torch.Tensor,
|
| 70 |
+
scale_fp8: torch.Tensor,
|
| 71 |
+
shift_fp8: torch.Tensor,
|
| 72 |
+
scale_deq: torch.Tensor,
|
| 73 |
+
shift_deq: torch.Tensor,
|
| 74 |
+
act_scale: torch.Tensor,
|
| 75 |
+
eps: float,
|
| 76 |
+
out: torch.Tensor,
|
| 77 |
+
) -> None:
|
| 78 |
+
_check_mod(x, scale_fp8, shift_fp8)
|
| 79 |
+
_check_scalar(scale_deq, "scale_deq")
|
| 80 |
+
_check_scalar(shift_deq, "shift_deq")
|
| 81 |
+
_check_scalar(act_scale, "act_scale")
|
| 82 |
+
if out.shape != x.shape:
|
| 83 |
+
raise RuntimeError("out must have the same shape as x")
|
| 84 |
+
return None
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@torch.library.register_fake(add_op_namespace_prefix("awq_ada_layer_norm_quant_fp8_bf16"))
|
| 88 |
+
def _awq_ada_layer_norm_quant_fp8_bf16_fake(
|
| 89 |
+
x: torch.Tensor,
|
| 90 |
+
scale: torch.Tensor,
|
| 91 |
+
shift: torch.Tensor,
|
| 92 |
+
inv_s: torch.Tensor,
|
| 93 |
+
act_scale: torch.Tensor,
|
| 94 |
+
eps: float,
|
| 95 |
+
out: torch.Tensor,
|
| 96 |
+
) -> None:
|
| 97 |
+
_check_mod(x, scale, shift)
|
| 98 |
+
if inv_s.shape != (x.shape[1],):
|
| 99 |
+
raise RuntimeError("inv_s must have shape (dim,)")
|
| 100 |
+
_check_scalar(act_scale, "act_scale")
|
| 101 |
+
if out.shape != x.shape:
|
| 102 |
+
raise RuntimeError("out must have the same shape as x")
|
| 103 |
+
return None
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
@torch.library.register_fake(add_op_namespace_prefix("ada_layer_norm_quant_nvfp4_swizzled_bf16"))
|
| 107 |
+
def _ada_layer_norm_quant_nvfp4_swizzled_bf16_fake(
|
| 108 |
+
x: torch.Tensor,
|
| 109 |
+
scale: torch.Tensor,
|
| 110 |
+
shift: torch.Tensor,
|
| 111 |
+
eps: float,
|
| 112 |
+
packed: torch.Tensor,
|
| 113 |
+
sf_swizzled: torch.Tensor,
|
| 114 |
+
) -> None:
|
| 115 |
+
_check_mod(x, scale, shift)
|
| 116 |
+
rows, dim = x.shape
|
| 117 |
+
if packed.shape != (rows, dim // 2):
|
| 118 |
+
raise RuntimeError("packed must have shape (rows, dim // 2)")
|
| 119 |
+
if sf_swizzled.numel() < swizzled_sf_size(rows, dim):
|
| 120 |
+
raise RuntimeError("sf_swizzled is too small")
|
| 121 |
+
return None
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
@torch.library.register_fake(add_op_namespace_prefix("ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16"))
|
| 125 |
+
def _ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16_fake(
|
| 126 |
+
x: torch.Tensor,
|
| 127 |
+
scale_fp8: torch.Tensor,
|
| 128 |
+
shift_fp8: torch.Tensor,
|
| 129 |
+
scale_deq: torch.Tensor,
|
| 130 |
+
shift_deq: torch.Tensor,
|
| 131 |
+
eps: float,
|
| 132 |
+
packed: torch.Tensor,
|
| 133 |
+
sf_swizzled: torch.Tensor,
|
| 134 |
+
) -> None:
|
| 135 |
+
_check_mod(x, scale_fp8, shift_fp8)
|
| 136 |
+
_check_scalar(scale_deq, "scale_deq")
|
| 137 |
+
_check_scalar(shift_deq, "shift_deq")
|
| 138 |
+
rows, dim = x.shape
|
| 139 |
+
if packed.shape != (rows, dim // 2):
|
| 140 |
+
raise RuntimeError("packed must have shape (rows, dim // 2)")
|
| 141 |
+
if sf_swizzled.numel() < swizzled_sf_size(rows, dim):
|
| 142 |
+
raise RuntimeError("sf_swizzled is too small")
|
| 143 |
+
return None
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
@torch.library.register_fake(add_op_namespace_prefix("layer_norm_no_affine_quant_fp8_static_bf16"))
|
| 147 |
+
def _layer_norm_no_affine_quant_fp8_static_bf16_fake(
|
| 148 |
+
x: torch.Tensor,
|
| 149 |
+
act_scale: torch.Tensor,
|
| 150 |
+
eps: float,
|
| 151 |
+
out: torch.Tensor,
|
| 152 |
+
) -> None:
|
| 153 |
+
_check_x(x)
|
| 154 |
+
_check_scalar(act_scale, "act_scale")
|
| 155 |
+
if out.shape != x.shape:
|
| 156 |
+
raise RuntimeError("out must have the same shape as x")
|
| 157 |
+
return None
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def ada_layer_norm_quant_fp8_bf16(
|
| 161 |
+
x: torch.Tensor,
|
| 162 |
+
scale: torch.Tensor,
|
| 163 |
+
shift: torch.Tensor,
|
| 164 |
+
act_scale: torch.Tensor,
|
| 165 |
+
eps: float = 1e-5,
|
| 166 |
+
out: torch.Tensor | None = None,
|
| 167 |
+
) -> torch.Tensor:
|
| 168 |
+
"""Fused no-affine LayerNorm + AdaLN scale/shift + static FP8 quantize."""
|
| 169 |
+
|
| 170 |
+
if out is None:
|
| 171 |
+
out = _alloc_fp8(x)
|
| 172 |
+
ops.ada_layer_norm_quant_fp8_bf16(x, scale, shift, act_scale, float(eps), out)
|
| 173 |
+
return out
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def ada_layer_norm_quant_fp8_modfp8_bf16(
|
| 177 |
+
x: torch.Tensor,
|
| 178 |
+
scale_fp8: torch.Tensor,
|
| 179 |
+
shift_fp8: torch.Tensor,
|
| 180 |
+
scale_deq: torch.Tensor,
|
| 181 |
+
shift_deq: torch.Tensor,
|
| 182 |
+
act_scale: torch.Tensor,
|
| 183 |
+
eps: float = 1e-5,
|
| 184 |
+
out: torch.Tensor | None = None,
|
| 185 |
+
) -> torch.Tensor:
|
| 186 |
+
"""Fused AdaLN producer when modulation vectors are stored in FP8."""
|
| 187 |
+
|
| 188 |
+
if out is None:
|
| 189 |
+
out = _alloc_fp8(x)
|
| 190 |
+
ops.ada_layer_norm_quant_fp8_modfp8_bf16(
|
| 191 |
+
x, scale_fp8, shift_fp8, scale_deq, shift_deq, act_scale, float(eps), out
|
| 192 |
+
)
|
| 193 |
+
return out
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
def awq_ada_layer_norm_quant_fp8_bf16(
|
| 197 |
+
x: torch.Tensor,
|
| 198 |
+
scale: torch.Tensor,
|
| 199 |
+
shift: torch.Tensor,
|
| 200 |
+
inv_s: torch.Tensor,
|
| 201 |
+
act_scale: torch.Tensor,
|
| 202 |
+
eps: float = 1e-5,
|
| 203 |
+
out: torch.Tensor | None = None,
|
| 204 |
+
) -> torch.Tensor:
|
| 205 |
+
"""Fused AdaLN producer with AWQ/SmoothQuant per-channel activation scale."""
|
| 206 |
+
|
| 207 |
+
if out is None:
|
| 208 |
+
out = _alloc_fp8(x)
|
| 209 |
+
ops.awq_ada_layer_norm_quant_fp8_bf16(x, scale, shift, inv_s, act_scale, float(eps), out)
|
| 210 |
+
return out
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def ada_layer_norm_quant_nvfp4_swizzled_bf16(
|
| 214 |
+
x: torch.Tensor,
|
| 215 |
+
scale: torch.Tensor,
|
| 216 |
+
shift: torch.Tensor,
|
| 217 |
+
eps: float = 1e-5,
|
| 218 |
+
packed: torch.Tensor | None = None,
|
| 219 |
+
sf_swizzled: torch.Tensor | None = None,
|
| 220 |
+
) -> tuple[torch.Tensor, torch.Tensor]:
|
| 221 |
+
"""Fused AdaLN producer to NVFP4 packed activations and swizzled scale factors."""
|
| 222 |
+
|
| 223 |
+
if packed is None or sf_swizzled is None:
|
| 224 |
+
packed, sf_swizzled = _alloc_nvfp4(x)
|
| 225 |
+
ops.ada_layer_norm_quant_nvfp4_swizzled_bf16(x, scale, shift, float(eps), packed, sf_swizzled)
|
| 226 |
+
return packed, sf_swizzled
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16(
|
| 230 |
+
x: torch.Tensor,
|
| 231 |
+
scale_fp8: torch.Tensor,
|
| 232 |
+
shift_fp8: torch.Tensor,
|
| 233 |
+
scale_deq: torch.Tensor,
|
| 234 |
+
shift_deq: torch.Tensor,
|
| 235 |
+
eps: float = 1e-5,
|
| 236 |
+
packed: torch.Tensor | None = None,
|
| 237 |
+
sf_swizzled: torch.Tensor | None = None,
|
| 238 |
+
) -> tuple[torch.Tensor, torch.Tensor]:
|
| 239 |
+
"""Fused FP8-modulated AdaLN producer to NVFP4 packed activations."""
|
| 240 |
+
|
| 241 |
+
if packed is None or sf_swizzled is None:
|
| 242 |
+
packed, sf_swizzled = _alloc_nvfp4(x)
|
| 243 |
+
ops.ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16(
|
| 244 |
+
x, scale_fp8, shift_fp8, scale_deq, shift_deq, float(eps), packed, sf_swizzled
|
| 245 |
+
)
|
| 246 |
+
return packed, sf_swizzled
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
def layer_norm_no_affine_quant_fp8_static_bf16(
|
| 250 |
+
x: torch.Tensor,
|
| 251 |
+
act_scale: torch.Tensor,
|
| 252 |
+
eps: float = 1e-5,
|
| 253 |
+
out: torch.Tensor | None = None,
|
| 254 |
+
) -> torch.Tensor:
|
| 255 |
+
"""Fused no-affine LayerNorm + static FP8 quantize."""
|
| 256 |
+
|
| 257 |
+
if out is None:
|
| 258 |
+
out = _alloc_fp8(x)
|
| 259 |
+
ops.layer_norm_no_affine_quant_fp8_static_bf16(x, act_scale, float(eps), out)
|
| 260 |
+
return out
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
__all__ = [
|
| 264 |
+
"ada_layer_norm_quant_fp8_bf16",
|
| 265 |
+
"ada_layer_norm_quant_fp8_modfp8_bf16",
|
| 266 |
+
"awq_ada_layer_norm_quant_fp8_bf16",
|
| 267 |
+
"ada_layer_norm_quant_nvfp4_swizzled_bf16",
|
| 268 |
+
"ada_layer_norm_quant_nvfp4_swizzled_modfp8_bf16",
|
| 269 |
+
"layer_norm_no_affine_quant_fp8_static_bf16",
|
| 270 |
+
"swizzled_sf_size",
|
| 271 |
+
]
|
build/torch212-cxx11-cu132-x86_64-linux/_adaptive_layernorm_producers_cuda_1798e7f.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a45ea9224f7cc33e5070a05bf421cec1c08839bb39ad208413b0f496824d78e4
|
| 3 |
+
size 1324184
|
build/torch212-cxx11-cu132-x86_64-linux/_ops.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from . import _adaptive_layernorm_producers_cuda_1798e7f
|
| 3 |
+
ops = torch.ops._adaptive_layernorm_producers_cuda_1798e7f
|
| 4 |
+
|
| 5 |
+
def add_op_namespace_prefix(op_name: str):
|
| 6 |
+
"""
|
| 7 |
+
Prefix op by namespace.
|
| 8 |
+
"""
|
| 9 |
+
return f"_adaptive_layernorm_producers_cuda_1798e7f::{op_name}"
|
build/torch212-cxx11-cu132-x86_64-linux/adaptive_layernorm_producers/__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": "adaptive-layernorm-producers",
|
| 3 |
+
"id": "_adaptive_layernorm_producers_cuda_1798e7f",
|
| 4 |
+
"version": 1,
|
| 5 |
+
"license": "Apache-2.0",
|
| 6 |
+
"python-depends": [],
|
| 7 |
+
"backend": {
|
| 8 |
+
"type": "cuda",
|
| 9 |
+
"archs": [
|
| 10 |
+
"10.0",
|
| 11 |
+
"11.0",
|
| 12 |
+
"12.0",
|
| 13 |
+
"12.1+PTX",
|
| 14 |
+
"7.5",
|
| 15 |
+
"8.0",
|
| 16 |
+
"8.6",
|
| 17 |
+
"8.7",
|
| 18 |
+
"8.9",
|
| 19 |
+
"9.0"
|
| 20 |
+
]
|
| 21 |
+
}
|
| 22 |
+
}
|