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

Accelerate Utilities #193

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open

Accelerate Utilities #193

wants to merge 28 commits into from

Conversation

kylesayrs
Copy link
Contributor

@kylesayrs kylesayrs commented Oct 21, 2024

Purpose

  • Implement offloading utility functions which greatly simplify/clarify offloading-related code in llm-compressor
  • Explicitly initialize quantization parameters as offloaded if the module is offloaded

Prerequisites

Changes

Changes not covered by prerequisites:

  • Implement getattr_chain utility function (also used by llm-compressor)
  • Implement depreciated utility decorator for future depreciations
  • Implement register_offload_parameter and delete_offload_parameter for easier initialization and removal of parameters related to quantization
  • Begin newly initialized quantization parameters on cpu if the module is offloaded offload
    • Faster performance, removes dependency on get_execution_device

Depreciation Strategy

These functions should be depreciated, each for their own reason. These strategies will be implemented in follow-up PRs

Function Depreciation Reason Depreciation Strategy
is_module_offloaded Use official has_offloaded_params redirect to has_offloaded_params & depreciation warning
get_execution_device Not useful as a general util Remove uses from LC & depreciation warning
get_offloaded_device Folded into update_offload_parameter Replace uses in LC with update_offload_parameter & depreciation warning
update_prefix_dict Folded into update_offload_parameter Replace uses in LC with update_offload_parameter & depreciation warning
update_parameter_data Use update_offload_data for better args ordering. Open to keeping this one around Remove uses from LC and CT & depreciation warning

Upstream Strategy

Upstreaming functions to accelerate is a low priority, but comes with the benefit of more reviews and more official support

Function Upstream Version
register_offload_parameter  N/A
update_offload_data  N/A
delete_offload_parameter  N/A
has_offloaded_params  1.1.0
align_module_device  1.1.0

@kylesayrs kylesayrs marked this pull request as ready for review November 19, 2024 02:46
@kylesayrs kylesayrs self-assigned this Nov 19, 2024
@kylesayrs kylesayrs changed the title [WIP] Accelerate Utilities Accelerate Utilities Nov 19, 2024
@kylesayrs kylesayrs requested a review from horheynm November 28, 2024 17:04
Copy link
Member

@rahul-tuli rahul-tuli left a comment

Choose a reason for hiding this comment

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

LGTM! with a few nits, good work on this!

src/compressed_tensors/utils/helpers.py Show resolved Hide resolved
src/compressed_tensors/utils/helpers.py Show resolved Hide resolved
src/compressed_tensors/utils/offload.py Show resolved Hide resolved
src/compressed_tensors/utils/offload.py Outdated Show resolved Hide resolved
src/compressed_tensors/utils/offload.py Show resolved Hide resolved
rahul-tuli
rahul-tuli previously approved these changes Dec 6, 2024
Signed-off-by: Kyle Sayers <kylesayrs@gmail.com>
@kylesayrs kylesayrs marked this pull request as ready for review December 6, 2024 06:40
@kylesayrs
Copy link
Contributor Author

Fixed a bug, added some tests

@kylesayrs kylesayrs marked this pull request as draft December 6, 2024 07:18
@kylesayrs kylesayrs marked this pull request as ready for review December 6, 2024 08:15
Copy link
Contributor

@dsikka dsikka left a comment

Choose a reason for hiding this comment

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

why do we need the per token fix to land as a prereq for this PR?

Copy link
Contributor

@dsikka dsikka left a comment

Choose a reason for hiding this comment

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

What would be the replacement for get_execution_device?

@kylesayrs
Copy link
Contributor Author

@dsikka The function update_parameter_data takes new_param_data as an input and uses this to update parameter data. Previously, this function would simply overwrite the old data with new_param_data. Now, in order to reduce complexity and increase performance, update_parameter_data requires that parameter being updated and the new parameter data are the same shape.

This assumption causes an error in mock_per_token_calibration, tests/test_quantization/test_configs/test_strategies.py, which revealed to me that the shape used to initialize the per_token strategy and the shape computed by calculate_qparams are different shapes. I consider this ambiguity to be a bug which was causing the test to fail.

Copy link
Contributor

@dsikka dsikka left a comment

Choose a reason for hiding this comment

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

This looks good overall.

Do you mind adding a simple lifecycle dosctring which shows the steps of offloaded modules/parameters to make it slightly easier to follow how the parameters are updated?

I also think we should kick-off W4A16/W8A8 oneshot workflows, similar to what we did here: https://app.asana.com/0/1207078450218847/1208568399648361/f to make sure it runs to completion. I think past issues we've seen have been with g_idx and activation quantization parameters.

@dsikka
Copy link
Contributor

dsikka commented Dec 9, 2024

What would be the replacement for get_execution_device?

I think I understand from your PR as to why this can be removed.

@kylesayrs
Copy link
Contributor Author

@dsikka w.r.t. get_execution_device

  1. The function isn't guaranteed to be performant for all device maps, for example half-offloaded models
  2. The function has very few uses, so it may be worth removing

For these reasons it's a candidate (and we'll need it for the immediate future), but future work can determine whether we want to keep/ update it

):
"""
Update the data of an existing parameter and its offload dict. Supports both
parameters of offloaded modules and non-offloaded modules
Copy link
Contributor

Choose a reason for hiding this comment

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

this supports non-offloaded modules? for what case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Supporting non-offloaded modules allows this function to be used throughout the codebase without having to duplicate code

Ugleh...

if not has_offloaded_params(module):
    param = getattr(module, name)
    data = data.to(param.dtype)
    if param.device != "meta":
        param.data.copy_(data)

else:
    update_offload_parameter(module, name, data)

Preetay!

update_offload_parameter(module, name, data)

module: torch.nn.Module,
name: str,
parameter: torch.nn.Parameter,
):
Copy link
Contributor

Choose a reason for hiding this comment

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

When registering the parameters during initialization, don't we know the device already, depending on if the module has been offloaded or not?

We can't pass that device to update_offload_parameter to be used when updating the weights_map?

Copy link
Contributor Author

@kylesayrs kylesayrs Dec 16, 2024

Choose a reason for hiding this comment

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

I've just now rewritten these parts to a bit clearer.

don't we know the device already, depending on if the module has been offloaded or not?

During initialization, the _initialize_scale_zero_point function determines the initial onload device

# begin on the same device as other parameters or cpu if offloaded.
# in the offloaded case, there's no point moving tensors to the execution device
# if they're going to be immediately offloaded by `register_offload_parameter`
params_device = next(module.parameters()).device
device = "cpu" if has_offloaded_params(module) else params_device

It's the job of register_offload_parameter (and by extension update_offload_parameter, offload_to_weights_map) to determine the offload device.

if isinstance(weights_map, dict):
  if key in weights_map:
      offload_device = weights_map[key].device
  else:
      tens = next(iter(weights_map.values()), None)
      offload_device = tens.device if tens is not None else default_device

src/compressed_tensors/utils/offload.py Show resolved Hide resolved
Signed-off-by: Kyle Sayers <kylesayrs@gmail.com>
Signed-off-by: Kyle Sayers <kylesayrs@gmail.com>
Signed-off-by: Kyle Sayers <kylesayrs@gmail.com>
Signed-off-by: Kyle Sayers <kylesayrs@gmail.com>
Signed-off-by: Kyle Sayers <kylesayrs@gmail.com>
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.

4 participants