code: def fake_linear_quantize(
      val, axis=-1, mode="LINEAR", dtype=types.int8
  ):
      from coremltools.optimize.coreml._quantization_passes import (
          AffineQuantParams,
      )
      from coremltools.converters.mil.mil.types.type_mapping import (
          nptype_from_builtin,
      )
  
      val_dtype = val.dtype
  
      def _ensure_numerical_range_and_cast(val, low, high, np_dtype):
          """
          For some cases, the computed quantized data might exceed the data range.
          For instance, after rounding and addition, we might get `128` for the int8 quantization.
          This utility function ensures the val in the data range before doing the cast.
          """
          val = np.minimum(val, high)
          val = np.maximum(val, low)
          return val.astype(np_dtype)
  
      mode_dtype_to_range = {
          (types.int8, "LINEAR"): (-128, 127),
          (types.int8, "LINEAR_SYMMETRIC"): (-127, 127),
          (types.uint8, "LINEAR"): (0, 255),
          (types.uint8, "LINEAR_SYMMETRIC"): (0, 254),
      }
  
      if not isinstance(val, (np.ndarray, np.generic)):
          raise ValueError("Only numpy arrays are supported")
  
      params = AffineQuantParams()
      axes = tuple([i for i in range(len(val.shape)) if i != axis])
      val_min = np.amin(val, axis=axes, keepdims=True)
      val_max = np.amax(val, axis=axes, keepdims=True)
  
      if mode == "LINEAR_SYMMETRIC":
          # For the linear_symmetric mode, the range is symmetrical to 0
          max_abs = np.maximum(np.abs(val_min), np.abs(val_max))
          val_min = -max_abs
          val_max = max_abs
      else:
          assert mode == "LINEAR"
          # For the linear mode, we need to make sure the data range contains `0`
          val_min = np.minimum(0.0, val_min)
          val_max = np.maximum(0.0, val_max)
  
      q_val_min, q_val_max = mode_dtype_to_range[(dtype, mode)]
  
      # Set the zero point to symmetric mode
      np_dtype = nptype_from_builtin(dtype)
      if mode == "LINEAR_SYMMETRIC":
          if dtype == types.int8:
              params.zero_point = (0 * np.ones(val_min.shape)).astype(
                  np.int8
              )
          else:
              assert dtype == types.uint8
              params.zero_point = (127 * np.ones(val_min.shape)).astype(
                  np.uint8
              )
      else:
          assert mode == "LINEAR"
          params.zero_point = (
              q_val_min * val_max - q_val_max * val_min
          ) / (val_max - val_min)
          params.zero_point = np.round(params.zero_point)
          params.zero_point = _ensure_numerical_range_and_cast(
              params.zero_point, q_val_min, q_val_max, np_dtype
          )
  
      # compute the params
      params.scale = (val_max - val_min) / (q_val_max - q_val_min)
      params.scale = params.scale.astype(val.dtype).squeeze()
  
      params.quantized_data = np.round(
          val * (q_val_max - q_val_min) / (val_max - val_min)
      )
      params.quantized_data = params.quantized_data + params.zero_point
      params.quantized_data = _ensure_numerical_range_and_cast(
          params.quantized_data, q_val_min, q_val_max, np_dtype
      )
  
      params.zero_point = params.zero_point.squeeze()
      params.axis = axis
  
      return (
          params.quantized_data.astype(val_dtype)
          - params.zero_point.astype(val_dtype)
      ) * params.scale
  
imports: from collections import OrderedDict
  from copy import deepcopy
  from functools import partial
  import argparse
  import gc
  import json
  import logging
  import numpy
  import os
  from PIL import Image
  from python_coreml_stable_diffusion.torch2coreml import compute_psnr, get_pipeline
  import time
  import torch
  import torch.nn
  import requests
  from tqdm import tqdm, trange
  from coremltools.converters.mil.mil import types
label_desc: Use image node_id 1 for calling a global function with example usage: fake_linear_quantize(val, axis, mode, dtype) and returns: unknown, val