Uploaded using `kernel-builder`.
Browse files- build/torch211-cxx11-cpu-x86_64-linux/__init__.py +77 -0
- build/torch211-cxx11-cpu-x86_64-linux/_decode_ops_cpu_8c2646d.abi3.so +3 -0
- build/torch211-cxx11-cpu-x86_64-linux/_ops.py +9 -0
- build/torch211-cxx11-cpu-x86_64-linux/decode_ops/__init__.py +26 -0
- build/torch211-cxx11-cpu-x86_64-linux/metadata.json +30 -0
- build/torch212-cxx11-cpu-x86_64-linux/__init__.py +77 -0
- build/torch212-cxx11-cpu-x86_64-linux/_decode_ops_cpu_8c2646d.abi3.so +3 -0
- build/torch212-cxx11-cpu-x86_64-linux/_ops.py +9 -0
- build/torch212-cxx11-cpu-x86_64-linux/decode_ops/__init__.py +26 -0
- build/torch212-cxx11-cpu-x86_64-linux/metadata.json +30 -0
- build/torch213-cxx11-cpu-x86_64-linux/__init__.py +77 -0
- build/torch213-cxx11-cpu-x86_64-linux/_decode_ops_cpu_8c2646d.abi3.so +3 -0
- build/torch213-cxx11-cpu-x86_64-linux/_ops.py +9 -0
- build/torch213-cxx11-cpu-x86_64-linux/decode_ops/__init__.py +26 -0
- build/torch213-cxx11-cpu-x86_64-linux/metadata.json +30 -0
build/torch211-cxx11-cpu-x86_64-linux/__init__.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""decode-ops: fused decode-step glue for LLM inference on CPUs, aarch64-first.
|
| 2 |
+
|
| 3 |
+
The non-matmul residue of a decode step as single NEON ops: RMSNorm with a
|
| 4 |
+
fused residual add, in-place RoPE, SiLU-gate, greedy argmax, and
|
| 5 |
+
deterministic temperature / top-k / top-p sampling from an explicit uniform
|
| 6 |
+
draw. Companions: bitnet-cpu and quant-matmul (linears), cpu-attn
|
| 7 |
+
(attention over an int8 KV cache).
|
| 8 |
+
|
| 9 |
+
High-level API:
|
| 10 |
+
rmsnorm(x, weight, eps)
|
| 11 |
+
add_rmsnorm(residual, x, weight, eps) residual += x, then norm (in place)
|
| 12 |
+
rope(q, k, pos, theta, interleaved) in place
|
| 13 |
+
silu_mul(gate, up)
|
| 14 |
+
argmax(logits)
|
| 15 |
+
sample(logits, temperature, top_k, top_p, generator)
|
| 16 |
+
"""
|
| 17 |
+
from typing import Optional
|
| 18 |
+
|
| 19 |
+
import torch
|
| 20 |
+
|
| 21 |
+
from ._ops import ops
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def rmsnorm(x: torch.Tensor, weight: torch.Tensor, eps: float = 1e-6) -> torch.Tensor:
|
| 25 |
+
"""out = x * weight / sqrt(mean(x^2, dim=-1) + eps), f32."""
|
| 26 |
+
orig = x.shape
|
| 27 |
+
x_flat = x.reshape(-1, orig[-1]).to(torch.float32).contiguous()
|
| 28 |
+
out = torch.empty_like(x_flat)
|
| 29 |
+
ops.do_rmsnorm(out, x_flat, weight.to(torch.float32).contiguous(), eps)
|
| 30 |
+
return out.view(orig)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def add_rmsnorm(residual: torch.Tensor, x: torch.Tensor, weight: torch.Tensor,
|
| 34 |
+
eps: float = 1e-6) -> torch.Tensor:
|
| 35 |
+
"""residual += x in place, then return rmsnorm(residual) * weight."""
|
| 36 |
+
assert residual.dtype == torch.float32 and residual.is_contiguous()
|
| 37 |
+
orig = residual.shape
|
| 38 |
+
r_flat = residual.view(-1, orig[-1])
|
| 39 |
+
x_flat = x.reshape(-1, orig[-1]).to(torch.float32).contiguous()
|
| 40 |
+
out = torch.empty_like(r_flat)
|
| 41 |
+
ops.do_add_rmsnorm(out, r_flat, x_flat, weight.to(torch.float32).contiguous(), eps)
|
| 42 |
+
return out.view(orig)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def rope(q: torch.Tensor, k: torch.Tensor, pos: int, theta: float = 10000.0,
|
| 46 |
+
interleaved: bool = False) -> None:
|
| 47 |
+
"""Rotate q [H, D] and k [Hkv, D] in place for one position."""
|
| 48 |
+
ops.do_rope(q, k, pos, theta, interleaved)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def silu_mul(gate: torch.Tensor, up: torch.Tensor) -> torch.Tensor:
|
| 52 |
+
"""out = silu(gate) * up, f32."""
|
| 53 |
+
g = gate.to(torch.float32).contiguous()
|
| 54 |
+
u = up.to(torch.float32).contiguous()
|
| 55 |
+
out = torch.empty_like(g)
|
| 56 |
+
ops.do_silu_mul(out, g, u)
|
| 57 |
+
return out
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def argmax(logits: torch.Tensor) -> int:
|
| 61 |
+
"""Greedy token: first index of the maximum."""
|
| 62 |
+
return int(ops.do_argmax(logits.to(torch.float32).contiguous()))
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def sample(logits: torch.Tensor, temperature: float = 1.0, top_k: int = 0,
|
| 66 |
+
top_p: float = 1.0, generator: Optional[torch.Generator] = None) -> int:
|
| 67 |
+
"""Deterministic sampling given the generator state.
|
| 68 |
+
|
| 69 |
+
HF warper order: temperature, top-k, then top-p over the renormalized
|
| 70 |
+
survivors; inverse CDF at one uniform draw. temperature <= 0 is greedy.
|
| 71 |
+
"""
|
| 72 |
+
u = float(torch.rand(1, generator=generator).item())
|
| 73 |
+
return int(ops.do_sample(logits.to(torch.float32).contiguous(),
|
| 74 |
+
temperature, top_k, top_p, u))
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
__all__ = ["rmsnorm", "add_rmsnorm", "rope", "silu_mul", "argmax", "sample"]
|
build/torch211-cxx11-cpu-x86_64-linux/_decode_ops_cpu_8c2646d.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fb8aa5aadc7b44ff71835c9995ca58d1310da456a349fc232a52265b48fdfbae
|
| 3 |
+
size 126696
|
build/torch211-cxx11-cpu-x86_64-linux/_ops.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from . import _decode_ops_cpu_8c2646d
|
| 3 |
+
ops = torch.ops._decode_ops_cpu_8c2646d
|
| 4 |
+
|
| 5 |
+
def add_op_namespace_prefix(op_name: str):
|
| 6 |
+
"""
|
| 7 |
+
Prefix op by namespace.
|
| 8 |
+
"""
|
| 9 |
+
return f"_decode_ops_cpu_8c2646d::{op_name}"
|
build/torch211-cxx11-cpu-x86_64-linux/decode_ops/__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-cpu-x86_64-linux/metadata.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "decode-ops",
|
| 3 |
+
"id": "_decode_ops_cpu_8c2646d",
|
| 4 |
+
"version": 1,
|
| 5 |
+
"license": "Apache-2.0",
|
| 6 |
+
"python-depends": [],
|
| 7 |
+
"backend": {
|
| 8 |
+
"type": "cpu"
|
| 9 |
+
},
|
| 10 |
+
"digest": {
|
| 11 |
+
"algorithm": "sha256",
|
| 12 |
+
"files": {
|
| 13 |
+
"__init__.py": "MN0BI7XRM8J/dKPXlvcpR6hCSFo6MSaPwuXe9tJXKYg=",
|
| 14 |
+
"_decode_ops_cpu_8c2646d.abi3.so": "+4qlqtx7RP9xg1yZlcpY0TENpFajSfwjKlImW0j9+64=",
|
| 15 |
+
"_ops.py": "gqDMeOOEyadaj1GoIyuOqr79beK7adRFQfWU7iLMaLI=",
|
| 16 |
+
"decode_ops/__init__.py": "DFYPlrhXwYjEqCl/8n0SmWGZV8NFml5DPhMjKfv98GY="
|
| 17 |
+
}
|
| 18 |
+
},
|
| 19 |
+
"provenance": {
|
| 20 |
+
"kernel-builder": {
|
| 21 |
+
"version": "0.17.0-dev0",
|
| 22 |
+
"sha": "19aaa6421e674e9fecc352bbae6eab81d19a6bf4",
|
| 23 |
+
"dirty": false
|
| 24 |
+
},
|
| 25 |
+
"kernel": {
|
| 26 |
+
"sha": "8c2646dfb2cb907a897461070385e5df56f8c57e",
|
| 27 |
+
"dirty": false
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
}
|
build/torch212-cxx11-cpu-x86_64-linux/__init__.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""decode-ops: fused decode-step glue for LLM inference on CPUs, aarch64-first.
|
| 2 |
+
|
| 3 |
+
The non-matmul residue of a decode step as single NEON ops: RMSNorm with a
|
| 4 |
+
fused residual add, in-place RoPE, SiLU-gate, greedy argmax, and
|
| 5 |
+
deterministic temperature / top-k / top-p sampling from an explicit uniform
|
| 6 |
+
draw. Companions: bitnet-cpu and quant-matmul (linears), cpu-attn
|
| 7 |
+
(attention over an int8 KV cache).
|
| 8 |
+
|
| 9 |
+
High-level API:
|
| 10 |
+
rmsnorm(x, weight, eps)
|
| 11 |
+
add_rmsnorm(residual, x, weight, eps) residual += x, then norm (in place)
|
| 12 |
+
rope(q, k, pos, theta, interleaved) in place
|
| 13 |
+
silu_mul(gate, up)
|
| 14 |
+
argmax(logits)
|
| 15 |
+
sample(logits, temperature, top_k, top_p, generator)
|
| 16 |
+
"""
|
| 17 |
+
from typing import Optional
|
| 18 |
+
|
| 19 |
+
import torch
|
| 20 |
+
|
| 21 |
+
from ._ops import ops
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def rmsnorm(x: torch.Tensor, weight: torch.Tensor, eps: float = 1e-6) -> torch.Tensor:
|
| 25 |
+
"""out = x * weight / sqrt(mean(x^2, dim=-1) + eps), f32."""
|
| 26 |
+
orig = x.shape
|
| 27 |
+
x_flat = x.reshape(-1, orig[-1]).to(torch.float32).contiguous()
|
| 28 |
+
out = torch.empty_like(x_flat)
|
| 29 |
+
ops.do_rmsnorm(out, x_flat, weight.to(torch.float32).contiguous(), eps)
|
| 30 |
+
return out.view(orig)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def add_rmsnorm(residual: torch.Tensor, x: torch.Tensor, weight: torch.Tensor,
|
| 34 |
+
eps: float = 1e-6) -> torch.Tensor:
|
| 35 |
+
"""residual += x in place, then return rmsnorm(residual) * weight."""
|
| 36 |
+
assert residual.dtype == torch.float32 and residual.is_contiguous()
|
| 37 |
+
orig = residual.shape
|
| 38 |
+
r_flat = residual.view(-1, orig[-1])
|
| 39 |
+
x_flat = x.reshape(-1, orig[-1]).to(torch.float32).contiguous()
|
| 40 |
+
out = torch.empty_like(r_flat)
|
| 41 |
+
ops.do_add_rmsnorm(out, r_flat, x_flat, weight.to(torch.float32).contiguous(), eps)
|
| 42 |
+
return out.view(orig)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def rope(q: torch.Tensor, k: torch.Tensor, pos: int, theta: float = 10000.0,
|
| 46 |
+
interleaved: bool = False) -> None:
|
| 47 |
+
"""Rotate q [H, D] and k [Hkv, D] in place for one position."""
|
| 48 |
+
ops.do_rope(q, k, pos, theta, interleaved)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def silu_mul(gate: torch.Tensor, up: torch.Tensor) -> torch.Tensor:
|
| 52 |
+
"""out = silu(gate) * up, f32."""
|
| 53 |
+
g = gate.to(torch.float32).contiguous()
|
| 54 |
+
u = up.to(torch.float32).contiguous()
|
| 55 |
+
out = torch.empty_like(g)
|
| 56 |
+
ops.do_silu_mul(out, g, u)
|
| 57 |
+
return out
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def argmax(logits: torch.Tensor) -> int:
|
| 61 |
+
"""Greedy token: first index of the maximum."""
|
| 62 |
+
return int(ops.do_argmax(logits.to(torch.float32).contiguous()))
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def sample(logits: torch.Tensor, temperature: float = 1.0, top_k: int = 0,
|
| 66 |
+
top_p: float = 1.0, generator: Optional[torch.Generator] = None) -> int:
|
| 67 |
+
"""Deterministic sampling given the generator state.
|
| 68 |
+
|
| 69 |
+
HF warper order: temperature, top-k, then top-p over the renormalized
|
| 70 |
+
survivors; inverse CDF at one uniform draw. temperature <= 0 is greedy.
|
| 71 |
+
"""
|
| 72 |
+
u = float(torch.rand(1, generator=generator).item())
|
| 73 |
+
return int(ops.do_sample(logits.to(torch.float32).contiguous(),
|
| 74 |
+
temperature, top_k, top_p, u))
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
__all__ = ["rmsnorm", "add_rmsnorm", "rope", "silu_mul", "argmax", "sample"]
|
build/torch212-cxx11-cpu-x86_64-linux/_decode_ops_cpu_8c2646d.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0d37977e0a4af6dcb09c469602d119c538162daeaf1df10459ac89a2b4293b69
|
| 3 |
+
size 126512
|
build/torch212-cxx11-cpu-x86_64-linux/_ops.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from . import _decode_ops_cpu_8c2646d
|
| 3 |
+
ops = torch.ops._decode_ops_cpu_8c2646d
|
| 4 |
+
|
| 5 |
+
def add_op_namespace_prefix(op_name: str):
|
| 6 |
+
"""
|
| 7 |
+
Prefix op by namespace.
|
| 8 |
+
"""
|
| 9 |
+
return f"_decode_ops_cpu_8c2646d::{op_name}"
|
build/torch212-cxx11-cpu-x86_64-linux/decode_ops/__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-cpu-x86_64-linux/metadata.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "decode-ops",
|
| 3 |
+
"id": "_decode_ops_cpu_8c2646d",
|
| 4 |
+
"version": 1,
|
| 5 |
+
"license": "Apache-2.0",
|
| 6 |
+
"python-depends": [],
|
| 7 |
+
"backend": {
|
| 8 |
+
"type": "cpu"
|
| 9 |
+
},
|
| 10 |
+
"digest": {
|
| 11 |
+
"algorithm": "sha256",
|
| 12 |
+
"files": {
|
| 13 |
+
"__init__.py": "MN0BI7XRM8J/dKPXlvcpR6hCSFo6MSaPwuXe9tJXKYg=",
|
| 14 |
+
"_decode_ops_cpu_8c2646d.abi3.so": "DTeXfgpK9tywnEaWAtEZxTgWLa6vHfEEWayJorQpO2k=",
|
| 15 |
+
"_ops.py": "gqDMeOOEyadaj1GoIyuOqr79beK7adRFQfWU7iLMaLI=",
|
| 16 |
+
"decode_ops/__init__.py": "DFYPlrhXwYjEqCl/8n0SmWGZV8NFml5DPhMjKfv98GY="
|
| 17 |
+
}
|
| 18 |
+
},
|
| 19 |
+
"provenance": {
|
| 20 |
+
"kernel-builder": {
|
| 21 |
+
"version": "0.17.0-dev0",
|
| 22 |
+
"sha": "19aaa6421e674e9fecc352bbae6eab81d19a6bf4",
|
| 23 |
+
"dirty": false
|
| 24 |
+
},
|
| 25 |
+
"kernel": {
|
| 26 |
+
"sha": "8c2646dfb2cb907a897461070385e5df56f8c57e",
|
| 27 |
+
"dirty": false
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
}
|
build/torch213-cxx11-cpu-x86_64-linux/__init__.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""decode-ops: fused decode-step glue for LLM inference on CPUs, aarch64-first.
|
| 2 |
+
|
| 3 |
+
The non-matmul residue of a decode step as single NEON ops: RMSNorm with a
|
| 4 |
+
fused residual add, in-place RoPE, SiLU-gate, greedy argmax, and
|
| 5 |
+
deterministic temperature / top-k / top-p sampling from an explicit uniform
|
| 6 |
+
draw. Companions: bitnet-cpu and quant-matmul (linears), cpu-attn
|
| 7 |
+
(attention over an int8 KV cache).
|
| 8 |
+
|
| 9 |
+
High-level API:
|
| 10 |
+
rmsnorm(x, weight, eps)
|
| 11 |
+
add_rmsnorm(residual, x, weight, eps) residual += x, then norm (in place)
|
| 12 |
+
rope(q, k, pos, theta, interleaved) in place
|
| 13 |
+
silu_mul(gate, up)
|
| 14 |
+
argmax(logits)
|
| 15 |
+
sample(logits, temperature, top_k, top_p, generator)
|
| 16 |
+
"""
|
| 17 |
+
from typing import Optional
|
| 18 |
+
|
| 19 |
+
import torch
|
| 20 |
+
|
| 21 |
+
from ._ops import ops
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def rmsnorm(x: torch.Tensor, weight: torch.Tensor, eps: float = 1e-6) -> torch.Tensor:
|
| 25 |
+
"""out = x * weight / sqrt(mean(x^2, dim=-1) + eps), f32."""
|
| 26 |
+
orig = x.shape
|
| 27 |
+
x_flat = x.reshape(-1, orig[-1]).to(torch.float32).contiguous()
|
| 28 |
+
out = torch.empty_like(x_flat)
|
| 29 |
+
ops.do_rmsnorm(out, x_flat, weight.to(torch.float32).contiguous(), eps)
|
| 30 |
+
return out.view(orig)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def add_rmsnorm(residual: torch.Tensor, x: torch.Tensor, weight: torch.Tensor,
|
| 34 |
+
eps: float = 1e-6) -> torch.Tensor:
|
| 35 |
+
"""residual += x in place, then return rmsnorm(residual) * weight."""
|
| 36 |
+
assert residual.dtype == torch.float32 and residual.is_contiguous()
|
| 37 |
+
orig = residual.shape
|
| 38 |
+
r_flat = residual.view(-1, orig[-1])
|
| 39 |
+
x_flat = x.reshape(-1, orig[-1]).to(torch.float32).contiguous()
|
| 40 |
+
out = torch.empty_like(r_flat)
|
| 41 |
+
ops.do_add_rmsnorm(out, r_flat, x_flat, weight.to(torch.float32).contiguous(), eps)
|
| 42 |
+
return out.view(orig)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def rope(q: torch.Tensor, k: torch.Tensor, pos: int, theta: float = 10000.0,
|
| 46 |
+
interleaved: bool = False) -> None:
|
| 47 |
+
"""Rotate q [H, D] and k [Hkv, D] in place for one position."""
|
| 48 |
+
ops.do_rope(q, k, pos, theta, interleaved)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def silu_mul(gate: torch.Tensor, up: torch.Tensor) -> torch.Tensor:
|
| 52 |
+
"""out = silu(gate) * up, f32."""
|
| 53 |
+
g = gate.to(torch.float32).contiguous()
|
| 54 |
+
u = up.to(torch.float32).contiguous()
|
| 55 |
+
out = torch.empty_like(g)
|
| 56 |
+
ops.do_silu_mul(out, g, u)
|
| 57 |
+
return out
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def argmax(logits: torch.Tensor) -> int:
|
| 61 |
+
"""Greedy token: first index of the maximum."""
|
| 62 |
+
return int(ops.do_argmax(logits.to(torch.float32).contiguous()))
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def sample(logits: torch.Tensor, temperature: float = 1.0, top_k: int = 0,
|
| 66 |
+
top_p: float = 1.0, generator: Optional[torch.Generator] = None) -> int:
|
| 67 |
+
"""Deterministic sampling given the generator state.
|
| 68 |
+
|
| 69 |
+
HF warper order: temperature, top-k, then top-p over the renormalized
|
| 70 |
+
survivors; inverse CDF at one uniform draw. temperature <= 0 is greedy.
|
| 71 |
+
"""
|
| 72 |
+
u = float(torch.rand(1, generator=generator).item())
|
| 73 |
+
return int(ops.do_sample(logits.to(torch.float32).contiguous(),
|
| 74 |
+
temperature, top_k, top_p, u))
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
__all__ = ["rmsnorm", "add_rmsnorm", "rope", "silu_mul", "argmax", "sample"]
|
build/torch213-cxx11-cpu-x86_64-linux/_decode_ops_cpu_8c2646d.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3368481bb338a801dfd148f575a99e274b83aa5a0acbbafb4e279c3b20a196dd
|
| 3 |
+
size 126512
|
build/torch213-cxx11-cpu-x86_64-linux/_ops.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from . import _decode_ops_cpu_8c2646d
|
| 3 |
+
ops = torch.ops._decode_ops_cpu_8c2646d
|
| 4 |
+
|
| 5 |
+
def add_op_namespace_prefix(op_name: str):
|
| 6 |
+
"""
|
| 7 |
+
Prefix op by namespace.
|
| 8 |
+
"""
|
| 9 |
+
return f"_decode_ops_cpu_8c2646d::{op_name}"
|
build/torch213-cxx11-cpu-x86_64-linux/decode_ops/__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/torch213-cxx11-cpu-x86_64-linux/metadata.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "decode-ops",
|
| 3 |
+
"id": "_decode_ops_cpu_8c2646d",
|
| 4 |
+
"version": 1,
|
| 5 |
+
"license": "Apache-2.0",
|
| 6 |
+
"python-depends": [],
|
| 7 |
+
"backend": {
|
| 8 |
+
"type": "cpu"
|
| 9 |
+
},
|
| 10 |
+
"digest": {
|
| 11 |
+
"algorithm": "sha256",
|
| 12 |
+
"files": {
|
| 13 |
+
"__init__.py": "MN0BI7XRM8J/dKPXlvcpR6hCSFo6MSaPwuXe9tJXKYg=",
|
| 14 |
+
"_decode_ops_cpu_8c2646d.abi3.so": "M2hIG7M4qAHf0Uj1dameJ0uDqloKy7r7TiecOyChlt0=",
|
| 15 |
+
"_ops.py": "gqDMeOOEyadaj1GoIyuOqr79beK7adRFQfWU7iLMaLI=",
|
| 16 |
+
"decode_ops/__init__.py": "DFYPlrhXwYjEqCl/8n0SmWGZV8NFml5DPhMjKfv98GY="
|
| 17 |
+
}
|
| 18 |
+
},
|
| 19 |
+
"provenance": {
|
| 20 |
+
"kernel-builder": {
|
| 21 |
+
"version": "0.17.0-dev0",
|
| 22 |
+
"sha": "19aaa6421e674e9fecc352bbae6eab81d19a6bf4",
|
| 23 |
+
"dirty": false
|
| 24 |
+
},
|
| 25 |
+
"kernel": {
|
| 26 |
+
"sha": "8c2646dfb2cb907a897461070385e5df56f8c57e",
|
| 27 |
+
"dirty": false
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
}
|