-
Notifications
You must be signed in to change notification settings - Fork 564
intel gpu : enable intel gpu #79
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
Open
xiaowangintel
wants to merge
1
commit into
meta-pytorch:main
Choose a base branch
from
xiaowangintel:main
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
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
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 |
|---|---|---|
|
|
@@ -18,6 +18,12 @@ | |
| from model import Attention, FeedForward, Transformer | ||
| from quantize import WeightOnlyInt4Linear | ||
|
|
||
| Int4Device = "cpu" | ||
|
|
||
| def global_device(device: str = "cpu"): | ||
| global Int4Device | ||
| Int4Device = device | ||
|
|
||
|
|
||
| def _get_rank() -> int: | ||
| return int(os.environ.get("LOCAL_RANK", "0")) | ||
|
|
@@ -33,7 +39,7 @@ def local_break(): | |
| def _get_world_size() -> int: | ||
| return int(os.environ.get("LOCAL_WORLD_SIZE", "1")) | ||
|
|
||
| def maybe_init_dist() -> Optional[int]: | ||
| def maybe_init_dist(device) -> Optional[int]: | ||
| try: | ||
| # provided by torchrun | ||
| rank = _get_rank() | ||
|
|
@@ -46,8 +52,21 @@ def maybe_init_dist() -> Optional[int]: | |
| # not run via torchrun, no-op | ||
| return None | ||
|
|
||
| torch.cuda.set_device(rank) | ||
| dist.init_process_group(backend="nccl", rank=rank, world_size=world_size) | ||
| if "cuda" in device: | ||
| torch.cuda.set_device(rank) | ||
| dist.init_process_group(backend="nccl", rank=rank, world_size=world_size) | ||
| elif "xpu" in device: | ||
| try: | ||
| import oneccl_bindings_for_pytorch | ||
| except: | ||
| raise ModuleNotFoundError(f"OneCCL bindings for PyTorch (oneccl_bindings_for_pytorch) is required to run tensor parallel on Intel GPU (XPU). Please check https://github.com/intel/torch-ccl for details.") | ||
|
|
||
| os.environ['CCL_PROCESS_LAUNCHER'] = 'none' | ||
| os.environ['CCL_LOCAL_SIZE'] = str(world_size) | ||
| os.environ['CCL_LOCAL_RANK'] = str(rank) | ||
|
|
||
| torch.xpu.set_device(rank) | ||
| dist.init_process_group(backend="ccl", rank=rank, world_size=world_size) | ||
|
Comment on lines
+64
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move these lines inside "try" region. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I do that. |
||
| return rank | ||
|
|
||
|
|
||
|
|
@@ -83,14 +102,32 @@ def shard_qkv(qkv, dim, weight_splits): | |
| assert len(weight_splits) == 3 | ||
|
|
||
| if isinstance(linear, WeightOnlyInt4Linear): | ||
| sharded_weight = shard_qkv(linear.weight, shard_dim, [i//8 for i in weight_splits]) | ||
| if ("xpu" in Int4Device): | ||
| in_features = linear.in_features | ||
| out_features = linear.out_features//8 | ||
| sharded_weight_size = list(linear.weight.size()) | ||
| sharded_weight_size[shard_dim] = -1 | ||
| weight = linear.weight.reshape((in_features, out_features)) | ||
| sharded_weight = shard_qkv(weight, 1 - shard_dim, [i//8 for i in weight_splits]) | ||
| sharded_weight = sharded_weight.reshape(sharded_weight_size) | ||
| else: | ||
| sharded_weight = shard_qkv(linear.weight, shard_dim, [i//8 for i in weight_splits]) | ||
| linear.scales_and_zeros = shard_qkv(linear.scales_and_zeros, 1 - shard_dim, weight_splits) | ||
| else: | ||
| sharded_weight = shard_qkv(linear.weight, shard_dim, weight_splits) | ||
| if hasattr(linear, "scales") and style == "colwise": | ||
| linear.scales = shard_qkv(linear.scales, 0, weight_splits) | ||
| else: | ||
| sharded_weight = shard(linear.weight, shard_dim) | ||
| if isinstance(linear, WeightOnlyInt4Linear) and ("xpu" in Int4Device): | ||
| in_features = linear.in_features | ||
| out_features = linear.out_features//8 | ||
| sharded_weight_size = list(linear.weight.size()) | ||
| sharded_weight_size[shard_dim] = -1 | ||
| weight = linear.weight.reshape((in_features, out_features)) | ||
| sharded_weight = shard(weight, 1 - shard_dim) | ||
| sharded_weight = sharded_weight.reshape(sharded_weight_size) | ||
| else: | ||
| sharded_weight = shard(linear.weight, shard_dim) | ||
| if isinstance(linear, WeightOnlyInt4Linear): | ||
| linear.scales_and_zeros = shard(linear.scales_and_zeros, 1 - shard_dim) | ||
| if style == "rowwise": | ||
|
|
||
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.
Intel GPU currently uses a PyTorch fork based on 2.1 which doesn't have
fx_graph_cacheyet.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've added a comment.