Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
A comprehensive collection of Agent Skills for context engineering, multi-agent architectures, and production agent systems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
examples/book-sft-pipeline/references/tinker-format.md
1# Tinker Format Specification23This reference documents the exact data structures required for Tinker supervised fine-tuning.45## Core Data Types67### Datum89The fundamental training unit in Tinker:1011```python12from tinker import types1314datum = types.Datum(15model_input=types.ModelInput.from_ints(tokens=input_tokens),16loss_fn_inputs={17"target_tokens": target_tokens, # List[int] - shifted by 1 for next-token prediction18"weights": weights # List[float] - 0.0 for prompt, 1.0 for completion19}20)21```2223### ModelInput2425Container for tokenized input:2627```python28# Simple text-only input29model_input = types.ModelInput.from_ints(tokens=[...])3031# Multi-modal (for VLMs)32model_input = types.ModelInput(chunks=[33types.EncodedTextChunk(tokens=[...]),34types.ImageChunk(data=image_bytes, format="png"),35types.EncodedTextChunk(tokens=[...])36])37```3839### Token Weight Assignment4041The weights array determines which tokens contribute to the loss:4243| Token Type | Weight | Description |44|------------|--------|-------------|45| System prompt | 0.0 | Context, not learned |46| User message | 0.0 | Input prompt |47| Assistant message | 1.0 | Target completion |48| Special tokens | 0.0 | EOS, BOS, delimiters |4950## Renderer System5152Tinker uses renderers to convert message lists to tokens with proper weights.5354### Using Built-in Renderers5556```python57from tinker_cookbook import renderers, tokenizer_utils5859# Get tokenizer for your model60tokenizer = tokenizer_utils.get_tokenizer("meta-llama/Llama-3.1-8B-Instruct")6162# Get appropriate renderer63renderer = renderers.get_renderer("llama3", tokenizer)6465# Convert messages to training format66messages = [67{"role": "system", "content": "You are a creative writer..."},68{"role": "user", "content": "Write a 500 word excerpt..."},69{"role": "assistant", "content": "The actual book text..."}70]7172model_input, weights = renderer.build_supervised_example(messages)73```7475### Renderer Output Visualization7677The renderer assigns weights per-token:7879```80Token Weight81<|im_start|> 0.082system 0.083\n 0.084You are... 0.085<|im_end|> 0.086... ...87<|im_start|> 0.088assistant 0.089\n 0.090The actual 1.0 <- Completion starts91book text 1.092... 1.093<|im_end|> 1.0 <- Final token weighted94```9596## JSONL Format9798For batch processing, use standard conversation JSONL:99100```json101{"messages": [{"role": "system", "content": "..."}, {"role": "user", "content": "..."}, {"role": "assistant", "content": "..."}]}102{"messages": [{"role": "system", "content": "..."}, {"role": "user", "content": "..."}, {"role": "assistant", "content": "..."}]}103```104105### Converting JSONL to Datum106107```python108import json109from tinker import types110from tinker_cookbook import renderers, tokenizer_utils111112def load_dataset(jsonl_path: str, model_name: str) -> list[types.Datum]:113"""Load JSONL and convert to Tinker Datum objects."""114115tokenizer = tokenizer_utils.get_tokenizer(model_name)116renderer = renderers.get_renderer("llama3", tokenizer)117118data = []119with open(jsonl_path) as f:120for line in f:121example = json.loads(line)122messages = example["messages"]123124model_input, weights = renderer.build_supervised_example(messages)125126# Get token sequences127input_tokens = model_input.to_ints()128target_tokens = input_tokens[1:] # Shift for next-token prediction129input_tokens = input_tokens[:-1]130weights = weights[1:] # Align weights with targets131132datum = types.Datum(133model_input=types.ModelInput.from_ints(tokens=input_tokens),134loss_fn_inputs={135"target_tokens": target_tokens,136"weights": weights137}138)139data.append(datum)140141return data142```143144## Training Loop Integration145146```python147import tinker148from tinker import types149150async def train_on_book_dataset(151dataset: list[types.Datum],152model_name: str,153learning_rate: float = 1e-4,154epochs: int = 1155):156"""Train on book SFT dataset."""157158service_client = tinker.ServiceClient()159training_client = await service_client.create_lora_training_client_async(160base_model=model_name,161rank=32162)163164for epoch in range(epochs):165for batch_start in range(0, len(dataset), 1): # Batch size 1166batch = dataset[batch_start:batch_start + 1]167168# Forward-backward with cross-entropy loss169fwd_bwd_future = await training_client.forward_backward_async(170batch,171loss_fn="cross_entropy"172)173174# Optimizer step with aggressive learning rate175optim_future = await training_client.optim_step_async(176types.AdamParams(learning_rate=learning_rate * 2.0)177)178179# Wait for completion180fwd_bwd_result = await fwd_bwd_future181optim_result = await optim_future182```183184## Key Constraints1851861. **Batch Size**: Use 1 for style transfer. Larger batches average out stylistic gradients.1871882. **Sequence Length**: Keep chunks under 1000 tokens. Longer sequences dilute local style patterns.1891903. **Learning Rate**: Use 2x multiplier (e.g., 2e-4 instead of 1e-4) for faster style convergence.1911924. **Token Alignment**: Target tokens must be shifted by 1 position from input tokens.1931945. **Weight Precision**: Weights should be float32, typically 0.0 or 1.0.195196## Model Selection197198For book SFT, consider:199200| Model | Use Case |201|-------|----------|202| meta-llama/Llama-3.1-8B-Instruct | General style transfer |203| Qwen/Qwen3-30B-A3B | Higher quality, MoE efficiency |204| GPT-4o (via OpenAI) | Data generation only, not Tinker |205206## References207208- Tinker Cookbook: `tinker_cookbook/supervised/train.py`209- Renderer implementations: `tinker_cookbook/renderers.py`210- Type definitions: `tinker/types.py`211212