dew.inputs.encoders
From raw conditioning data to the value a model keyword takes.
An encoder tokenizes on the host, in the data workers or before a sampling call, and encodes on device as a pure function of explicit parameters. The parameters are a leaf of the objective’s tree, placed by the trainer’s layout like any other, so a frozen tower’s weights arrive at the compiled step as arguments, not as constants baked into it.
An encoder is rebuilt from a run’s record by rebuild(name, fields), where
fields is what to_json wrote.
| Name | Summary |
|---|---|
CLIPText | The CLIP text tower, vendored in dew.nn.text_encoders, with the checkpoint’s tokenizer. |
CharTable | Encodes text as a table lookup: one id per character, one fixed random vector per id. |
ConditionEncoder | Carries one modality from raw data to a conditioning value. |
T5Text | The T5 encoder tower, vendored in dew.nn.text_encoders, with the checkpoint’s tokenizer. |
rebuild | The named encoder rebuilt from its JSON fields. |
CLIPText
Section titled “CLIPText”class CLIPText( checkpoint: str, transformer: CLIPTextTransformer, params: Variables, tokenizer: PreTrainedTokenizerBase, dtype: Dtype | None = None, revision: str | None = None, param_dtype: str = 'float32',)The CLIP text tower, vendored in dew.nn.text_encoders, with the
checkpoint’s tokenizer.
Prompts are padded to the checkpoint’s own context length, which the tokenizer reports.
CLIPText.from_pretrained
Section titled “CLIPText.from_pretrained”def from_pretrained( checkpoint: str = DEFAULT_MODEL, *, dtype=None, revision: str | None = None, param_dtype: str = 'float32', params: Variables | None = None,) -> CLIPTextCharTable
Section titled “CharTable”class CharTable( params: Variables, tokens: int = 8, features: int = 16, vocab: int = 130, seed: int = 0, dtype: Dtype | None = None, param_dtype: str = 'float32',)Encodes text as a table lookup: one id per character, one fixed random vector per id.
It costs nothing and downloads nothing, which makes it the text encoder
of tests, benchmarks and smoke runs. It has the shape of a real one, a
TextContext with a mask, so a model that takes CLIP’s output takes this
one unchanged.
CharTable.from_pretrained
Section titled “CharTable.from_pretrained”def from_pretrained( checkpoint: str = 'char_table', *, dtype=None, tokens: int = 8, features: int = 16, vocab: int = 130, seed: int = 0, param_dtype: str = 'float32', params: Variables | None = None,)The table seed draws, or the one params already holds.
There is nothing to load, so checkpoint goes unread here. It is on
the signature because rebuild hands every encoder the name its
to_json wrote, and this one writes the fixed "char_table".
CharTable.tokenize
Section titled “CharTable.tokenize”def tokenize(texts: Sequence[str]) -> dict[str, np.ndarray]CharTable.encode
Section titled “CharTable.encode”def encode(params, tokens) -> TextContextCharTable.captions
Section titled “CharTable.captions”def captions(tokens) -> tuple[str, ...]CharTable.to_json
Section titled “CharTable.to_json”def to_json() -> dictConditionEncoder
Section titled “ConditionEncoder”class ConditionEncoder(ABC, Generic[Raw, Encoded])Carries one modality from raw data to a conditioning value.
parameter_collections: tuple[str, ...] | None-
None declares a bare parameter tree; otherwise these collections own learned weights, including frozen ones. Other collections retain their dtype.
ConditionEncoder.from_pretrained
Section titled “ConditionEncoder.from_pretrained”def from_pretrained(checkpoint: str, *, params: Variables | None = None) -> SelfLoads the tower named checkpoint, the one call that opens files.
Whatever else a checkpoint needs is a keyword field with a default,
which is what to_json records and the registry rebuilds from.
Supplied params are authoritative: the load reads metadata and never
source weights, and keeps their values, dtypes and placement.
ConditionEncoder.tokenize
Section titled “ConditionEncoder.tokenize”def tokenize(texts: Sequence[Raw]) -> Mapping[str, np.ndarray]Raw data to the host arrays encode reads, one row per item.
ConditionEncoder.encode
Section titled “ConditionEncoder.encode”def encode(params: Variables, tokens) -> EncodedTokens to the conditioning value, on device, under params.
ConditionEncoder.captions
Section titled “ConditionEncoder.captions”def captions(tokens) -> tuple[str, ...]What the tokens say, for a rendered artifact.
A modality that is not text has nothing to say and answers nothing.
ConditionEncoder.to_json
Section titled “ConditionEncoder.to_json”def to_json() -> dictThe keyword fields from_pretrained rebuilds this encoder from.
T5Text
Section titled “T5Text”class T5Text( checkpoint: str, transformer: T5EncoderTransformer, params: Variables, tokenizer: PreTrainedTokenizerBase, dtype: Dtype | None = None, revision: str | None = None, param_dtype: str = 'float32', max_length: int = 256,)The T5 encoder tower, vendored in dew.nn.text_encoders, with the
checkpoint’s tokenizer.
It is the text half of an SD3.5/Flux-class run, whose MMDiT conditions on
T5-XXL’s last hidden states. Prompts are padded to max_length, which
the run’s record carries.
T5Text.from_pretrained
Section titled “T5Text.from_pretrained”def from_pretrained( checkpoint: str = DEFAULT_T5_MODEL, *, dtype=None, revision: str | None = None, max_length: int = 256, param_dtype: str = 'float32', params: Variables | None = None,) -> T5Textrebuild
Section titled “rebuild”def rebuild( name: str, fields: Mapping[str, object], *, params: Variables | None = None,) -> ConditionEncoderThe named encoder rebuilt from its JSON fields.
A run’s record stores the registry name with the keyword fields to_json
wrote. Those fields are unpacked here, so each encoder’s from_pretrained
keeps its own concrete signature. The checkpoint is the one field every
encoder takes and is read here; the rest are the encoder’s own and its
signature checks them.