-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[NPU]:Replace 'cuda' in the project with abstract interfaces #1207
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
base: main
Are you sure you want to change the base?
Changes from all commits
19ce304
3b662da
f4d06ce
a3c2744
209a350
19e429d
5c0b07d
dce77ec
ad91d41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,10 +1,11 @@ | ||||||
| import torch | ||||||
| from typing import Optional, Union | ||||||
| from .qwen_image_text_encoder import QwenImageTextEncoder | ||||||
| from ..core.device.npu_compatible_device import get_device_type, get_torch_device | ||||||
|
|
||||||
|
|
||||||
| class Step1xEditEmbedder(torch.nn.Module): | ||||||
| def __init__(self, model: QwenImageTextEncoder, processor, max_length=640, dtype=torch.bfloat16, device="cuda"): | ||||||
| def __init__(self, model: QwenImageTextEncoder, processor, max_length=640, dtype=torch.bfloat16, device=get_device_type()): | ||||||
| super().__init__() | ||||||
| self.max_length = max_length | ||||||
| self.dtype = dtype | ||||||
|
|
@@ -77,13 +78,13 @@ def forward(self, caption, ref_images): | |||||
| self.max_length, | ||||||
| self.model.config.hidden_size, | ||||||
| dtype=torch.bfloat16, | ||||||
| device=torch.cuda.current_device(), | ||||||
| device=get_torch_device().current_device(), | ||||||
| ) | ||||||
| masks = torch.zeros( | ||||||
| len(text_list), | ||||||
| self.max_length, | ||||||
| dtype=torch.long, | ||||||
| device=torch.cuda.current_device(), | ||||||
| device=get_torch_device().current_device(), | ||||||
|
Contributor
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. Using
Suggested change
|
||||||
| ) | ||||||
|
|
||||||
| def split_string(s): | ||||||
|
|
@@ -158,7 +159,7 @@ def split_string(s): | |||||
| else: | ||||||
| token_list.append(token_each) | ||||||
|
|
||||||
| new_txt_ids = torch.cat(token_list, dim=1).to("cuda") | ||||||
| new_txt_ids = torch.cat(token_list, dim=1).to(get_device_type()) | ||||||
|
|
||||||
| new_txt_ids = new_txt_ids.to(old_inputs_ids.device) | ||||||
|
|
||||||
|
|
@@ -167,15 +168,15 @@ def split_string(s): | |||||
| inputs.input_ids = ( | ||||||
| torch.cat([old_inputs_ids[0, :idx1], new_txt_ids[0, idx2:]], dim=0) | ||||||
| .unsqueeze(0) | ||||||
| .to("cuda") | ||||||
| .to(get_device_type()) | ||||||
| ) | ||||||
| inputs.attention_mask = (inputs.input_ids > 0).long().to("cuda") | ||||||
| inputs.attention_mask = (inputs.input_ids > 0).long().to(get_device_type()) | ||||||
| outputs = self.model_forward( | ||||||
| self.model, | ||||||
| input_ids=inputs.input_ids, | ||||||
| attention_mask=inputs.attention_mask, | ||||||
| pixel_values=inputs.pixel_values.to("cuda"), | ||||||
| image_grid_thw=inputs.image_grid_thw.to("cuda"), | ||||||
| pixel_values=inputs.pixel_values.to(get_device_type()), | ||||||
| image_grid_thw=inputs.image_grid_thw.to(get_device_type()), | ||||||
| output_hidden_states=True, | ||||||
| ) | ||||||
|
|
||||||
|
|
@@ -188,7 +189,7 @@ def split_string(s): | |||||
| masks[idx, : min(self.max_length, emb.shape[1] - 217)] = torch.ones( | ||||||
| (min(self.max_length, emb.shape[1] - 217)), | ||||||
| dtype=torch.long, | ||||||
| device=torch.cuda.current_device(), | ||||||
| device=get_torch_device().current_device(), | ||||||
|
Contributor
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. Using
Suggested change
|
||||||
| ) | ||||||
|
|
||||||
| return embs, masks | ||||||
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.
Using
get_torch_device().current_device()will cause a crash on CPU-only systems. Theget_torch_devicefunction falls back totorch.cudawhen the device is 'cpu', andtorch.cuda.current_device()will then fail if no CUDA device is available. A simpler and more robust approach is to useget_device_type()directly, as it returns a device string ('cpu', 'cuda', 'npu') that is accepted by PyTorch tensor creation functions.