unstructured_prune.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. from paddle.utils import try_import
  18. from ppdet.core.workspace import register, serializable
  19. from ppdet.utils.logger import setup_logger
  20. logger = setup_logger(__name__)
  21. @register
  22. @serializable
  23. class UnstructuredPruner(object):
  24. def __init__(self,
  25. stable_epochs,
  26. pruning_epochs,
  27. tunning_epochs,
  28. pruning_steps,
  29. ratio,
  30. initial_ratio,
  31. prune_params_type=None):
  32. self.stable_epochs = stable_epochs
  33. self.pruning_epochs = pruning_epochs
  34. self.tunning_epochs = tunning_epochs
  35. self.ratio = ratio
  36. self.prune_params_type = prune_params_type
  37. self.initial_ratio = initial_ratio
  38. self.pruning_steps = pruning_steps
  39. def __call__(self, model, steps_per_epoch, skip_params_func=None):
  40. paddleslim = try_import('paddleslim')
  41. from paddleslim import GMPUnstructuredPruner
  42. configs = {
  43. 'pruning_strategy': 'gmp',
  44. 'stable_iterations': self.stable_epochs * steps_per_epoch,
  45. 'pruning_iterations': self.pruning_epochs * steps_per_epoch,
  46. 'tunning_iterations': self.tunning_epochs * steps_per_epoch,
  47. 'resume_iteration': 0,
  48. 'pruning_steps': self.pruning_steps,
  49. 'initial_ratio': self.initial_ratio,
  50. }
  51. pruner = GMPUnstructuredPruner(
  52. model,
  53. ratio=self.ratio,
  54. skip_params_func=skip_params_func,
  55. prune_params_type=self.prune_params_type,
  56. local_sparsity=True,
  57. configs=configs)
  58. return pruner