Shortcuts

torchtune.data

Text templates

Templates for instruct prompts and chat prompts. Includes some specific formatting for difference datasets and models.

GrammarErrorCorrectionTemplate

A prompt template for grammar error correction tasks.

SummarizeTemplate

A prompt template for summarization tasks.

QuestionAnswerTemplate

A prompt template for question answering tasks.

PromptTemplate

Quickly define a custom prompt template by passing in a dictionary mapping role to the prepend and append tags. For example, to achieve the following prompt template::.

PromptTemplateInterface

Interface for prompt templates.

ChatMLTemplate

OpenAI's Chat Markup Language used by their chat models.

Types

Message

This class represents individual messages in a fine-tuning dataset.

Role

alias of Literal['system', 'user', 'assistant', 'ipython']

Message transforms

Converts data from common schema and conversation JSON formats into a list of torchtune Message.

InputOutputToMessages

Message transform class that converts a single sample with "input" and "output" fields, (or equivalent fields specified in column_map) to user and assistant messages, respectively. This is useful for datasets that have two columns, one containing the user prompt string and the other containing the model response string::.

ShareGPTToMessages

Convert a single chat sample adhering to the ShareGPT JSON structure to torchtune's Message structure.

OpenAIToMessages

Convert a single chat sample adhering to the OpenAI chat completion JSON structure to torchtune's Message structure.

ChosenRejectedToMessages

Transform for converting a single sample from datasets with "chosen" and "rejected" columns containing conversations to a list of chosen and rejected messages. For example::.

AlpacaToMessages

Message transform class for Alpaca-style datasets with "instruction", "input", and "output" (or equivalent fields specified in column_map) columns.

Collaters

Collaters used to collect samples into batches and handle any padding.

padded_collate

A generic padding collation function which pads keys_to_pad entries in a batch of sequences from the given pad_direction to the maximum sequence length for each entry in the batch.

padded_collate_tiled_images_and_mask

Pad a batch of text sequences, tiled image tensors, aspect ratios, and cross attention masks.

padded_collate_sft

Pad a batch of sequences to the longest sequence length in the batch, and convert integer lists to tensors.

padded_collate_dpo

Pad a batch of sequences for Direct Preference Optimization (DPO).

left_pad_sequence

This function is identical to torch.nn.utils.rnn.pad_sequence(), but instead pads a list of variable length Tensors from the left to the length of the longest sequence.

Helper functions

Miscellaneous helper functions used in modifying data.

validate_messages

Given a list of messages, ensure that messages form a valid back-and-forth conversation.

truncate

Truncate a list of tokens to a maximum length.

load_image

Convenience method to load an image in PIL format from a local file path or remote source.

format_content_with_images

Given a raw text string, split by the specified image_tag and form into list of dictionaries to be used in the Message content field.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources