nncf icon indicating copy to clipboard operation
nncf copied to clipboard

Post-training Activation Pruning algorithm

Open yujiepan-work opened this issue 9 months ago • 5 comments

Changes

Add torch backend implementation for Post-training Activation Pruning algorithm.

The current interface is

nncf.experimental.torch.sparsify_activations.sparsify_activations(
  torch_model, calibration_dataset, 
  target_sparsity_by_scope={"{re}.*linear.*": 0.5},
  ignored_scope=IgnoredScope(),
)

See model accuracy check at https://github.com/openvinotoolkit/nncf/pull/2683#issuecomment-2161049420

Tests

Added unit tests at tests/torch/experimental/sparsify_activations Added conformance tests at tests/post_training/experimental/sparsify_activations

yujiepan-work avatar May 16 '24 08:05 yujiepan-work

Thanks for the contribution!

Could you add to the description any results by accuracy and performance?

Thanks for your reply! We will add this by the beginning of ww24.

yujiepan-work avatar Jun 07 '24 07:06 yujiepan-work

Codecov Report

Attention: Patch coverage is 0% with 187 lines in your changes missing coverage. Please review.

Project coverage is 62.10%. Comparing base (3d11e8a) to head (3c9a7b7). Report is 49 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##           develop    #2683       +/-   ##
============================================
+ Coverage    47.13%   62.10%   +14.96%     
============================================
  Files          495      486        -9     
  Lines        45986    46630      +644     
============================================
+ Hits         21675    28959     +7284     
+ Misses       24311    17671     -6640     
Files Coverage Δ
...xperimental/torch/sparsify_activations/__init__.py 0.00% <0.00%> (ø)
.../sparsify_activations/sparsify_activations_impl.py 0.00% <0.00%> (ø)
...mental/torch/sparsify_activations/torch_backend.py 0.00% <0.00%> (ø)

... and 223 files with indirect coverage changes

Flag Coverage Δ
COMMON 41.93% <ø> (-1.64%) :arrow_down:
ONNX 34.04% <0.00%> (-0.71%) :arrow_down:
OPENVINO 40.80% <0.00%> (+0.87%) :arrow_up:
TENSORFLOW 29.27% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
common 87.56% <ø> (+17.89%) :arrow_up:
torch 32.82% <0.00%> (-0.37%) :arrow_down:
tensorflow 93.26% <ø> (+93.26%) :arrow_up:
onnx 93.06% <ø> (+<0.01%) :arrow_up:
openvino 94.51% <ø> (+0.36%) :arrow_up:
ptq 82.04% <ø> (+2.64%) :arrow_up:

codecov[bot] avatar Jun 07 '24 17:06 codecov[bot]

Model accuracy check:

Llama-2-7b-hf

model-level sparsity avg. zero-shot accuracy (8 tasks) wikitext perplexity
FP16 baseline - 71.15 8.79
FP16 sparse 25% (up/gate32%+down52%) 70.73 (-0.59%) 9.01
INT8 sparse 25% (up/gate32%+down52%) 70.88 (-0.38%) 9.01

Mixtral-8x7B-Instruct-v0.1

model-level sparsity avg. zero-shot accuracy (8 tasks) wikitext perplexity
FP16 baseline - 80.08 6.22
FP16 sparse 40% (up/gate42%+down52%) 79.70 (-0.48%) 6.56

Tasks: 'arc_easy', 'arc_challenge', 'boolq', "piqa", 'lambada_openai', 'winogrande', 'sciq', 'hellaswag'

yujiepan-work avatar Jun 11 '24 15:06 yujiepan-work

Remaining issues at this moment:

  • [x] README markdown at the algo folder
  • [x] Documentation: example of using the interface
  • [x] target_sparsity_by_scope interface design

yujiepan-work avatar Jun 25 '24 14:06 yujiepan-work

@daniil-lyakhov could you please review the PyTorch backend part of the implementation, specifically PTSparsifyActivationsAlgoBackend class?

nikita-savelyevv avatar Jul 01 '24 09:07 nikita-savelyevv

@daniil-lyakhov Hi Daniil, thanks for reviewing this PR. Are there any openings that we need to change?

yujiepan-work avatar Jul 10 '24 06:07 yujiepan-work

Conformance test build (id=12) has passed. The time it takes is about half an hour.

The only thing left is some documentation of the method.

nikita-savelyevv avatar Jul 10 '24 11:07 nikita-savelyevv

There is a failed test but seems not caused by this PR:

FAILED tests/common/graph/test_dot_file_rw.py::test_colons_are_replaced_in_written_dot_file - AssertionError: assert False
 +  where False = <function cmp at 0x7f52758f80d0>(PosixPath('/tmp/pytest-of-runner/pytest-0/popen-gw0/test_colons_are_replaced_in_wr0/graph.dot'), PosixPath('/home/runner/work/nncf/nncf/tests/common/data/reference_graphs/dot_rw_reference.dot'))
 +    where <function cmp at 0x7f52758f80d0> = filecmp.cmp

Update: the failed test can pass after retry

yujiepan-work avatar Jul 16 '24 08:07 yujiepan-work

Thank you all for the reviews! Since we have resolved all the issues, I wonder whether this PR is ready to be merged. If there are any further changes needed, we would be glad to deal with them. 🙂

yujiepan-work avatar Jul 16 '24 15:07 yujiepan-work

@alexsu52, please take a look. The PR should be ready for merging.

cc @AlexKoff88 @MaximProshin

nikita-savelyevv avatar Jul 17 '24 11:07 nikita-savelyevv