mstar.model.utils#
Functions
|
|
|
A capture ladder (token buckets or batch sizes) from model_kwargs: a 0, negative or repeated entry would go into the capture build, and an empty one would quietly mean the default. |
|
Refuse a |
|
Raise unless |
|
Drop the stop tokens that ended a text reply, unless the request sets |
|
A fast tokenizer from |
|
Pages for |
|
The sampling defaults |
|
The checkpoint's tokenizer, through |
|
|
|
|
|
Load weights from a HuggingFace safetensors checkpoint. |
|
Finalize kernel layouts across a freshly loaded module tree: any submodule exposing |
|
The |
|
A local checkpoint directory as given, a Hub id as its snapshot (fetched if not cached), None for no path. |
|
Warn that the environment variable |
Classes
Token ids that are bytes, as a reduced model's byte tokenizer ( |
|
|
Byte-faithful per-token detokenization for byte-level BPE tokenizers (GPT-2 / Whisper / Qwen). |
|
|
|
|
|
|
|
|
|
Per-token detokenization for SentencePiece-style vocabularies ( |
|
One decode step's text chunk for a model that streams a token per step. |
|
How a model's text output, token ids, becomes bytes: |
|
- class mstar.model.utils.ByteIdDetokenizer[source]#
Bases:
objectToken ids that are bytes, as a reduced model’s byte tokenizer (
tokenizer_mode: byte) makes them: an id is its low byte.
- class mstar.model.utils.ByteLevelDetokenizer(tokenizer, keep_special=False)[source]#
Bases:
objectByte-faithful per-token detokenization for byte-level BPE tokenizers (GPT-2 / Whisper / Qwen).
Streaming models emit one token per step to the client, so a multi-byte UTF-8 character split across tokens must be emitted as raw bytes and reassembled client-side — per-token
tokenizer.decodewould yield U+FFFD for each partial character.Special tokens are dropped, like
skip_special_tokens, unlesskeep_special: then the joined bytes of any id sequence decode to whattokenizer.decode(ids)gives.- Parameters:
keep_special (bool)
- class mstar.model.utils.ByteLevelTextOutput[source]#
Bases:
objectModel.text_outputfor a model over a byte-level BPE tokenizer: each token’s raw bytes with special tokens dropped,config.eos_token_idsending a reply. Built on first use, as the host’stokenizermay load lazily; a reduced model’s byte ids (_tokenizer_modeof"byte") never touch it.
- class mstar.model.utils.KeysAndConverter(keys: set[str] = <factory>, converter: mstar.model.utils.WeightConverter | None = None)[source]#
Bases:
object- Parameters:
converter (WeightConverter | None)
- converter: WeightConverter | None = None#
- class mstar.model.utils.ModuleAndPrefix(module: torch.nn.modules.module.Module, prefix: str = None, enforce_missing_keys: bool = True)[source]#
Bases:
object
- class mstar.model.utils.SentencePieceDetokenizer(tokenizer_json, extra_skip_ids=frozenset())[source]#
Bases:
objectPer-token detokenization for SentencePiece-style vocabularies (
▁marks a word start,<0xNN>pieces are byte fallback), read from an HFtokenizer.json. Same contract asByteLevelDetokenizer: bytes per token, so a character split across byte pieces reassembles client-side.
- class mstar.model.utils.TextChunkDetokenizer(tokenizer)[source]#
Bases:
objectOne decode step’s text chunk for a model that streams a token per step.
With a byte-level tokenizer the chunk is the tokens’ raw bytes, special tokens kept, so a character split across steps reassembles in the API server’s incremental decoder and the joined reply equals
tokenizer.decodeof all its ids. Any other tokenizer keeps the per-stepdecode.- tokenizer#
- class mstar.model.utils.TextOutput(detokenizer, stop_token_ids=frozenset({}))[source]#
Bases:
NamedTupleHow a model’s text output, token ids, becomes bytes:
Model.text_outputdeclares it, andModel.postprocessandModel.postprocess_inlinederive their text paths from it.- Parameters:
detokenizer (ByteLevelDetokenizer | TextChunkDetokenizer | SentencePieceDetokenizer | ByteIdDetokenizer)
- detokenizer: ByteLevelDetokenizer | TextChunkDetokenizer | SentencePieceDetokenizer | ByteIdDetokenizer#
Alias for field number 0
- class mstar.model.utils.WeightConverter(source_patterns: list[str], target_patterns: str, operations: list[mstar.model.utils.Operation])[source]#
Bases:
object
- mstar.model.utils.capture_ladder(key, value)[source]#
A capture ladder (token buckets or batch sizes) from model_kwargs: a 0, negative or repeated entry would go into the capture build, and an empty one would quietly mean the default.
- mstar.model.utils.check_kv_pools_fit(model, config, config_path, log, request_layouts=None, loop_caps=None)[source]#
Refuse a
max_concurrent_requestsa resource the model builds cannot hold, as its spec’s capacity_problem answers: the first problem raises.A request keeps its pages from prefill to its last token. Past a pool, a prefill batch is refused. With host pages (
cpu_offload_pages) the worker offloads decoding requests to admit it, which costs far more than queueing would. Without them it retries until requests time out. So the cap’s requests at full length must fit each cache next to its sink page; the conductor queues the rest. Warnings go to the model’slog.request_layoutsgives the Layout per array and label of the request the deployment is sized for, with its loops atloop_caps, and a pool it doesn’t name holds one stream of itsmax_seq_len.
- mstar.model.utils.check_loaded(module, loaded, what)[source]#
Raise unless
loadednames every parameter ofmodule: one built onmetaand filled by a loader would otherwise serve uninitialized memory.
- mstar.model.utils.drop_stop_tokens(output, stop_token_ids, request_kwargs)[source]#
Drop the stop tokens that ended a text reply, unless the request sets
ignore_eos.A stop token ends the turn, not the reply: a client sends the reply back as an assistant message, whose template closes it. Under
ignore_eosit ends nothing and stays, one chunk per token.
- mstar.model.utils.fast_tokenizer_from_snapshot(source)[source]#
A fast tokenizer from
tokenizer.json, which is version-independent, with the snapshot’s chat template and the special tokenstokenizer_config.jsondeclares.- Parameters:
source (str)
- mstar.model.utils.kv_pages(tokens, page_size, requests)[source]#
Pages for
requestssequences oftokenseach, plus the sink page every KV cache keeps out of circulation.
- mstar.model.utils.load_generation_defaults(checkpoint_dir, prefix='', stage='', switch_only=False)[source]#
The sampling defaults
checkpoint_dir/generation_config.jsondeclares.Keys come back under mstar names (
max_new_tokens->max_output_tokens).prefixselects one stage’s keys in a multi-stage file ("talker_"readstalker_temperature) andstageprefixes the returned names ("code_predictor_").do_sample: falseis greedy: temperature 0.switch_onlyreadsdo_samplealone, for a file that is the backbone’s chat preset rather than the model’s own recipe. Returns{}when there is no file, so the caller’s own defaults apply.
- mstar.model.utils.load_snapshot_tokenizer(model_path_hf, cache_dir=None)[source]#
The checkpoint’s tokenizer, through
AutoTokenizeror, where transformers 4.x cannot build the backend class a transformers-5tokenizer_config.jsonnames,fast_tokenizer_from_snapshot.
- mstar.model.utils.load_weights_from_file(safetensors_file, modules, device='cpu')[source]#
- Parameters:
safetensors_file (str)
modules (list[ModuleAndPrefix])
device (str)
- mstar.model.utils.load_weights_from_hf_shards(repo_dir, modules, device='cpu', conv=None)[source]#
Load weights from a HuggingFace safetensors checkpoint.
Reads model.safetensors.index.json to find which shard each key lives in, then loads from each shard file. Checkpoints that ship a single unsharded
model.safetensors(no index — e.g. whisper-large-v3) are handled by treating that one file as the whole weight map.- Parameters:
modules (list[ModuleAndPrefix])
device (str)
conv (list[WeightConverter] | None)
- mstar.model.utils.process_weights_after_loading(root, device)[source]#
Finalize kernel layouts across a freshly loaded module tree: any submodule exposing
process_weights_after_loading(device)gets it called (the attention layers build their absorbed w_kc/w_vc here).
- mstar.model.utils.read_fp8_block_quant_config(checkpoint_dir)[source]#
The
Fp8BlockQuantConfigincheckpoint_dir/config.json, or None: no file, none declared, or unreadable (the weights then load as they are, bf16).- Parameters:
checkpoint_dir (str)
- mstar.model.utils.resolve_checkpoint(model_path_hf, cache_dir=None)[source]#
A local checkpoint directory as given, a Hub id as its snapshot (fetched if not cached), None for no path.