Arama Yap Mesaj Submit
Request a Callback
+90
X
X

Select Your Currency

Turkish Lira $ US Dollar Euro
X
X

Select Your Currency

Turkish Lira $ US Dollar Euro

Contact Us

Location Halkali merkez neighborhood fatih st ozgur apt no 46 , Kucukcekmece , Istanbul , 34303 , TR
EKA SUNUCU · AI TRAINING ERROR CENTER

SFTTrainer Loss 0.0, NaN and Eval NaN: Why Fine-Tuning Is Not Learning

Diagnose TRL SFTTrainer loss=0.0, NaN and eval_loss NaN using dataset format, -100 labels, truncation, assistant_only_loss, learning rate and precision.

PyTorchTransformersPEFT / TRLLast technical review: 14 August 2026
01

Key facts verified with official documentation

01

With TRL `assistant_only_loss=True`, loss is computed only on assistant tokens and the chat template must produce a valid assistant mask.

02

If assistant tokens are truncated beyond max length, labels can become entirely -100 and training may show zero-loss behavior without real supervision.

03

Prompt-completion and conversational datasets have different masking semantics in TRL; the dataset format must match the training configuration.

02

A zero loss is not necessarily good news

A true language-model loss of exactly zero at the start of SFT is suspicious. If it is 0.0 from the first step, suspect masked labels rather than perfect learning.

03

First check: how many real labels exist?

In cross-entropy, `-100` is commonly the ignore index. If every label is -100, no supervised token remains. Inspect the collated batch and count `labels.ne(-100).sum()` before training.

Diagnostic / validation commands
python - <<'PY'
# batch = next(iter(trainer.get_train_dataloader()))
# print((batch['labels'] != -100).sum(dim=1))
PY
04

Truncation may remove the whole assistant answer

With long system/user prompts and a short max length, the assistant response may be entirely truncated. TRL issues show this can produce all-zero assistant masks and zero-loss behavior. Measure token-length percentiles across the dataset.

05

`assistant_only_loss` and chat-template compatibility

Current TRL requires the chat template to support assistant-token masks through generation markers for assistant-only loss. If the template cannot produce the mask, the flag may fail or mask incorrectly.

06

NaN loss: precision and learning rate

NaN is not only a dataset problem. Excessive learning rate, FP16 overflow, custom-loss bugs, invalid logits or model-specific attention bugs can produce NaNs. Run a direct training-mode forward pass and verify finite logits and loss.

07

Eval loss NaN while train loss is normal

The eval set may contain examples without assistant responses, different formatting, different truncation distribution or fully masked batches. Verify train and eval preprocessing use the same assumptions.

08

Choosing the wrong dataset format

TRL supports language-modeling, prompt-completion and conversational formats. A plain `text` dataset and a `messages` conversational dataset do not share the same masking semantics. Identify the format before choosing loss masking.

09

Sanity-test outside the trainer

To isolate Trainer complexity, feed one batch directly to the model. Inspect shapes, supervised-label counts, finite logits and raw loss. If NaN appears here, the problem is in model/data rather than Trainer logging.

10

How to verify the fix

For the first 20-50 steps, supervised-token count should be >0, loss and grad norm finite, and learning rate sane. Compare fixed validation prompts between the base model and checkpoint using identical decoding settings.

DIAGNOSTIC MATRIX

Symptom → likely cause

SymptomFirst suspectValidation
Loss=0 at first stepAll labels -100Count labels
Zero only on long examplesTruncationToken-length distribution
Loss NaNLR/precision/logitsSingle-batch forward
Eval NaNEval masking/formatInspect eval labels
Production note

Do not blindly upgrade packages in a working training environment. Record GPU, driver, CUDA/PyTorch runtime, Transformers, Accelerate, PEFT, TRL, bitsandbytes/Diffusers, model revision and dataset fingerprint for every run. Reproduce minimally before changing production training.

FAQ

Frequently asked questions

Does loss 0 mean the model learned perfectly?

Usually no. Zero from the beginning often indicates no supervised labels or a masking/logging issue.

Does `assistant_only_loss=True` work with every model?

No. The chat template must be able to produce assistant-token masks.

OFFICIAL SOURCES

Official technical sources and project issues

CLUSTER

Related AI training guides

EKA SUNUCU · GPU ALTYAPISI

Choose the GPU around the training configuration.

Evaluate model size, precision, context, batch, LoRA/QLoRA or full fine-tuning and multi-GPU needs together instead of choosing by GPU name alone.

GPU Sunucu VPS Destek
Top