Skip to content

[Torchtrain][Checkpoint] Add support to allow dtype conversion - #222

Merged
wz337 merged 9 commits into
pytorch:mainfrom
wz337:add_export_dtype
Apr 12, 2024
Merged

wz337 merged 9 commits into
pytorch:mainfrom
wz337:add_export_dtype

Conversation

@wz337

@wz337 wz337 commented Apr 11, 2024 •

Copy link
Copy Markdown
Contributor

Adds a field of checkpoint.export_dtype: we allow dtype conversion only when we are checkpoint model weights only and the current dtype is not the same as the export dtype at the end of the training.

Also add a change to get rid of freqs_cis buffer when exporting.

We can see with export_dtype=bf16, the model weights is about half of the size when comparing to export_dtype=fp32.

# model_weights_only=false
(pytorch-3.10) [[email protected] ~/local/torchtrain (add_export_dtype)]$ du -h test_runner_checkpoint_full_checkpoint
212M    test_runner_checkpoint_full_checkpoint/step-5
212M    test_runner_checkpoint_full_checkpoint/step-10
212M    test_runner_checkpoint_full_checkpoint/step-15
212M    test_runner_checkpoint_full_checkpoint/step-20
846M    test_runner_checkpoint_full_checkpoint

# model_weights_only=true and export_dtype = fp32
(pytorch-3.10) [[email protected] ~/local/torchtrain (add_export_dtype)]$ du -h test_runner_checkpoint_model_weights_only
212M    test_runner_checkpoint_model_weights_only/step-5
70M     test_runner_checkpoint_model_weights_only/step-10
281M    test_runner_checkpoint_model_weights_only

# model_weights_only=true and export_dtype = bf16
(pytorch-3.10) [[email protected] ~/local/torchtrain (add_export_dtype)]$ du -h test_runner_checkpoint_model_weights_only_bf16
212M    test_runner_checkpoint_model_weights_only_bf16/step-5
35M     test_runner_checkpoint_model_weights_only_bf16/step-10
247M    test_runner_checkpoint_model_weights_only_bf16

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Meta Open Source bot. label Apr 11, 2024
@wz337
wz337 marked this pull request as ready for review April 11, 2024 04:30
Comment thread torchtrain/checkpoint.py Resolved
@wz337
wz337 marked this pull request as draft April 11, 2024 20:28
@wz337
wz337 marked this pull request as ready for review April 11, 2024 22:59
Comment thread torchtitan/checkpoint.py
# Context: https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/llama/model.py#L348
self.states["model"].pop("freqs_cis")

if self.export_dtype != torch.float32:

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.

technically, shouldn't we do the check once per parameter whether the current parameter matches the export type?

and shouldn't we not assume that float32 is the type that the model came from? (what if the model was actually in bf16 but someone wanted to export in .. fp8

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For now, the state dict generated by the model is fp32. So if we train with torchtitan and we do a state_dict call, the dtype is fp32.

Comment thread torchtitan/checkpoint.py Outdated


DTYPE_MAP = {
"fp16": torch.float16,

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.

do we have helpers in torch somewhere for short versions of our torch dtypes? would be nice not to invent our own mapping if there is one already

@wz337 wz337 Apr 11, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried searching in PyTorch, but I can't seem to find a map.
Maybe we should just do.

{
"torch.float16": torch.float16,
...
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Alright taking the decision of doing:

{
"float16": torch.float16,
...
}

@wz337
wz337 requested review from gnadathur and wconstab April 12, 2024 08:32

@gnadathur gnadathur 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

@wz337
wz337 merged commit bf23f10 into pytorch:main Apr 12, 2024
lessw2020 pushed a commit that referenced this pull request Apr 18, 2024
Adds a field of checkpoint.export_dtype: we allow dtype conversion only
when we are checkpoint model weights only and the current dtype is not
the same as the export dtype at the end of the training.

Also add a change to get rid of `freqs_cis` buffer when exporting.


We can see with export_dtype=bf16, the model weights is about half of
the size when comparing to export_dtype=fp32.
```
# model_weights_only=false
(pytorch-3.10) [[email protected] ~/local/torchtrain (add_export_dtype)]$ du -h test_runner_checkpoint_full_checkpoint
212M    test_runner_checkpoint_full_checkpoint/step-5
212M    test_runner_checkpoint_full_checkpoint/step-10
212M    test_runner_checkpoint_full_checkpoint/step-15
212M    test_runner_checkpoint_full_checkpoint/step-20
846M    test_runner_checkpoint_full_checkpoint

# model_weights_only=true and export_dtype = fp32
(pytorch-3.10) [[email protected] ~/local/torchtrain (add_export_dtype)]$ du -h test_runner_checkpoint_model_weights_only
212M    test_runner_checkpoint_model_weights_only/step-5
70M     test_runner_checkpoint_model_weights_only/step-10
281M    test_runner_checkpoint_model_weights_only

# model_weights_only=true and export_dtype = bf16
(pytorch-3.10) [[email protected] ~/local/torchtrain (add_export_dtype)]$ du -h test_runner_checkpoint_model_weights_only_bf16
212M    test_runner_checkpoint_model_weights_only_bf16/step-5
35M     test_runner_checkpoint_model_weights_only_bf16/step-10
247M    test_runner_checkpoint_model_weights_only_bf16
```
philippguevorguian pushed a commit to YerevaNN/YNNtitan that referenced this pull request Aug 17, 2024
…ch#222)

Adds a field of checkpoint.export_dtype: we allow dtype conversion only
when we are checkpoint model weights only and the current dtype is not
the same as the export dtype at the end of the training.

Also add a change to get rid of `freqs_cis` buffer when exporting.


We can see with export_dtype=bf16, the model weights is about half of
the size when comparing to export_dtype=fp32.
```
# model_weights_only=false
(pytorch-3.10) [[email protected] ~/local/torchtrain (add_export_dtype)]$ du -h test_runner_checkpoint_full_checkpoint
212M    test_runner_checkpoint_full_checkpoint/step-5
212M    test_runner_checkpoint_full_checkpoint/step-10
212M    test_runner_checkpoint_full_checkpoint/step-15
212M    test_runner_checkpoint_full_checkpoint/step-20
846M    test_runner_checkpoint_full_checkpoint

# model_weights_only=true and export_dtype = fp32
(pytorch-3.10) [[email protected] ~/local/torchtrain (add_export_dtype)]$ du -h test_runner_checkpoint_model_weights_only
212M    test_runner_checkpoint_model_weights_only/step-5
70M     test_runner_checkpoint_model_weights_only/step-10
281M    test_runner_checkpoint_model_weights_only

# model_weights_only=true and export_dtype = bf16
(pytorch-3.10) [[email protected] ~/local/torchtrain (add_export_dtype)]$ du -h test_runner_checkpoint_model_weights_only_bf16
212M    test_runner_checkpoint_model_weights_only_bf16/step-5
35M     test_runner_checkpoint_model_weights_only_bf16/step-10
247M    test_runner_checkpoint_model_weights_only_bf16
```
songhappy added a commit to songhappy/torchtitan that referenced this pull request Oct 15, 2026
oneCCL 2022 hangs when a process that hosts a TorchStore storage volume also runs FSDP collectives (oneCCL pytorch#222), and concurrent first weight pulls deadlock in communicator creation (pytorch#225). On XPU the storage volumes therefore run in their own procs, a twin of the trainer mesh on the same devices, and the first pull after setup runs one generator at a time. Both are config fields defaulting to device_type == "xpu", so CUDA is unchanged.
songhappy added a commit to songhappy/torchtitan that referenced this pull request Oct 28, 2026
oneCCL 2022 hangs when a process that hosts a TorchStore storage volume also runs FSDP collectives (oneCCL pytorch#222), and concurrent first weight pulls deadlock in communicator creation (pytorch#225). On XPU, MonarchActorSystem therefore runs the storage volumes in their own procs, a twin of the trainer mesh on the same devices (MonarchActorSystem.Config.storage_volumes_in_own_procs), and the router runs the first pull after setup one generator at a time (InterGeneratorRouter.Config.sequential_initial_pull). Both default to device_type == "xpu", so CUDA is unchanged.
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.

4 participants