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

[Good First Issue][NNCF]: Remove backend-specific methods from the common layer attributes #3249

Open
daniil-lyakhov opened this issue Feb 4, 2025 · 4 comments
Assignees
Labels
good first issue Good for newcomers

Comments

@daniil-lyakhov
Copy link
Collaborator

daniil-lyakhov commented Feb 4, 2025

Context

Greetings, fellow scientists😺!

Recently we've discovered an old code that becomes confusing when used with the latest algorithms:

https://github.com/openvinotoolkit/nncf/blob/develop/nncf/common/graph/layer_attributes.py#L56-L79
https://github.com/openvinotoolkit/nncf/blob/develop/nncf/common/graph/layer_attributes.py#L135-L136

the problematic methods are get_weights_shape, get_target_dim_for_compression and get_bias_shape . Current algorithms use NNCFGraph to retrieve correct info, and not every backend follow the hardcoded weights shape layout / compression dim.

What needs to be done?

The task is to create the following functions in the https://github.com/openvinotoolkit/nncf/blob/develop/nncf/common/graph/utils.py file:

  • get_weights_shape_legacy(layer_attributes: WeightedLayerAttributes)
  • get_target_dim_for_compression_legacy(layer_attributes: WeightedLayerAttributes)
  • get_bias_shape_legacy(layer_attributes: WeightedLayerAttributes)

Additionally, all calls to the following methods should be replaced with their corresponding legacy function calls:

  • get_weights_shape -> get_weights_shape_legacy
  • get_target_dim_for_compression -> get_target_dim_for_compression_legacy
  • get_bias_shape -> get_bias_shape_legacy

Furthermore, the docstrings of these methods should clarify that they are valid only for PyTorch and TensorFlow backends.

Example Pull Requests

#3179

Resources

Contact points

@daniil-lyakhov

Ticket

No response

@shumaari
Copy link

shumaari commented Feb 4, 2025

.take

Copy link

github-actions bot commented Feb 4, 2025

Thank you for looking into this issue! Please let us know if you have any questions or require any help.

@github-project-automation github-project-automation bot moved this to Contributors Needed in Good first issues Feb 4, 2025
@ljaljushkin ljaljushkin moved this from Contributors Needed to Assigned in Good first issues Feb 4, 2025
@shumaari
Copy link

shumaari commented Feb 6, 2025

@daniil-lyakhov

I am still looking into the issue and would like to clarify my understanding:

Function calls for three methods need to be replaced by their legacy versions

layer_attributes.get_weight_shape -> layer_attributes.get_weight_shape_legacy
layer_attributes.get_target_dim_for_compression -> layer_attributes.get_target_dim_for_compression_legacy
layer_attributes.get_bias_shape -> layer_attributes.get_bias_shape_legacy

Possible locations for layer_attributes.get_weight_shape

https://github.com/openvinotoolkit/nncf/blob/develop/nncf/experimental/tensorflow/quantization/algorithm.py#L136
https://github.com/openvinotoolkit/nncf/blob/develop/nncf/experimental/torch/sparsity/movement/layers.py#L170
https://github.com/openvinotoolkit/nncf/blob/develop/nncf/experimental/torch/sparsity/movement/structured_mask_handler.py#L212
https://github.com/openvinotoolkit/nncf/blob/develop/nncf/torch/quantization/algo.py#L776
https://github.com/openvinotoolkit/nncf/blob/develop/nncf/torch/quantization/algo.py#L1185
https://github.com/openvinotoolkit/nncf/blob/develop/nncf/torch/quantization/init_range.py#L229
https://github.com/openvinotoolkit/nncf/blob/develop/nncf/torch/sparsity/const/algo.py#L29
https://github.com/openvinotoolkit/nncf/blob/develop/nncf/torch/sparsity/magnitude/algo.py#L47
https://github.com/openvinotoolkit/nncf/blob/develop/nncf/torch/sparsity/rb/algo.py#L47

Possible locations for layer_attributes.get_target_dim_for_compression

https://github.com/openvinotoolkit/nncf/blob/develop/nncf/experimental/common/pruning/nodes_grouping.py#L79
https://github.com/openvinotoolkit/nncf/blob/develop/nncf/torch/quantization/algo.py#L779
https://github.com/openvinotoolkit/nncf/blob/develop/nncf/torch/quantization/init_range.py#L230

Possible locations for layer_attributes.get_bias_shape

https://github.com/openvinotoolkit/nncf/blob/develop/nncf/experimental/torch/sparsity/movement/layers.py#L188
https://github.com/openvinotoolkit/nncf/blob/develop/nncf/experimental/torch/sparsity/movement/structured_mask_handler.py#L214

Create the three legacy methods with docstrings

I am still unclear about where these functions could be created? Within class definitions for WeightedLayerAttributes and LinearLayerAttributes classes in https://github.com/openvinotoolkit/nncf/blob/develop/nncf/common/graph/layer_attributes.py like below or somewhere else?

Image

I would also like to mention that get_weight_shape_legacy and get_target_dim_for_compression_legacy methods in WeightedLayerAttributes class are mere placeholders. The method which caused the reported error in #3230 (comment) must be defined elsewhere.

I intend to recreate the error reported above. What else can I do to investigate further?

@daniil-lyakhov
Copy link
Collaborator Author

Hi @shumaari,

List of the method usage is correct, thanks for your analysis. I'm apologies for not clear instructions, the task is to create 3 functions (not class methods) in the https://github.com/openvinotoolkit/nncf/blob/develop/nncf/common/graph/utils.py file. Then, the assignee have to replate method calls with the functions calls.

Please don't spend too much time on the error reproduction: it isn't a error indeed, it is a wrong usage of a backend-specific method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
Status: Assigned
Development

No branches or pull requests

2 participants