code: def activate_int8_int8_matrix_multiplications(spec, selector=None):
      """
      Utility function that takes in either a full precision (float) spec or
      an nbit quantized spec to selectively enable int8 activation + weight quantization
      of matrix multiplication operations where the second matrix represents a constant weight.
  
      spec: MLModel.get_spec()
          Currently conversion for only neural network models is supported.
          If a pipeline model is passed in then all embedded neural network models embedded within
          will be modified.
  
      selector: (optional) MatrixMultiplyLayerSelector
          A MatrixMultiplyLayerSelector object that enables int8 activation + weight quantization
          only on those layers for which the user-specified criterion on the minimum/maximum number
          of size/channels in constant weight parameters is met.
          It can also be derived to provide custom selection.
  
      """
  
      # Recursively convert all pipeline models
      if spec.WhichOneof("Type") == "pipeline":
          for model_spec in spec.pipeline.models:
              activate_int8_int8_matrix_multiplications(
                  model_spec, selector=selector
              )
          return spec
  
      elif spec.WhichOneof("Type") in [
          "pipelineClassifier",
          "pipelineRegressor",
      ]:
          activate_int8_int8_matrix_multiplications(
              spec.pipeline, selector=selector
          )
          return spec
  
      # Neural network models
      elif spec.WhichOneof("Type") in [
          "neuralNetwork",
          "neuralNetworkClassifier",
          "neuralNetworkRegressor",
      ]:
          if selector is None:
              selector = MatrixMultiplyLayerSelector()
  
          # Dequantize all the selected matrix multiplication layers
          spec = _quantize_spec_weights(
              spec,
              nbits=None,
              quantization_mode=_QUANTIZATION_MODE_DEQUANTIZE,
              selector=selector,
          )
  
          def _quantized_weight_and_scale(W):
              W_max = max(_np.abs(_np.min(W)), _np.abs(_np.max(W)))
              W_normalized = W / W_max  # [-1,1]
              W_quantized_int8 = 127.0 * W_normalized  # [-127, 127]
              W_quantized_int8 = W_quantized_int8.astype(_np.int8)
              quant_scale = W_max / 127.0
              return W_quantized_int8, quant_scale
  
          if spec.WhichOneof("Type") == "neuralNetwork":
              nn_spec = spec.neuralNetwork
  
          elif spec.WhichOneof("Type") in "neuralNetworkClassifier":
              nn_spec = spec.neuralNetworkClassifier
  
          elif spec.WhichOneof("Type") in "neuralNetworkRegressor":
              nn_spec = spec.neuralNetworkRegressor
  
          def _process_nn_layers(nn_spec):
              layers = nn_spec.layers
  
              # Replacing each matrix multiplication
              for layer in layers:
                  layer_type = layer.WhichOneof("layer")
                  if not selector.do_quantize(layer):
                      continue
  
                  if layer_type == "branch":
                      _process_nn_layers(layer.branch.ifBranch)
                      _process_nn_layers(layer.branch.elseBranch)
  
                  elif layer_type == "loop":
                      _process_nn_layers(layer.loop.conditionNetwork)
                      _process_nn_layers(layer.loop.bodyNetwork)
  
                  elif layer_type in ["innerProduct", "batchedMatmul"]:
                      # Bump up to appropriate spec version if at least one replacement occurs
                      spec.specificationVersion = max(
                          _SPECIFICATION_VERSION_IOS_14,
                          spec.specificationVersion,
                      )
  
                      # InnerProduct
                      if layer_type == "innerProduct":
                          matmul_layer = layer.innerProduct
  
                      # BatchedMatmul
                      elif layer_type == "batchedMatmul":
                          matmul_layer = layer.batchedMatmul
  
                      wp = matmul_layer.weights
  
                      if len(wp.floatValue) == 0:
                          continue
                      else:
                          qw, qs = _quantized_weight_and_scale(
                              wp.floatValue
                          )
  
                      print(
                          "Modifying layer {} with size of weights {}, to use Int8 * Int8 matrix multiplication".format(
                              layer.name, qw.size
                          )
                      )
  
                      matmul_layer.int8DynamicQuantize = True
                      wp.quantization.numberOfBits = 8
                      wp.quantization.linearQuantization.scale.extend(
                          map(float, [qs])
                      )
                      wp.int8RawValue = bytes()
                      wp.int8RawValue += qw.tobytes()
                      del wp.floatValue[:]
  
          _process_nn_layers(nn_spec)
  
          return spec
  
      else:
          raise ValueError(
              "Model Type {} not supported.".format(
                  spec.WhichOneof("Type")
              )
          )
  
imports: from __future__ import print_function
  from __future__ import division
  from __future__ import absolute_import
  import numpy
  from sys import stdout
  from os import listdir
  from six import string_types
  from .optimization_utils import _optimize_nn
  from coremltools.models import _SUPPORTED_QUANTIZATION_MODES, _QUANTIZATION_MODE_DEQUANTIZE, _QUANTIZATION_MODE_LOOKUP_TABLE_LINEAR, _QUANTIZATION_MODE_LOOKUP_TABLE_KMEANS, _QUANTIZATION_MODE_CUSTOM_LOOKUP_TABLE, _QUANTIZATION_MODE_LINEAR_QUANTIZATION, _QUANTIZATION_MODE_LINEAR_SYMMETRIC, _LUT_BASED_QUANTIZATION
  from ..utils import _get_nn_layers, _wp_to_fp16wp, _get_model, _macos_version
  from ..._deps import _HAS_SKLEARN
  from ...None import _MINIMUM_QUANTIZED_MODEL_SPEC_VERSION, _MINIMUM_FP16_SPEC_VERSION, _SPECIFICATION_VERSION_IOS_14
label_desc: Use image node_id 20 for calling a global function with example usage: activate_int8_int8_matrix_multiplications(spec, selector) and returns: spec, spec, spec, W_quantized_int8,  quant_scale