Skip to content
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

Implement Automatic Mixed Precision with GradScaler to Address NaN Loss Issues #13

Merged
merged 1 commit into from
Sep 23, 2024

Conversation

aiihn
Copy link
Contributor

@aiihn aiihn commented Aug 26, 2024

Description

This pull request addresses the issue of NaN losses occurring during mixed-precision training with --fp16 enabled (#12).

Key Changes

  • Integrated torch.cuda.amp.GradScaler to dynamically adjust loss scaling.
  • Replaced the manual loss scaling approach. Note: GradScaler will override the loss_scale set manually by --ls.

Usage

Use --fp16=True along with --enable_gradscaler=True. For example, below is the mixed-training command modified from run_ecm_1hour.sh.

torchrun --nnodes=1 --nproc_per_node=1 --rdzv_backend=c10d --rdzv_endpoint=localhost:9901 ct_train.py  \
    --outdir=ct-runs --data=datasets/cifar10-32x32.zip  \
    --cond=0 --arch=ddpmpp --metrics=fid50k_full        \
    --transfer=https://nvlabs-fi-cdn.nvidia.com/edm/pretrained/edm-cifar10-32x32-uncond-vp.pkl    \
    --duration=25.6 --tick=12.8 --double=250 --batch=128 --lr=0.0001 --optim=RAdam --dropout=0.2 --augment=0.0 \
    -q 256 --double 10000 --ema_beta 0.9993 --eval_every 80 --dump 80     \
    --desc bs128.200k \
    --fp16=True --enable_gradscaler=True

The FID records obtained using the above command are shown in the following images:
image
image

@Gsunshine Gsunshine self-requested a review September 6, 2024 06:03
Copy link
Member

@Gsunshine Gsunshine left a comment

Choose a reason for hiding this comment

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

Merge AMP via Gradscalar into ECT.

@@ -78,6 +78,7 @@ def convert(self, value, param, ctx):
@click.option('--fp16', help='Enable mixed-precision training', metavar='BOOL', type=bool, default=False, show_default=True)
@click.option('--tf32', help='Enable tf32 for A100/H100 training speed', metavar='BOOL', type=bool, default=False, show_default=True)
@click.option('--ls', help='Loss scaling', metavar='FLOAT', type=click.FloatRange(min=0, min_open=True), default=1, show_default=True)
@click.option('--enable_gradscaler', help='Enable torch.cuda.amp.GradScaler, NOTE overwritting loss_scale set by --ls', metavar='BOOL', type=bool, default=False, show_default=True)
Copy link
Member

Choose a reason for hiding this comment

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

Hi Zixiang @aiihn ,

Thanks for your neat PR!

Would it be better to use a short abbreviation like amp as the option name? AMP already stands for Automatic Mixed Precision.

if enable_gradscaler:
if 'gradscaler_state' in data:
dist.print0(f'Loading GradScaler state from "{resume_state_dump}"...')
# Although not loading the state_dict of the GradScaler works well, loading it can improve reproducibility.
Copy link
Member

Choose a reason for hiding this comment

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

Gotcha. Thanks for the comments!

scaler.step(optimizer)
scaler.update()
else:
# Update weights.
Copy link
Member

Choose a reason for hiding this comment

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

TODO is also unclear to me either. It seems still useful and compatible per Claude.

It's fine to remove my commented code for lr rampup.

@Gsunshine Gsunshine merged commit f8cdf75 into locuslab:main Sep 23, 2024
@Gsunshine
Copy link
Member

Hi @aiihn ,

Thank you again for your PR! I had another AMP implementation that could also be helpful for ECT. I’ll check it out later and test torch.autocast, bu feel free to take a look if you’re working with mixed precision!

Links for reference:
https://github.com/locuslab/torchdeq/blob/main/deq-zoo/deq-flow/main.py
https://github.com/locuslab/torchdeq/blob/main/deq-zoo/deq-flow/core/deq_flow.py

Cheers,
Zhengyang

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants