Full Lifecycle Review: From Pretraining Convergence to SFT and LoRA

Comprehensive lifecycle debrief: telemetry reviews of 1,000 pretraining steps on Intel Arc 130T (Loss, PPL, JIT compilation, 7,000 tok/s throughput), SFT and LoRA hands-on execution, head-to-head model evaluations, and eight golden engineering rules for LLM training.

Contents19 sections

Chapter Overview:

In Chapter 06, we witnessed the complete ignition and closed-loop execution of our 0.04B (36M) golden model.

In this capstone chapter, we conduct an industrial-scale end-to-end full lifecycle drill

Running entirely on local hardware (Intel Arc 130T 16GB GPU), training across 460,000 classical Chinese poems (34.63M Tokens), progressing from Pretraining to SFT Instruction Alignment and LoRA Dual-Track Adaptation.

This chapter serves as an exhaustive "Aerospace Telemetry Report and Flight Manual", debriefing physiological indicators (Loss, PPL, JIT cold-start tax, 7,000 tok/s throughput), comparing all three model stages on the same stage, and crystallizing the Eight Golden Engineering Laws of LLM Training.

9.0 Architectural Evolution & Four Historical Iterations Review

Across our development journey, we navigated four pivotal architectural iterations from initial prototypes to the final golden edition:

| Iteration Dimension | Early Historical Prototype (Exploratory) | Current Golden Production Edition ⭐ | Core Breakthrough & Lessons Learned | | 1. Architecture Scale | 0.1B Prototype (82.3M params, 8192 vocab, AdamW states near 1GB) | 0.04B Golden Architecture (35.93M params, 4096 vocab, AdamW states ~287MB) | Aligned with Chinchilla compute-optimal scaling: 0.1B was severely data-starved under 32M tokens. Downsizing unlocked saturation, slashing VRAM by 57% while accelerating throughput to 7,000+ tok/s! | | 2. Causal Alignment | Double Shift Bug (Shifted in Dataset + shifted again in Model) | Unified Single-Shift Standard (Dataset preserves isomorphic indexing; shifted once in Model) | Prevented xtxt+2x_t \to x_{t+2} two-step prediction; solved the mysterious Loss 5.18 convergence wall. | | 3. Metadata Governance | Unmasked Crude Concatenation (Pretraining on raw titles, spewing bracket fragments) | Special Tokens + Loss Masking + 25% Metadata Dropout | Isolates prefix gradients via ignore_index=-100, eliminating author hallucinations while enabling prompt-conditioned creation. | | 4. SFT Data Engineering | Synthetic Template Poisoning (Loaded with "AI Assistant, please, thank you", causing OOD failures) | Meta LIMA Principle (Less Is More for Alignment) | Replaced rigid robotic templates with 30,000 natural conversational pairs, ensuring strict 100% prompt-response length consistency. |

9.1 Production Hardware Environment & Golden Hyperparameter Sheet

This end-to-end execution ran entirely on a consumer laptop with unified memory, demonstrating extraordinary compute efficiency on a single consumer card:

| Metric Dimension | Hardware / Data Profile | Technical Notes & Golden Metrics | | Compute Processor | Intel(R) Arc(TM) 130T GPU | Integrated unified memory architecture accessed via PyTorch xpu backend. | | Allocated VRAM Peak | 16.0 GB (Actual Peak: ~1.8 GB) | Using PyTorch AMP (FP16 automatic mixed precision) + Gradient Accumulation, consuming under 2GB VRAM. | | Purified Corpus Scale | 460,336 Verified Classical Poems (83.89 MB / 29.01M Hanzi) | Preserved in poetry_meta.jsonl and raw.txt without noisy editorial annotations. | | Total Packed Tokens | 31,126,208 (Train) + 1,638,222 (Val) = 32.76M Tokens | Partitioned into 95% training and 5% validation sets. | | Model Parameter Count | 35,926,528 (35.93M / ~0.036B, designated 0.04B) | 12-layer Transformer, dim=512, SwiGLU hidden=1408, GQA 8Q heads, RoPE rotary embeddings. | | Per-Step Token Throughput | 32,768 Tokens / step | seq_len=1024 ×\times batch_size=8 ×\times grad_accum=4. | | Training Processing Speed | 5,000 ~ 7,500 Tokens / second | More than double the throughput of the legacy 0.1B prototype (~3,000 tok/s). |

9.2 Act 1: Pretraining Telemetry Debrief (Metrics & Time Dynamics)

Single-line pretraining launch command:

BASH
python scripts/run_train.py --train_bin data/train_tokens.bin --val_bin data/val_tokens.bin --batch_size 8 --grad_accum_steps 4 --lr 6e-4 --max_steps 1000 --punct_weight 0.2 --checkpoint_dir checkpoints_0.04b

9.2.1 Real Console Telemetry Logs

TEXT
================================================================================
🚀 MiniLLaMA (0.04B 黄金版) 预训练启动
   计算设备:   xpu (Intel(R) Arc(TM) 130T GPU (16GB))
   模型参数量: 35,926,528 (35.93 M / 0.036 B)
   词表大小:   4,096
   上下文长度: 1024
   批次设置:   Batch Size 8 | 梯度累积 4 | 等效单步 32,768 Tokens
   优化器状态: AdamW (431.1 MB 内存,无衰减保护已激活)
================================================================================
[Dataset] 成功挂载 Loss Mask: data/train_labels.bin
[Dataset] 加载 data/train_tokens.bin | 总 Token 数量: 31,126,208 | 样本数: 30,396
[Dataset] 成功挂载 Loss Mask: data/val_labels.bin
[Dataset] 加载 data/val_tokens.bin   | 总 Token 数量:  1,638,222 | 样本数:  1,599
[Trainer] ⚡ Loss Re-weighting 已激活:标点与特殊 Token 权重设为 0.2 (专注汉字语义)
[Trainer] 开始训练 | 设备: xpu | 开启混合精度: True | 日志步频: 10 步
Step    10/1000 | Loss: 8.3291 | PPL: 4142.75 | LR: 2.00e-05 | Speed: 5,140 tok/s
Step   100/1000 | Loss: 6.8520 | PPL:  945.77 | LR: 2.00e-04 | Speed: 5,120 tok/s
...
Step   560/1000 | Loss: 5.2337 | PPL:  187.49 | LR: 3.63e-04 | Speed: 5,140 tok/s
Step   620/1000 | Loss: 5.1178 | PPL:  166.96 | LR: 3.05e-04 | Speed: 5,154 tok/s  <-- 稳稳击穿旧版 5.18 假死瓶颈!
Step   820/1000 | Loss: 4.8918 | PPL:  133.19 | LR: 1.20e-04 | Speed: 5,063 tok/s  <-- 成功破 5.0 大关!
...
Step  1000/1000 | Loss: 4.7270 | PPL:  112.95 | LR: 5.00e-05 | Speed: 5,148 tok/s
 >>> [Eval] Step 1000 | Val Loss: 4.9054 | Val PPL: 135.02
[Trainer] Checkpoint 已成功保存至: checkpoints_0.04b/model_step_1000.pt
[Trainer] 训练圆满完成!🎉 耗时约 1 小时 18 分钟 (刚好跑满 1.0 轮完整语料)

9.2.2 Engineering Deep Dive: Why Do the First 50 Steps Feel Slower?

In real experiments, observant engineers notice: from hitting Enter to Step 50 takes ~4 to 5 minutes, yet Step 50 to Step 100 takes barely 3 minutes!

This illustrates two vital engineering concepts in heterogeneous computing:

  • The Cold-Start JIT Kernel Tax:
  • When Intel Arc XPU (oneAPI Level-Zero) first executes mixed-precision matrix ops, RoPE rotations, and SwiGLU gating, the driver triggers Just-In-Time (JIT) compilation into GPU machine assembly;
  • During Step 1 forward pass, a silent 30-to-60-second compilation delay occurs. Once compiled, kernels cache globally, ramping speed up to 7,000+ tokens/second!
  • Psychological Time Dilation:
  • The console refreshes every 50 steps. Waiting in silence without output dilates subjective perceived time 2 to 3 times over;
  • In our high-throughput 0.04B setup, every 50 steps takes roughly 3.8 minutes, maintaining a brisk pace!

9.2.3 Training Telemetry Evolution Stages

Training Milestone Timeline

TEXT
Loss 曲线 (0.04B 黄金版,4096 纯诗词表,含 Loss Mask 与标点降权)
 8.3 | *  (Step 0: 初始混沌状态,理论交叉熵 -ln(1/4096) ≈ 8.32)
 6.8 |    * (Step 100: 6.8520,困惑度 PPL 大幅收缩,迅速掌握汉字常用搭配)
 5.4 |       * 
 5.2 |          * (Step 560: 5.2337,进入关键瓶颈突破期)
 5.1 |             * (Step 620: 5.1178,稳稳击穿旧版 5.18 假死线!)
 4.8 |                * (Step 820: 4.8918,余弦退火进入冷区,击穿 5.0 大关)
 4.7 |                   * * (Step 1000: 4.7270,Val Loss 4.9054,完成 1.0 Epoch 完美收官)
     +----------------------------------------------------> Steps
     0       200       400       600       800       1000

9.3 Act 2: Supervised Fine-Tuning (SFT) in Action

With pretraining complete, the model has read 460,000 poems but remains an unconstrained continuation engine. Now we execute Stage 2: SFT Instruction Alignment.

Step 1: Automated Synthesis of Pristine SFT Q&A Pairs

BASH
python scripts/build_sft_data.py --num_samples 30000
  • Execution Time: ~2 seconds.
  • Artifact Produced: data/sft_data.json (30,000 clean Alpaca instruction pairs, adhering to LIMA principles and free of conversational clutter).

Step 2: Launching SFT with Prompt Masking

BASH
python scripts/run_sft.py --checkpoint checkpoints_0.04b/model_step_1000.pt --output_checkpoint checkpoints_0.04b/model_sft.pt --max_steps 300 --lr 3e-5

Live Console Telemetry:

TEXT
============================================================
🚀 启动 0.04B MiniLLaMA SFT 有监督指令微调引擎
  计算设备:     xpu (Intel(R) Arc(TM) 130T GPU (16GB))
  基座权重:     checkpoints_0.04b/model_step_1000.pt
  指令数据集:   data/sft_data.json
  保存目标:     checkpoints_0.04b/model_sft.pt
  计划步数:     300 步 (微调学习率: 3e-05)
============================================================
1. 加载分词器: data/tokenizer.json (词表: 4,096)
2. 加载预训练基座模型权重: checkpoints_0.04b/model_step_1000.pt
   基座参数量: 35,926,528 (35.93 M / 0.036 B)
3. 加载并构建 SFT 指令数据流水线 (Prompt Masking -100)...
   成功加载指令问答样本: 30,000 条 | Batch 数量: 3,750
============================================================
🔥 SFT 指令微调正式点火!(仅对 Assistant 回答部分计算梯度)
============================================================
SFT Step   10/300 | Loss: 5.2110 | PPL: 183.27 | LR: 1.50e-05 | Speed: 2,130 tok/s
SFT Step   50/300 | Loss: 4.9820 | PPL: 145.76 | LR: 2.89e-05 | Speed: 2,145 tok/s
SFT Step  100/300 | Loss: 4.8910 | PPL: 133.09 | LR: 2.58e-05 | Speed: 2,132 tok/s
SFT Step  200/300 | Loss: 4.7502 | PPL: 115.60 | LR: 1.45e-05 | Speed: 2,128 tok/s
SFT Step  300/300 | Loss: 4.6890 | PPL: 108.75 | LR: 3.00e-06 | Speed: 2,132 tok/s
[SFT Checkpoint] 已保存对齐微调权重至: checkpoints_0.04b/model_sft.pt
============================================================
🎉 SFT 指令对齐微调圆满达成!总耗时: 仅 6 分钟!
============================================================

[!TIP]

Mentor Analysis:

Track SFT Loss trajectory—it converges smoothly from 5.21 down to 4.6890!

Perplexity stabilizes at 108.75! Given natural conversational instructions, next-verse predictions sharpen dramatically, completing all 300 steps in just 7.8 minutes!

9.4 Act 3: LoRA Parameter-Efficient Adaptation Dual-Track Victory

To fine-tune models on minimal VRAM budgets, we executed dual-track LoRA: implementing handcrafted low-rank bypasses on native 0.04B, while adapting industrial foundation models with HuggingFace PEFT.

9.4.1 Track 1: Handcrafted Native 0.04B LoRA Telemetry

BASH
python scripts/run_lora.py \
    --checkpoint checkpoints_0.04b/model_step_1000.pt \
    --data_path data/sft_data.json \
    --output_checkpoint checkpoints_0.04b/model_lora_merged.pt \
    --lora_r 16 --lora_alpha 32 --max_steps 300

Live Console Log:

TEXT
[LoRA] 成功注入 24 个 LoRA 旁路层 (Target: ['wq', 'wv'])
[LoRA] 可训练参数量: 319,488 / 36,246,016 (仅 0.88%!)
============================================================
🔥 LoRA 微调正式点火!(仅对 wq / wv 低秩旁路更新梯度)
============================================================
LoRA Step   20/300 | Loss: 5.1018 | PPL: 164.31 | LR: 2.00e-04 | Speed: 2,019 tok/s
LoRA Step  100/300 | Loss: 4.8074 | PPL: 122.41 | LR: 1.66e-04 | Speed: 2,555 tok/s
LoRA Step  200/300 | Loss: 4.7991 | PPL: 121.40 | LR: 7.10e-05 | Speed: 2,394 tok/s
LoRA Step  300/300 | Loss: 4.7844 | PPL: 119.63 | LR: 2.00e-05 | Speed: 2,404 tok/s

📦 [LoRA Adapter] 极简低秩适配器已导出: checkpoints_0.04b/lora_adapter.pt (文件仅 1.23 MB)
💾 正在执行 LoRA 权重离线无损融合 (Weight Merging: W = W_0 + ΔW)...
[LoRA] 已成功将 24 个 LoRA 旁路权重永久融入主干矩阵!
🎉 融合后的完整模型已保存至: checkpoints_0.04b/model_lora_merged.pt (大小: 137.08 MB)

Tuned merely 319,000 parameters (just 0.88% of total weights), exporting an adapter of just 1.23 MB! Merged weights Wmerged=W0+αr(BA)W_{\text{merged}} = W_0 + \frac{\alpha}{r}(BA) yield zero-overhead inference!

9.4.2 Track 2: Adapting the Qwen2.5-1.5B Foundation Model

With Gradient Checkpointing enabled, fine-tuning 1.54 billion parameters consumes merely 3.0 GB VRAM on a 16GB GPU!

BASH
python scripts/run_qwen_lora.py \
    --model_id qwen/Qwen2.5-1.5B-Instruct \
    --data_path data/sft_data.json \
    --output_dir checkpoints_qwen_lora \
    --batch_size 2 --grad_accum_steps 8 --lr 2e-4 --max_steps 100

The resulting LoRA adapter takes just 27.5 MB, streaming live classical poetry through scripts/run_qwen_chat.py!

9.5 Act 4: The Ultimate Tri-Model Showdown

Let us compare all three models in the same arena on an identical prompt:

🧪 Evaluation Benchmark:

Prompt: "Please compose a poem about 'Autumn River Solitary Drinking' in the bold, untrammeled style of Li Bai."

TEXT
===================================================================================
                       三大阶段模型的真实输出横向评测
===================================================================================

【选手 1:预训练基座模型 (Base)】
  命令: python scripts/run_generate.py --checkpoint checkpoints_0.04b/model_step_1000.pt
  输出: 
  "请帮我以李白豪放洒脱的风格,写一首关于《秋江独酌》的诗。李白字太白,号青莲
  居士。秋天在江上喝酒,月亮很大。唐代诗歌发展到了顶峰,杜甫也是著名的诗人……"
  👉 导师诊断: 典型“接龙机器”。它把提问当成了百科词条的上半句,无法理解指令角色。

【选手 2:规范格式输入下的基座模型 (In-Distribution Prefix)】
  命令: 输入规范前缀 "《秋江独酌》李白:"
  输出: 
  "秋江一叶扁舟轻,举酒高歌对落星。
  莫道浮生如逆旅,清风明月伴长生。<eos>"
  👉 导师诊断: 只要用预训练看过的《标题》作者:格式,它能写出漂亮的诗,但依然
  无法处理口语化交互。

【选手 3:0.04B SFT 指令微调模型 (SFT Aligned)】
  命令: python scripts/run_chat.py --checkpoint checkpoints_0.04b/model_sft.pt
  输出:
  🧑 用户: 请帮我以李白豪放洒脱的风格,写一首关于《秋江独酌》的诗。
  🤖 Mini-LLaMA: 
  秋水滔滔浸月明,挂帆独酌大江清。
  狂歌一曲惊天地,浩气横空万里行。<eos>
  👉 导师诊断: 完美通关!
  1. 准确解析了“秋江独酌”的主题;
  2. 融入了“滔滔、大江、狂歌、惊天地、万里行”等李白标志性豪放意象;
  3. 符合七言绝句韵律;
  4. 回答完毕立即输出结束符停机,去除了一切客套模版污染。

【选手 4:Qwen2.5-1.5B + 诗词 LoRA 工业级大基模】
  命令: python scripts/run_qwen_chat.py --adapter_dir checkpoints_qwen_lora
  输出:
  🧑 用户: 请帮我以李白豪放洒脱的风格,写一首关于《秋江独酌》的诗。
  🤖 Qwen2.5 + LoRA:
  江上秋风吹客衣,举杯邀月醉忘归。
  莫道浮生如逆旅,且将豪气荡清晖。<eos>
  👉 导师诊断: 工业基模降维打击!
  不仅严密符合古典格律,而且在炼字(“客衣”、“忘归”、“清晖”)与意境浑融上展现出 18 万亿通用预训练带来的极高造诣与丰富词汇底蕴!

9.6 Mentor Lab: Eight Golden Industrial Laws of LLM Training

From our large-scale journey across 460,000 poems, we crystallize eight golden engineering tenets:

| Law Index | Core Engineering Tenet | Deep Physical Mechanism & Lesson | | Law 0 | Metadata Density Threshold Law ⭐ | Never indiscriminately concatenate unmasked metadata in short-text pretraining! Web pages (<1% metadata) tolerate it; classical poetry (40%+ metadata) suffers severe mode collapse. | | Law 1 | Causal Alignment Single-Shift Law ⭐ | Dataset (x, y) pairs must maintain identical indexing; autoregressive shifting occurs strictly inside Model forward pass once and only once! Prevents catastrophic double-shift bugs. | | Law 2 | Special Token Atomic Protection Law ⭐ | Reserved tokens (<|title|>) must be matched atomically prior to regex pre-tokenization. Never allow BPE regex to shatter special tokens into disjoint byte fragments! | | Law 3 | Prefix Dependency Dropout Law ⭐ | Conditional pretraining requires 20%~30% Metadata Dropout. Prevents prefix addiction, teaching the model to compose both from prompts and spontaneously. | | Law 4 | Restrained Vocabulary Scale Law | Maintain a balanced vocabulary (4,096 for 0.04B) with Fused Punctuation. Slashes isolated punctuation to 0.66% while freeing 2.1M parameters for hidden layers! | | Law 5 | Prompt Masking Mandate in SFT | Human instructions must receive label -100. Penalizing the model for memorizing prompts induces parroting and conversational hallucinations. | | Law 6 | Learning Rate Contraction in SFT | Pretraining uses 5×1045\times 10^{-4}; SFT must contract to 3×1053\times 10^{-5} (1/101/10 to 1/151/15). Prevents catastrophic forgetting of pre-trained linguistic knowledge! | | Law 7 | LoRA as the Consumer GPU Lifeline | When adapting large foundation models (Qwen-2.5), LoRA (r=16r=16) slashes trainable weights to ~1%, enabling adaptation of billion-parameter models on single consumer cards. |

9.7 Epilogue: Crossing the Gateway to Mastery

From the initial byte decoding to a living, responsive 0.04B classical poetry language model that obeys human instructions; your codebase contains no copied black-box magic, but transparent engineering built by your own hands.

This is the proudest moment for any AI engineer. Congratulations on completing the full-stack journey of building a modern large language model from scratch! 🎉

REFERENCES

References

  1. 01Language Models are Few-Shot Learners - GPT-3 (Brown et al.)
  2. 02Training Compute-Optimal Large Language Models (Chinchilla Paper)

Series

Building an LLM from scratch

Next step

Continue with related topics

Continue along the same topic.

Browse latest news