|
6 | 6 | from __future__ import annotations |
7 | 7 |
|
8 | 8 | import dataclasses |
9 | | -import os |
10 | 9 |
|
11 | 10 | import onnx_ir as ir |
12 | 11 | import pytest |
@@ -484,192 +483,3 @@ def test_decoder_without_source_skips_tokenizer(tmp_path): |
484 | 483 | artifacts = write_onnx_genai_config(object(), str(tmp_path), config=_Cfg()) |
485 | 484 | assert "tokenizer" not in artifacts |
486 | 485 | assert not (tmp_path / "tokenizer.json").exists() |
487 | | - |
488 | | - |
489 | | -class _GraphlessModel: |
490 | | - """Stand-in for a built component whose ONNX graph was streamed to disk. |
491 | | -
|
492 | | - Large decoders built with external-data / streamed weights do not retain |
493 | | - the ``ir.Graph`` in memory, so the in-memory package value exposes no |
494 | | - ``.graph`` attribute. The port contract must instead be derived from the |
495 | | - ``model.onnx`` written next to the sidecar. |
496 | | - """ |
497 | | - |
498 | | - |
499 | | -class _GraphlessPkg(dict): |
500 | | - pass |
501 | | - |
502 | | - |
503 | | -def _ir_value(name: str, dtype: ir.DataType, shape: list[int | str]) -> ir.Value: |
504 | | - return ir.Value(name=name, type=ir.TensorType(dtype), shape=ir.Shape(shape)) |
505 | | - |
506 | | - |
507 | | -def _ir_decoder_model( |
508 | | - inputs: list[ir.Value], |
509 | | - output_specs: list[tuple[str, ir.DataType, list[int | str]]], |
510 | | -) -> ir.Model: |
511 | | - outputs = [_ir_value(*spec) for spec in output_specs] |
512 | | - nodes = [ |
513 | | - ir.Node("", "Identity", [inputs[0]], outputs=[out], name=f"emit_{out.name}") |
514 | | - for out in outputs |
515 | | - ] |
516 | | - graph = ir.Graph( |
517 | | - inputs=inputs, |
518 | | - outputs=outputs, |
519 | | - nodes=nodes, |
520 | | - name="decoder", |
521 | | - opset_imports={"": 21}, |
522 | | - ) |
523 | | - return ir.Model(graph, ir_version=10) |
524 | | - |
525 | | - |
526 | | -@dataclasses.dataclass |
527 | | -class _HybridCfg(_Cfg): |
528 | | - # A hybrid decoder mirroring Qwen3.6-27B: linear-attention layers carrying |
529 | | - # conv_state + recurrent_state interleaved with grouped-query-attention KV |
530 | | - # layers. This is the shape that shipped thin metadata (io block dropped). |
531 | | - layer_types: list[str] | None = dataclasses.field( |
532 | | - default_factory=lambda: [ |
533 | | - "linear_attention", |
534 | | - "linear_attention", |
535 | | - "full_attention", |
536 | | - ] |
537 | | - ) |
538 | | - |
539 | | - |
540 | | -def _write_hybrid_decoder_onnx(directory: str) -> None: |
541 | | - inputs = [ |
542 | | - _ir_value("input_ids", ir.DataType.INT64, ["batch", "sequence"]), |
543 | | - _ir_value("attention_mask", ir.DataType.INT64, ["batch", "past_sequence + sequence"]), |
544 | | - _ir_value("position_ids", ir.DataType.INT64, ["batch", "sequence"]), |
545 | | - ] |
546 | | - output_specs = [("logits", ir.DataType.FLOAT, ["batch", "sequence", 128])] |
547 | | - # Layers 0-1: linear attention (conv_state + recurrent_state replace-state). |
548 | | - for layer in (0, 1): |
549 | | - inputs.extend( |
550 | | - [ |
551 | | - _ir_value( |
552 | | - f"past_key_values.{layer}.conv_state", |
553 | | - ir.DataType.FLOAT, |
554 | | - ["batch", 10240, 3], |
555 | | - ), |
556 | | - _ir_value( |
557 | | - f"past_key_values.{layer}.recurrent_state", |
558 | | - ir.DataType.FLOAT, |
559 | | - ["batch", 48, 128, 128], |
560 | | - ), |
561 | | - ] |
562 | | - ) |
563 | | - output_specs.extend( |
564 | | - [ |
565 | | - (f"present.{layer}.conv_state", ir.DataType.FLOAT, ["batch", 10240, 3]), |
566 | | - ( |
567 | | - f"present.{layer}.recurrent_state", |
568 | | - ir.DataType.FLOAT, |
569 | | - ["batch", 48, 128, 128], |
570 | | - ), |
571 | | - ] |
572 | | - ) |
573 | | - # Layer 2: grouped-query attention (KV append cache). |
574 | | - inputs.extend( |
575 | | - [ |
576 | | - _ir_value( |
577 | | - "past_key_values.2.key", ir.DataType.FLOAT, ["batch", 4, "past_sequence", 256] |
578 | | - ), |
579 | | - _ir_value( |
580 | | - "past_key_values.2.value", |
581 | | - ir.DataType.FLOAT, |
582 | | - ["batch", 4, "past_sequence", 256], |
583 | | - ), |
584 | | - ] |
585 | | - ) |
586 | | - output_specs.extend( |
587 | | - [ |
588 | | - ("present.2.key", ir.DataType.FLOAT, ["batch", 4, "total_sequence", 256]), |
589 | | - ("present.2.value", ir.DataType.FLOAT, ["batch", 4, "total_sequence", 256]), |
590 | | - ] |
591 | | - ) |
592 | | - model = _ir_decoder_model(inputs, output_specs) |
593 | | - ir.save(model, os.path.join(directory, "model.onnx")) |
594 | | - |
595 | | - |
596 | | -def test_graphless_decoder_reloads_io_from_disk(tmp_path): |
597 | | - # Regression: when the in-memory package value lacks `.graph` (external-data / |
598 | | - # streamed build), the sidecar MUST still gain its explicit `model.io` port |
599 | | - # contract by reloading `model.onnx` from disk — never ship thin metadata. |
600 | | - _write_hybrid_decoder_onnx(str(tmp_path)) |
601 | | - pkg = _GraphlessPkg({"model": _GraphlessModel()}) |
602 | | - assert not hasattr(pkg["model"], "graph") |
603 | | - |
604 | | - arts = write_onnx_genai_config(pkg, str(tmp_path), config=_HybridCfg()) |
605 | | - with open(arts["inference_metadata"]) as handle: |
606 | | - meta = yaml.safe_load(handle) |
607 | | - |
608 | | - io = meta["model"]["io"] |
609 | | - assert io["kv_inputs"] == ["past_key_values.2.key", "past_key_values.2.value"] |
610 | | - assert io["kv_outputs"] == ["present.2.key", "present.2.value"] |
611 | | - # The linear-attention layers' conv/recurrent state are replace-state pairs, |
612 | | - # locking the 27B hybrid scenario. |
613 | | - state_inputs = {pair["input"] for pair in io["state_pairs"]} |
614 | | - assert state_inputs == { |
615 | | - "past_key_values.0.conv_state", |
616 | | - "past_key_values.0.recurrent_state", |
617 | | - "past_key_values.1.conv_state", |
618 | | - "past_key_values.1.recurrent_state", |
619 | | - } |
620 | | - assert all(pair["update"] == "replace" for pair in io["state_pairs"]) |
621 | | - assert io["token_input"] == "input_ids" |
622 | | - assert io["logits_output"] == "logits" |
623 | | - |
624 | | - |
625 | | -def test_graphless_decoder_without_disk_model_warns_and_skips(tmp_path, caplog): |
626 | | - # If the graph is unavailable in memory AND absent on disk, we must not ship |
627 | | - # thin metadata silently — emit a loud warning and leave the sidecar without |
628 | | - # the `io` block (downstream runtimes derive it from the graph at load). |
629 | | - import logging |
630 | | - |
631 | | - pkg = _GraphlessPkg({"model": _GraphlessModel()}) |
632 | | - with caplog.at_level(logging.WARNING): |
633 | | - arts = write_onnx_genai_config(pkg, str(tmp_path), config=_Cfg()) |
634 | | - |
635 | | - with open(arts["inference_metadata"]) as handle: |
636 | | - meta = yaml.safe_load(handle) |
637 | | - assert "io" not in meta.get("model", {}) |
638 | | - assert any("model.io" in rec.message for rec in caplog.records) |
639 | | - |
640 | | - |
641 | | -def test_in_memory_graph_is_not_reloaded_from_disk(tmp_path, monkeypatch): |
642 | | - # When `.graph` is present in memory, we must derive io directly and never |
643 | | - # touch the disk reload path (preserving existing fast-path behavior). |
644 | | - from mobius.integrations.onnx_genai import auto_export |
645 | | - |
646 | | - def _boom(_model_path): |
647 | | - raise AssertionError("disk reload must not run when .graph is present") |
648 | | - |
649 | | - monkeypatch.setattr(auto_export, "_load_graph_from_disk", _boom) |
650 | | - |
651 | | - inputs = [ |
652 | | - _ir_value("input_ids", ir.DataType.INT64, ["batch", "sequence"]), |
653 | | - _ir_value("attention_mask", ir.DataType.INT64, ["batch", "past_sequence + sequence"]), |
654 | | - _ir_value("position_ids", ir.DataType.INT64, ["batch", "sequence"]), |
655 | | - _ir_value( |
656 | | - "past_key_values.0.key", ir.DataType.FLOAT, ["batch", 4, "past_sequence", 256] |
657 | | - ), |
658 | | - _ir_value( |
659 | | - "past_key_values.0.value", ir.DataType.FLOAT, ["batch", 4, "past_sequence", 256] |
660 | | - ), |
661 | | - ] |
662 | | - output_specs = [ |
663 | | - ("logits", ir.DataType.FLOAT, ["batch", "sequence", 128]), |
664 | | - ("present.0.key", ir.DataType.FLOAT, ["batch", 4, "total_sequence", 256]), |
665 | | - ("present.0.value", ir.DataType.FLOAT, ["batch", 4, "total_sequence", 256]), |
666 | | - ] |
667 | | - pkg = _GraphlessPkg({"model": _ir_decoder_model(inputs, output_specs)}) |
668 | | - |
669 | | - arts = write_onnx_genai_config(pkg, str(tmp_path), config=_Cfg()) |
670 | | - with open(arts["inference_metadata"]) as handle: |
671 | | - meta = yaml.safe_load(handle) |
672 | | - assert meta["model"]["io"]["kv_inputs"] == [ |
673 | | - "past_key_values.0.key", |
674 | | - "past_key_values.0.value", |
675 | | - ] |
0 commit comments