-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Torchao float8 training #3348
Open
muellerzr
wants to merge
22
commits into
main
Choose a base branch
from
torchao-float8
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Torchao float8 training #3348
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
febc634
Bookmark
muellerzr a7663c5
bookmark
muellerzr ed1adb1
Add torchao base example
muellerzr 3d34f8e
Currently broken
muellerzr a032f1a
Clean
muellerzr 7197943
DDP varient working
muellerzr dc797fd
FSDP as well
muellerzr 145dec2
Works for all but zero3
muellerzr 676d5ac
Bookmark: currently zero3 is underperforming
muellerzr 92b3d9b
Bookmark
muellerzr 660b6b5
Another diff
muellerzr ca45f46
Fin
muellerzr a0193ce
Fin
muellerzr b271b13
Add req huggingface suite
muellerzr a5d2c29
update tests for fp8/torchao/ddp
muellerzr 002c4be
Log FP8 backend used and adjust typing
muellerzr 95fbb8d
add documentation for convert_to_float8_training
muellerzr ac22296
Rename to convert_model_to_fp8_ao
muellerzr 06642ca
Call superinit"
muellerzr d46b0a1
Add types
muellerzr 62881ac
Clean
muellerzr f3ceb37
Use filter_first_and_last_linear_layers
muellerzr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
FROM nvcr.io/nvidia/pytorch:24.07-py3 | ||
|
||
RUN pip install transformers evaluate datasets | ||
RUN git clone https://github.com/huggingface/accelerate.git | ||
|
||
RUN cd accelerate && \ | ||
pip install -e . && \ | ||
cd benchmarks/fp8 | ||
|
||
RUN /bin/bash | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# FP8 Benchmarks | ||
|
||
Comparing and running [torchao](https://github.com/pytorch/ao/tree/main/torchao/float8) FP8 with accelerate | ||
|
||
## Overview | ||
|
||
This repo provides scripts which compare native `torchao` model training against `accelerate`'s own integration. Each modeling type is segmented out via a script, supporting the following: | ||
|
||
* Single GPU training (`non_distributed.py`) | ||
* Multi-GPU training via DistributedDataParallelism (`ddp.py`) | ||
* Fully Sharded Data Parallelism (`fsdp.py`) | ||
* DeepSpeed ZeRO 1-3 (`deepspeed.py`) | ||
|
||
To run them, it's recommended to use a docker image (see the attached `Dockerfile`) and not install `torchao` manually. | ||
|
||
## Running: | ||
|
||
There are official Docker images located at `huggingface/accelerate:gpu-fp8-torchao-nightly` which can be used. | ||
|
||
You can run all scripts using the core `accelerate launch` command without any `accelerate config` being needed. | ||
|
||
For single GPU, run it via `python`: | ||
|
||
```bash | ||
python non_distributed.py | ||
``` | ||
|
||
For the rest, run it via `accelerate launch`: | ||
|
||
```bash | ||
accelerate launch ddp.py # or distrib_deepspeed.py, ddp.py | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
# Copyright 2025 The HuggingFace Inc. team. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
This script tests to ensure that `accelerate` performs at the same level as raw `torchao`. | ||
|
||
This particular script verifies this for DDP training. | ||
""" | ||
|
||
from functools import partial | ||
|
||
import evaluate | ||
import torch | ||
from fp8_utils import get_training_utilities | ||
from torch.nn.parallel import DistributedDataParallel as DDP | ||
from torchao.float8 import convert_to_float8_training | ||
|
||
from accelerate import Accelerator | ||
from accelerate.state import AcceleratorState | ||
from accelerate.utils import AORecipeKwargs, set_seed | ||
|
||
|
||
MODEL_NAME = "bert-base-cased" | ||
METRIC = evaluate.load("glue", "mrpc") | ||
|
||
|
||
def evaluate_model(model, dataloader, metric, accelerator=None): | ||
"Turns model to .eval(), runs dataloader, calculates metric, then turns eval back on" | ||
model.eval() | ||
for step, batch in enumerate(dataloader): | ||
with torch.no_grad(): | ||
outputs = model(**batch) | ||
predictions = outputs.logits.argmax(dim=-1) | ||
references = batch["labels"] | ||
if accelerator is not None and accelerator.num_processes > 1: | ||
predictions, references = accelerator.gather_for_metrics((predictions, references)) | ||
metric.add_batch(predictions=predictions, references=references) | ||
return metric.compute() | ||
|
||
|
||
def filter_linear_layers(module, fqn, first_layer_name=None, last_layer_name=None): | ||
if isinstance(module, torch.nn.Linear): | ||
if module.in_features % 16 != 0 or module.out_features % 16 != 0: | ||
return False | ||
# For stability reasons, we skip the first and last linear layers | ||
# Otherwise can lead to the model not training or converging properly | ||
if fqn in (first_layer_name, last_layer_name): | ||
return False | ||
return True | ||
|
||
|
||
def train_baseline(): | ||
set_seed(42) | ||
model, optimizer, train_dataloader, eval_dataloader, lr_scheduler = get_training_utilities(MODEL_NAME) | ||
first_linear = None | ||
last_linear = None | ||
for name, module in model.named_modules(): | ||
if isinstance(module, torch.nn.Linear): | ||
if first_linear is None: | ||
first_linear = name | ||
last_linear = name | ||
func = partial(filter_linear_layers, first_layer_name=first_linear, last_layer_name=last_linear) | ||
accelerator = Accelerator() | ||
device = accelerator.device | ||
model.to(device) | ||
|
||
convert_to_float8_training(model, module_filter_fn=func) | ||
|
||
# Convert the model to DDP | ||
device_ids, output_device = [accelerator.local_process_index], accelerator.local_process_index | ||
model = DDP(model, device_ids=device_ids, output_device=output_device) | ||
|
||
base_model_results = evaluate_model(model, eval_dataloader, METRIC, accelerator=accelerator) | ||
model.train() | ||
|
||
for batch in train_dataloader: | ||
with torch.autocast(device_type="cuda", dtype=torch.bfloat16): | ||
batch = batch.to(device) | ||
outputs = model(**batch) | ||
loss = outputs.loss | ||
loss.backward() | ||
optimizer.step() | ||
optimizer.zero_grad() | ||
lr_scheduler.step() | ||
|
||
trained_model_results = evaluate_model(model, eval_dataloader, METRIC, accelerator=accelerator) | ||
|
||
assert ( | ||
trained_model_results["accuracy"] > base_model_results["accuracy"] | ||
), f'Accuracy should be higher for the trained model: {trained_model_results["accuracy"]} > {base_model_results["accuracy"]}' | ||
assert ( | ||
trained_model_results["f1"] > base_model_results["f1"] | ||
), f'F1 score should be higher for the trained model: {trained_model_results["f1"]} > {base_model_results["f1"]}' | ||
|
||
return base_model_results, trained_model_results | ||
|
||
|
||
def train_integration(): | ||
AcceleratorState()._reset_state(True) | ||
accelerator = Accelerator(mixed_precision="fp8", kwargs_handlers=[AORecipeKwargs()]) | ||
set_seed(42) | ||
model, optimizer, train_dataloader, eval_dataloader, lr_scheduler = get_training_utilities( | ||
MODEL_NAME, accelerator=accelerator | ||
) | ||
|
||
model, optimizer = accelerator.prepare(model, optimizer) | ||
base_model_results = evaluate_model(model, eval_dataloader, METRIC, accelerator=accelerator) | ||
model.train() | ||
|
||
for batch in train_dataloader: | ||
outputs = model(**batch) | ||
loss = outputs.loss | ||
accelerator.backward(loss) | ||
optimizer.step() | ||
optimizer.zero_grad() | ||
lr_scheduler.step() | ||
|
||
trained_model_results = evaluate_model(model, eval_dataloader, METRIC, accelerator=accelerator) | ||
|
||
assert ( | ||
trained_model_results["accuracy"] > base_model_results["accuracy"] | ||
), f'Accuracy should be higher for the trained model: {trained_model_results["accuracy"]} > {base_model_results["accuracy"]}' | ||
assert ( | ||
trained_model_results["f1"] > base_model_results["f1"] | ||
), f'F1 score should be higher for the trained model: {trained_model_results["f1"]} > {base_model_results["f1"]}' | ||
|
||
return base_model_results, trained_model_results | ||
|
||
|
||
if __name__ == "__main__": | ||
baseline_not_trained, baseline_trained = train_baseline() | ||
accelerator_not_trained, accelerator_trained = train_integration() | ||
|
||
assert ( | ||
baseline_not_trained["accuracy"] == accelerator_not_trained["accuracy"] | ||
), f'Accuracy should be the same for the baseline and accelerator: {baseline_not_trained["accuracy"]} == {accelerator_not_trained["accuracy"]}' | ||
assert ( | ||
baseline_not_trained["f1"] == accelerator_not_trained["f1"] | ||
), f'F1 score should be the same for the baseline and accelerator: {baseline_not_trained["f1"]} == {accelerator_not_trained["f1"]}' | ||
assert ( | ||
baseline_trained["accuracy"] == accelerator_trained["accuracy"] | ||
), f'Accuracy should be the same for the baseline and accelerator: {baseline_trained["accuracy"]} == {accelerator_trained["accuracy"]}' | ||
assert ( | ||
baseline_trained["f1"] == accelerator_trained["f1"] | ||
), f'F1 score should be the same for the baseline and accelerator: {baseline_trained["f1"]} == {accelerator_trained["f1"]}' | ||
|
||
torch.distributed.destroy_process_group() |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a quick explanation for that ?