Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion conversion/deepseek41.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ def index_tensors(self, remote_hf_model_id: str | None = None) -> dict[str, Call
# num_hidden_layers lives in text_config, which is merged only after indexing; MTP stages are skipped anyway
return TextModel.index_tensors(self, remote_hf_model_id=remote_hf_model_id)

def _has_fp8_source_scale(self, name: str) -> bool:
# DeepSeek-V4.1 stores FP8 E8M0 block scales as "<stem>.scale",
# not the generic "<stem>.weight_scale[_inv]" spelling.
return (name.removesuffix(".weight") + ".scale" in self.model_tensors
or super()._has_fp8_source_scale(name))

def set_gguf_parameters(self):
super().set_gguf_parameters()
hp = self.hparams
Expand Down Expand Up @@ -223,7 +229,7 @@ def chunks() -> Iterable[np.ndarray]:
self.gguf_writer.add_tensor(self.format_tensor_name(key, bid), data, raw_dtype=gguf.GGMLQuantizationType.MXFP4)

def _write_engram_tables(self, layer_ids: list[int]) -> None:
# official lookup e4m3 * 2^(e8m0 - 127), exact in BF16
# Preserve native MXFP8 with --outtype auto; explicit output types dequantize.
parts, dim = [], None
for bid in layer_ids:
wn, sn = f"layers.{bid}.engram.embed.weight", f"layers.{bid}.engram.embed.scale"
Expand All @@ -237,6 +243,21 @@ def _write_engram_tables(self, layer_ids: list[int]) -> None:
assert dim is not None
name = self.format_tensor_name(gguf.MODEL_TENSOR.PER_LAYER_TOKEN_EMBD)
n_rows = sum(p[2] for p in parts)
if self._preserve_fp8:
def blocks() -> Iterable[np.ndarray]:
from safetensors import safe_open
for wn, sn, rows, wpart, spart in parts:
with safe_open(wpart, framework="pt") as fw, safe_open(spart, framework="pt") as fs:
ws, ss = fw.get_slice(wn), fs.get_slice(sn)
for r0 in range(0, rows, 1 << 18):
q = ws[r0:r0 + (1 << 18)].view(torch.uint8).reshape(-1, 32).numpy()
e = ss[r0:r0 + (1 << 18)].view(torch.uint8).reshape(-1, 1).numpy()
if (e == 0xFF).any() or ((q & 0x7F) == 0x7F).any():
raise ValueError(f"{wn}: NaN E8M0 scale or E4M3 code")
yield np.concatenate((e, q), axis=1)
data = cast(np.ndarray, _StreamedBytes((n_rows, dim // 32 * 33), blocks))
self.gguf_writer.add_tensor(name, data, raw_dtype=gguf.GGMLQuantizationType.MXFP8)
return

def chunks() -> Iterable[np.ndarray]:
from safetensors import safe_open
Expand Down
4 changes: 3 additions & 1 deletion ggml/include/ggml.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ extern "C" {
GGML_TYPE_Q1_0 = 41,
GGML_TYPE_Q2_0 = 42,
GGML_TYPE_F8_E4M3 = 43,
GGML_TYPE_COUNT = 44,
// packed E8M0 scale + 32 E4M3 values (Engram GET_ROWS only).
GGML_TYPE_MXFP8 = 44,
GGML_TYPE_COUNT = 45,
};

// [TAG_GGML_PREC]
Expand Down
6 changes: 6 additions & 0 deletions ggml/src/ggml-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ typedef struct {
uint8_t qs[QK_MXFP4/2];
} block_mxfp4;
static_assert(sizeof(block_mxfp4) == sizeof(uint8_t) + QK_MXFP4/2, "wrong mxfp4 block size/padding");
#define QK_MXFP8 32
typedef struct {
uint8_t e; // E8M0 exponent shared by 32 OCP E4M3 values
uint8_t qs[QK_MXFP8];
} block_mxfp8;
static_assert(sizeof(block_mxfp8) == 33, "wrong mxfp8 block size/padding");

#define QK_NVFP4 64
#define QK_NVFP4_SUB 16 // sub-block size for per-group scales
Expand Down
10 changes: 10 additions & 0 deletions ggml/src/ggml-cpu/ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ void ggml_compute_forward_add(
case GGML_TYPE_Q5_1:
case GGML_TYPE_Q8_0:
case GGML_TYPE_MXFP4:
case GGML_TYPE_MXFP8:
case GGML_TYPE_NVFP4:
case GGML_TYPE_F8_E4M3:
case GGML_TYPE_Q2_K:
Expand Down Expand Up @@ -1160,6 +1161,10 @@ void ggml_compute_forward_add1(
{
ggml_compute_forward_add1_q_f32(params, dst);
} break;
case GGML_TYPE_MXFP8:
{
GGML_ABORT("ADD1 not supported for MXFP8");
} break;
default:
{
GGML_ABORT("fatal error");
Expand Down Expand Up @@ -1271,6 +1276,7 @@ void ggml_compute_forward_acc(
case GGML_TYPE_Q8_0:
case GGML_TYPE_Q8_1:
case GGML_TYPE_MXFP4:
case GGML_TYPE_MXFP8:
case GGML_TYPE_NVFP4:
case GGML_TYPE_F8_E4M3:
case GGML_TYPE_Q2_K:
Expand Down Expand Up @@ -5011,6 +5017,7 @@ void ggml_compute_forward_out_prod(
case GGML_TYPE_Q5_1:
case GGML_TYPE_Q8_0:
case GGML_TYPE_MXFP4:
case GGML_TYPE_MXFP8:
case GGML_TYPE_NVFP4:
case GGML_TYPE_F8_E4M3:
case GGML_TYPE_Q2_K:
Expand Down Expand Up @@ -5342,6 +5349,7 @@ void ggml_compute_forward_set(
case GGML_TYPE_Q8_0:
case GGML_TYPE_Q8_1:
case GGML_TYPE_MXFP4:
case GGML_TYPE_MXFP8:
case GGML_TYPE_NVFP4:
case GGML_TYPE_F8_E4M3:
case GGML_TYPE_Q2_K:
Expand Down Expand Up @@ -5568,6 +5576,7 @@ void ggml_compute_forward_get_rows(
case GGML_TYPE_Q8_0:
case GGML_TYPE_Q8_1:
case GGML_TYPE_MXFP4:
case GGML_TYPE_MXFP8:
case GGML_TYPE_NVFP4:
case GGML_TYPE_F8_E4M3:
case GGML_TYPE_Q2_K:
Expand Down Expand Up @@ -6275,6 +6284,7 @@ void ggml_compute_forward_clamp(
case GGML_TYPE_Q8_0:
case GGML_TYPE_Q8_1:
case GGML_TYPE_MXFP4:
case GGML_TYPE_MXFP8:
case GGML_TYPE_NVFP4:
case GGML_TYPE_F8_E4M3:
case GGML_TYPE_Q2_K:
Expand Down
4 changes: 4 additions & 0 deletions ggml/src/ggml-cpu/quants.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ void quantize_row_mxfp4(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, i
quantize_row_mxfp4_ref(x, y, k);
}

void quantize_row_mxfp8(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k) {
quantize_row_mxfp8_ref(x, y, k);
}

void quantize_row_nvfp4(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k) {
quantize_row_nvfp4_ref(x, y, k);
}
Expand Down
1 change: 1 addition & 0 deletions ggml/src/ggml-cpu/quants.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void quantize_row_q8_0(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, in
void quantize_row_q8_1(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);

void quantize_row_mxfp4(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);
void quantize_row_mxfp8(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);
void quantize_row_nvfp4(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);
void quantize_row_f8_e4m3(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);

Expand Down
9 changes: 9 additions & 0 deletions ggml/src/ggml-cuda/dequantize.cuh
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#include "common.cuh"
#include "convert.cuh"

// Gather-only MXFP8 Engram block: E8M0 scale, then 32 E4M3 values.
static __device__ __forceinline__ void dequantize_mxfp8(
const void * vx, const int64_t ib, const int iqs, float2 & v) {
const block_mxfp8 * x = (const block_mxfp8 *) vx;
const float scale = x[ib].e == 0xFF ? NAN : ggml_cuda_e8m0_to_fp32(x[ib].e);
v.x = ggml_cuda_f8_e4m3_to_fp32(x[ib].qs[iqs ]) * scale;
v.y = ggml_cuda_f8_e4m3_to_fp32(x[ib].qs[iqs + 1]) * scale;
}

static __device__ __forceinline__ void dequantize_q1_0(const void * vx, const int64_t ib, const int iqs, float2 & v){
const block_q1_0 * x = (const block_q1_0 *) vx;

Expand Down
4 changes: 4 additions & 0 deletions ggml/src/ggml-cuda/getrows.cu
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ static void ggml_cuda_get_rows_switch_src0_type(
get_rows_cuda_kq<32, dst_t, dequantize_mxfp4<dst_t>>(src0_d, src1_d, dst_d,
ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
break;
case GGML_TYPE_MXFP8:
get_rows_cuda_q<QK_MXFP8, 1, dequantize_mxfp8>(src0_d, src1_d, dst_d,
ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
break;
default:
GGML_ABORT("%s: unsupported src0 type: %s\n", __func__, ggml_type_name(src0_type));
}
Expand Down
1 change: 1 addition & 0 deletions ggml/src/ggml-cuda/ggml-cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4305,6 +4305,7 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g
case GGML_TYPE_Q5_1:
case GGML_TYPE_Q8_0:
case GGML_TYPE_F8_E4M3:
case GGML_TYPE_MXFP8:
case GGML_TYPE_Q2_K:
case GGML_TYPE_Q3_K:
case GGML_TYPE_Q4_K:
Expand Down
63 changes: 63 additions & 0 deletions ggml/src/ggml-quants.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,34 @@ static inline int best_index_mxfp4(float x, float e) {
return best_index;
}

// Reference encoder for a 32-value OCP MXFP8 block (E8M0 + 32 E4M3).
void quantize_row_mxfp8_ref(const float * GGML_RESTRICT x, block_mxfp8 * GGML_RESTRICT y, int64_t k) {
static const int qk = QK_MXFP8;

assert(k % qk == 0);

const int64_t nb = k / qk;
for (int64_t i = 0; i < nb; i++) {
float amax = 0.0f;
for (int j = 0; j < qk; j++) {
amax = MAX(amax, fabsf(x[i*qk + j]));
}
// Smallest power-of-two scale with amax / scale <= 448 (largest finite E4M3).
int e = 0;
if (amax > 0.0f) {
e = (int) ceilf(log2f(amax / 448.0f)) + 127;
e = MAX(0, MIN(254, e));
}
const float d = GGML_E8M0_TO_FP32((uint8_t) e);
const float id = 1.0f/d;

y[i].e = (uint8_t) e;
for (int j = 0; j < qk; ++j) {
// Upstream's E4M3 encoder replaces the equivalent fork-local helper.
y[i].qs[j] = ggml_fp32_to_f8_e4m3(x[i*qk + j]*id);
}
}
}
void quantize_row_mxfp4_ref(const float * GGML_RESTRICT x, block_mxfp4 * GGML_RESTRICT y, int64_t k) {
static const int qk = QK_MXFP4;

Expand Down Expand Up @@ -589,6 +617,17 @@ void dequantize_row_mxfp4(const block_mxfp4 * GGML_RESTRICT x, float * GGML_REST
}
}

// MXFP8 stores one E8M0 scale followed by 32 OCP E4M3 bytes per block.
// Unlike MXFP4, the E4M3 values are *not* stored doubled.
void dequantize_row_mxfp8(const block_mxfp8 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
assert(k % QK_MXFP8 == 0);
for (int64_t i = 0; i < k / QK_MXFP8; ++i) {
const float scale = x[i].e == 0xFF ? NAN : GGML_E8M0_TO_FP32(x[i].e);
for (int j = 0; j < QK_MXFP8; ++j) {
y[i*QK_MXFP8 + j] = ggml_f8_e4m3_to_fp32(x[i].qs[j]) * scale;
}
}
}
void dequantize_row_nvfp4(const block_nvfp4 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
static const int qk = QK_NVFP4;
static const int qk_sub = QK_NVFP4_SUB;
Expand Down Expand Up @@ -2315,6 +2354,12 @@ size_t quantize_mxfp4(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst,
return nrow * ggml_row_size(GGML_TYPE_MXFP4, n_per_row);
}

size_t quantize_mxfp8(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
GGML_UNUSED(quant_weights);
quantize_row_mxfp8_ref(src, dst, (int64_t)nrow*n_per_row);
return nrow * ggml_row_size(GGML_TYPE_MXFP8, n_per_row);
}

size_t quantize_nvfp4(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
GGML_UNUSED(quant_weights);
quantize_row_nvfp4_ref(src, dst, (int64_t)nrow*n_per_row);
Expand Down Expand Up @@ -5491,6 +5536,20 @@ static bool validate_e_e8m0(uint8_t e, size_t i) {
} \
}

#define VALIDATE_ROW_DATA_MXFP8_IMPL(type, data, nb) \
const type * q = (const type *) (data); \
for (size_t i = 0; i < (nb); ++i) { \
if (!validate_e_e8m0(q[i].e, i)) { \
return false; \
} \
for (int j = 0; j < QK_MXFP8; ++j) { \
if ((q[i].qs[j] & 0x7f) == 0x7f) { \
fprintf(stderr, "%s: found NaN E4M3 code at block %zu, element %d\n", __func__, i, j); \
return false; \
} \
} \
}

#define VALIDATE_ROW_DATA_DVEC_F16_IMPL(type, data, nb, nr) \
const type * q = (const type *) (data); \
for (size_t i = 0; i < (nb); ++i) { \
Expand Down Expand Up @@ -5656,6 +5715,10 @@ bool ggml_validate_row_data(enum ggml_type type, const void * data, size_t nbyte
{
VALIDATE_ROW_DATA_E_E8M0_IMPL(block_mxfp4, data, nb);
} break;
case GGML_TYPE_MXFP8:
{
VALIDATE_ROW_DATA_MXFP8_IMPL(block_mxfp8, data, nb);
} break;
case GGML_TYPE_NVFP4:
{
// TODO: validate each unsigned E4M3 scale; NaN encodings and scales with the sign bit set are invalid.
Expand Down
3 changes: 3 additions & 0 deletions ggml/src/ggml-quants.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ GGML_API void quantize_row_q8_0_ref(const float * GGML_RESTRICT x, block_q8_0 *
GGML_API void quantize_row_q8_1_ref(const float * GGML_RESTRICT x, block_q8_1 * GGML_RESTRICT y, int64_t k);

GGML_API void quantize_row_mxfp4_ref(const float * GGML_RESTRICT x, block_mxfp4 * GGML_RESTRICT y, int64_t k);
GGML_API void quantize_row_mxfp8_ref(const float * GGML_RESTRICT x, block_mxfp8 * GGML_RESTRICT y, int64_t k);
GGML_API void quantize_row_nvfp4_ref(const float * GGML_RESTRICT x, block_nvfp4 * GGML_RESTRICT y, int64_t k);
GGML_API void quantize_row_f8_e4m3_ref(const float * GGML_RESTRICT x, ggml_fp8_e4m3_t * GGML_RESTRICT y, int64_t k);

Expand Down Expand Up @@ -54,6 +55,7 @@ GGML_API void dequantize_row_q8_0(const block_q8_0 * GGML_RESTRICT x, float * GG
//GGML_API void dequantize_row_q8_1(const block_q8_1 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k);

GGML_API void dequantize_row_mxfp4(const block_mxfp4 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k);
GGML_API void dequantize_row_mxfp8(const block_mxfp8 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k);
GGML_API void dequantize_row_nvfp4(const block_nvfp4 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k);
GGML_API void dequantize_row_f8_e4m3(const ggml_fp8_e4m3_t * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k);

Expand Down Expand Up @@ -105,6 +107,7 @@ GGML_API size_t quantize_q5_1(const float * GGML_RESTRICT src, void * GGML_RESTR
GGML_API size_t quantize_q8_0(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrows, int64_t n_per_row, const float * imatrix);

GGML_API size_t quantize_mxfp4(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrows, int64_t n_per_row, const float * imatrix);
GGML_API size_t quantize_mxfp8(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrows, int64_t n_per_row, const float * imatrix);
GGML_API size_t quantize_nvfp4(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrows, int64_t n_per_row, const float * imatrix);
GGML_API size_t quantize_f8_e4m3(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrows, int64_t n_per_row, const float * imatrix);

Expand Down
9 changes: 9 additions & 0 deletions ggml/src/ggml.c
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,14 @@ static const struct ggml_type_traits type_traits[GGML_TYPE_COUNT] = {
.to_float = (ggml_to_float_t) dequantize_row_mxfp4,
.from_float_ref = (ggml_from_float_t)quantize_row_mxfp4_ref,
},
[GGML_TYPE_MXFP8] = {
.type_name = "mxfp8",
.blck_size = QK_MXFP8,
.type_size = sizeof(block_mxfp8),
.is_quantized = true,
.to_float = (ggml_to_float_t) dequantize_row_mxfp8,
.from_float_ref = (ggml_from_float_t) quantize_row_mxfp8_ref,
},
[GGML_TYPE_NVFP4] = {
.type_name = "nvfp4",
.blck_size = QK_NVFP4,
Expand Down Expand Up @@ -8290,6 +8298,7 @@ size_t ggml_quantize_chunk(
case GGML_TYPE_Q5_1: result = quantize_q5_1 (src + start, (char *) dst + start_row * row_size, nrows, n_per_row, imatrix); break;
case GGML_TYPE_Q8_0: result = quantize_q8_0 (src + start, (char *) dst + start_row * row_size, nrows, n_per_row, imatrix); break;
case GGML_TYPE_MXFP4: result = quantize_mxfp4 (src + start, (char *) dst + start_row * row_size, nrows, n_per_row, imatrix); break;
case GGML_TYPE_MXFP8: result = quantize_mxfp8 (src + start, (char *) dst + start_row * row_size, nrows, n_per_row, imatrix); break;
case GGML_TYPE_NVFP4: result = quantize_nvfp4 (src + start, (char *) dst + start_row * row_size, nrows, n_per_row, imatrix); break;
case GGML_TYPE_F8_E4M3: result = quantize_f8_e4m3(src + start, (char *) dst + start_row * row_size, nrows, n_per_row, imatrix); break;
case GGML_TYPE_Q2_K: result = quantize_q2_K (src + start, (char *) dst + start_row * row_size, nrows, n_per_row, imatrix); break;
Expand Down
2 changes: 2 additions & 0 deletions gguf-py/gguf/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7511,6 +7511,7 @@ class GGMLQuantizationType(IntEnum):
Q1_0 = 41
Q2_0 = 42
F8_E4M3 = 43
MXFP8 = 44


class ExpertGatingFuncType(IntEnum):
Expand Down Expand Up @@ -7762,6 +7763,7 @@ class VisionProjectorType:
GGMLQuantizationType.Q1_0: (128, 2 + 16),
GGMLQuantizationType.Q2_0: (64, 2 + 16),
GGMLQuantizationType.F8_E4M3: (1, 1),
GGMLQuantizationType.MXFP8: (32, 33),
}


Expand Down
2 changes: 2 additions & 0 deletions src/llama-quant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ static void llama_tensor_dequantize_impl(ggml_type type,
// do we allow this tensor to be quantized?
static bool tensor_allows_quantization(const llama_model_quantize_params * params, llm_arch arch, const ggml_tensor * tensor) {
if (params->only_copy) { return false; }
// Preserve the original MXFP8 Engram bytes, despite the reference encoder.
if (tensor->type == GGML_TYPE_MXFP8) { return false; }

// quantize only 2D+ tensors (matrices, experts, conv kernels)
if (ggml_n_dims(tensor) < 2) { return false; }
Expand Down
9 changes: 9 additions & 0 deletions tests/test-backend-ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12313,6 +12313,15 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
for (int n : {1, 3, 257}) {
test_cases.emplace_back(new test_get_rows(GGML_TYPE_F8_E4M3, n, 5, 3, 1, 1, false));
}
// MXFP8 is used for packed Engram lookup tables (GET_ROWS only, no MUL_MAT).
// Exercise single/multiple blocks, batched indices, and a view of the indices.
for (int n : {32, 64, 160}) {
test_cases.emplace_back(new test_get_rows(GGML_TYPE_MXFP8, n, 13, 7, 1, 1, false));
test_cases.emplace_back(new test_get_rows(GGML_TYPE_MXFP8, n, 13, 7, 2, 1, true));
}
for (int r : {1, 128}) {
test_cases.emplace_back(new test_get_rows(GGML_TYPE_MXFP8, 160, 4096, r, 1, 1, false));
}
// many rows of a wide table, as a per-layer embedding lookup does
test_cases.emplace_back(new test_get_rows(GGML_TYPE_F8_E4M3, 160, 100000, 32768, 1, 1, false));
// past the fused dequant GEMM's column limit the reordered weight goes through the conversion kernels
Expand Down
Loading
Loading