dew.nn.wrapping
Models around a backbone: a module around one causal decoder, answering the decoder’s training, cache
and serving hooks (dew.nn.protocols) as the decoder answers them, its decoder’s parameters under the
field that holds it (a multimodal model’s language model, DiffusionGemma’s text decoder); and a
backbone with a head over its final states, two modules over one variables tree (HeadedModel).
| Name | Summary |
|---|---|
DecoderWrapper | The hooks a module around one decoder answers as the decoder does: its geometry, its banks and cache under the wrapper’s field, a clone over the decoder each hook returns, and the decoder’s projection of its own parameters. |
HeadedModel | Any backbone with final states (HiddenStates, or a native module’s own forward that gives them: states_method) and a head over them, applied as two modules over one variables tree: the two under backbone and head in every collection (dew.objectives.base.joined). |
DecoderWrapper
Section titled “DecoderWrapper”class DecoderWrapper(wraps: str)The hooks a module around one decoder answers as the decoder does: its geometry, its
banks and cache under the wrapper’s field, a clone over the decoder each hook returns, and the
decoder’s projection of its own parameters. A subclass names the field (wraps) and keeps
its own modules beside it as they are.
wraps: str-
The field that holds the decoder, the name its parameters sit under in the wrapper’s tree.
DecoderWrapper.reads_progress
Section titled “DecoderWrapper.reads_progress”def reads_progress() -> boolWhether its decoder’s computation changes over a run (dew.nn.progress.ReadsProgress), which
a run’s objective then passes or refuses.
DecoderWrapper.replicated_flops
Section titled “DecoderWrapper.replicated_flops”def replicated_flops(stages: int, lengths) -> floatDecoderWrapper.fixed_routers
Section titled “DecoderWrapper.fixed_routers”def fixed_routers() -> Mapping[str, Callable[[tuple[str, ...]], bool]]Its decoder’s (ExpertRouted), each over the paths under the field that holds it.
DecoderWrapper.bank_sites
Section titled “DecoderWrapper.bank_sites”def bank_sites(store: Variables) -> tuple[DecoderBank, ...]DecoderWrapper.stage_sites
Section titled “DecoderWrapper.stage_sites”def stage_sites(store: Variables, stages: int) -> tuple[DecoderBank, ...]DecoderWrapper.with_cache_capacity
Section titled “DecoderWrapper.with_cache_capacity”def with_cache_capacity(capacity: int) -> SelfDecoderWrapper.with_kv_cache
Section titled “DecoderWrapper.with_kv_cache”def with_kv_cache(layout: KVCache) -> SelfDecoderWrapper.projected
Section titled “DecoderWrapper.projected”def projected(params, *, updated: bool)The decoder’s projection of its own parameters (ProjectsParameters); the wrapper’s
other parameters as they are.
DecoderWrapper.generation_refusal
Section titled “DecoderWrapper.generation_refusal”def generation_refusal() -> str | NoneDecoderWrapper.block_chunks
Section titled “DecoderWrapper.block_chunks”def block_chunks() -> tuple[int | None, ...]The decoder’s trunk layers that read blocks, by their attention chunk (BlockReading); the
wrapper’s own modules read none.
DecoderWrapper.with_remat
Section titled “DecoderWrapper.with_remat”def with_remat(policy: RematPolicy | None) -> SelfThe decoder’s layer policy; the wrapper’s other modules as they were.
DecoderWrapper.recompute_record
Section titled “DecoderWrapper.recompute_record”def recompute_record() -> JSONDecoderWrapper.recompute_more
Section titled “DecoderWrapper.recompute_more”def recompute_more() -> Self | NoneDecoderWrapper.recompute_half
Section titled “DecoderWrapper.recompute_half”def recompute_half() -> Self | NoneDecoderWrapper.restore_recompute
Section titled “DecoderWrapper.restore_recompute”def restore_recompute(record: JSON) -> SelfHeadedModel
Section titled “HeadedModel”class HeadedModel(backbone: nn.Module, head: nn.Module)Any backbone with final states (HiddenStates, or a native module’s own forward that gives them:
states_method) and a head over them, applied as two modules
over one variables tree: the two under backbone and head in every collection
(dew.objectives.base.joined). Each is applied as its own module over its part, so a backbone
that Pretrained.adapt gave LoRA factors runs with its own adapter, with its base weights under
frozen. A task’s model reads its rows through it (dew.decision.DecisionModel); a head reads
the backbone’s final states, its vocabulary rows or its output table, and its own logits at a
label’s tokens (label_logits).
states_method: str-
The backbone’s method a head reads its final states by: its
hidden_states, or a native module’s own forward (dew.nn.protocols.forward_method). context_length: int | None-
The most context tokens a row carries: the backbone’s cross-attention’s (
CrossAttending), or None for a backbone that reads no context.
HeadedModel.stage_sites
Section titled “HeadedModel.stage_sites”def stage_sites(store: Variables, stages: int) -> tuple[DecoderBank, ...]The backbone’s runs a pipeline over stages stacks, under backbone (StagedStacks); a head
has none.
HeadedModel.table
Section titled “HeadedModel.table”def table(variables: Variables) -> jax.Array | np.ndarrayReturn the backbone’s head as [V, D] rows (VocabularyRows), which a head may read.
HeadedModel.output_table
Section titled “HeadedModel.output_table”def output_table(variables: Variables) -> OutputTable | NoneThe backbone’s head as one matrix (AffineHead), or None where no matrix alone is it.
HeadedModel.label_logits
Section titled “HeadedModel.label_logits”def label_logits( variables: Variables, answers: jax.Array, ids: tuple[tuple[int, ...], ...],) -> jax.ArrayThe backbone’s own logits [B, N, K, V] at each label’s ids of each answer state of
answers [B, N, features] (dew.nn.protocols.label_logits).
HeadedModel.head_size
Section titled “HeadedModel.head_size”def head_size(backbone: nn.Module) -> tuple[int, Dtype | None]Return the width of backbone’s final states, and the dtype a head over them computes in.
The backbone is traced, not run, so this costs no compute. A backbone that computes in reduced precision has its head compute in it too; over full-precision states a head computes at its parameters’ precision.