-
Notifications
You must be signed in to change notification settings - Fork 16
Fix distributed loading when using paddle #19
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,13 +1,13 @@ | ||
| import paddle | ||
| from fastsafetensors import SafeTensorsFileLoader, SingleGroup | ||
| device = "gpu:0" if paddle.is_compiled_with_cuda() else "cpu" | ||
| device = "gpu:0" if paddle.device.cuda.device_count() else "cpu" | ||
| loader = SafeTensorsFileLoader(SingleGroup(), device, nogds=False, debug_log=True, framework="paddle") | ||
| loader.add_filenames({0: ["a_paddle.safetensors", "b_paddle.safetensors"]}) # {rank: files} | ||
| fb = loader.copy_files_to_device() | ||
| tensor_a0 = fb.get_tensor(tensor_name="a0") | ||
| tensor_b0 = fb.get_tensor(tensor_name="b0") | ||
| print(f"a0: {tensor_a0}") | ||
| mycat = paddle.concat([tensor_a0, tensor_b0], axis=1) | ||
| print(f"a0: {tensor_a0}\n b0 : {tensor_b0}") | ||
| mycat = paddle.concat([tensor_a0, tensor_b0]) | ||
| print(f"cat: {mycat}, size={mycat.size}") | ||
| fb.close() | ||
| loader.close() |
This file contains hidden or 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,9 @@ | ||
| # !/usr/bin/env python3 | ||
| PIDS=() | ||
|
|
||
| runner="python -m paddle.distributed.launch" | ||
|
|
||
| cd paddle_case | ||
| rm -rf log | ||
| # It can only be used on the CPU version of paddlepaddle | ||
| ${runner} --nproc_per_node 2 run_parallel.py |
This file contains hidden or 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,11 @@ | ||
| # !/usr/bin/env python3 | ||
| PIDS=() | ||
|
|
||
| runner="python -m paddle.distributed.launch" | ||
|
|
||
| cd paddle_case | ||
| rm -rf log | ||
| # It can only be used on the GPU version of paddlepaddle-gpu | ||
| # A machine multy gpu (case : 1 machine 2 gpus) | ||
| # Different to torch script because the paddle distributed use nccl to communicate in gpus | ||
| CUDA_VISIBLE_DEVICES=0,1 ${runner} --gpus 0,1 run_parallel.py |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,11 @@ | ||
| import pytest | ||
| import sys | ||
| import os | ||
|
|
||
| if __name__ == "__main__": | ||
| # There are 4 commands before this test | ||
| # GPU ditributed need at least 2 GPU | ||
| rank = int(os.getenv("PADDLE_TRAINER_ID")) + 4 | ||
| os.environ["COVERAGE_FILE"] = f".coverage_{rank}" | ||
| pytest_args = sys.argv[1:] | ||
| sys.exit(pytest.main(pytest_args)) |
This file contains hidden or 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
This file contains hidden or 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,45 @@ | ||
| # Copyright 2024- IBM Inc. All rights reserved | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import pytest | ||
| import torch | ||
| import paddle | ||
| from safetensors import safe_open | ||
|
|
||
| from fastsafetensors import cpp as fstcpp | ||
| from fastsafetensors import SafeTensorsFileLoader, SingleGroup, SafeTensorsMetadata | ||
| from fastsafetensors.common import paddle_loaded | ||
|
|
||
| def test_shuffle_paddle(fstcpp_log, input_files, pg_paddle): | ||
| if paddle_loaded: | ||
| device = "gpu" if paddle.device.cuda.device_count() else "cpu" | ||
| loader = SafeTensorsFileLoader(pg_paddle, device, nogds=True, debug_log=True, framework="paddle") | ||
| loader.add_filenames({0: input_files}) | ||
| bufs = loader.copy_files_to_device() | ||
| key_dims = {key: -1 for key in loader.get_keys()} | ||
| for i in range(0, 12): | ||
| key_dims[f"h.{i}.mlp.c_proj.weight"] = 0 | ||
| key_dims[f"h.{i}.mlp.c_fc.weight"] = 1 | ||
| tensors = bufs.as_dict(key_dims) | ||
| with safe_open(input_files[0], framework="pt") as f: | ||
| for key in tensors.keys(): | ||
| dim = key_dims[key] | ||
| if dim == 0 or dim == 1: | ||
| t = f.get_slice(key) | ||
| rank_slices = () | ||
| shape = t.get_shape() | ||
| size = shape[dim] | ||
| block_size = (size + pg_paddle.process_group.size() - 1) // pg_paddle.process_group.size() | ||
| for i in range(0, len(shape)): | ||
| if i < dim: | ||
| rank_slices += (slice(None,None,None),) | ||
| elif i == dim: | ||
| rank_slices += (slice(pg_paddle.process_group.rank() * block_size, (pg_paddle.process_group.rank() + 1) * block_size, 1),) | ||
| break | ||
| t = t[rank_slices] | ||
| t = t.clone().detach() | ||
| else: | ||
| t = f.get_tensor(key) | ||
| assert paddle.all(paddle.to_tensor(t.numpy(),place=loader.device).equal(tensors[key])) | ||
| bufs.close() | ||
| loader.close() |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
In this part, maybe, It dose not need to consider distributed case in fastsafetensors.
We just need to load the tensors to correct device which provided by user.
In a machine with multi gpus, user should set the device like that
device="gpu:{pg.rank()}"in distributed code then senddeviceto theSafeTensorsFileLoaderso that different processes can load tensors to different gpus .What do you think?
Uh oh!
There was an error while loading. Please reload this page.
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.
I don't think so because safetensors files that are distributed online are not composed like that.