-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[FEAT] Model loading refactor #10604
base: main
Are you sure you want to change the base?
Conversation
FLAX CPU failing test is unrelated, failing in other PRs too |
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.
Thanks for starting this! Left some comments from a first pass.
I think we will need to also add tests for seeing if device_map
works as expected for quantization. Okay to not test that a bit later once there is consensus about the design changes. Maybe we could add that as a TODO.
Other tests could include checking if we can do low_cpu_mem_usage=True
along with some changed config values. This will ensure we're well tested for cases like #9343.
Additionally, I ran some tests on FailuresFAILED tests/models/unets/test_models_unet_2d_condition.py::UNet2DConditionModelTests::test_load_sharded_checkpoint_from_hub_0_hf_internal_testing_unet2d_sharded_dummy - RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1! (when checking argumen...
FAILED tests/models/unets/test_models_unet_2d_condition.py::UNet2DConditionModelTests::test_load_sharded_checkpoint_from_hub_1_hf_internal_testing_tiny_sd_unet_sharded_latest_format - RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1! (when checking argumen...
FAILED tests/models/unets/test_models_unet_2d_condition.py::UNet2DConditionModelTests::test_load_sharded_checkpoint_from_hub_local - RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1! (when checking argumen...
FAILED tests/models/unets/test_models_unet_2d_condition.py::UNet2DConditionModelTests::test_load_sharded_checkpoint_from_hub_local_subfolder - RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1! (when checking argumen...
FAILED tests/models/unets/test_models_unet_2d_condition.py::UNet2DConditionModelTests::test_load_sharded_checkpoint_from_hub_subfolder_0_hf_internal_testing_unet2d_sharded_dummy_subfolder - RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1! (when checking argumen...
FAILED tests/models/unets/test_models_unet_2d_condition.py::UNet2DConditionModelTests::test_load_sharded_checkpoint_from_hub_subfolder_1_hf_internal_testing_tiny_sd_unet_sharded_latest_format_subfolder - RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1! (when checking argumen...
FAILED tests/models/unets/test_models_unet_2d_condition.py::UNet2DConditionModelTests::test_sharded_checkpoints - RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1! (when checking argumen...
FAILED tests/models/unets/test_models_unet_2d_condition.py::UNet2DConditionModelTests::test_sharded_checkpoints_device_map - RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1! (when checking argumen...
FAILED tests/models/unets/test_models_unet_2d_condition.py::UNet2DConditionModelTests::test_sharded_checkpoints_with_variant - RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1! (when checking argument... ^^ passes when using with Same for following: FAILED tests/models/unets/test_models_unet_2d_condition.py::UNet2DConditionModelTests::test_sharded_checkpoints_device_map - RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1! And then I also ran: RUN_SLOW=1 pytest tests/pipelines/stable_diffusion/test_stable_diffusion.py::StableDiffusionPipelineDeviceMapTests Everything passes. |
|
||
for param_name, param in named_buffers: |
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.
We need to keep this or equivalent elsewhere, context: #10523
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.
The changes I did should also cover this use case. The test you added should pass with my PR. The is mainly due to adding the dispatch_model function at the end.
Co-authored-by: Sayak Paul <[email protected]>
…odel-loading-refactor
Co-authored-by: YiYi Xu <[email protected]>
…odel-loading-refactor
logger = logging.get_logger(__name__) | ||
|
||
_REGEX_SHARD = re.compile(r"(.*?)-\d{5}-of-\d{5}") | ||
|
||
TORCH_INIT_FUNCTIONS = { |
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.
Not a merge blocker, but is it possible to dynamically create this mapping? Then we could avoid having to make manual updates in case new inits are added to torch.
Although I suppose that doesn't happen too often.
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.
Something like this could work:
import torch.nn.init as init
init_functions = {
name: getattr(init, name) for name in dir(init) if callable(getattr(init, name))
and name.endswith("_")
and not name.startswith("_")
}
print("Available initialization functions:")
for name in init_functions:
print(name)
Prints:
Available initialization functions:
constant_
dirac_
eye_
kaiming_normal_
kaiming_uniform_
normal_
ones_
orthogonal_
sparse_
trunc_normal_
uniform_
xavier_normal_
xavier_uniform_
zeros_
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.
WDYT @DN6 ? I'm fine with either. Also there are some missing function since you only choose the one finishing with "_", though I don't think these are deprecated now.
"uniform": nn.init.uniform,
"normal": nn.init.normal,
"xavier_uniform": nn.init.xavier_uniform,
"xavier_normal": nn.init.xavier_normal,
"kaiming_uniform": nn.init.kaiming_uniform,
"kaiming_normal": nn.init.kaiming_normal,
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.
Some more comments.
I am running the 4bit quantization tests currently. And so far things are looking nice! Some tests that might be worth including/consdering:
- Device map with quantization
- Effectiveness of
keep_modules_in_fp32
when not using quantization.
WDYT?
Edit: 4bit and 8bit tests (bitsandbytes) are passing.
@@ -362,17 +362,18 @@ def from_single_file(cls, pretrained_model_link_or_path_or_dict: Optional[str] = | |||
|
|||
if is_accelerate_available(): | |||
param_device = torch.device(device) if device else torch.device("cpu") | |||
named_buffers = model.named_buffers() | |||
unexpected_keys = load_model_dict_into_meta( | |||
unexpected_keys = [ |
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.
Are the single-file related changes to uniformize the use of load_model_dict_into_meta()
(with the new signature)?
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.
yeah that's right !
logger = logging.get_logger(__name__) | ||
|
||
_REGEX_SHARD = re.compile(r"(.*?)-\d{5}-of-\d{5}") | ||
|
||
TORCH_INIT_FUNCTIONS = { |
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.
Something like this could work:
import torch.nn.init as init
init_functions = {
name: getattr(init, name) for name in dir(init) if callable(getattr(init, name))
and name.endswith("_")
and not name.startswith("_")
}
print("Available initialization functions:")
for name in init_functions:
print(name)
Prints:
Available initialization functions:
constant_
dirac_
eye_
kaiming_normal_
kaiming_uniform_
normal_
ones_
orthogonal_
sparse_
trunc_normal_
uniform_
xavier_normal_
xavier_uniform_
zeros_
|
||
|
||
@contextmanager | ||
def no_init_weights(): |
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.
Could you briefly then elaborate what happens in this codepath?
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. |
…odel-loading-refactor
Co-authored-by: Aryan <[email protected]>
Done ! Please check |
What does this PR do?
Fixes #10013 . This PR refactors model loading in diffusers. Here's a list of major changes in this PR.
low_cpu_mem_usage=True
andlow_cpu_mem_usage = False
). We don't rely onload_checkpoint_and_dispatch
anymore and we don't merge sharded checkpoint also.keep_module_in_fp32
support for sharded checkpointsFor
low_cpu_mem_usage = False
:assign_to_params_buffers
). I didn't benchmarked it but it should be as fast aslow_cpu_mem_usage=True
or maybe even faster. We did a similar PR in transformers thanks to @muellerzr.torch_dtype support
We don't initialize anymore the model in fp32 then cast the model to a specific dtype after finishing to load the weights.For
low_cpu_mem_usage = True
ordevice_map!=None
:load_checkpoint_and_dispatch
device_map
support for quantizationdispatch_model
( the test you added is passing cc @hlky )Single format file:
from_pretrained
. This way we have the same features as this function (device_map, quantization ...). Feel free to share your opinion @DN6, I didn't expect to touch this but I felt that we could simplify a bitTODO (some items can be done in follow-up PRs):
low_cpu_mem_usage=False
since we are initializing the whole model)Please let me know your thoughts on the PR !
cc @sayakpaul, @DN6 , @yiyixuxu , @hlky , @a-r-r-o-w