mct-nightly 2.1.0.20240617.451__py3-none-any.whl → 2.1.0.20240618.432__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. {mct_nightly-2.1.0.20240617.451.dist-info → mct_nightly-2.1.0.20240618.432.dist-info}/METADATA +2 -2
  2. {mct_nightly-2.1.0.20240617.451.dist-info → mct_nightly-2.1.0.20240618.432.dist-info}/RECORD +38 -12
  3. model_compression_toolkit/__init__.py +1 -1
  4. model_compression_toolkit/gptq/keras/gptq_training.py +1 -1
  5. model_compression_toolkit/gptq/keras/graph_info.py +1 -1
  6. model_compression_toolkit/gptq/pytorch/gptq_training.py +5 -2
  7. model_compression_toolkit/gptq/pytorch/graph_info.py +2 -1
  8. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_pytorch.py +3 -2
  9. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tpc_pytorch.py +3 -2
  10. model_compression_toolkit/xquant/__init__.py +19 -0
  11. model_compression_toolkit/xquant/common/__init__.py +15 -0
  12. model_compression_toolkit/xquant/common/constants.py +38 -0
  13. model_compression_toolkit/xquant/common/core_report_generator.py +83 -0
  14. model_compression_toolkit/xquant/common/dataset_utils.py +43 -0
  15. model_compression_toolkit/xquant/common/framework_report_utils.py +89 -0
  16. model_compression_toolkit/xquant/common/model_analyzer.py +99 -0
  17. model_compression_toolkit/xquant/common/model_folding_utils.py +104 -0
  18. model_compression_toolkit/xquant/common/similarity_calculator.py +194 -0
  19. model_compression_toolkit/xquant/common/similarity_functions.py +81 -0
  20. model_compression_toolkit/xquant/common/tensorboard_utils.py +101 -0
  21. model_compression_toolkit/xquant/common/xquant_config.py +39 -0
  22. model_compression_toolkit/xquant/keras/__init__.py +15 -0
  23. model_compression_toolkit/xquant/keras/dataset_utils.py +57 -0
  24. model_compression_toolkit/xquant/keras/facade_xquant_report.py +63 -0
  25. model_compression_toolkit/xquant/keras/keras_report_utils.py +60 -0
  26. model_compression_toolkit/xquant/keras/model_analyzer.py +136 -0
  27. model_compression_toolkit/xquant/keras/similarity_functions.py +75 -0
  28. model_compression_toolkit/xquant/keras/tensorboard_utils.py +84 -0
  29. model_compression_toolkit/xquant/pytorch/__init__.py +15 -0
  30. model_compression_toolkit/xquant/pytorch/dataset_utils.py +76 -0
  31. model_compression_toolkit/xquant/pytorch/facade_xquant_report.py +62 -0
  32. model_compression_toolkit/xquant/pytorch/model_analyzer.py +132 -0
  33. model_compression_toolkit/xquant/pytorch/pytorch_report_utils.py +61 -0
  34. model_compression_toolkit/xquant/pytorch/similarity_functions.py +68 -0
  35. model_compression_toolkit/xquant/pytorch/tensorboard_utils.py +87 -0
  36. {mct_nightly-2.1.0.20240617.451.dist-info → mct_nightly-2.1.0.20240618.432.dist-info}/LICENSE.md +0 -0
  37. {mct_nightly-2.1.0.20240617.451.dist-info → mct_nightly-2.1.0.20240618.432.dist-info}/WHEEL +0 -0
  38. {mct_nightly-2.1.0.20240617.451.dist-info → mct_nightly-2.1.0.20240618.432.dist-info}/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mct-nightly
3
- Version: 2.1.0.20240617.451
3
+ Version: 2.1.0.20240618.432
4
4
  Summary: A Model Compression Toolkit for neural networks
5
5
  Home-page: UNKNOWN
6
6
  License: UNKNOWN
@@ -14,7 +14,7 @@ Description-Content-Type: text/markdown
14
14
  Requires-Dist: networkx !=2.8.1
15
15
  Requires-Dist: tqdm
16
16
  Requires-Dist: Pillow
17
- Requires-Dist: numpy
17
+ Requires-Dist: numpy <2.0
18
18
  Requires-Dist: opencv-python
19
19
  Requires-Dist: scikit-image
20
20
  Requires-Dist: scikit-learn
@@ -1,4 +1,4 @@
1
- model_compression_toolkit/__init__.py,sha256=1OlgfgkWMboHXhaDyyG9E0dc_vedCsy6r_gAtSq-lfY,1573
1
+ model_compression_toolkit/__init__.py,sha256=VC4_Q3irB2XPx2tSa3QkUZSTQtm_TP2S5WwZu1g2liM,1573
2
2
  model_compression_toolkit/constants.py,sha256=9pVleMwnhlM4QwIL2HcEq42I1uF4rlSw63RUjkxOF4w,3923
3
3
  model_compression_toolkit/defaultdict.py,sha256=LSc-sbZYXENMCw3U9F4GiXuv67IKpdn0Qm7Fr11jy-4,2277
4
4
  model_compression_toolkit/logger.py,sha256=3DByV41XHRR3kLTJNbpaMmikL8icd9e1N-nkQAY9oDk,4567
@@ -342,8 +342,8 @@ model_compression_toolkit/gptq/common/gptq_training.py,sha256=efnwgKSGk9wtnirlLR
342
342
  model_compression_toolkit/gptq/keras/__init__.py,sha256=cco4TmeIDIh32nj9ZZXVkws4dd9F2UDrmjKzTN8G0V0,697
343
343
  model_compression_toolkit/gptq/keras/gptq_keras_implementation.py,sha256=axBwnCSjq5xk-xGymOwSOqjp39It-CVtGcCTRTf0E_4,1248
344
344
  model_compression_toolkit/gptq/keras/gptq_loss.py,sha256=rbRkF15MYd6nq4G49kcjb_dPTa-XNq9cTkrb93mXawo,6241
345
- model_compression_toolkit/gptq/keras/gptq_training.py,sha256=zyVcEQzdnNsrIz32U1pqqoi08hzxRdJ2CumaPFGwbDM,19123
346
- model_compression_toolkit/gptq/keras/graph_info.py,sha256=5IvgGlJlgOmQYmldjdCBv7tuzAoY0HazatG5Pedrg0Q,4639
345
+ model_compression_toolkit/gptq/keras/gptq_training.py,sha256=RAUZvve-kUMTfXY-aXQWEM4IejaeVedrKejBNrO6szI,19156
346
+ model_compression_toolkit/gptq/keras/graph_info.py,sha256=MKIfrRTRH3zCuxCR1g9ZVIFyuSSr0e0sDybqh4LDM7E,4672
347
347
  model_compression_toolkit/gptq/keras/quantization_facade.py,sha256=SjmBTuSwki4JTPVhxvJMFK9uAsmEm2c6VV11NnM6eEo,15117
348
348
  model_compression_toolkit/gptq/keras/quantizer/__init__.py,sha256=-DK1CDXvlsnEbki4lukZLpl6Xrbo91_jcqxXlG5Eg6Q,963
349
349
  model_compression_toolkit/gptq/keras/quantizer/base_keras_gptq_quantizer.py,sha256=2YU-x4-Q5f6hkUJf0tw6vcwdNwRMHdefrFjhhyHYsvA,4782
@@ -359,8 +359,8 @@ model_compression_toolkit/gptq/keras/quantizer/ste_rounding/symmetric_ste.py,sha
359
359
  model_compression_toolkit/gptq/pytorch/__init__.py,sha256=cco4TmeIDIh32nj9ZZXVkws4dd9F2UDrmjKzTN8G0V0,697
360
360
  model_compression_toolkit/gptq/pytorch/gptq_loss.py,sha256=kDuWw-6zh17wZpYWh4Xa94rpoodf82DksgjQCnL7nBc,2719
361
361
  model_compression_toolkit/gptq/pytorch/gptq_pytorch_implementation.py,sha256=tECPTavxn8EEwgLaP2zvxdJH6Vg9jC0YOIMJ7857Sdc,1268
362
- model_compression_toolkit/gptq/pytorch/gptq_training.py,sha256=xkDa62AdIRwv8dEshffALW9Ri66eseEpyUF9taMUKns,16509
363
- model_compression_toolkit/gptq/pytorch/graph_info.py,sha256=yXJzDd24zfGs2_vfMovxD1WSh1RxXoPxN4GztOf3P5c,3967
362
+ model_compression_toolkit/gptq/pytorch/gptq_training.py,sha256=2pe_caivE7Fr9zCvmZENKbFTS6AUFbSjHN-TODEhbSY,16631
363
+ model_compression_toolkit/gptq/pytorch/graph_info.py,sha256=4mVM-VvnBaA64ACVdOe6wTGHdMSa2UTLIUe7nACLcdo,4008
364
364
  model_compression_toolkit/gptq/pytorch/quantization_facade.py,sha256=PqVF1T0unY7V6jB1qUnwBQntLN5lEob83_3NkJE0hG0,13558
365
365
  model_compression_toolkit/gptq/pytorch/quantizer/__init__.py,sha256=ZHNHo1yzye44m9_ht4UUZfTpK01RiVR3Tr74-vtnOGI,968
366
366
  model_compression_toolkit/gptq/pytorch/quantizer/base_pytorch_gptq_quantizer.py,sha256=TCA1hAc7raPnrjl06sjFtVM4XUtLtuwAhCGX4U3KGZo,4137
@@ -456,11 +456,11 @@ model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_
456
456
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/__init__.py,sha256=gAeebYCKyIXH9-Qwze7FwvTihudzAHk_Qsg94fQbkjQ,717
457
457
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tp_model.py,sha256=edMH4lM7Bq7FaPAFZLU5UMX-bWSWiaaAIXnQE7lZ7rI,11844
458
458
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_keras.py,sha256=T5YMv-RzgYlzBaagnMO7WnKgbZ7PrOvm29Nn4vUhCHI,6587
459
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_pytorch.py,sha256=-q6Tnn7diPCCoATmLDzJwWwviQcbMMISqgpLu2n42JY,5726
459
+ model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_pytorch.py,sha256=HRo0W5l4IJesr_np4ZhXoMk_xfdiV53LgamquQIryJA,5800
460
460
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/__init__.py,sha256=C2kwyDE1-rtukkbNSoKRv9q8Nt2GOCaBbl0BdOr3goA,721
461
461
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tp_model.py,sha256=HoGjDwoSx2Y4dQua5v1qzzlnSl_HfDMK6bGWuZhPOzQ,11577
462
462
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tpc_keras.py,sha256=LvqUkvpJKXBb9QETcHsmp9OGDwl9KWr457deag8GVuM,6595
463
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tpc_pytorch.py,sha256=4Y2D14rE0SnWIkBTYsVqCryB-gkHU1ZlbdkWF864mPU,5733
463
+ model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tpc_pytorch.py,sha256=nP05jqvh6uaj30a3W7zEkJfKtqfP0Nz5bobwRqbYrdM,5807
464
464
  model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/__init__.py,sha256=cco4TmeIDIh32nj9ZZXVkws4dd9F2UDrmjKzTN8G0V0,697
465
465
  model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/target_platform_capabilities.py,sha256=7KVcuz0LfngRKOsfcvBysxGVb9fqgoAO6MVTl1CmB5c,2082
466
466
  model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/latest/__init__.py,sha256=UUvUCcTots_sehdRnDfgkaE8WPQ7dPbeuhDF4Qy2nzw,1510
@@ -491,8 +491,34 @@ model_compression_toolkit/trainable_infrastructure/keras/quantize_wrapper.py,sha
491
491
  model_compression_toolkit/trainable_infrastructure/keras/quantizer_utils.py,sha256=MVwXNymmFRB2NXIBx4e2mdJ1RfoHxRPYRgjb1MQP5kY,1797
492
492
  model_compression_toolkit/trainable_infrastructure/pytorch/__init__.py,sha256=huHoBUcKNB6BnY6YaUCcFvdyBtBI172ZoUD8ZYeNc6o,696
493
493
  model_compression_toolkit/trainable_infrastructure/pytorch/base_pytorch_quantizer.py,sha256=MxylaVFPgN7zBiRBy6WV610EA4scLgRJFbMucKvvNDU,2896
494
- mct_nightly-2.1.0.20240617.451.dist-info/LICENSE.md,sha256=aYSSIb-5AFPeITTvXm1UAoe0uYBiMmSS8flvXaaFUks,10174
495
- mct_nightly-2.1.0.20240617.451.dist-info/METADATA,sha256=-ZUI2y7SZOGKyLl6qpBE9onj-lZwfGE0wLLJI5WeqIE,19721
496
- mct_nightly-2.1.0.20240617.451.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
497
- mct_nightly-2.1.0.20240617.451.dist-info/top_level.txt,sha256=gsYA8juk0Z-ZmQRKULkb3JLGdOdz8jW_cMRjisn9ga4,26
498
- mct_nightly-2.1.0.20240617.451.dist-info/RECORD,,
494
+ model_compression_toolkit/xquant/__init__.py,sha256=vdmr8sQw3jIBLF9ck7qrskPoXzDKtksHWlMOkU1JUnQ,1003
495
+ model_compression_toolkit/xquant/common/__init__.py,sha256=ycb1Xt7PtixY2Uabr94JGSwBMcct66O8ZMVf3Qa3ud8,719
496
+ model_compression_toolkit/xquant/common/constants.py,sha256=LRh7q0GtyLTSwOc-XL5yNcPKVq68RvKnORYEC4KK-Ss,1513
497
+ model_compression_toolkit/xquant/common/core_report_generator.py,sha256=LQ9QUST9xyvm4B5sp68rjVPnpnxyosn_9jDBcyRciLs,4951
498
+ model_compression_toolkit/xquant/common/dataset_utils.py,sha256=91uXF9UwxdY7BvUT0FNkFm8a69c8oK8Xdl-y7lbuJxk,1649
499
+ model_compression_toolkit/xquant/common/framework_report_utils.py,sha256=3hzTg5xqdcxHnxmxO8B06o5sW8R-NH1Ixa75U0kie-o,3891
500
+ model_compression_toolkit/xquant/common/model_analyzer.py,sha256=T_8OetIQNqR0nkfSatWsEceXSPYpHfYjboBPIyR03-w,3953
501
+ model_compression_toolkit/xquant/common/model_folding_utils.py,sha256=y5Vmc-hJ2rJhzWdM53HdY-PrT5LlspejTUNlXaCrq9Q,4720
502
+ model_compression_toolkit/xquant/common/similarity_calculator.py,sha256=yCs_vlOThLzq7z-u2PkcEErLj7N7qCBPpRa6_5h34J8,10460
503
+ model_compression_toolkit/xquant/common/similarity_functions.py,sha256=Atah1otdX9oUUch2JK-p-e291QHtkP_c4DfLG9WWo1Y,2935
504
+ model_compression_toolkit/xquant/common/tensorboard_utils.py,sha256=YWvTvp7DyZDhybLnjte1Em90lev-NAa-hYp445BX-Y4,4473
505
+ model_compression_toolkit/xquant/common/xquant_config.py,sha256=Qt56cra2tU1PeHlLx_Cqztf5q-ED8MPelhb8coSumFw,1675
506
+ model_compression_toolkit/xquant/keras/__init__.py,sha256=zbtceCVRsi-Gvl_pOmq5laqVqu55vAU1ie2FR2RK1Po,709
507
+ model_compression_toolkit/xquant/keras/dataset_utils.py,sha256=quvVymhvpcPIOneCu5J6K_QAqBHOCIj8IxZxSN2fItA,2258
508
+ model_compression_toolkit/xquant/keras/facade_xquant_report.py,sha256=ZBwu1PwBgMbhQK-GvVCmn8CE6a1joKxZPluNNt9RqSw,3375
509
+ model_compression_toolkit/xquant/keras/keras_report_utils.py,sha256=Yk-VpyNYi5NWKTVYz-alfLK0JvM9CZDwGXBLu6HNJtI,2987
510
+ model_compression_toolkit/xquant/keras/model_analyzer.py,sha256=WXi9BPI9_TzRWn50lM1i-6cwPPRW0p43Shg_xpHFclU,6521
511
+ model_compression_toolkit/xquant/keras/similarity_functions.py,sha256=P2qMJAo94Sz_BCao-bnhEeewKtjeLLDDH2r9luDXJ04,2710
512
+ model_compression_toolkit/xquant/keras/tensorboard_utils.py,sha256=I1JMlSYe8eoYBpvHmc7H08iC9jdwgAWT4O5c7SMFOfc,4230
513
+ model_compression_toolkit/xquant/pytorch/__init__.py,sha256=ycb1Xt7PtixY2Uabr94JGSwBMcct66O8ZMVf3Qa3ud8,719
514
+ model_compression_toolkit/xquant/pytorch/dataset_utils.py,sha256=KFKiFkhIPpEr1ZH5jekZFrgs20VzzKVxSV9YMgH68yI,2894
515
+ model_compression_toolkit/xquant/pytorch/facade_xquant_report.py,sha256=g5uHlFW9vECkTsrgUs8iohbCCQ4_9tPUcoUv1QZH9uI,3146
516
+ model_compression_toolkit/xquant/pytorch/model_analyzer.py,sha256=b93o800yVB3Z-ihJBLy5Cic-MQiUM_ZGV6SCXoNdscE,5549
517
+ model_compression_toolkit/xquant/pytorch/pytorch_report_utils.py,sha256=yrZNVRm2IRU7r7R-hjS2lOQ6wvEEvbeunvf2jKoWjXk,3277
518
+ model_compression_toolkit/xquant/pytorch/similarity_functions.py,sha256=CERxq5K8rqaiE-DlwhZBTUd9x69dtYJlkHOPLB54vm8,2354
519
+ model_compression_toolkit/xquant/pytorch/tensorboard_utils.py,sha256=eyMoXt5o5EnMr6d-rpCwQdX5mAiYiymvbgKv4tf7-a0,4576
520
+ mct_nightly-2.1.0.20240618.432.dist-info/LICENSE.md,sha256=aYSSIb-5AFPeITTvXm1UAoe0uYBiMmSS8flvXaaFUks,10174
521
+ mct_nightly-2.1.0.20240618.432.dist-info/METADATA,sha256=oQDb0iDkegJzq1J15CZ59NnILw3BnPrgFuRFki8h95Y,19726
522
+ mct_nightly-2.1.0.20240618.432.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
523
+ mct_nightly-2.1.0.20240618.432.dist-info/top_level.txt,sha256=gsYA8juk0Z-ZmQRKULkb3JLGdOdz8jW_cMRjisn9ga4,26
524
+ mct_nightly-2.1.0.20240618.432.dist-info/RECORD,,
@@ -27,4 +27,4 @@ from model_compression_toolkit import data_generation
27
27
  from model_compression_toolkit import pruning
28
28
  from model_compression_toolkit.trainable_infrastructure.keras.load_model import keras_load_quantized_model
29
29
 
30
- __version__ = "2.1.0.20240617.000451"
30
+ __version__ = "2.1.0.20240618.000432"
@@ -353,7 +353,7 @@ class KerasGPTQTrainer(GPTQTrainer):
353
353
  node.final_activation_quantization_cfg.set_quant_config_attr(config_attr, config_value)
354
354
  if self.gptq_config.train_bias:
355
355
  use_bias = layer.layer.get_config().get(USE_BIAS)
356
- if use_bias is not None and use_bias:
356
+ if use_bias is not None and use_bias and layer.layer.bias is not None:
357
357
  new_bias = layer.layer.bias.numpy()
358
358
  node.set_weights_by_keys(BIAS, new_bias)
359
359
 
@@ -63,7 +63,7 @@ def get_gptq_trainable_parameters(fxp_model: Model,
63
63
  kernel_ops_attrs = fw_info.kernel_ops_attributes_mapping.get(type(layer.layer))
64
64
  use_bias = kernel_ops_attrs is not None and kernel_ops_attrs[0] is not None \
65
65
  and layer.layer.get_config().get(USE_BIAS)
66
- if use_bias is not None and use_bias:
66
+ if use_bias is not None and use_bias and layer.layer.bias is not None:
67
67
  bias_weights.append([layer.layer.bias])
68
68
 
69
69
  return trainable_weights, bias_weights, trainable_threshold
@@ -299,7 +299,9 @@ class PytorchGPTQTrainer(GPTQTrainer):
299
299
  for config_attr, config_value in activation_quant_config.items():
300
300
  node.final_activation_quantization_cfg.set_quant_config_attr(config_attr, config_value)
301
301
  if self.gptq_config.train_bias and hasattr(layer.layer, BIAS):
302
- node.set_weights_by_keys(BIAS, self.fw_impl.to_numpy(getattr(layer.layer, BIAS)))
302
+ bias = getattr(layer.layer, BIAS)
303
+ if bias is not None:
304
+ node.set_weights_by_keys(BIAS, self.fw_impl.to_numpy(bias))
303
305
 
304
306
  return graph_quant
305
307
 
@@ -316,4 +318,5 @@ class PytorchGPTQTrainer(GPTQTrainer):
316
318
  if isinstance(layer, PytorchQuantizationWrapper):
317
319
  if hasattr(layer.layer, BIAS):
318
320
  bias = getattr(layer.layer, BIAS)
319
- bias.requires_grad = self.gptq_config.train_bias
321
+ if bias is not None:
322
+ bias.requires_grad = self.gptq_config.train_bias
@@ -56,7 +56,8 @@ def get_gptq_trainable_parameters(fxp_model: nn.Module,
56
56
 
57
57
  if add_bias and hasattr(layer.layer, BIAS):
58
58
  bias = getattr(layer.layer, BIAS)
59
- trainable_bias.append(bias)
59
+ if bias is not None:
60
+ trainable_bias.append(bias)
60
61
 
61
62
  return trainable_aux_weights, trainable_bias, trainable_threshold
62
63
 
@@ -18,7 +18,7 @@ import operator
18
18
  import torch
19
19
  from torch import add, sub, mul, div, flatten, reshape, split, unsqueeze, dropout, sigmoid, tanh, chunk, unbind, topk, \
20
20
  gather, equal, transpose, permute, argmax, squeeze
21
- from torch.nn import Conv2d, Linear, ConvTranspose2d
21
+ from torch.nn import Conv2d, Linear, ConvTranspose2d, MaxPool2d
22
22
  from torch.nn import Dropout, Flatten, Hardtanh, Identity
23
23
  from torch.nn import ReLU, ReLU6, PReLU, SiLU, Sigmoid, Tanh, Hardswish, LeakyReLU
24
24
  from torch.nn.functional import relu, relu6, prelu, silu, hardtanh, hardswish, leaky_relu
@@ -83,7 +83,8 @@ def generate_pytorch_tpc(name: str, tp_model: tp.TargetPlatformModel):
83
83
  argmax,
84
84
  gather,
85
85
  topk,
86
- squeeze])
86
+ squeeze,
87
+ MaxPool2d])
87
88
 
88
89
  tp.OperationsSetToLayers("Conv", [Conv2d, ConvTranspose2d],
89
90
  attr_mapping=pytorch_linear_attr_mapping)
@@ -18,7 +18,7 @@ import operator
18
18
  import torch
19
19
  from torch import add, sub, mul, div, flatten, reshape, split, unsqueeze, dropout, sigmoid, tanh, chunk, unbind, topk, \
20
20
  gather, equal, transpose, permute, argmax, squeeze
21
- from torch.nn import Conv2d, Linear, ConvTranspose2d
21
+ from torch.nn import Conv2d, Linear, ConvTranspose2d, MaxPool2d
22
22
  from torch.nn import Dropout, Flatten, Hardtanh, Identity
23
23
  from torch.nn import ReLU, ReLU6, PReLU, SiLU, Sigmoid, Tanh, Hardswish, LeakyReLU
24
24
  from torch.nn.functional import relu, relu6, prelu, silu, hardtanh, hardswish, leaky_relu
@@ -82,7 +82,8 @@ def generate_pytorch_tpc(name: str, tp_model: tp.TargetPlatformModel):
82
82
  argmax,
83
83
  gather,
84
84
  topk,
85
- squeeze])
85
+ squeeze,
86
+ MaxPool2d])
86
87
 
87
88
  tp.OperationsSetToLayers("Conv", [Conv2d, ConvTranspose2d],
88
89
  attr_mapping=pytorch_linear_attr_mapping)
@@ -0,0 +1,19 @@
1
+ # Copyright 2024 Sony Semiconductor Israel, Inc. 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
+ # ==============================================================================
15
+
16
+ from model_compression_toolkit.xquant.common.xquant_config import XQuantConfig
17
+ from model_compression_toolkit.xquant.keras.facade_xquant_report import xquant_report_keras_experimental
18
+ from model_compression_toolkit.xquant.pytorch.facade_xquant_report import xquant_report_pytorch_experimental
19
+
@@ -0,0 +1,15 @@
1
+ # Copyright 2024 Sony Semiconductor Israel, Inc. 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
+ # ==============================================================================
15
+ #
@@ -0,0 +1,38 @@
1
+ # Copyright 2024 Sony Semiconductor Israel, Inc. 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
+ # ==============================================================================
15
+
16
+ # Default similarity metric names:
17
+ CS_SIMILARITY_METRIC_NAME = 'cs'
18
+ SQNR_SIMILARITY_METRIC_NAME = 'sqnr'
19
+ MSE_SIMILARITY_METRIC_NAME = 'mse'
20
+
21
+ # Report components names:
22
+ OUTPUT_SIMILARITY_METRICS_REPR = 'output_similarity_metrics_repr'
23
+ OUTPUT_SIMILARITY_METRICS_VAL = 'output_similarity_metrics_val'
24
+ INTERMEDIATE_SIMILARITY_METRICS_REPR = 'intermediate_similarity_metrics_repr'
25
+ INTERMEDIATE_SIMILARITY_METRICS_VAL = 'intermediate_similarity_metrics_val'
26
+
27
+ # Graph attribute names:
28
+ XQUANT_REPR = 'xquant_repr'
29
+ XQUANT_VAL = 'xquant_val'
30
+
31
+ # Report file name:
32
+ REPORT_FILENAME = 'quant_report.json'
33
+
34
+ # Tag to use in tensorboard for the graph we plot:
35
+ TENSORBOARD_DEFAULT_TAG = 'xquant'
36
+
37
+ # When extracting the activations of a model we hold the output using a dedicated key:
38
+ MODEL_OUTPUT_KEY = 'model_output_key'
@@ -0,0 +1,83 @@
1
+ # Copyright 2024 Sony Semiconductor Israel, Inc. 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
+ # ==============================================================================
15
+ from tqdm import tqdm
16
+ from typing import Callable, Any, Dict
17
+
18
+ from model_compression_toolkit.core.common.model_collector import ModelCollector
19
+ from model_compression_toolkit.xquant import XQuantConfig
20
+ from model_compression_toolkit.xquant.common.constants import OUTPUT_SIMILARITY_METRICS_REPR, OUTPUT_SIMILARITY_METRICS_VAL, INTERMEDIATE_SIMILARITY_METRICS_REPR, \
21
+ INTERMEDIATE_SIMILARITY_METRICS_VAL
22
+ from model_compression_toolkit.xquant.common.framework_report_utils import FrameworkReportUtils
23
+
24
+
25
+ def core_report_generator(float_model: Any,
26
+ quantized_model: Any,
27
+ repr_dataset: Callable,
28
+ validation_dataset: Callable,
29
+ fw_report_utils: FrameworkReportUtils,
30
+ xquant_config: XQuantConfig) -> Dict[str, Any]:
31
+ """
32
+ Generate report in tensorboard with a graph of the quantized model and similarity metrics that
33
+ have been measured when comparing to the float model (or any other two models).
34
+ The report also contains histograms that are collected on the baseline model (usually, the float
35
+ model).
36
+
37
+ Args:
38
+ float_model (Any): The original floating-point model.
39
+ quantized_model (Any): The model after quantization.
40
+ repr_dataset (Callable): Representative dataset used for similarity metrics computation.
41
+ validation_dataset (Callable): Validation dataset used for similarity metrics computation.
42
+ fw_report_utils (FrameworkReportUtils): Utilities for generating framework-specific reports.
43
+ xquant_config (XQuantConfig): Configuration settings for explainable quantization.
44
+
45
+ Returns:
46
+ Dict[str, Any]: A dictionary containing the collected similarity metrics and report data.
47
+ """
48
+ # Collect histograms on the float model.
49
+ float_graph = fw_report_utils.model_folding_utils.create_float_folded_graph(float_model, repr_dataset)
50
+ mi = ModelCollector(float_graph, fw_report_utils.fw_impl, fw_report_utils.fw_info)
51
+ for _data in tqdm(repr_dataset(), desc="Collecting Histograms"):
52
+ mi.infer(_data)
53
+
54
+ # Collect histograms and add them to Tensorboard.
55
+ fw_report_utils.tb_utils.add_histograms_to_tensorboard(graph=float_graph)
56
+
57
+ # Compute similarity metrics on representative dataset and validation set.
58
+ repr_similarity = fw_report_utils.similarity_calculator.compute_similarity_metrics(float_model=float_model,
59
+ quantized_model=quantized_model,
60
+ dataset=repr_dataset,
61
+ custom_similarity_metrics=xquant_config.custom_similarity_metrics)
62
+ val_similarity = fw_report_utils.similarity_calculator.compute_similarity_metrics(float_model=float_model,
63
+ quantized_model=quantized_model,
64
+ dataset=validation_dataset,
65
+ custom_similarity_metrics=xquant_config.custom_similarity_metrics,
66
+ is_validation=True)
67
+ similarity_metrics = {
68
+ OUTPUT_SIMILARITY_METRICS_REPR: repr_similarity[0],
69
+ OUTPUT_SIMILARITY_METRICS_VAL: val_similarity[0],
70
+ INTERMEDIATE_SIMILARITY_METRICS_REPR: repr_similarity[1],
71
+ INTERMEDIATE_SIMILARITY_METRICS_VAL: val_similarity[1]
72
+ }
73
+
74
+ # Add a graph of the quantized model with the similarity metrics to TensorBoard for visualization.
75
+ fw_report_utils.tb_utils.add_graph_to_tensorboard(quantized_model,
76
+ similarity_metrics,
77
+ repr_dataset)
78
+
79
+ # Save data to a json file.
80
+ fw_report_utils.dump_report_to_json(report_dir=xquant_config.report_dir,
81
+ collected_data=similarity_metrics)
82
+
83
+ return similarity_metrics
@@ -0,0 +1,43 @@
1
+ # Copyright 2024 Sony Semiconductor Israel, Inc. 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
+ # ==============================================================================
15
+
16
+ from typing import Callable
17
+
18
+ from model_compression_toolkit.logger import Logger
19
+
20
+
21
+ class DatasetUtils:
22
+ """
23
+ Class with helpful methods for handling different kinds of datasets from the user.
24
+ """
25
+
26
+ @staticmethod
27
+ def prepare_dataset(dataset: Callable, is_validation: bool, device: str = None):
28
+ """
29
+ Prepare the dataset so calling it will return only inputs for the model (like in the case
30
+ of the representative dataset). For example, when the validation dataset is used, the labels
31
+ should be removed.
32
+
33
+ Args:
34
+ dataset: Dataset to prepare.
35
+ is_validation: Whether it's validation dataset or not.
36
+ device: Device to transfer the data to.
37
+
38
+ Returns:
39
+ Generator to use for retrieving the dataset inputs.
40
+ """
41
+
42
+ Logger.critical("This method should be implemented by the framework-specific DatasetUtils.") # pragma: no cover
43
+
@@ -0,0 +1,89 @@
1
+ # Copyright 2024 Sony Semiconductor Israel, Inc. 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
+ # ==============================================================================
15
+
16
+ import json
17
+ import os
18
+
19
+ from model_compression_toolkit.core.common.framework_implementation import FrameworkImplementation
20
+ from model_compression_toolkit.core.common.framework_info import FrameworkInfo
21
+ from typing import Any, Dict
22
+
23
+ from model_compression_toolkit.xquant.common.constants import REPORT_FILENAME
24
+ from model_compression_toolkit.xquant.common.dataset_utils import DatasetUtils
25
+ from model_compression_toolkit.xquant.common.model_folding_utils import ModelFoldingUtils
26
+ from model_compression_toolkit.xquant.common.similarity_calculator import SimilarityCalculator
27
+ from model_compression_toolkit.xquant.common.tensorboard_utils import TensorboardUtils
28
+ from model_compression_toolkit.logger import Logger
29
+
30
+
31
+ class FrameworkReportUtils:
32
+ """
33
+ Class with various utility components required for generating the report in a specific framework.
34
+ """
35
+
36
+ def __init__(self,
37
+ fw_info: FrameworkInfo,
38
+ fw_impl: FrameworkImplementation,
39
+ similarity_calculator: SimilarityCalculator,
40
+ dataset_utils: DatasetUtils,
41
+ model_folding_utils: ModelFoldingUtils,
42
+ tb_utils: TensorboardUtils):
43
+ """
44
+ Initializes the FrameworkReportUtils class with various utility components required for generating the report.
45
+
46
+ Args:
47
+ fw_info (FrameworkInfo): Information about the framework being used.
48
+ fw_impl (FrameworkImplementation): The implemented functions of the framework.
49
+ similarity_calculator (SimilarityCalculator): A utility for calculating similarity metrics.
50
+ dataset_utils (DatasetUtils): Utilities for handling datasets.
51
+ model_folding_utils (ModelFoldingUtils): Utilities for model folding operations.
52
+ tb_utils (TensorboardUtils): Utilities for TensorBoard operations.
53
+ """
54
+ self.fw_info = fw_info
55
+ self.fw_impl = fw_impl
56
+ self.similarity_calculator = similarity_calculator
57
+ self.dataset_utils = dataset_utils
58
+ self.model_folding_utils = model_folding_utils
59
+ self.tb_utils = tb_utils
60
+
61
+ def create_report_directory(self, dir_path: str):
62
+ """
63
+ Create a directory for saving reports.
64
+
65
+ Args:
66
+ dir_path (str): The path to the directory to create.
67
+
68
+ """
69
+ if not os.path.exists(dir_path):
70
+ os.makedirs(dir_path, exist_ok=True)
71
+ Logger.info(f"Directory created at: {dir_path}")
72
+
73
+ def dump_report_to_json(self,
74
+ report_dir: str,
75
+ collected_data: Dict[str, Any]):
76
+ """
77
+ Dump the collected data (similarity, etc.) into a JSON file.
78
+
79
+ Args:
80
+ report_dir (str): Directory where the report will be saved.
81
+ collected_data (Dict[str, Any]): Data collected during report generation.
82
+
83
+ """
84
+ report_file_name = os.path.join(report_dir, REPORT_FILENAME)
85
+ report_file_name = os.path.abspath(report_file_name)
86
+ Logger.info(f"Dumping report data to: {report_file_name}")
87
+
88
+ with open(report_file_name, 'w') as f:
89
+ json.dump(collected_data, f, indent=4)
@@ -0,0 +1,99 @@
1
+ # Copyright 2024 Sony Semiconductor Israel, Inc. 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
+ # ==============================================================================
15
+ from typing import Any, Dict, List, Tuple
16
+
17
+ from abc import ABC, abstractmethod
18
+
19
+ from model_compression_toolkit.logger import Logger
20
+
21
+
22
+ class ModelAnalyzer(ABC):
23
+ """
24
+ This class provides abstract methods for analyzing a model, specifically for
25
+ extracting activations and comparing float and quantized models.
26
+ """
27
+
28
+ @abstractmethod
29
+ def extract_model_activations(self,
30
+ float_model: Any,
31
+ quantized_model: Any,
32
+ float_name2quant_name: Dict[str, str],
33
+ data: List[Any]) -> Tuple[Dict[str, Any], Dict[str, Any]]:
34
+ """
35
+ Extracts activations from both the float and quantized models.
36
+
37
+ Args:
38
+ float_model: The float model.
39
+ quantized_model: The quantized model.
40
+ float_name2quant_name: A mapping from float model layer names to quantized model layer
41
+ names.
42
+ data: Input data for which to compute activations.
43
+
44
+ Returns:
45
+ - Dictionary of activations for the float model.
46
+ - Dictionary of activations for the quantized model.
47
+ """
48
+ Logger.critical("This method should be implemented by the framework-specific ModelAnalyzer.") # pragma: no cover
49
+
50
+
51
+ @abstractmethod
52
+ def identify_quantized_compare_points(self, quantized_model: Any) -> List[str]:
53
+ """
54
+ Identifies the layers in the quantized model that are wrapped with the quantization wrapper.
55
+ These layers will serve as comparison points.
56
+
57
+ Notes:
58
+ This currently means that the quantized compare points are the linear layers that are wrapped,
59
+ but this may be changed in the future.
60
+
61
+ Args:
62
+ quantized_model: The quantized model from which to identify comparison points.
63
+
64
+ Returns:
65
+ List[str]: Names of the layers wrapped with the quantization wrapper.
66
+ """
67
+ Logger.critical("This method should be implemented by the framework-specific ModelAnalyzer.") # pragma: no cover
68
+
69
+
70
+ @abstractmethod
71
+ def find_corresponding_float_layer(self,
72
+ quant_compare_point: List[str],
73
+ quantized_model: Any) -> str:
74
+ """
75
+ Finds the corresponding float model layer for a given quantized model layer.
76
+
77
+ Args:
78
+ quant_compare_point: The name of the quantized model layer.
79
+ quantized_model: The quantized model.
80
+
81
+ Returns:
82
+ str: The name of the corresponding layer in the float model.
83
+ """
84
+ Logger.critical("This method should be implemented by the framework-specific ModelAnalyzer.") # pragma: no cover
85
+
86
+ @abstractmethod
87
+ def extract_float_layer_names(self, float_model: Any) -> List[str]:
88
+ """
89
+ Extracts the names of all layers in the float model.
90
+
91
+ Args:
92
+ float_model: The float model from which to extract layer names.
93
+
94
+ Returns:
95
+ List[str]: Names of all layers in the float model.
96
+ """
97
+ Logger.critical("This method should be implemented by the framework-specific ModelAnalyzer.") # pragma: no cover
98
+
99
+