bison-1-MLX-4bit / engine /readout.py
pratikgx's picture
Add bison-1 MLX 4-bit weights, System One engine, and model card
9eefb05 verified
Raw History Blame Contribute Delete
3.06 kB
"""Load Baseten / OneJev readout settings from readout.json and config.json.
bison-1 and groundhog-1 store the mask under `baseten_modeling_overrides`. Older Qwen
checkpoints (elephant-1, mammoth-1) use `modeling_overrides`. Both keys are accepted.
`block_bidirectional` on full-attention layers is the `noncausal` setting in OneJev/qev.
"""
from __future__ import annotations
import json
from dataclasses import dataclass
from pathlib import Path
from typing import Any
OVERRIDE_KEYS = ("baseten_modeling_overrides", "modeling_overrides")
FULL_ATTENTION_MASK = {"causal": "causal", "block_bidirectional": "noncausal"}
@dataclass(frozen=True)
class ReadoutConfig:
attention: str # "causal" or "noncausal"
readout: str = "slot_lm"
head_dim: int = 256
n_slots: int = 256
sliding_attention: str = "causal"
logit_softcap: float | None = 30.0
@property
def block_bidirectional(self) -> bool:
return self.attention == "noncausal"
def _overrides(config: dict[str, Any]) -> dict[str, Any] | None:
for key in OVERRIDE_KEYS:
if key in config and config[key]:
return config[key]
return None
def load_readout_config(model_dir: str | Path, logit_softcap: float | None = None) -> ReadoutConfig:
model_dir = Path(model_dir)
readout_path = model_dir / "readout.json"
config_path = model_dir / "config.json"
fields: dict[str, Any] = json.loads(readout_path.read_text()) if readout_path.is_file() else {}
config: dict[str, Any] = json.loads(config_path.read_text()) if config_path.is_file() else {}
overrides = _overrides(config)
if overrides:
mask = (overrides.get("attention") or {}).get("full_attention")
attention = FULL_ATTENTION_MASK.get(mask)
if attention is None:
raise ValueError(f"unsupported full_attention mask {mask!r} in {config_path}")
if fields.get("attention", attention) != attention:
raise ValueError(
f"{readout_path} says attention {fields.get('attention')!r} but {config_path} says {mask!r}"
)
fields["attention"] = attention
sliding = (overrides.get("attention") or {}).get("sliding_attention", "causal")
fields["sliding_attention"] = sliding
fields.setdefault("readout", overrides.get("readout", "slot_lm"))
fields.setdefault("head_dim", overrides.get("head_dim", 256))
fields.setdefault("n_slots", overrides.get("n_slots", 256))
fields.setdefault("attention", "causal")
fields.setdefault("readout", "slot_lm")
fields.setdefault("head_dim", 256)
fields.setdefault("n_slots", 256)
fields.setdefault("sliding_attention", "causal")
if logit_softcap is None:
text = config.get("text_config") or config
logit_softcap = text.get("final_logit_softcapping", 30.0)
fields["logit_softcap"] = logit_softcap
allowed = {"attention", "readout", "head_dim", "n_slots", "sliding_attention", "logit_softcap"}
return ReadoutConfig(**{k: v for k, v in fields.items() if k in allowed})