Gate set_detect_anomaly behind STYLETTS2_DETECT_ANOMALY env var#3
Open
shreyaskarnik wants to merge 1 commit intosemidark:mainfrom
Open
Gate set_detect_anomaly behind STYLETTS2_DETECT_ANOMALY env var#3shreyaskarnik wants to merge 1 commit intosemidark:mainfrom
shreyaskarnik wants to merge 1 commit intosemidark:mainfrom
Conversation
Currently train_second.py turns torch.autograd.set_detect_anomaly(True) on
unconditionally at module import time. That's a debugging tool, not a
production setting:
* 5–10× slower backward (anomaly detection traces every op).
* Materially more memory held (causes OOMs that wouldn't happen otherwise).
* On single-A100 + bf16, can deadlock the first backward pass entirely
(no error, just stuck) — this bit us on a Marathi fine-tune Stage 2 run
until we manually patched the file.
Make it opt-in via an env var. Default off; set STYLETTS2_DETECT_ANOMALY=1
when diagnosing a NaN/inf to get the same behaviour as before.
No behaviour change for users who explicitly want anomaly detection.
Diagnosing-by-default is the regression we're fixing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
train_second.py:10currently turns ontorch.autograd.set_detect_anomaly(True)unconditionally at module import. This PR gates it behind aSTYLETTS2_DETECT_ANOMALY=1env var (default off; opt-in for debugging).Why
Anomaly detection is a debugging feature, not a production setting. With it on:
mixed_precision=bf16+bs=8 + PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True. Removing the line let training proceed normally.So the default cost is high (slower, less memory, occasional hang) for the benefit of a feature most users don't actually want during a normal training run.
Change
To get the previous behaviour:
STYLETTS2_DETECT_ANOMALY=1 accelerate launch ... train_second.py ....Context
Hit this while training shreyaskarnik/bol-tts-marathi, a Marathi fine-tune of Kokoro-82M built on the semidark/kokoro-deutsch recipe (which uses this fork as a submodule). Worth flagging since anyone running Stage 2 single-GPU on bf16 will likely hit it.
Happy to amend if you'd prefer different env var naming or a different gating mechanism (e.g. CLI flag).
Test plan
set_detect_anomalyis not invoked → faster, no hang.STYLETTS2_DETECT_ANOMALY=1:set_detect_anomaly(True)is invoked → previous behaviour restored.