"""DewConfig: one config describes every Dew model, from a 15M phone model to a large LLM. The stack is embedding -> prelude -> body x (up to max_loops passes) -> coda -> norm -> head. Each of prelude / body / coda is a string of layer types: G Gated DeltaNet layer (fixed-size memory, linear in text length) A attention layer (Qwen3-style: grouped-query, q/k norm, RoPE) Examples transformer prelude="", body="AAAAAAAA", coda="", max_loops=1, ffn="dense" hybrid (2:1) prelude="", body="GGAGGA", coda="", max_loops=1, ffn="dense" Nap hybrid same as hybrid with ffn="nap" Nap & Loop prelude="G", body="GGA", coda="A", max_loops=3, doorman=True, ffn="nap" Every layer has a mixer (G or A) and a thinker (ffn): "dense" is a SwiGLU block, "nap" is the Nap thinker (router over num_experts small experts + a nap room that does nothing, and a shared expert with a per-word on/off switch). causal=False makes an encoder (masked-LM base): attention sees both sides, and every DeltaNet layer reads the text forwards and backwards (two memories, outputs added). Encoders run one pass, no doorman; padding is masked by `attention_mask` and writes nothing to either memory. The doorman (doorman=True) decides per word, before every body pass, whether the word goes through the body again. Stopping is final. A word that does not take a pass neither writes DeltaNet memory nor enters that pass's attention memory. With max_loops=1 the doorman is layer napping; with max_loops>1 it is "Nap & Loop". """ from __future__ import annotations try: from transformers import PretrainedConfig except ImportError: # lets the model run without transformers installed class PretrainedConfig: # type: ignore[no-redef] model_type = "" def __init__(self, **kw): for k, v in kw.items(): setattr(self, k, v) def to_dict(self): return {k: v for k, v in self.__dict__.items() if not k.startswith("_")} class DewConfig(PretrainedConfig): model_type = "dew" keys_to_ignore_at_inference = ["past_key_values"] def __init__( self, vocab_size: int = 16384, hidden_size: int = 320, causal: bool = True, # layout prelude: str = "G", body: str = "GGA", coda: str = "A", max_loops: int = 3, doorman: bool = True, target_passes: float = 1.33, pass_cap_prob: float = 0.25, warm_start: float = 0.2, # attention (A) num_attention_heads: int = 5, num_key_value_heads: int | None = None, head_dim: int | None = None, qk_norm: bool = True, rope_theta: float = 10000.0, max_position_embeddings: int = 2048, # Gated DeltaNet (G) linear_num_heads: int | None = None, linear_head_dim: int | None = None, conv_kernel: int = 4, # thinker ffn: str = "nap", intermediate_size: int = 864, num_experts: int = 8, expert_intermediate_size: int = 160, shared_intermediate_size: int = 320, router_threshold: float = 0.2, shared_target: float = 0.15, # auxiliary losses aux_balance_coef: float = 0.01, aux_passes_coef: float = 0.5, aux_shared_coef: float = 0.5, # misc rms_norm_eps: float = 1e-6, initializer_range: float = 0.02, tie_word_embeddings: bool = True, **kwargs, ): for s, name in ((prelude, "prelude"), (body, "body"), (coda, "coda")): if set(s) - {"G", "A"}: raise ValueError(f"{name} may only contain G and A, got {s!r}") if not body and max_loops > 1: raise ValueError("max_loops > 1 needs a body") if not causal and (max_loops > 1 or doorman): raise ValueError("encoders (causal=False) run one pass without a doorman") if ffn not in ("dense", "nap"): raise ValueError("ffn must be 'dense' or 'nap'") self.vocab_size = vocab_size self.hidden_size = hidden_size self.causal = bool(causal) self.prelude, self.body, self.coda = prelude, body, coda self.max_loops = max(1, int(max_loops)) self.doorman = bool(doorman) self.target_passes = target_passes self.pass_cap_prob = pass_cap_prob self.warm_start = warm_start self.num_attention_heads = num_attention_heads self.num_key_value_heads = num_key_value_heads or num_attention_heads self.head_dim = head_dim or hidden_size // num_attention_heads self.qk_norm = qk_norm self.rope_theta = rope_theta self.max_position_embeddings = max_position_embeddings self.linear_num_heads = linear_num_heads or num_attention_heads self.linear_head_dim = linear_head_dim or hidden_size // self.linear_num_heads self.conv_kernel = conv_kernel self.ffn = ffn self.intermediate_size = intermediate_size self.num_experts = num_experts self.expert_intermediate_size = expert_intermediate_size self.shared_intermediate_size = shared_intermediate_size self.router_threshold = router_threshold self.shared_target = shared_target self.aux_balance_coef = aux_balance_coef self.aux_passes_coef = aux_passes_coef self.aux_shared_coef = aux_shared_coef self.rms_norm_eps = rms_norm_eps self.initializer_range = initializer_range super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs) @property def layer_passes(self) -> float: """Layer-passes per word at the target loop count (compute, not parameters).""" passes = self.target_passes if (self.doorman and self.max_loops > 1) else self.max_loops return len(self.prelude) + passes * len(self.body) + len(self.coda) def describe(self) -> str: loop = f" x{self.max_loops} passes" + (" (doorman)" if self.doorman else "") if self.max_loops > 1 else \ (" (doorman napping)" if self.doorman else "") kind = "Dew-enc" if not self.causal else "Dew" think = (f" {self.num_experts}x{self.expert_intermediate_size}+nap+shared{self.shared_intermediate_size}" if self.ffn == "nap" else f" {self.intermediate_size}") return f"{kind} [{self.prelude or '-'} | {self.body}{loop} | {self.coda or '-'}] d={self.hidden_size} ffn={self.ffn}{think}"