Skip to content

Commit 3ca7c36

Browse files
authored
Merge branch 'main' into perf/matmulnbits-accuracy-level
2 parents 0adc247 + 57177b7 commit 3ca7c36

10 files changed

Lines changed: 679 additions & 203 deletions

File tree

src/mobius/__main__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,10 @@ def _cmd_build_gguf(args: argparse.Namespace) -> None:
351351
raise SystemExit(1)
352352

353353
if args.keep_quantized:
354-
print("Quantized mode: preserving GGUF quantization as MatMulNBits...")
354+
print(
355+
"Quantized mode: preserving GGUF quantization as "
356+
"MatMulNBits/GatherBlockQuantized..."
357+
)
355358

356359
gguf_path = args.gguf_path
357360
output_dir = args.output or os.path.splitext(gguf_path)[0] + "_onnx"
@@ -594,7 +597,10 @@ def main(argv: list[str] | None = None) -> None:
594597
gguf_parser.add_argument(
595598
"--keep-quantized",
596599
action="store_true",
597-
help="Preserve quantization via MatMulNBits (Q4_0/Q4_1/Q8_0).",
600+
help=(
601+
"Preserve supported projection, output-head, and embedding "
602+
"quantization via MatMulNBits/GatherBlockQuantized."
603+
),
598604
)
599605
gguf_parser.add_argument(
600606
"--dtype",

src/mobius/_configs/_quantization.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ class QuantizationConfig:
2727
# (e.g. Tencent SEQ uses 1.5).
2828
float_zero_point: bool = False
2929
# When True, the input embedding table is block-wise quantized and is
30-
# looked up with GatherBlockQuantized instead of a plain Gather. Set by
31-
# Olive RTN exports that pass ``embeds: true``.
30+
# looked up with GatherBlockQuantized instead of a plain Gather. Used by
31+
# Olive RTN exports and quantized GGUF imports.
3232
quantize_embeddings: bool = False
3333
# When True, the LM head projection is block-wise quantized (MatMulNBits).
34-
# Set by Olive RTN exports that pass ``lm_head: true``.
34+
# Used by Olive RTN exports and quantized GGUF imports.
3535
quantize_lm_head: bool = False
3636
# When True, the input embedding and LM head share one weight table. Olive
3737
# RTN records this in its own config (``tie_word_embeddings``) and may clear

src/mobius/components/_quantized_linear.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ def __init__(
212212

213213
self._bits = bits
214214
self._block_size = block_size
215+
self._embedding_dim = embedding_dim
215216
self.padding_idx = padding_idx
216217

217218
n_blocks = embedding_dim // block_size
@@ -245,14 +246,18 @@ def forward(self, op: OpBuilder, input_ids: ir.Value) -> ir.Value:
245246
if self.zero_points is not None:
246247
inputs.append(self.zero_points)
247248

248-
return op.GatherBlockQuantized(
249+
result = op.GatherBlockQuantized(
249250
*inputs,
250251
bits=self._bits,
251252
block_size=self._block_size,
252253
gather_axis=0,
253254
quantize_axis=1,
254255
_domain=_MICROSOFT_DOMAIN,
255256
)
257+
result.dtype = self.scales.dtype
258+
if input_ids.shape is not None:
259+
result.shape = ir.Shape([*input_ids.shape, self._embedding_dim])
260+
return result
256261

257262

258263
class TiedQuantizedLMHead(nn.Module):

src/mobius/components/_quantized_linear_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ def test_graph_has_gather_block_quantized_node(self):
354354
result = qe(op, ids)
355355
b._adapt_outputs([result], "")
356356
assert count_op_type(graph, "GatherBlockQuantized") == 1
357+
assert result.dtype == ir.DataType.FLOAT
358+
assert result.shape == ir.Shape([1, 4, self.DIM])
357359

358360
def test_node_domain_and_attributes(self):
359361
import onnx_ir as ir

0 commit comments

Comments
 (0)