Skip to content

Second version of degub/deterministic configs. - #1807

Merged
tianyu-l merged 47 commits into
pytorch:mainfrom
githubsgi:deterministic02
Oct 29, 2025
Merged

tianyu-l merged 47 commits into
pytorch:mainfrom
githubsgi:deterministic02

Conversation

@githubsgi

Copy link
Copy Markdown
Contributor

Incorporating input from converastion in #1761

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 7, 2025
@githubsgi

Copy link
Copy Markdown
Contributor Author

@tianyu-l , please review.

@githubsgi githubsgi changed the title Second version of degub/deterinistic configs. Second version of degub/deterministic configs. Oct 7, 2025

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Left some comments.

Comment thread torchtitan/config/job_config.py Resolved
Comment thread torchtitan/config/job_config.py Resolved
Comment thread torchtitan/config/job_config.py Resolved
Comment thread torchtitan/train.py Resolved
Comment thread torchtitan/config/job_config.py Resolved
Comment thread torchtitan/config/job_config.py Resolved
@tianyu-l tianyu-l linked an issue Oct 9, 2025 that may be closed by this pull request

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Comment thread docs/debugging.md Resolved
Comment thread docs/debugging.md Resolved
Comment thread docs/debugging.md Resolved
Comment thread docs/debugging.md Resolved
Comment thread torchtitan/train.py Resolved
@githubsgi

Copy link
Copy Markdown
Contributor Author

@tianyu-l , do not review yet. Not sure the rebase was valid.

@githubsgi

Copy link
Copy Markdown
Contributor Author

@tianyu-l , please review.

Comment thread torchtitan/models/attention.py Resolved
Comment thread torchtitan/config/job_config.py Resolved
Comment thread torchtitan/experiments/flux/train.py Resolved
Comment thread torchtitan/experiments/forge/engine.py Resolved

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks a lot!

@tianyu-l

Copy link
Copy Markdown
Contributor

@githubsgi Please address lint issues.

@tianyu-l

Copy link
Copy Markdown
Contributor

@githubsgi
sorry it seems you need to rebase

@tianyu-l

Copy link
Copy Markdown
Contributor

@githubsgi unfortunately lint needs to be fixed again

@tianyu-l

Copy link
Copy Markdown
Contributor

@githubsgi linting failed

Comment thread torchtitan/models/attention.py Resolved
@tianyu-l

Copy link
Copy Markdown
Contributor

@githubsgi can you make sure linting passes before commiting?

@githubsgi

Copy link
Copy Markdown
Contributor Author

Would like to do that . How do I run the TorchTitan linter locally ?

@tianyu-l

Copy link
Copy Markdown
Contributor

fegin and others added 22 commits October 28, 2025 18:12
…h#1808)

With max-autotune, FlexAttention is not deterministic even if
torch.use_deterministic_algorithms is True. When deterministic mode is
set, we should also remove the usage of `max-autotune`.
In VLM interleaved training, with native resolution and aspect ratio,
the number of tokens participating in loss computation differ per rank.
Naive FSDP gradient averaging across data ranks can causes tokens on
ranks with fewer valid tokens to contribute more to the loss than on
other ranks.
This PR address this via loss balancing, which incur an additional comm
in the loss computation.
In practice, I haven't notice any impacts from this comm.

#### Quick sanity check
Let have a sum loss of all tokens on each rank i, with $N_i$ number of
tokens $L_i = \sum_{j=1}^{N_i}\ell_{ij}$ and its gradient $g_i =
\sum_{j=1}^{N_i}\nabla\ell_{ij}$

If we multiply the *loss* on each rank by a constant factor **c** (the
same for all ranks), then after `backward()`:

$$
\tilde g_i = c \cdot g_i .
$$

FSDP will *average* these gradients across ranks:

$$
g_{\text{FSDP}}=\frac{1}{R}\sum_{i=1}^{R} \tilde g_i
                =\frac{c}{R}\sum_{i=1}^{R} g_i .
$$

We want this to equal the **global‑sample average**:

$$
g_{\text{true}}
=\frac{1}{N_{\text{total}}}\sum_{i=1}^{R}\sum_{j=1}^{N_i}\nabla
\ell_{ij}
   =\frac{1}{N_{\text{total}}}\sum_{i=1}^{R} g_i .
$$

Thus for FSDP gradient to be correct, we need

$$
\frac{c}{R}= \frac{1}{N_{\text{total}}}\quad\Longrightarrow\quad
c=\frac{R}{N_{\text{total}}}.
$$

So the *right* scaling factor is $R/N_{\text{total}}$, which mean divide
the per-rank sum loss with $N_{\text{total}}/R$, which is **average
number of tokens per rank**.
Intuitively, this is the same as default cross-entropy loss, but instead
of diving sum loss on a rank by the number of tokens **on that rank**,
we now divide by the **average number of tokens across all rank**


P/s: sorry this PR is based on pytorch#1802 but I couldn't choose that as the
base branch. Maybe it will be easier to review once that PR is merged.
…ytorch#1776)

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0)
(oldest at bottom):
* pytorch#1797
* __->__ pytorch#1776

**Status**
1. Change all models, including the experimental ones.
2. E2E loss verification.
3. We should add an unittest for attention. But since we don't have GPU
unittest, this can be done in a separate PR.

**Summary**
This PR aims to refactor how TorchTitan build the attention masks and
pass to model. Before this PR, init_attention_masks() is called in
Trainer but the masks are stored as a class variable of
FlexAttentionWrapper(). We chose this shortcut to support the case where
a single model requires multiple masks.

The previous design has several issues, one particular one is
pytorch#1723.

pytorch/pytorch#164111 proves that we can let
PP split BlockMask, this PR performs the refactor to pass masks as an
argument of model.forward().

The new design:
1. Model needs to provide `get_attention_masks()` that accepts
`create_mask_fn`, `batch`, and `eos_id`. If the attention op is SDPA,
then this API should return None as SDPA currently doesn't support
varlen. But once it does, we may have to return some tuple of int that
represents the mask.

Justification: attention logic is technically a part of the model, but
requires some information from trainer/dataloader. So it's model
author's responsibility to provide some API that let trainer calls to
get the masks.

2. `get_attention_masks()` will be called from the trainer and the
resulting masks are passed to the model.forward().

Justification: this will allow us to fix
pytorch#1723 with
pytorch/pytorch#164111 and this PR.

3. Now SDPA and FlexAttention are wrapped in two different classes.
~~Note: we still have two very very thin op wrappers that are used for
CP. I keep these two for the CP education purpose. But this certainly
can be confusion for Titan's users. I'm opnn to merge them to
AttentionOp.~~

See the discussion in pytorch#1723.

**Verification**
*llama3*
```
./loss_compare.sh main 9dc16675b272ffdc3ed616e3244bcf7dc2d257f2 --steps=100 --no-seed-checkpoint --config="./torchtitan/models/llama3/train_configs/debug_model.toml"
```
*llama3 flex*
```
./loss_compare.sh main 9dc16675b272ffdc3ed616e3244bcf7dc2d257f2 --steps=100 --no-seed-checkpoint --config="./torchtitan/models/llama3/train_configs/debug_model.toml" --baseline-train-options="--model.flavor=debugmodel_flex_attn"
```
*llama4*
```
./loss_compare.sh main 9dc16675b272ffdc3ed616e3244bcf7dc2d257f2 --steps=100 --no-seed-checkpoint
```
*llama4 irope*
```
./loss_compare.sh main 9dc16675b272ffdc3ed616e3244bcf7dc2d257f2 --steps=100 --no-seed-checkpoint
```
*deepseek*
```
./loss_compare.sh main 9dc16675b272ffdc3ed616e3244bcf7dc2d257f2 --steps=100 --no-seed-checkpoint --config="./torchtitan/models/deepseek_v3/train_configs/debug_model.toml"
```
*deepseek flex*
```
./loss_compare.sh main 9dc16675b272ffdc3ed616e3244bcf7dc2d257f2 --steps=100 --no-seed-checkpoint --config="./torchtitan/models/deepseek_v3/train_configs/debug_model.toml" --baseline-train-options="--model.flavor=debugmodel_flex_attn"
```
Summary:
the script adds configuration options to run training locally with ft
enabled

---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with
[ReviewStack](https://reviewstack.dev/pytorch/torchtitan/pull/1812).
* pytorch#1840
* pytorch#1811
* pytorch#1810
* __->__ pytorch#1812
* pytorch#1809

---------

Co-authored-by: Tushar Jain <[email protected]>
Incorporating input from converastion in pytorch#1761
Summary:
the script adds configuration options to run training locally with ft
enabled

---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with
[ReviewStack](https://reviewstack.dev/pytorch/torchtitan/pull/1812).
* pytorch#1840
* pytorch#1811
* pytorch#1810
* __->__ pytorch#1812
* pytorch#1809

---------

Co-authored-by: Tushar Jain <[email protected]>
This PR:

- let `ExpertParallel` handles indices permute / unpermute when EP is
used
- move `to_local` to model code to be more explicit
- rename the `expert_parallel` wrapper which does permute / unpermute to
`indices_permutation_wrapper` to be more accurate
Next is step is to move `qwen3` and `llama4` to core, and remove
outdated experiments.
We originally thought each model should have its own `pipeline.py`
function.

However, for most LLMs, it turns out a single function would suffice,
and all models which needs PP are reusing `pipeline_llama.py` originally
written for llama3.

(For diffusion models, the model size doesn't justify the usage of PP.)

This PR consolidates them and moves `pipeline_llm` into
`torchtitan/distributed/pipeline_parallel.py`.

We can refactor later if things change.
…pytorch#1871)

In the past, the terms "args" and "config" have been used in a mix.

To make it unambiguous, in torchtitan we use
- "args" as in `ModelArgs` to refer to parameters used to define a model
in model code
- "config" as in `JobConfig` to refer to configurable training job
commands used in training script

This also PR also moves `custom_args_module` (which should be
`custom_config_module` according to the naming rule above) from
`Experimental` to `Job`, as it has been extensively used by various
models in torchtitan, especially those in the `experiments` folder.
Comment thread scripts/generate/test_generate.py Resolved
@tianyu-l
tianyu-l merged commit e61f2cc into pytorch:main Oct 29, 2025
jquesnelle pushed a commit to NousResearch/torchtitan that referenced this pull request Nov 10, 2025
Incorporating input from converastion in
pytorch#1761

---------

Co-authored-by: Jiani Wang <[email protected]>
Co-authored-by: Chien-Chin Huang <[email protected]>
Co-authored-by: Phúc H. Lê Khắc <[email protected]>
Co-authored-by: tianyu-l <[email protected]>
Co-authored-by: Ruisi Zhang <[email protected]>
Co-authored-by: Tushar Jain <[email protected]>
Co-authored-by: Tushar Jain <[email protected]>
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
Incorporating input from converastion in
pytorch#1761

---------

Co-authored-by: Jiani Wang <[email protected]>
Co-authored-by: Chien-Chin Huang <[email protected]>
Co-authored-by: Phúc H. Lê Khắc <[email protected]>
Co-authored-by: tianyu-l <[email protected]>
Co-authored-by: Ruisi Zhang <[email protected]>
Co-authored-by: Tushar Jain <[email protected]>
Co-authored-by: Tushar Jain <[email protected]>
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 25, 2026
Incorporating input from converastion in
pytorch#1761

---------

Co-authored-by: Jiani Wang <[email protected]>
Co-authored-by: Chien-Chin Huang <[email protected]>
Co-authored-by: Phúc H. Lê Khắc <[email protected]>
Co-authored-by: tianyu-l <[email protected]>
Co-authored-by: Ruisi Zhang <[email protected]>
Co-authored-by: Tushar Jain <[email protected]>
Co-authored-by: Tushar Jain <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding options to enable some determinism related configs

8 participants