"""Named Dew-1 layouts and sizing to a parameter budget. from dew import dew_config, fit_params cfg = fit_params(dew_config("dew-1-loop", vocab_size=16384, hidden_size=320), 15_160_640) """ from __future__ import annotations import torch from .configuration_dew import DewConfig PRESETS = { # Nap & Loop: 1 awake layer, a shared G-G-A block run 0-3 times per word, 1 awake attention layer "dew-1-loop": dict(prelude="G", body="GGA", coda="A", max_loops=3, doorman=True, ffn="nap"), # Nap hybrid: DeltaNet:attention 2:1, Nap thinker in every layer, no doorman "dew-1-nap": dict(prelude="", body="GGAGGA", coda="", max_loops=1, doorman=False, ffn="nap"), # Selective-memory hybrid: as dew-1-nap with a doorman that lets words skip the middle layers "dew-1-napskip": dict(prelude="G", body="GGAG", coda="A", max_loops=1, doorman=True, ffn="nap", target_passes=0.5), # Encoders (masked-LM bases): DeltaNet both ways + attention without the causal mask "dew-1-enc": dict(prelude="", body="GGAGGA", coda="", max_loops=1, doorman=False, ffn="nap", causal=False), "dew-1-enc-hybrid": dict(prelude="", body="GGAGGA", coda="", max_loops=1, doorman=False, ffn="dense", causal=False), "dew-1-enc-dense": dict(prelude="", body="AAAAAAAA", coda="", max_loops=1, doorman=False, ffn="dense", causal=False), # Controls "dew-1-hybrid": dict(prelude="", body="GGAGGA", coda="", max_loops=1, doorman=False, ffn="dense"), "dew-1-dense": dict(prelude="", body="AAAAAAAA", coda="", max_loops=1, doorman=False, ffn="dense"), } def dew_config(preset: str, **overrides) -> DewConfig: if preset not in PRESETS: raise KeyError(f"unknown preset {preset!r}; choose from {sorted(PRESETS)}") return DewConfig(**{**PRESETS[preset], **overrides}) def count_params(cfg: DewConfig) -> int: from .modeling_dew import DewForCausalLM, DewForMaskedLM with torch.device("meta"): m = DewForCausalLM(cfg) if cfg.causal else DewForMaskedLM(cfg) if cfg.tie_word_embeddings: m.lm_head.weight = m.model.embed_tokens.weight return sum(p.numel() for p in m.parameters()) def fit_params(cfg: DewConfig, target: int, multiple: int = 8) -> DewConfig: """Resize the thinkers so the whole model has about `target` parameters. Nap thinker: expert width (shared expert = 2x expert width). Dense: intermediate size.""" key = "expert_intermediate_size" if cfg.ffn == "nap" else "intermediate_size" def with_width(w): kw = cfg.to_dict() kw[key] = int(w) if cfg.ffn == "nap": kw["shared_intermediate_size"] = int(2 * w) for k in ("model_type", "transformers_version", "architectures"): kw.pop(k, None) return DewConfig(**kw) a, b = 64, 128 na, nb = count_params(with_width(a)), count_params(with_width(b)) slope = (nb - na) / (b - a) w = max(multiple, round((a + (target - na) / slope) / multiple) * multiple) return with_width(w)