Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/NicolasHoyosDevss/MaternaQA-es/llms.txt

Use this file to discover all available pages before exploring further.

train_qlora_trl.py is the canonical QLoRA training script for MaternaQA-es. It uses TRL’s SFTTrainer with PEFT LoRA and bitsandbytes 4-bit quantization. The script converts conversational JSONL to prompt/completion format in memory, computing loss only over the completion. This keeps loss masking explicit and avoids depending on model-specific chat templates that may not expose assistant masks.

Usage

Smoke test — validates that the configuration loads, trains, and saves correctly before committing to a full run:
python scripts/train_qlora_trl.py \
  --model-name google/gemma-4-E2B-it \
  --dataset-variant sft_grounded \
  --output-dir outputs/smoke-gemma4-e2b-grounded \
  --max-steps 10 \
  --train-limit 64 \
  --eval-limit 32
Full training — Gemma 4 grounded:
python scripts/train_qlora_trl.py \
  --model-name google/gemma-4-E2B-it \
  --dataset-variant sft_grounded \
  --output-dir outputs/gemma4-e2b-grounded
Full training — MedGemma 1.5 4B grounded:
python scripts/train_qlora_trl.py \
  --model-name google/medgemma-1.5-4b-it \
  --dataset-variant sft_grounded \
  --output-dir outputs/medgemma-grounded

All CLI Flags

FlagTypeDefaultDescription
--model-namestrgoogle/gemma-4-E2B-itHugging Face model ID or local path of the base model.
--model-classstrautoModel loading class. auto uses AutoModelForImageTextToText for Gemma 4/MedGemma and AutoModelForCausalLM for all others. Choices: auto, causal-lm, image-text-to-text.
--dataset-rootPathdatasets/obstetrics/qa/publicationRoot folder containing the publication variant subdirectories.
--dataset-variantstrsft_groundedSFT variant to use for training. Choices: sft_closed_book, sft_grounded.
--output-dirPath(required)Directory where the LoRA adapter and tokenizer are saved.
--max-lengthint1024Maximum sequence length for tokenization.
--num-train-epochsfloat2.0Number of training epochs.
--max-stepsintNoneIf set, overrides --num-train-epochs and stops training after this many steps. Useful for smoke tests.
--per-device-train-batch-sizeint1Training batch size per GPU.
--per-device-eval-batch-sizeint1Evaluation batch size per GPU.
--gradient-accumulation-stepsint8Number of steps to accumulate gradients before an optimizer step (effective batch size = 8).
--learning-ratefloat2e-4AdamW optimizer learning rate.
--warmup-ratiofloat0.03Fraction of total steps used for linear warmup.
--weight-decayfloat0.0Weight decay applied to non-bias parameters.
--max-grad-normfloat0.3Maximum gradient norm for gradient clipping.
--lora-rint16LoRA rank (number of decomposition dimensions).
--lora-alphaint16LoRA scaling factor (alpha).
--lora-dropoutfloat0.05Dropout probability applied to LoRA layers.
--logging-stepsint10Log training metrics every N steps.
--save-stepsintNoneSave a checkpoint every N steps. Only relevant when --save-strategy steps is set.
--eval-stepsintNoneRun evaluation every N steps. Only relevant when --eval-strategy steps is set.
--save-strategystrepochWhen to save checkpoints. Choices: no, steps, epoch.
--eval-strategystrepochWhen to run evaluation. Choices: no, steps, epoch.
--train-limitintNoneLimit the train split to the first N examples. Use for smoke tests.
--eval-limitintNoneLimit the validation split to the first N examples. Use for smoke tests.
--seedint3407Random seed for reproducibility.
--assistant-only-lossboolFalseAdvanced: use chat template masks to compute loss only on assistant turns. Requires templates with {% generation %}. Disabled by default in favor of the explicit prompt/completion split.
--packingboolFalsePack short sequences to improve GPU utilization. Disabled by default to avoid cross-sample contamination on models whose attention does not support safe packing.
--gradient-checkpointingboolTrueTrade compute for VRAM by recomputing activations during the backward pass.
--trust-remote-codeboolFalsePass trust_remote_code=True to model and tokenizer loaders. Only enable if the model requires it.
--attn-implementationstrNoneOptional attention backend override: eager, sdpa, flash_attention_2, etc.
--dry-runflagFalseLoad and validate the dataset without loading the model or running training.
--allow-cpuflagFalseAllow dry-runs without CUDA. Real QLoRA training always requires an NVIDIA/CUDA GPU.

Dataset Variants

VariantInput formatUse case
sft_groundedUser message contains Contexto fuente followed by the questionTrain and evaluate evidence-guided QA behavior; the model receives source context at inference time
sft_closed_bookUser message contains only the clinical questionMeasure domain adaptation without explicit context; the model must answer from its learned parameters alone

Training Configuration

The script builds a QLoRA setup using the following fixed configuration, derived directly from the source:
  • 4-bit NF4 quantization via BitsAndBytesConfig: load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_use_double_quant=True
  • Compute dtype: bfloat16 on Ampere+ GPUs (compute capability ≥ 8); float16 on older CUDA devices
  • LoRA rank: r=16, lora_alpha=16, lora_dropout=0.05, bias="none"
  • Target modules: "all-linear" — adapters are applied to all linear layers in the model
  • Task type: CAUSAL_LM
  • Optimizer: adamw_8bit (8-bit AdamW for additional VRAM savings)
  • LR scheduler: cosine
  • Gradient checkpointing: enabled by default with use_reentrant=False
  • Loss masking: completion_only_loss=True — loss is computed only over the assistant (completion) portion, not the prompt
  • TensorBoard reporting: enabled; logs written to runs/ inside --output-dir
  • Sequence packing: disabled by default

Model Class Handling

The --model-class flag controls which Transformers class is used to load the base model:
ValueClass usedWhen to use
autoDetermined by model name heuristicDefault; uses AutoModelForImageTextToText for any model name containing gemma-4 or medgemma, and AutoModelForCausalLM for everything else
image-text-to-textAutoModelForImageTextToTextExplicit override for Gemma 4 and MedGemma checkpoints
causal-lmAutoModelForCausalLMOverride for models that fail with an architecture error under image-text-to-text
If MedGemma 1.5 raises an architecture mismatch error, add --model-class causal-lm as a workaround:
python scripts/train_qlora_trl.py \
  --model-name google/medgemma-1.5-4b-it \
  --dataset-variant sft_grounded \
  --output-dir outputs/medgemma-grounded \
  --model-class causal-lm

Outputs

After training completes, the following are saved to --output-dir:
  • adapter_config.json — LoRA adapter configuration (rank, alpha, target modules, base model path)
  • adapter_model.safetensors — trained LoRA adapter weights
  • tokenizer.json — full tokenizer vocabulary and merge rules
  • tokenizer_config.json — tokenizer metadata and special token mappings
  • chat_template.jinja — the chat template saved alongside the tokenizer
  • training_args.bin — serialized SFTConfig training arguments for reproducibility
  • runs/ — TensorBoard event files (loss, learning rate, evaluation metrics per step)
  • checkpoint-N/ — intermediate checkpoints saved according to --save-strategy (one per epoch by default)
The publication README defines a four-experiment matrix using the canonical TRL + PEFT + bitsandbytes (QLoRA) route:
Model familyDataset variantTraining routePurpose
Gemma 4 instructsft_groundedTRL + PEFT + bitsandbytes (QLoRA)Measure evidence-guided QA on a general instruction-tuned model
Gemma 4 instructsft_closed_bookTRL + PEFT + bitsandbytes (QLoRA)Measure domain adaptation without context
MedGemma 1.5 4B ITsft_groundedTRL + PEFT + bitsandbytes (QLoRA)Measure evidence-guided QA on a medical instruct model
MedGemma 1.5 4B ITsft_closed_bookTRL + PEFT + bitsandbytes (QLoRA)Measure domain adaptation on a medical model without context
Requires an NVIDIA GPU with at least 16 GB VRAM. QLoRA with bitsandbytes will not run on CPU — the script exits with an error if CUDA is unavailable (unless --dry-run --allow-cpu is used for dataset validation only). Full fine-tuning is not the target: only the QLoRA adapter weights are saved to --output-dir, not the full model.

Build docs developers (and LLMs) love