bridge.data.sft_processing#
Shared declarative preprocessing and canonical examples for SFT backends.
Module Contents#
Classes#
Canonical structured chat example. |
|
Canonical unformatted prompt-completion example. |
|
Declarative preprocessing for structured conversation examples. |
|
Declarative preprocessing for unformatted prompt-completion examples. |
|
Tokenized prompt-completion row with an unshifted loss mask. |
Functions#
Validate one supported SFT preprocessing variant. |
|
Normalize one adapted row for the selected preprocessing semantics. |
|
Normalize adapted rows for one explicit SFT preprocessing variant. |
|
Return whether a row is a text-only prompt-completion example. |
|
Return non-training columns retained as example metadata. |
|
Return whether a leading word-boundary space changes tokenization. |
|
Tokenize a prompt-completion row without applying a chat template. |
Data#
API#
- class bridge.data.sft_processing.ChatSFTExample#
Bases:
typing.TypedDictCanonical structured chat example.
Initialization
Initialize self. See help(type(self)) for accurate signature.
- conversation: list[dict[str, Any]]#
None
- class bridge.data.sft_processing.PromptCompletionSFTExample#
Bases:
typing.TypedDictCanonical unformatted prompt-completion example.
Initialization
Initialize self. See help(type(self)) for accurate signature.
- prompt: str#
None
- completion: str#
None
- bridge.data.sft_processing.CanonicalSFTExample#
None
- class bridge.data.sft_processing.ChatSFTPreprocessingConfig#
Declarative preprocessing for structured conversation examples.
- Parameters:
loss_mode – Tokens that contribute to the loss.
assistanttrains all assistant turns,last_turntrains only the final assistant span, andfulltrains the complete rendered conversation.
- loss_mode: Literal[assistant, last_turn, full]#
‘assistant’
- validate() None#
Validate chat preprocessing settings.
- class bridge.data.sft_processing.PromptCompletionSFTPreprocessingConfig#
Declarative preprocessing for unformatted prompt-completion examples.
The prompt and completion are tokenized separately and concatenated without calling
apply_chat_template.prompt_templatemay contain only the{prompt}placeholder; raw column selection remains explicit throughprompt_columnandcompletion_column.- Parameters:
prompt_column – Source column containing the prompt text.
completion_column – Source column containing the completion text.
prompt_template – Formatting applied to the prompt before tokenization.
separator – Text inserted between the rendered prompt and completion.
loss_mode –
completionmasks the prompt;fulltrains all tokens.strip_whitespace – Strip leading and trailing ASCII spaces from both fields.
add_bos – Add the tokenizer BOS token before the prompt.
add_sep – Add the tokenizer SEP token between prompt and completion.
add_eos – Add the tokenizer EOS token after the completion.
truncation_method – Side retained when a prompt or completion must be truncated.
- prompt_column: str#
‘prompt’
- completion_column: str#
‘completion’
- prompt_template: str#
‘{prompt}’
- separator: str = <Multiline-String>#
- loss_mode: Literal[completion, full]#
‘completion’
- strip_whitespace: bool#
True
- add_bos: bool#
False
- add_sep: bool#
False
- add_eos: bool#
True
- truncation_method: Literal[left, right]#
‘right’
- validate() None#
Validate prompt-completion preprocessing settings.
- bridge.data.sft_processing.SFTPreprocessingConfig#
None
- class bridge.data.sft_processing.TokenizedPromptCompletion#
Tokenized prompt-completion row with an unshifted loss mask.
- input_ids: torch.Tensor#
None
- loss_mask: torch.Tensor#
None
- prompt_ids: torch.Tensor#
None
- completion_ids: torch.Tensor#
None
- bridge.data.sft_processing._CONVERSATION_KEYS#
(‘messages’, ‘conversation’, ‘conversations’)
- bridge.data.sft_processing._CANONICAL_PROMPT_KEY#
‘prompt’
- bridge.data.sft_processing._CANONICAL_COMPLETION_KEY#
‘completion’
- bridge.data.sft_processing._MEDIA_KEYS#
(‘image’, ‘images’, ‘image_path’, ‘image_paths’, ‘video’, ‘videos’, ‘video_path’, ‘video_paths’, ‘au…
- bridge.data.sft_processing.validate_sft_preprocessing_config( ) None#
Validate one supported SFT preprocessing variant.
- bridge.data.sft_processing._canonical_prompt_completion_values(
- example: collections.abc.Mapping[str, Any],
- bridge.data.sft_processing.normalize_sft_example(
- example: collections.abc.Mapping[str, Any],
- preprocessing: bridge.data.sft_processing.SFTPreprocessingConfig,
Normalize one adapted row for the selected preprocessing semantics.
Prompt-completion rows may be promoted to a two-turn chat. Structured conversations are deliberately not flattened into prompt-completion text, because doing so without a chat template would silently change their model semantics.
- bridge.data.sft_processing.normalize_sft_examples(
- examples: collections.abc.Sequence[collections.abc.Mapping[str, Any]],
- preprocessing: bridge.data.sft_processing.SFTPreprocessingConfig,
Normalize adapted rows for one explicit SFT preprocessing variant.
- bridge.data.sft_processing.is_text_only_prompt_completion_example(
- example: collections.abc.Mapping[str, Any],
- preprocessing: bridge.data.sft_processing.PromptCompletionSFTPreprocessingConfig,
Return whether a row is a text-only prompt-completion example.
- bridge.data.sft_processing.sft_example_metadata(
- example: collections.abc.Mapping[str, Any],
- preprocessing: bridge.data.sft_processing.SFTPreprocessingConfig,
Return non-training columns retained as example metadata.
- bridge.data.sft_processing._tokenize_text(
- tokenizer_or_processor: Any,
- text: str,
- bridge.data.sft_processing._is_space_sensitive(tokenizer_or_processor: Any) bool#
Return whether a leading word-boundary space changes tokenization.
- bridge.data.sft_processing._token_id(
- tokenizer_or_processor: Any,
- *names: str,
- bridge.data.sft_processing._truncate(
- ids: list[int],
- target_length: int,
- method: Literal[left, right],
- bridge.data.sft_processing.tokenize_prompt_completion_example(
- example: collections.abc.Mapping[str, Any],
- tokenizer_or_processor: Any,
- preprocessing: bridge.data.sft_processing.PromptCompletionSFTPreprocessingConfig,
- *,
- max_length: int | None = None,
- skipped_tokens: torch.Tensor | None = None,
- allow_missing_completion: bool = False,
- prefix_token_ids: collections.abc.Sequence[int] = (),
- minimum_completion_length: int = 0,
- sep_token_id: int | None = None,
Tokenize a prompt-completion row without applying a chat template.