mstar.model.utils#
Functions
|
|
|
Refuse a |
|
Raise unless |
|
Drop the stop tokens that ended a text reply, unless the request sets |
|
Pages for |
|
The sampling defaults |
|
|
|
|
|
Load weights from a HuggingFace safetensors checkpoint. |
|
Finalize kernel layouts across a freshly loaded module tree: any submodule exposing |
Classes
|
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.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.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)
- detokenizer: ByteLevelDetokenizer | TextChunkDetokenizer#
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.check_kv_pools_fit(model, config, config_path, log)[source]#
Refuse a
max_concurrent_requeststhe model’s KV caches cannot hold.A request keeps its pages from prefill to its last token. Past a pool, a prefill batch is refused and the worker offloads decoding requests to admit it, which costs far more than queueing would. 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’s
log.
- 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.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='')[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. Returns{}when there is no file, so the caller’s own defaults apply.
- 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)