← Back to Pull requests

Add templated SFT notebook (#69) #74

Closed
unknown closed this pull request without merging on Jun 30, 2026 14:33 UTC
View on GitHub
unknown unknown commented on May 17, 2026 00:36 UTC
Author

Resolves part of #69 — adds the SFT templated notebook. OSFT and LoRA versions are out of scope for this PR but can follow the same pattern.

What's in this PR

A new examples/notebooks/templated_sft.ipynb that lets a user with a dataset and a base model fine-tune by setting four hardware inputs and running through. The notebook does the memory-influencing hyperparameter calculation for them.

Inputs the notebook asks for

| Input | Description | Default |
|---|---|---|
| model_path | HuggingFace ID or local path | Qwen/Qwen2.5-1.5B-Instruct |
| num_gpus | GPUs per node | 1 |
| num_nodes | Number of nodes | 1 |
| vram_per_gpu_gb | Per-GPU VRAM in GB | None → auto-detect via torch.cuda |

How memory-influencing params are chosen

1. Per-GPU VRAM is auto-detected via torch.cuda.get_device_properties(0).total_memory when vram_per_gpu_gb is None. Otherwise the user's declared value is trusted.
2. The notebook walks a list of candidate max_tokens_per_gpu values (largest first: 16384, 8192, 4096, 2048, 1024, 512, 256), asks training_hub.estimate(training_method="sft", ...) for the expected memory at each, and picks the largest value whose expected usage falls within safety_margin vram_per_gpu_bytes (default safety margin 0.90).
3. If even the smallest candidate doesn't fit, the cell raises a RuntimeError listing the obvious options: smaller model, more/larger GPUs, LoRA instead of full SFT, or lowering the safety margin.

The rest of the config

Conservative defaults for num_epochs (3), effective_batch_size (1 per GPU), learning_rate (2e-5), and max_seq_len (min(4096, max_tokens_per_gpu) to respect the training_hub constraint). The notebook's "Where to go from here" section calls these out explicitly as knobs to tune after the first run is stable.

The final cell calls training_hub.sft(...) with all parameters wired through, including nproc_per_node and nnodes so multi-node works by just raising num_nodes.

Scope decisions worth flagging

  • SFT only in this PR. Issue #69 calls for SFT, OSFT, and LoRA templated notebooks. I scoped this to SFT to keep the review tractable and confirm the pattern before extending. The same shape extends to OSFT (swap estimate → training_method="osft", add unfreeze_rank_ratio to inputs, swap sft → osft) and LoRA (use LoRAEstimator, swap to lora_sft, add lora_r to inputs). Happy to do those as follow-ups or in this PR if you'd prefer one combined review.

  • No data_path / ckpt_output_dir validation. The notebook uses placeholder strings and asks the user to update them before running the launch cell. Adding dataset-format validation feels like a bigger scope question — the comprehensive tutorial covers it.

  • VRAM auto-detection probes GPU 0 only. If your GPUs are heterogeneous, set vram_per_gpu_gb explicitly.
  • Validation

  • The notebook parses as valid nbformat 4.5 JSON; all six code cells parse as valid Python (ast.parse).

  • sft() and estimate() parameter names were cross-checked against src/training_hub/algorithms/sft.py and src/training_hub/profiling/memory_estimator.py.

  • The notebook was not executed end-to-end — that needs a GPU and a training dataset. Cells fail gracefully when CUDA is missing (the VRAM auto-detect cell raises with a clear error pointing the user at the explicit-value path).
  • Open questions

    1. Candidate list and safety margin. I picked [16384, 8192, 4096, 2048, 1024, 512, 256] and safety_margin=0.90 by eyeballing the existing runnable_l40_sft.ipynb (which uses 8192) and the memory_estimator example (which talks about a 1.3x upper bound). Are there established values you'd rather use?
    2. Default model. Picked Qwen/Qwen2.5-1.5B-Instruct because it matches the existing runnable SFT notebook. Open to a different default if you prefer Granite or something else as the canonical starter.
    3. File location. Placed at examples/notebooks/templated_sft.ipynb alongside the existing notebooks. Happy to move (e.g., to examples/notebooks/templated/sft.ipynb) if you'd rather group templated notebooks separately.

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    Summary by CodeRabbit

    New Features
    Added a templated Supervised Fine-Tuning notebook that guides users through hardware setup, validates GPU/node inputs, and auto-detects VRAM when not provided.
    Automatically chooses a safe token limit based on available memory, shows expected usage and utilization, and provides clear feedback if no configuration fits.
    Sets up training parameters and launches distributed fine-tuning across the selected GPUs and nodes.

    Documentation
    * Includes detailed in-notebook guidance, limitations, and next-step recommendations.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 612 lines (+459 / -153) across 7 files Reviewer: RobotSail Review duration: 64197m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.50) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 612 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.384
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #74"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.2% (MEDIUM) · Higher = larger blast radius
    50.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 21.7% · Safe to merge
    21.7%