-
Notifications
You must be signed in to change notification settings - Fork 6.4k
[LoRA] fix: lora loading when using with a device_mapped model. #9449
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
Changes from 5 commits
dc1aee2
949a929
64b3ad1
6d03c12
d4bd94b
5479198
2846549
1ed0eb0
d2d59c3
5f3cae2
8f670e2
e42ec19
f63b04c
eefda54
ea727a3
71989e3
f62afac
2334f78
5ea1173
f64751e
c0dee87
4b6124a
fe2cca8
2db5d48
61903c8
03377b7
0bd40cb
a61b754
ccd8d2a
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 |
|---|---|---|
|
|
@@ -387,6 +387,11 @@ def to(self, *args, **kwargs): | |
|
|
||
| device = device or device_arg | ||
|
|
||
| def model_has_device_map(model): | ||
|
||
| if not is_accelerate_available() or is_accelerate_version("<", "0.14.0"): | ||
| return False | ||
| return getattr(model, "hf_device_map", None) is not None | ||
|
|
||
| # throw warning if pipeline is in "offloaded"-mode but user tries to manually set to GPU. | ||
| def module_is_sequentially_offloaded(module): | ||
| if not is_accelerate_available() or is_accelerate_version("<", "0.14.0"): | ||
|
|
@@ -404,6 +409,13 @@ def module_is_offloaded(module): | |
|
|
||
| return hasattr(module, "_hf_hook") and isinstance(module._hf_hook, accelerate.hooks.CpuOffload) | ||
|
|
||
| # device-mapped modules should not go through any device placements. | ||
| pipeline_has_device_mapped_modules = any(model_has_device_map(module) for _, module in self.components.items()) | ||
sayakpaul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if pipeline_has_device_mapped_modules: | ||
| raise ValueError( | ||
| "It seems like you have device-mapped modules in the pipeline which doesn't allow explicit device placement using `to()`." | ||
sayakpaul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| # .to("cuda") would raise an error if the pipeline is sequentially offloaded, so we raise our own to make it clearer | ||
| pipeline_is_sequentially_offloaded = any( | ||
| module_is_sequentially_offloaded(module) for _, module in self.components.items() | ||
|
|
@@ -976,6 +988,19 @@ def enable_model_cpu_offload(self, gpu_id: Optional[int] = None, device: Union[t | |
| The PyTorch device type of the accelerator that shall be used in inference. If not specified, it will | ||
| default to "cuda". | ||
| """ | ||
|
|
||
| def model_has_device_map(model): | ||
| if not is_accelerate_available() or is_accelerate_version("<", "0.14.0"): | ||
| return False | ||
| return getattr(model, "hf_device_map", None) is not None | ||
|
|
||
| # device-mapped modules should not go through any device placements. | ||
| pipeline_has_device_mapped_modules = any(model_has_device_map(module) for _, module in self.components.items()) | ||
| if pipeline_has_device_mapped_modules: | ||
| raise ValueError( | ||
| "It seems like you have device-mapped modules in the pipeline which doesn't allow explicit device placement using `to()`." | ||
sayakpaul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| is_pipeline_device_mapped = self.hf_device_map is not None and len(self.hf_device_map) > 1 | ||
| if is_pipeline_device_mapped: | ||
| raise ValueError( | ||
|
|
@@ -1069,6 +1094,19 @@ def enable_sequential_cpu_offload(self, gpu_id: Optional[int] = None, device: Un | |
| The PyTorch device type of the accelerator that shall be used in inference. If not specified, it will | ||
| default to "cuda". | ||
| """ | ||
|
|
||
| def model_has_device_map(model): | ||
| if not is_accelerate_available() or is_accelerate_version("<", "0.14.0"): | ||
| return False | ||
| return getattr(model, "hf_device_map", None) is not None | ||
|
|
||
| # device-mapped modules should not go through any device placements. | ||
| pipeline_has_device_mapped_modules = any(model_has_device_map(module) for _, module in self.components.items()) | ||
| if pipeline_has_device_mapped_modules: | ||
sayakpaul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| raise ValueError( | ||
| "It seems like you have device-mapped modules in the pipeline which doesn't allow explicit device placement using `to()`." | ||
| ) | ||
|
|
||
| if is_accelerate_available() and is_accelerate_version(">=", "0.14.0"): | ||
| from accelerate import cpu_offload | ||
| else: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.