thinking-machines-lab/tinker-cookbook

GitHub: thinking-machines-lab/tinker-cookbook

Tinker Cookbook 是一个基于 Tinker API 的大语言模型后训练代码库,提供从监督微调到强化学习的完整训练方案和可复现实例。

Stars: 3864 | Forks: 484

Tinker Cookbook

[![pytest](https://static.pigsec.cn/wp-content/uploads/repos/cas/97/979d2256ce245e407797dfb391c7e1bceeacb39078f7256dca49693bac60c201.svg)](https://github.com/thinking-machines-lab/tinker-cookbook/actions/workflows/pytest.yaml) [![pyright](https://static.pigsec.cn/wp-content/uploads/repos/cas/87/87262c8b03b98a6dc0d74d4ea316df6dee92722196d7e0c64aea36d0f3779403.svg)](https://github.com/thinking-machines-lab/tinker-cookbook/actions/workflows/pyright.yaml) [![smoke-test-recipes](https://static.pigsec.cn/wp-content/uploads/repos/cas/12/12cfadf8e9314c54f77bd14f1e1fbb6d5240eb067727e66b9ec6a4c8749e6eca.svg)](https://github.com/thinking-machines-lab/tinker-cookbook/actions/workflows/smoke-test-recipes.yaml) [![PyPI](https://img.shields.io/pypi/v/tinker-cookbook)](https://pypi.org/project/tinker-cookbook/)
- `tinker` 是一个供研究人员和开发者对语言模型进行 fine-tuning 的训练 SDK。您向我们发送 API 请求,我们来处理分布式训练的复杂性。 - `tinker-cookbook` 包含了 fine-tuning 语言模型的真实示例。它构建在 Tinker API 之上,并提供了用于 fine-tuning 语言模型的通用抽象。 ## 安装说明 1. 在[这里](https://auth.thinkingmachines.ai/sign-up)注册 Tinker。 2. 获取访问权限后,从[控制台](https://tinker-console.thinkingmachines.ai)创建一个 API key,并将其导出为环境变量 `TINKER_API_KEY`。 3. 安装 `tinker-cookbook`(包含 `tinker` SDK 作为依赖): # 来自 PyPI 的最新稳定版本 uv pip install tinker-cookbook # 或者安装 nightly 构建 uv pip install 'tinker-cookbook @ git+https://github.com/thinking-machines-lab/tinker-cookbook.git@nightly' ## 使用 Inkling Inkling 是 Thinking Machines Lab 专门为 Tinker 定制的模型。它是一个通用模型,能够编写代码、进行推理、调用工具以及处理图像和音频输入。Tinker Cookbook 通过独立的 `tml-renderers` 库支持对 Inkling 进行采样和 post-training;使用 `inkling` extra 选项进行安装,这也会引入所需的 `torch>=2.10`: ``` uv pip install 'tinker-cookbook[inkling]' ``` 在 cookbook 接受模型名称的任何地方传入 `model_name="thinkingmachines/Inkling"`。系统会自动选择合适的 renderer 和 tokenizer。Inkling 支持[可控的推理努力](https://tinker-docs.thinkingmachines.ai/cookbook/inkling/thinking-effort/)以及多模态[音频](https://tinker-docs.thinkingmachines.ai/cookbook/inkling/audio/)和[图像](https://tinker-docs.thinkingmachines.ai/cookbook/inkling/images/)输入。有关设置和使用详情,请参阅 [Inkling 文档](https://tinker-docs.thinkingmachines.ai/cookbook/inkling/)。 ## Tinker 在这里,我们介绍几个 Tinker 原语——fine-tune LLM 的基本组件(有关更多详细信息,请参阅[快速入门指南](https://tinker-docs.thinkingmachines.ai/tinker/quickstart/)): ``` import tinker service_client = tinker.ServiceClient() training_client = service_client.create_lora_training_client( base_model="meta-llama/Llama-3.2-1B", rank=32, ) training_client.forward_backward(...) training_client.optim_step(...) training_client.save_state(...) training_client.load_state(...) sampling_client = training_client.save_weights_and_get_sampling_client() sampling_client.sample(...) ``` 有关使用这些原语来 fine-tune LLM 的最小示例,请参阅 [tinker_cookbook/recipes/sl_loop.py](tinker_cookbook/recipes/sl_loop.py) 和 [tinker_cookbook/recipes/rl_loop.py](tinker_cookbook/recipes/rl_loop.py)。 ### 教程 刚开始接触 Tinker?[`tutorials/`](tutorials/) 目录包含 20 多个渐进式的 [marimo](https://marimo.io/) notebook,逐步介绍了核心概念——rendering、loss 函数、completer、权重管理——以及自定义 RL 环境、DPO、RLHF 和权重导出等高级主题。使用 `marimo edit tutorials/101_hello_tinker.py` 运行任何教程。有关完整列表,请参阅[教程 README](tutorials/README.md),或在 [Tinker 文档网站](https://tinker-docs.thinkingmachines.ai/tutorials)上浏览渲染后的版本。 要下载任何模型的权重: ``` rest_client = service_client.create_rest_client() future = rest_client.get_checkpoint_archive_url_from_tinker_path(sampling_client.model_path) with open(f"model-checkpoint.tar.gz", "wb") as f: f.write(future.result()) ``` ### Tinker Cookbook 除了这些原语之外,我们还提供了 **Tinker Cookbook**(即此 repo),这是一个包含大量抽象的库,可帮助您自定义训练环境。 [`tinker_cookbook/recipes/sl_basic.py`](tinker_cookbook/recipes/sl_basic.py) 和 [`tinker_cookbook/recipes/rl_basic.py`](tinker_cookbook/recipes/rl_basic.py) 包含了配置监督学习和强化学习的最简示例。 我们还在 [`tinker_cookbook/recipes/`](tinker_cookbook/recipes/) 文件夹中包含了更完整的示例: - **[Chat SFT](tinker_cookbook/recipes/chat_sl/)**:在对话数据集(例如 Tulu3)上进行监督微调。 - **[Math RL](tinker_cookbook/recipes/math_rl/)**:使用可验证奖励进行数学推理的强化学习。 - **[Code RL](tinker_cookbook/recipes/code_rl/)**:在竞技编程中使用沙盒代码执行进行 RL(DeepCoder 复现)。 - **[偏好学习](tinker_cookbook/recipes/preference/)**:DPO 和三阶段 RLHF pipeline(SFT、奖励模型、RL)。 - **[蒸馏](tinker_cookbook/recipes/distillation/)**:在单教师和多教师配置下进行 on-policy 和 off-policy 知识蒸馏。 - **[工具使用](tinker_cookbook/recipes/search_tool/)**:用于检索增强生成(Search-R1 复现)的 RL。 - **[多智能体](tinker_cookbook/recipes/multiplayer_rl/)**:具有 self-play 和 cross-play 的多智能体 RL。 - **[音频](tinker_cookbook/recipes/audio/)**:针对 Inkling 音频输入的 SFT 和 RL,包括语音识别和说话风格分类。 - **[VLM 图像分类](tinker_cookbook/recipes/vlm_classifier/)**:视觉语言模型的监督训练和评估。 [recipes README](tinker_cookbook/recipes/README.md) 涵盖了所有可用的 recipe,包括 Harbor RL、基于 rubric 的评分、VLM 分类和 SDFT。每个 recipe 都包含一个 `README.md`,其中包含实现细节、启动命令和预期结果。 ### 评估(实验性) Tinker Cookbook 包含一个用于评估已训练模型的[基准测试框架](tinker_cookbook/eval/): ``` from tinker_cookbook.eval.benchmarks import run_benchmarks, BenchmarkConfig results = await run_benchmarks( ["gsm8k", "mmlu_pro", "ifeval"], sampling_client, renderer, BenchmarkConfig(save_dir="evals/step500"), ) ``` 该框架目前支持 12 个基准测试(GSM8K、MATH-500、MMLU-Pro、MMLU-Redux、GPQA、IFEval、MBPP、C-Eval、SuperGPQA、IFBench、AIME 2025、AIME 2026),其分数已根据发布的结果进行了验证,此外还包括 LiveCodeBench、Terminal Bench 和 SWE-bench 等实验性基准测试。基准测试还可以通过 `BenchmarkEvaluator` 作为内联训练评估器。 **注意:** 基准测试分数对评估配置非常敏感——系统提示、`max_tokens`、temperature 和超时设置会显著影响结果。我们在所有报告的分数旁边记录了确切的设置。该框架正在积极开发中;欢迎提供反馈和贡献。有关验证分数、配置详细信息以及添加新基准测试的说明,请参阅 [eval README](tinker_cookbook/eval/README.md)。 ### 文档 有关完整的 Tinker 文档,请访问 [tinker-docs.thinkingmachines.ai](https://tinker-docs.thinkingmachines.ai)。 ### 实用工具 Tinker Cookbook 还提供了可重用的构建模块: - [`renderers`](tinker_cookbook/renderers/) — token 序列和结构化聊天消息之间的双向转换 - [`hyperparam_utils`](tinker_cookbook/hyperparam_utils.py) — 用于 LoRA 训练的 learning rate 和超参数缩放 - [`eval`](tinker_cookbook/eval/) — 基准测试框架和内联训练评估器(参见上方的[评估](#evaluation-experimental)) ## Claude Code 技能 Tinker Cookbook 附带了 [Claude Code 技能](https://docs.anthropic.com/en/docs/claude-code/skills),可以指导 Claude 如何使用 Tinker API。安装它们,以便 Claude 可以帮助您在任何项目中编写训练代码: ``` /plugin marketplace add thinking-machines-lab/tinker-cookbook ``` 然后从 Discover 选项卡(`/plugin` → Discover)安装 **tinker** 插件。安装完成后,将提供两项技能: | 命令 | 作用 | |---|---| | `/tinker:research` | 规划并运行 post-training 实验——SFT、RL、DPO、蒸馏、评估、超参数、模型选择等 | | `/tinker:debug` | 诊断训练缓慢、挂起、输出不匹配、renderer 问题和错误 | 技能也会根据上下文自动触发——要求 Claude“设置 SFT 训练”,它就会在不使用斜杠命令的情况下加载正确的技能。当 repo 更新时,技能会自动更新。 ## 开发设置 ``` uv sync --extra dev pre-commit install ``` 这将安装开发依赖项,并注册在每次提交时运行 `ruff` 格式化和 linting 的 pre-commit 钩子。CI 会在所有 pull request 上强制执行这些检查。 ## 引用 如果您在研究中使用 Tinker,请按如下格式引用: ``` Thinking Machines Lab, 2026. Tinker. https://thinkingmachines.ai/tinker/. ``` 或者使用以下 BibTeX 引用: ``` @misc{tml2026tinker, author = {Thinking Machines Lab}, title = {Tinker}, year = {2026}, url = {https://thinkingmachines.ai/tinker/}, } ```
标签:DLL 劫持, 人工智能, 凭据扫描, 分布式训练, 大语言模型, 安全规则引擎, 模型微调, 用户模式Hook绕过, 示例代码, 网络攻击, 逆向工具