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

Integrate distributed inference with chat/server #1381

Merged

Conversation

mreso
Copy link
Contributor

@mreso mreso commented Nov 16, 2024

This PR integrates distributed inference with the CLI options chat and server and implements Option 2b of #1376.

To test run on machine with 4 gpus:

python  torchchat.py chat llama3.1  --max-new-tokens 10  --distributed --tp 2 --pp 2
python  torchchat.py server llama3.1   --distributed --tp 2 --pp 2

Copy link

pytorch-bot bot commented Nov 16, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/torchchat/1381

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

❌ 1 New Failure, 1 Pending, 1 Unrelated Failure

As of commit 7650153 with merge base 90749d2 (image):

NEW FAILURE - The following job has failed:

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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 Meta Open Source bot. label Nov 16, 2024
@mreso mreso requested review from lessw2020 and Jack-Khuu November 16, 2024 05:45
"Meta-Llama-3-70B": "meta-llama/Meta-Llama-3-70B-Instruct",
"Meta-Llama-3.1-70B": "meta-llama/Meta-Llama-3.1-70B-Instruct",

}
Copy link
Contributor

@mikekgfb mikekgfb Nov 18, 2024

Choose a reason for hiding this comment

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

What's the gap to using the models described in model_config/models.json. (as implied by TODO comment)

Definitely should not be part of the present PR, but I think as a north star, it would be desirable to grab the same models (and download and mgmt infra etc) for non-distributed and distributed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the comment @mikekgfb. The gap wasn't that big, the args.model just wasn't accessible at that point and I wanted to take a deeper look to fix it right. Removed the cruel hack and I now save the distribution_path when creating the builder_arg. Still not sure if this is the intended way of locating the checkpoint though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure what you're trying to do. If it's just that the dict/json data structure describing the mapping isn't in scope, maybe what you want is some methods that give you the relevant info?

Also, these seem to be mapping of short names to HF network paths - should we not have a way to [ick them from the local filesystem (since the torchchat cli already manages download and all that). Oh, and if the answer is "we have bigger fish to fry, hooking this up is not highest priority" I will wholeheartedly agree. This is more about understanding the context of this PR.

Where I'm lacking the context is how you go from all the weights being available locally on a node to reading those weights on another node? And maybe that's why you prefer to straight up pick the files from HF? (Although local distribution from an already downloaded set of weights probs has higher bandwidth?) Again, there's much bigger fish to fry, and I think this PR is a good step in the direction of frying those fish ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I see, let me try to fill in some context for this PR. Previously, the distributed inference solution lived completely separate in its own script (within the torchchat repo but) separate from torchchat.py. Distributed provides its own utils to load either hf or torchchat weights (where the torchchat part is currently broken IIRC). In a previous PR (#1327), I enabled the usage of torchchat.py generate with a distributed model. This PR only progresses the integration into the cli by enabling chat/server but stops short from replacing the weight loading mechanics which are still custom to distributed.

So, yes, for this PR I was only looking for a quick and dirty way to map arg.model_name -> "huggingface distribution str" (without actually having model_name at hand) to load the weights from the hf cache. I now modified the PR to use the information provided in the model_config/models.json as you suggested. In a next PR we should then alter torchchat/distributed/checkpoint_utils.py to leverage torchchat infra like (e.g. builder_args) to locate and access the files.

Copy link
Contributor

@Jack-Khuu Jack-Khuu left a comment

Choose a reason for hiding this comment

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

Partial review so far, still need to dig through generate

cc: @Gasoonjia as secondary reviewer

torchchat/usages/openai_api.py Outdated Show resolved Hide resolved
torchchat/usages/openai_api.py Outdated Show resolved Hide resolved
torchchat/usages/server.py Show resolved Hide resolved
torchchat/usages/server.py Outdated Show resolved Hide resolved
torchchat/generate.py Outdated Show resolved Hide resolved
@@ -358,7 +368,6 @@ def prefill(
sequential_prefill=True,
**sampling_kwargs,
) -> torch.Tensor:
# logging.debug(f"x: {x}, input_pos: {input_pos}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Heads up the logging changes might get merge conflicted with #1336

torchchat/generate.py Show resolved Hide resolved
torchchat/generate.py Outdated Show resolved Hide resolved
torchchat/generate.py Outdated Show resolved Hide resolved
Comment on lines 1485 to 1503
if self.pp_rank == self.last_pp_rank:
new_token, next_prob = self.sample(logits, need_probs=need_probs, **sampling_kwargs)
else:
new_token = torch.zeros(1, 1, device=self.device, dtype=torch.int64)

if self.pp_rank == self.last_pp_rank and self.pp_rank != self.first_pp_rank:
dist.send(
new_token,
dst=self.first_pp_rank_global_id,
group=self.pp_group,
)
elif self.pp_rank == self.first_pp_rank and self.pp_rank != self.last_pp_rank:
dist.recv(
new_token,
src=self.last_pp_rank_global_id,
group=self.pp_group,
)
#TODO: Why do we get 2d tensor here?
new_token=new_token[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto above as prefill

torchchat/generate.py Outdated Show resolved Hide resolved
Comment on lines +1575 to +1576
else:
run_generator(args)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shuold setup_env also be triggering the generator?

Maybe it's just a naming change (run_with_setup~?), but setup_env doesn't make me think that the fn is called

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, valid point. I've renamed it to run_in_dist_env to reflect that it runs the target as well as connecting it to distributed.

from torchchat.distributed.generate import DistributedGenerator
from torchchat.distributed.utils import (
Color as color,
setup_env,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: setup_env is pretty generic, let's alias it so it's obviously for a distributed context

Comment on lines 71 to 76
if builder_args.distributed:
world_size = builder_args.tp * builder_args.pp
mp_context = mp.get_context('spawn')
queue = mp_context.Queue()

if builder_args.distributed:
Copy link
Contributor

Choose a reason for hiding this comment

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

Identical conditional check

@Jack-Khuu Jack-Khuu merged commit cc0ffce into pytorch:main Dec 19, 2024
51 of 53 checks passed
@mikekgfb
Copy link
Contributor

mikekgfb commented Dec 20, 2024

This PR integrates distributed inference with the CLI options chat and server and implements Option 2b of #1376.

To test run on machine with 4 gpus:

python  torchchat.py chat llama3.1  --max-new-tokens 10  --distributed --tp 2 --pp 2
python  torchchat.py server  llama3.1  --max-new-tokens 10  --distributed --tp 2 --pp 2

Should torchchat generate work as well, code would suggest that we can do like so (not sure about max new tokens?):

python  torchchat.py generate llama3.1 --distributed --tp 2 --pp 2

@mreso
Copy link
Contributor Author

mreso commented Dec 20, 2024

@mikekgfb Yes, distributed generate was already working before this PR, thats why I did not list it separately.

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