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

add xpu device check before moving tensor directly to xpu device #2928

Merged
merged 3 commits into from
Jul 15, 2024

Conversation

faaany
Copy link
Contributor

@faaany faaany commented Jul 11, 2024

What does this PR do?

The following code snippet will throw an error on XPU

import transformers
import torch

model_id = "meta-llama/Llama-2-7b-chat-hf"

pipeline = transformers.pipeline(
    "text-generation",
    model=model_id,
    model_kwargs={"torch_dtype": torch.bfloat16},
    device_map="xpu"
)
Traceback (most recent call last):
  File "/workspace1/fanli/workspace/test.py", line 6, in <module>
    pipeline = transformers.pipeline(
  File "/home/fanli/miniforge3/envs/ds-ut-ww26/lib/python3.10/site-packages/transformers/pipelines/__init__.py", line 895, in pipeline
    framework, model = infer_framework_load_model(
  File "/home/fanli/miniforge3/envs/ds-ut-ww26/lib/python3.10/site-packages/transformers/pipelines/base.py", line 283, in infer_framework_load_model
    model = model_class.from_pretrained(model, **kwargs)
  File "/home/fanli/miniforge3/envs/ds-ut-ww26/lib/python3.10/site-packages/transformers/models/auto/auto_factory.py", line 564, in from_pretrained
    return model_class.from_pretrained(
  File "/home/fanli/miniforge3/envs/ds-ut-ww26/lib/python3.10/site-packages/transformers/modeling_utils.py", line 3838, in from_pretrained
    ) = cls._load_pretrained_model(
  File "/home/fanli/miniforge3/envs/ds-ut-ww26/lib/python3.10/site-packages/transformers/modeling_utils.py", line 4298, in _load_pretrained_model
    new_error_msgs, offload_index, state_dict_index = _load_state_dict_into_meta_model(
  File "/home/fanli/miniforge3/envs/ds-ut-ww26/lib/python3.10/site-packages/transformers/modeling_utils.py", line 895, in _load_state_dict_into_meta_model
    set_module_tensor_to_device(model, param_name, param_device, **set_module_kwargs)
  File "/workspace1/fanli/workspace/accelerate/src/accelerate/utils/modeling.py", line 410, in set_module_tensor_to_device
    new_value = value.to(device)
NotImplementedError: Could not run 'aten::empty_strided' with arguments from the 'XPU' backend. This could be because the operator doesn't exist for this backend, or was omitted during the selective/custom build process (if using custom build). If you are a Facebook employee using PyTorch on mobile, please visit https://fburl.com/ptmfixes for possible resolutions. 'aten::empty_strided' is only available for these backends: [CPU, Meta, QuantizedCPU, BackendSelect, Python, FuncTorchDynamicLayerBackMode, Functionalize, Named, Conjugate, Negative, ZeroTensor, ADInplaceOrView, AutogradOther, AutogradCPU, AutogradCUDA, AutogradHIP, AutogradXLA, AutogradMPS, AutogradIPU, AutogradXPU, AutogradHPU, AutogradVE, AutogradLazy, AutogradMTIA, AutogradPrivateUse1, AutogradPrivateUse2, AutogradPrivateUse3, AutogradMeta, AutogradNestedTensor, Tracer, AutocastCPU, AutocastCUDA, FuncTorchBatched, BatchedNestedTensor, FuncTorchVmapMode, Batched, VmapMode, FuncTorchGradWrapper, PythonTLSSnapshot, FuncTorchDynamicLayerFrontMode, PreDispatch, PythonDispatcher].

similar to the check here in transformers.pipeline.

@faaany
Copy link
Contributor Author

faaany commented Jul 11, 2024

@yao-matrix

@faaany faaany changed the title add xpu device check before moving tensor directly to XPU device add xpu device check before moving tensor directly to xpu device Jul 11, 2024
@faaany faaany marked this pull request as ready for review July 12, 2024 01:54
@yao-matrix
Copy link

OK for me.

@faaany
Copy link
Contributor Author

faaany commented Jul 12, 2024

@muellerzr and @SunMarc pls have a review, thx a lot!

Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

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

Thanks for fixing @faaany ! Left a comment !

src/accelerate/utils/modeling.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@muellerzr muellerzr left a comment

Choose a reason for hiding this comment

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

Okay to me as well!

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

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

Nice !

@muellerzr muellerzr merged commit 24bf5ec into huggingface:main Jul 15, 2024
25 checks passed
@faaany faaany deleted the import-ipex branch November 4, 2024 06:09
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.

5 participants