classical-ja-pretrain / code /scripts /README_pretrain_hf.md
kuramot6f's picture
Initial release (history squashed)
7a088d4
|
Raw History Blame Contribute Delete
8.7 kB

Historical-text pretraining

Run commands from /home/kuramoto/ds. This runner initializes a new model using the SmolLM2-135M architecture and the existing 32K tokenizer (124,635,456 parameters). It does not load pretrained SmolLM2 weights. PyTorch provides tensors/autograd; Transformers provides Llama; Accelerate handles BF16, compilation, accumulation, and full-state checkpoints. No JAX is used.

Environment

The prepared environment is .venv-pretrain, with an official CUDA 13.0 ARM64 PyTorch wheel. To recreate it on the same Ubuntu ARM64 host:

python3 -m venv .venv-pretrain
.venv-pretrain/bin/python -m pip install --upgrade pip
bash scripts/bootstrap_pretrain_headers.sh
.venv-pretrain/bin/python scripts/repair_cuda_wheel.py
.venv-pretrain/bin/python -m pip install --extra-index-url https://download.pytorch.org/whl/cu130 -r scripts/requirements-pretrain.lock.txt
.venv-pretrain/bin/python -m pip check

The header package is extracted under .local/python-dev; no root installation occurs. The runner supplies its include directories to Triton. repair_cuda_wheel.py verifies the PyPI SHA256 and AArch64 ELF architecture, corrects NVIDIA's internal sbsa wheel tag to match its aarch64 filename, and rebuilds the wheel RECORD. Library bytes are unchanged; provenance is saved in worklog/2026-10-24-cuda-wheel-repair.json. The complete lock and a shorter requirements file are both provided. Re-running the repair can produce a different wheel archive checksum because ZIP timestamps vary; library hashes must remain identical.

Start the shared trunk

.venv-pretrain/bin/accelerate launch --config_file scripts/accelerate_single_gpu.yaml \
  scripts/pretrain_hf.py train --config scripts/pretrain_hf_config.json \
  --output runs/classical-smollm2-trunk --stop-before-decay

Use a new output directory. This runs the 800M-target shared trunk and stops after update 24,415 with a protected full-state anchor. Omit --stop-before-decay to continue the configured high_quality branch to update 30,518 (1,000,013,824 targets). These commands launch long training; the implementation/verification task has not launched them.

The training stream uses only the frozen train binaries in unified/training-tokens-v1/build-006. Source/era sampling is deterministic, with cumulative exposure guards. Each sequence contains 2,049 saved IDs; the model sees 2,048 inputs and predicts 2,048 targets. BF16 compute uses FP32 parameters, gradients, and Adam moments. WSD raises LR for 611 updates, holds 3e-4 through update 24,415, then decays linearly over 6,103 updates. The data mix ramps during the first half of decay.

FlashAttention-backed PyTorch SDPA is required, so an unavailable kernel raises an error. Fixed, unpadded training batches use a registered HF causal SDPA interface, avoiding Transformers' generic packed-position mask construction under Dynamo. Positions advance continuously across concatenated documents; BOS/EOS are retained, with no position reset or document attention isolation. Validation batches are grouped by equal length. Inductor compiles training through Accelerate; evaluation uses the unwrapped eager module.

Resume or fork

runs/<run>/latest.json records the latest checkpoint. Resume into the same output directory with the original flags/configuration:

.venv-pretrain/bin/accelerate launch --config_file scripts/accelerate_single_gpu.yaml \
  scripts/pretrain_hf.py train --output runs/classical-smollm2-trunk \
  --resume runs/classical-smollm2-trunk/checkpoints/checkpoint-00000500 \
  --stop-before-decay

Checkpoint restore verifies data/tokenizer/configuration/code/runtime identities and file hashes. Use the checkpoint named in latest.json, which may be an anchor. SIGINT/SIGTERM finishes the current update and saves; a forced kill requires restoring the latest complete checkpoint. Periodic retention keeps the two latest plus the selected best; anchors are protected. A controlled stop also writes an HF model/tokenizer under export/.

Fork an anneal mix into a new directory:

.venv-pretrain/bin/accelerate launch --config_file scripts/accelerate_single_gpu.yaml \
  scripts/pretrain_hf.py train --output runs/classical-smollm2-moderate \
  --fork runs/classical-smollm2-trunk/checkpoints/pre-decay-anchor-00024415 \
  --branch moderate

Other branches are high_quality, wiki_favored, and no_mix_switch_control. Forks retain weights, optimizer, RNG, sampler progress, and source exposures, with new Trackio identities and recorded parent hashes. An ordinary resume cannot change branch, code, versions, or numerical configuration. No budget/exposure reset occurs.

Monitoring and evaluation

Try text completion

Run the interactive local sampler (defaults to the validation-selected checkpoint in best.json):

.venv-pretrain/bin/python scripts/try_pretrain.py

Enter the beginning of a classical passage, such as 春はあけぼの。, then press Enter. Each prompt is independent; /quit exits. This is a pretrained base model, so use passage beginnings rather than chat instructions. Training sequences were 2,048 tokens; keep initial experiments short.

For a single prompt or the final-step export:

.venv-pretrain/bin/python scripts/try_pretrain.py --prompt '昔、ある所に' --max-new-tokens 128
.venv-pretrain/bin/python scripts/try_pretrain.py --checkpoint final --prompt '昔、ある所に' --max-new-tokens 128

Use --greedy for deterministic argmax decoding, or adjust --temperature 0.8 --top-p 0.95 for sampling. --seed defaults to 42. CUDA is used when available; --cpu forces CPU execution. --run selects another run, and --checkpoint also accepts a checkpoint directory. Model, tokenizer, and generation configuration are loaded locally; no training or test evaluation is performed.

Dashboard and validation

.venv-pretrain/bin/python -c "import trackio; trackio.show(project='classical-smollm2-pretraining', host='127.0.0.1', share=False, open_browser=False, block_thread=True)"

Trackio logs locally; no Space/server upload is configured. Training logs LR, loss, gradient norm, target progress, throughput/ETA, source consumption, validation metrics, and CUDA allocation/reservation. Supported GPU/CPU metrics are sampled every 10 seconds. metrics.jsonl, events.jsonl, run.json, and status.json provide local backups. Resume retains the run name; each anneal fork has its own run and family lineage.

Fast validation runs every 250 updates; full fixed-window validation and raw-text tasks run every 2,500 updates, curriculum boundaries, and completion. Small task fixtures remain automatically screened and pending manual clean-text review. Treat their model selection as provisional; inspect per-source/era NLL alongside paired work-group task uncertainty and equal-source bits per character. Do not treat a tiny engineering smoke score as model quality.

.venv-pretrain/bin/python scripts/pretrain_hf.py evaluate \
  --model runs/classical-smollm2-trunk/export --tasks \
  --output runs/classical-smollm2-trunk/validation.json

For a bounded engineering check, add --limit-per-source 1 --task-limit 2. Exports include the custom tokenizer adapter; HF loading uses AutoTokenizer.from_pretrained(path, trust_remote_code=True) for this local, inspectable code.

After selecting a final branch using validation only, the explicit final-test gate enables test-window evaluation. Test task packs can be frozen with the same recipe at that point:

.venv-pretrain/bin/python scripts/build_validation_tasks.py --split test --final-test
.venv-pretrain/bin/python scripts/pretrain_hf.py evaluate \
  --model runs/SELECTED/export --split test --final-test --tasks \
  --task-pack unified/evaluation-tasks-v1/test-001 --output runs/SELECTED/final-test.json

No test task pack is created during development verification.

Reproduce bounded checks

.venv-pretrain/bin/python -m unittest discover -s scripts -p 'test_pretrain*.py'
.venv-pretrain/bin/python scripts/verify_hf_runtime.py --output runs/check-new --compile
.venv-pretrain/bin/python scripts/verify_hf_kernels.py --output runs/kernel-check-new.json
.venv-pretrain/bin/python scripts/pretrain_hf.py train \
  --output runs/benchmark-new --max-updates 55 --skip-evaluation

Use fresh paths. --max-updates caps additional updates without compressing the WSD schedule. Never resume a verification checkpoint for production pretraining. --no-compile, --no-trackio, and --cpu are explicit diagnostic options; changing compilation/device changes the checkpoint identity.