Uploaded using `kernel-builder`.
Browse files- build/torch212-metal-aarch64-darwin/__init__.py +62 -0
- build/torch212-metal-aarch64-darwin/_gguf_kernels_metal_5d3209c_dirty.abi3.so +3 -0
- build/torch212-metal-aarch64-darwin/_ops.py +9 -0
- build/torch212-metal-aarch64-darwin/metadata.json +29 -0
- build/torch213-metal-aarch64-darwin/__init__.py +62 -0
- build/torch213-metal-aarch64-darwin/_gguf_kernels_metal_5d3209c_dirty.abi3.so +3 -0
- build/torch213-metal-aarch64-darwin/_ops.py +9 -0
- build/torch213-metal-aarch64-darwin/metadata.json +29 -0
build/torch212-metal-aarch64-darwin/__init__.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Compute directly on the packed blocks of a GGUF checkpoint.
|
| 2 |
+
|
| 3 |
+
A GGUF weight is stored as blocks of 32 or 256 values sharing a scale. These kernels read that
|
| 4 |
+
layout as it is, so a quantized model can be loaded and run without ever materializing a dense
|
| 5 |
+
copy — which is the whole memory saving.
|
| 6 |
+
|
| 7 |
+
Ported from llama.cpp's ggml-cuda; `vendor/UPSTREAM` pins the revision. The ops are named for what
|
| 8 |
+
they do rather than for a backend: each backend registers its own implementation of the same
|
| 9 |
+
schema, so calls dispatch on the tensor's device.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import torch
|
| 13 |
+
|
| 14 |
+
from ._ops import add_op_namespace_prefix, ops
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
__all__ = ["GEMV_TYPES", "MAX_GEMV_ROWS", "dequantize", "mul_mat_vec"]
|
| 18 |
+
|
| 19 |
+
# Upstream's MMVQ_MAX_BATCH_SIZE: `mul_mat_vec` has no implementation beyond this many rows, so a
|
| 20 |
+
# caller with more (prefill) dequantizes and uses an ordinary matmul.
|
| 21 |
+
MAX_GEMV_ROWS = 8
|
| 22 |
+
|
| 23 |
+
# ggml type ids `mul_mat_vec` implements, as this build reports them -- it is backend-specific, and
|
| 24 |
+
# a type routed into a gemv that has no kernel for it is a fault, not a fallback. CUDA covers
|
| 25 |
+
# Q4_0/Q4_1/Q5_0/Q5_1/Q8_0, the K quants, the IQ quants, MXFP4/NVFP4 and Q1_0/Q2_0; Metal is the
|
| 26 |
+
# same minus NVFP4/Q1_0/Q2_0, which its metallib has no kernels for. `dequantize` covers more, so
|
| 27 |
+
# check this before choosing the fused path.
|
| 28 |
+
try:
|
| 29 |
+
GEMV_TYPES = frozenset(ops.gemv_types())
|
| 30 |
+
except AttributeError:
|
| 31 |
+
# A build published before `gemv_types` existed; those are CUDA-only, so its list is theirs.
|
| 32 |
+
GEMV_TYPES = frozenset({2, 3, 6, 7, 8, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 29, 39, 40, 41, 42})
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def dequantize(
|
| 36 |
+
blocks: torch.Tensor, ggml_type: int, rows: int, cols: int, dtype: torch.dtype
|
| 37 |
+
) -> torch.Tensor:
|
| 38 |
+
"""`(rows, bytes_per_row)` uint8 blocks -> `(rows, cols)` values of `dtype`."""
|
| 39 |
+
return ops.dequantize(blocks, ggml_type, rows, cols, dtype)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def mul_mat_vec(
|
| 43 |
+
blocks: torch.Tensor, x: torch.Tensor, ggml_type: int, out_features: int
|
| 44 |
+
) -> torch.Tensor:
|
| 45 |
+
"""Fused dequantize-gemv: `x @ blocks.T` without unpacking `blocks`.
|
| 46 |
+
|
| 47 |
+
`x` is `(rows, in_features)` and `rows` must be at most `MAX_GEMV_ROWS`. The result is f32
|
| 48 |
+
whatever `x`'s dtype was, since the kernel writes an f32 destination; cast it at the call site,
|
| 49 |
+
where the cast can be fused into whatever consumes it.
|
| 50 |
+
"""
|
| 51 |
+
return ops.mul_mat_vec(blocks, x, ggml_type, out_features)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# Without these, torch.compile cannot trace the ops and breaks the graph at every call.
|
| 55 |
+
@torch.library.register_fake(add_op_namespace_prefix("dequantize"))
|
| 56 |
+
def _dequantize_fake(blocks, ggml_type, rows, cols, dtype):
|
| 57 |
+
return blocks.new_empty((rows, cols), dtype=dtype)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
@torch.library.register_fake(add_op_namespace_prefix("mul_mat_vec"))
|
| 61 |
+
def _mul_mat_vec_fake(blocks, x, ggml_type, out_features):
|
| 62 |
+
return x.new_empty((x.shape[0], out_features), dtype=torch.float32)
|
build/torch212-metal-aarch64-darwin/_gguf_kernels_metal_5d3209c_dirty.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:416d3b6d1969700652047a0d373f6f3025d312a1f044cdf83bdeb96d99b04246
|
| 3 |
+
size 7403160
|
build/torch212-metal-aarch64-darwin/_ops.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from . import _gguf_kernels_metal_5d3209c_dirty
|
| 3 |
+
ops = torch.ops._gguf_kernels_metal_5d3209c_dirty
|
| 4 |
+
|
| 5 |
+
def add_op_namespace_prefix(op_name: str):
|
| 6 |
+
"""
|
| 7 |
+
Prefix op by namespace.
|
| 8 |
+
"""
|
| 9 |
+
return f"_gguf_kernels_metal_5d3209c_dirty::{op_name}"
|
build/torch212-metal-aarch64-darwin/metadata.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gguf-kernels",
|
| 3 |
+
"id": "_gguf_kernels_metal_5d3209c_dirty",
|
| 4 |
+
"version": 1,
|
| 5 |
+
"license": "MIT",
|
| 6 |
+
"python-depends": [],
|
| 7 |
+
"backend": {
|
| 8 |
+
"type": "metal"
|
| 9 |
+
},
|
| 10 |
+
"digest": {
|
| 11 |
+
"algorithm": "sha256",
|
| 12 |
+
"files": {
|
| 13 |
+
"__init__.py": "GoFcHJbbJ9C8b40XNznMxsTcC+CV9kaFE8a1DP763kU=",
|
| 14 |
+
"_gguf_kernels_metal_5d3209c_dirty.abi3.so": "QW07bRlpcAZSBHoNNz9vMCXTEqHwRM34O965bZmwQkY=",
|
| 15 |
+
"_ops.py": "P0YXhjRkGKAKtdyb3TiOjCFsI4YDDGkzttoDI2JiJHg="
|
| 16 |
+
}
|
| 17 |
+
},
|
| 18 |
+
"provenance": {
|
| 19 |
+
"kernel-builder": {
|
| 20 |
+
"version": "0.17.0-dev0",
|
| 21 |
+
"sha": "81f55ea30fd8f819dcf93a3c934dd584c895bd2f",
|
| 22 |
+
"dirty": false
|
| 23 |
+
},
|
| 24 |
+
"kernel": {
|
| 25 |
+
"sha": "5d3209c5797b32001022de342dad3b792a94e921",
|
| 26 |
+
"dirty": true
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
}
|
build/torch213-metal-aarch64-darwin/__init__.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Compute directly on the packed blocks of a GGUF checkpoint.
|
| 2 |
+
|
| 3 |
+
A GGUF weight is stored as blocks of 32 or 256 values sharing a scale. These kernels read that
|
| 4 |
+
layout as it is, so a quantized model can be loaded and run without ever materializing a dense
|
| 5 |
+
copy — which is the whole memory saving.
|
| 6 |
+
|
| 7 |
+
Ported from llama.cpp's ggml-cuda; `vendor/UPSTREAM` pins the revision. The ops are named for what
|
| 8 |
+
they do rather than for a backend: each backend registers its own implementation of the same
|
| 9 |
+
schema, so calls dispatch on the tensor's device.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import torch
|
| 13 |
+
|
| 14 |
+
from ._ops import add_op_namespace_prefix, ops
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
__all__ = ["GEMV_TYPES", "MAX_GEMV_ROWS", "dequantize", "mul_mat_vec"]
|
| 18 |
+
|
| 19 |
+
# Upstream's MMVQ_MAX_BATCH_SIZE: `mul_mat_vec` has no implementation beyond this many rows, so a
|
| 20 |
+
# caller with more (prefill) dequantizes and uses an ordinary matmul.
|
| 21 |
+
MAX_GEMV_ROWS = 8
|
| 22 |
+
|
| 23 |
+
# ggml type ids `mul_mat_vec` implements, as this build reports them -- it is backend-specific, and
|
| 24 |
+
# a type routed into a gemv that has no kernel for it is a fault, not a fallback. CUDA covers
|
| 25 |
+
# Q4_0/Q4_1/Q5_0/Q5_1/Q8_0, the K quants, the IQ quants, MXFP4/NVFP4 and Q1_0/Q2_0; Metal is the
|
| 26 |
+
# same minus NVFP4/Q1_0/Q2_0, which its metallib has no kernels for. `dequantize` covers more, so
|
| 27 |
+
# check this before choosing the fused path.
|
| 28 |
+
try:
|
| 29 |
+
GEMV_TYPES = frozenset(ops.gemv_types())
|
| 30 |
+
except AttributeError:
|
| 31 |
+
# A build published before `gemv_types` existed; those are CUDA-only, so its list is theirs.
|
| 32 |
+
GEMV_TYPES = frozenset({2, 3, 6, 7, 8, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 29, 39, 40, 41, 42})
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def dequantize(
|
| 36 |
+
blocks: torch.Tensor, ggml_type: int, rows: int, cols: int, dtype: torch.dtype
|
| 37 |
+
) -> torch.Tensor:
|
| 38 |
+
"""`(rows, bytes_per_row)` uint8 blocks -> `(rows, cols)` values of `dtype`."""
|
| 39 |
+
return ops.dequantize(blocks, ggml_type, rows, cols, dtype)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def mul_mat_vec(
|
| 43 |
+
blocks: torch.Tensor, x: torch.Tensor, ggml_type: int, out_features: int
|
| 44 |
+
) -> torch.Tensor:
|
| 45 |
+
"""Fused dequantize-gemv: `x @ blocks.T` without unpacking `blocks`.
|
| 46 |
+
|
| 47 |
+
`x` is `(rows, in_features)` and `rows` must be at most `MAX_GEMV_ROWS`. The result is f32
|
| 48 |
+
whatever `x`'s dtype was, since the kernel writes an f32 destination; cast it at the call site,
|
| 49 |
+
where the cast can be fused into whatever consumes it.
|
| 50 |
+
"""
|
| 51 |
+
return ops.mul_mat_vec(blocks, x, ggml_type, out_features)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# Without these, torch.compile cannot trace the ops and breaks the graph at every call.
|
| 55 |
+
@torch.library.register_fake(add_op_namespace_prefix("dequantize"))
|
| 56 |
+
def _dequantize_fake(blocks, ggml_type, rows, cols, dtype):
|
| 57 |
+
return blocks.new_empty((rows, cols), dtype=dtype)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
@torch.library.register_fake(add_op_namespace_prefix("mul_mat_vec"))
|
| 61 |
+
def _mul_mat_vec_fake(blocks, x, ggml_type, out_features):
|
| 62 |
+
return x.new_empty((x.shape[0], out_features), dtype=torch.float32)
|
build/torch213-metal-aarch64-darwin/_gguf_kernels_metal_5d3209c_dirty.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2033fc4c4d2457754641c8a895601968c998314db1a11913846215047b80f50c
|
| 3 |
+
size 7403160
|
build/torch213-metal-aarch64-darwin/_ops.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from . import _gguf_kernels_metal_5d3209c_dirty
|
| 3 |
+
ops = torch.ops._gguf_kernels_metal_5d3209c_dirty
|
| 4 |
+
|
| 5 |
+
def add_op_namespace_prefix(op_name: str):
|
| 6 |
+
"""
|
| 7 |
+
Prefix op by namespace.
|
| 8 |
+
"""
|
| 9 |
+
return f"_gguf_kernels_metal_5d3209c_dirty::{op_name}"
|
build/torch213-metal-aarch64-darwin/metadata.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gguf-kernels",
|
| 3 |
+
"id": "_gguf_kernels_metal_5d3209c_dirty",
|
| 4 |
+
"version": 1,
|
| 5 |
+
"license": "MIT",
|
| 6 |
+
"python-depends": [],
|
| 7 |
+
"backend": {
|
| 8 |
+
"type": "metal"
|
| 9 |
+
},
|
| 10 |
+
"digest": {
|
| 11 |
+
"algorithm": "sha256",
|
| 12 |
+
"files": {
|
| 13 |
+
"__init__.py": "GoFcHJbbJ9C8b40XNznMxsTcC+CV9kaFE8a1DP763kU=",
|
| 14 |
+
"_gguf_kernels_metal_5d3209c_dirty.abi3.so": "IDP8TE0kV3VGQciolWAZaMmYMU2xoRkThGIVBHuA9Qw=",
|
| 15 |
+
"_ops.py": "P0YXhjRkGKAKtdyb3TiOjCFsI4YDDGkzttoDI2JiJHg="
|
| 16 |
+
}
|
| 17 |
+
},
|
| 18 |
+
"provenance": {
|
| 19 |
+
"kernel-builder": {
|
| 20 |
+
"version": "0.17.0-dev0",
|
| 21 |
+
"sha": "81f55ea30fd8f819dcf93a3c934dd584c895bd2f",
|
| 22 |
+
"dirty": false
|
| 23 |
+
},
|
| 24 |
+
"kernel": {
|
| 25 |
+
"sha": "5d3209c5797b32001022de342dad3b792a94e921",
|
| 26 |
+
"dirty": true
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
}
|