@@ -0,0 +1,108 @@ |
| 1 | +"""Modality dispatch base class — predicate flags + method hooks. |
| 2 | + |
| 3 | +Callers that used to branch on ``spec.modality == "vision-language"`` |
| 4 | +or ``"audio-language"`` now read from a registered |
| 5 | +:class:`ModalityDispatch` instance. Three concrete subclasses live |
| 6 | +under the ``dlm.modality`` package — one per supported modality — |
| 7 | +registered in :data:`MODALITIES` and resolved via |
| 8 | +:func:`modality_for`. The split keeps the "does this spec accept |
| 9 | +images?" predicate next to the "route the export through the VL |
| 10 | +path" method: both are modality-specific concerns. |
| 11 | + |
| 12 | +Each instance carries: |
| 13 | + |
| 14 | +- ``modality`` (string tag — the only place a `"vision-language"` |
| 15 | + string literal appears outside the base-model schema); |
| 16 | +- predicate flags (``requires_processor``, ``accepts_images``, |
| 17 | + ``accepts_audio``) callers read instead of comparing the tag; |
| 18 | +- dispatch hooks (``dispatch_export``, ``dispatch_prompt``) that |
| 19 | + forward to the modality-specific pipeline. |
| 20 | + |
| 21 | +A pregate grep-gate refuses new ``spec.modality ==`` comparisons |
| 22 | +outside this package so next-modality work lands here rather than |
| 23 | +scattering another set of branches. |
| 24 | +""" |
| 25 | + |
| 26 | +from __future__ import annotations |
| 27 | + |
| 28 | +from typing import TYPE_CHECKING, Any |
| 29 | + |
| 30 | +from dlm.modality.errors import UnknownModalityError |
| 31 | + |
| 32 | +if TYPE_CHECKING: |
| 33 | + from dlm.base_models import BaseModelSpec |
| 34 | + from dlm.export.dispatch import DispatchResult |
| 35 | + |
| 36 | + |
| 37 | +class ModalityDispatch: |
| 38 | + """Base class — subclasses override per-modality predicates + hooks. |
| 39 | + |
| 40 | + The base implementation defaults to the text-path semantics |
| 41 | + (nothing to probe, nothing to dispatch). Subclasses narrow the |
| 42 | + predicates and override the dispatch hooks. |
| 43 | + """ |
| 44 | + |
| 45 | + modality: str = "text" |
| 46 | + """The modality tag. The only place modality string literals |
| 47 | + should appear outside this package.""" |
| 48 | + |
| 49 | + requires_processor: bool = False |
| 50 | + """True for media modalities that ship a feature extractor / |
| 51 | + processor alongside the tokenizer. Text-only bases set this |
| 52 | + False — the trainer skips the BlobStore + preprocess pass.""" |
| 53 | + |
| 54 | + accepts_images: bool = False |
| 55 | + """True for vision-language bases. Drives the ``dlm prompt |
| 56 | + --image`` guardrail.""" |
| 57 | + |
| 58 | + accepts_audio: bool = False |
| 59 | + """True for audio-language bases. Drives the ``dlm prompt |
| 60 | + --audio`` guardrail.""" |
| 61 | + |
| 62 | + def load_processor(self, spec: BaseModelSpec) -> Any | None: |
| 63 | + """Load the HF processor if this modality needs one. Text → None.""" |
| 64 | + return None |
| 65 | + |
| 66 | + def dispatch_export( |
| 67 | + self, |
| 68 | + *, |
| 69 | + store: Any, |
| 70 | + spec: BaseModelSpec, |
| 71 | + adapter_name: str | None, |
| 72 | + quant: str | None, |
| 73 | + merged: bool, |
| 74 | + adapter_mix_raw: str | None, |
| 75 | + gguf_emission_context: dict[str, Any] | None = None, |
| 76 | + ) -> DispatchResult | None: |
| 77 | + """Route an export through the modality-specific path. |
| 78 | + |
| 79 | + Returns ``None`` on the text path — the caller falls back to |
| 80 | + the GGUF `run_export` pipeline, which has a different result |
| 81 | + shape (`run_export` returns `RunResult`, not `DispatchResult`, |
| 82 | + and the text path prints its own banner inline). |
| 83 | + """ |
| 84 | + return None |
| 85 | + |
| 86 | + |
| 87 | +class TextModality(ModalityDispatch): |
| 88 | + """Text-only base — defaults carry the whole contract.""" |
| 89 | + |
| 90 | + modality = "text" |
| 91 | + |
| 92 | + |
| 93 | +def _unknown(mod: str) -> UnknownModalityError: |
| 94 | + return UnknownModalityError( |
| 95 | + f"modality={mod!r} has no registered dispatcher. " |
| 96 | + "Register a ModalityDispatch subclass in dlm.modality and " |
| 97 | + "add it to MODALITIES." |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +def modality_for(spec: BaseModelSpec) -> ModalityDispatch: |
| 102 | + """Resolve a spec's ``ModalityDispatch``, raising if unregistered.""" |
| 103 | + from dlm.modality import MODALITIES # late import to avoid cycle |
| 104 | + |
| 105 | + try: |
| 106 | + return MODALITIES[spec.modality] |
| 107 | + except KeyError as exc: |
| 108 | + raise _unknown(spec.modality) from exc |