Skip to content

Low-bit optim support for DTensor [to be closed] #490

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

Closed
wants to merge 11 commits into from

Conversation

gau-nernst
Copy link
Collaborator

@gau-nernst gau-nernst commented Jul 9, 2024

I only opened this so it's easy to see the diff. Refer to #484

@awgu @wanchaol @weifengpy

If I don't .view(-1) to the DTensor, I will hit cache size limit. A min repro shows that torch.compile cannot generate dynamic kernel for the sharded dimension?

# Modified from https://github.com/pytorch/pytorch/tree/main/torch/distributed/_tensor
# to run this file (i.e. dtensor_example.py):
# TORCH_LOGS="recompiles" torchrun --standalone --nnodes=1 --nproc-per-node=4 dtensor_example.py
import os
import torch
from torch.distributed._tensor import init_device_mesh, Shard, distribute_tensor, _dtensor_init_helper

# Create a mesh topology with the available devices:
# 1. We can directly create the mesh using elastic launcher, (recommended)
# 2. If using mp.spawn, one need to initialize the world process_group first and set device
#   i.e. torch.distributed.init_process_group(backend="nccl", world_size=world_size)

@torch.compile(fullgraph=True, dynamic=True)
def f(x):
    return torch.sin(x) + torch.cos(x)

mesh = init_device_mesh("cuda", (int(os.environ["WORLD_SIZE"]),))

for shape in [(1024, 2048), (2048, 1024)]:
    big_tensor = torch.randn(shape)

    # Shard this tensor over the mesh by sharding `big_tensor`'s 0th dimension over the 0th dimension of `mesh`.
    my_dtensor = distribute_tensor(big_tensor, mesh, [Shard(dim=0)])
    f(my_dtensor)
[rank0]: torch/_dynamo/guards.py:2584] [0/1] [__recompiles] Recompiling function f in /home/ubuntu/code/ao/debug.py:12
[rank0]: torch/_dynamo/guards.py:2584] [0/1] [__recompiles]     triggered by the following guard failure(s):
[rank0]: torch/_dynamo/guards.py:2584] [0/1] [__recompiles]     - tensor 'L['x']' size mismatch at index 0. expected 1024, actual 2048

If I add .view(-1), torch compile will throw the following error

AssertionError: s8 (could be from ["L['grad']._base._local_tensor.size()[0]"]) not in {s3: ["L['exp_avg']._local_tensor.scale.size()[0]", "L['exp_avg']._local_tensor.scale.size()[0]", "L['exp_avg']._local_tensor.scale.size()[0]", "L['exp_avg']._local_tensor.scale.size()[0]", "L['exp_avg']._local_tensor.scale.size()[0]"], s4: ["L['exp_avg']._local_tensor.qmap.size()[0]", "L['exp_avg']._local_tensor.qmap.size()[0]", "L['exp_avg']._local_tensor.qmap.size()[0]", "L['exp_avg']._local_tensor.qmap.size()[0]", "L['exp_avg']._local_tensor.qmap.size()[0]", "L['exp_avg_sq']._local_tensor.qmap.size()[0]", "L['exp_avg_sq']._local_tensor.qmap.size()[0]", "L['exp_avg_sq']._local_tensor.qmap.size()[0]", "L['exp_avg_sq']._local_tensor.qmap.size()[0]", "L['exp_avg_sq']._local_tensor.qmap.size()[0]"], s12: ["L['grad']._local_tensor.size()[0]", "L['grad']._local_tensor.size()[0]"], s10: ["L['grad']._local_tensor.storage_offset()", "L['grad']._local_tensor.storage_offset()"], s16: ["L['exp_avg_sq']._local_tensor.scale.size()[0]", "L['exp_avg_sq']._local_tensor.scale.size()[0]", "L['exp_avg_sq']._local_tensor.scale.size()[0]", "L['exp_avg_sq']._local_tensor.scale.size()[0]", "L['exp_avg_sq']._local_tensor.scale.size()[0]"], s23: ["L['p']._local_tensor.size()[0]", "L['p']._local_tensor.size()[0]"]}. If this assert is failing, it could be due to the issue described in pytorch/pytorch#90665

I'm seeing 2 ways to solve this (with optim state being DTensor)

  1. Just increase cache size limit (and don't do .view(-1))
  2. Unwrap all DTensor before calling adam step

cc @msaroufim @janeyx99

Copy link

pytorch-bot bot commented Jul 9, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/490

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure, 1 Unrelated Failure

As of commit 598569a with merge base 12ac498 (image):

NEW FAILURE - The following job has failed:

FLAKY - The following job failed but was likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 9, 2024
@bdhirsh
Copy link
Contributor

bdhirsh commented Jul 10, 2024

Hey @guangy10, where do I need to add the view(-1) call to repro? I'm running with a nightly of pytorch, and I tweaked your repro code to the below, but it doesn't error for me:

import os
import torch
from torch.distributed._tensor import init_device_mesh, Shard, distribute_tensor, _dtensor_init_helper

# Create a mesh topology with the available devices:
# 1. We can directly create the mesh using elastic launcher, (recommended)
# 2. If using mp.spawn, one need to initialize the world process_group first and set device
#   i.e. torch.distributed.init_process_group(backend="nccl", world_size=world_size)

@torch.compile(fullgraph=True, dynamic=True)
def f(x):
    out = torch.sin(x) + torch.cos(x)
    return out.view(-1)

mesh = init_device_mesh("cuda", (int(os.environ["WORLD_SIZE"]),))

for shape in [(1024, 2048), (2048, 1024)]:
    big_tensor = torch.randn(shape)

    # Shard this tensor over the mesh by sharding `big_tensor`'s 0th dimension over the 0th dimension of `mesh`.
    my_dtensor = distribute_tensor(big_tensor, mesh, [Shard(dim=0)])
    f(my_dtensor)

# but you will get the following error
# AssertionError: s8 (could be from ["L['grad']._base._local_tensor.size()[0]"]) not in {s3: ["L['exp_avg']._local_tensor.scale.size()[0]", "L['exp_avg']._local_tensor.scale.size()[0]", "L['exp_avg']._local_tensor.scale.size()[0]", "L['exp_avg']._local_tensor.scale.size()[0]", "L['exp_avg']._local_tensor.scale.size()[0]"], s4: ["L['exp_avg']._local_tensor.qmap.size()[0]", "L['exp_avg']._local_tensor.qmap.size()[0]", "L['exp_avg']._local_tensor.qmap.size()[0]", "L['exp_avg']._local_tensor.qmap.size()[0]", "L['exp_avg']._local_tensor.qmap.size()[0]", "L['exp_avg_sq']._local_tensor.qmap.size()[0]", "L['exp_avg_sq']._local_tensor.qmap.size()[0]", "L['exp_avg_sq']._local_tensor.qmap.size()[0]", "L['exp_avg_sq']._local_tensor.qmap.size()[0]", "L['exp_avg_sq']._local_tensor.qmap.size()[0]"], s12: ["L['grad']._local_tensor.size()[0]", "L['grad']._local_tensor.size()[0]"], s10: ["L['grad']._local_tensor.storage_offset()", "L['grad']._local_tensor.storage_offset()"], s16: ["L['exp_avg_sq']._local_tensor.scale.size()[0]", "L['exp_avg_sq']._local_tensor.scale.size()[0]", "L['exp_avg_sq']._local_tensor.scale.size()[0]", "L['exp_avg_sq']._local_tensor.scale.size()[0]", "L['exp_avg_sq']._local_tensor.scale.size()[0]"], s23: ["L['p']._local_tensor.size()[0]", "L['p']._local_tensor.size()[0]"]}. If this assert is failing, it could be due to the issue described in https://github.com/pytorch/pytorch/pull/90665
# p = p.view(-1)
# grad = grad.view(-1)
Copy link
Collaborator Author

@gau-nernst gau-nernst Jul 10, 2024

Choose a reason for hiding this comment

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

@bdhirsh The .view(-1) is here. The snippet in the PR description is just to show that torch.compile can't generate dynamic kernel for DTensor (from what I understand).

To give more context. The low-bit optim here works fine with normal tensor. To avoid re-compilation when p has different ndim, I call .view(-1) to flatten it (I observe torch.compile(dynamic=True) still re-compiles when p has different ndim, thus the .view(-1) trick).

So if I use the same trick for DTensor, the above error will show up.

The recompilation issue is worse for DTensor, because it seems like torch.compile can't generate dynamic kernel for it (as reported in the PR description as a standalone example).

@gau-nernst gau-nernst closed this Jul 11, 2024
@gau-nernst gau-nernst deleted the low_bit_optim_dtensor branch July 17, 2024 15:52
yanbing-j pushed a commit to yanbing-j/ao that referenced this pull request Dec 9, 2024
* Fix runner-et on Mac and Android

* Fix typo

* Avoid fatal message

* Change fatal to warning
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 Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants