class_docstr: Remove duplicated large constants (tensor with 100+ elements)
  
  For example
  
  .. code-block::
  
      Input graph (where weight and bias are large constants):
          weight_q = const(weight)
          weight_k = const(weight)
          bias_q = const(bias)
          bias_k = const(bias)
          q_embedding = linear(x=q, weight=weight_q, bias=bias_q)
          k_embedding = linear(x=k, weight=weight_k, bias=bias_k)
  
      Output graph:
          weight_q = const(weight)
          bias_q = const(bias)
          q_embedding = linear(x=q, weight=weight_q, bias=bias_q)
          k_embedding = linear(x=k, weight=weight_q, bias=bias_q)
  
  Concretely, this graph pass consists of two stages:
  
  (1) Deduplication of ``const`` op:
  
      We consider a ``const`` as duplicated if there exists such a previous ``const`` that has same dtype and value
  
  (2) Deduplication of ``constexpr_*`` op:
  
      We consider a ``constexpr_*`` as duplicated if there exists such a previous ``constexpr_*`` that has the same ``op_type`` and input attributes.
docstr: Given a block, return all constants in the block in such a format:
      {unique_var_0: [duplicated_var_0_0, duplicated_var_0_1, ...],
       unique_var_1: [duplicated_var_1_0, duplicated_var_1_1, ...],
       ...
      }
docstr_tok: ["Given","a","block",",","return","all","constants","in","the","block","in","such","a","format",":","{","unique_var_0",":","[","duplicated_var_0_0",",","duplicated_var_0_1",",","...","]",",","unique_var_1",":","[","duplicated_var_1_0",",","duplicated_var_1_1",",","...","]",",","...","}"]
code: def find_constants(self, block: Block) -> Dict[Var, List[Var]]:
      """
      Given a block, return all constants in the block in such a format:
          {unique_var_0: [duplicated_var_0_0, duplicated_var_0_1, ...],
           unique_var_1: [duplicated_var_1_0, duplicated_var_1_1, ...],
           ...
          }
      """
      unique2duplicates: Dict[Var, List[Var]] = {}
  
      # instead of brute-force C_N^2 comparison, use a hash map to be O(N)
      constant_dict: Dict[
          Tuple[str, types.type, Tuple[int], str], List[Var]
      ] = {}
      for op in list(block.operations):
          if op.op_type == "const":
              constant_var = op.outputs[0]
              if isinstance(constant_var, ListVar):
                  continue
              shape = constant_var.shape
  
              numel = np.prod(shape)
              if numel < self.NUMEL_THRESH:
                  continue
  
              dtype = constant_var.dtype
              value = constant_var.val
              hash = hashlib.sha1(
                  np.ascontiguousarray(
                      value.reshape(-1)[: self.NUMEL_THRESH]
                  )
              ).hexdigest()
              key = (dtype, shape, hash)
  
              if key not in constant_dict:
                  constant_dict[key] = [constant_var]
                  unique2duplicates[constant_var] = []
              else:
                  hash_collisions = constant_dict[key]
  
                  existing_constant_var = None
                  for var in hash_collisions:
                      if np.allclose(
                          value,
                          var.val,
                          rtol=0.0,
                          atol=self.DTYPE2ATOL.get(dtype, 1e-12),
                      ):
                          existing_constant_var = var
                          break
  
                  if existing_constant_var is None:
                      hash_collisions.append(constant_var)
                      unique2duplicates[constant_var] = []
                  else:
                      unique2duplicates[existing_constant_var].append(
                          constant_var
                      )
  
      return unique2duplicates
  
file_path: coremltools/coremltools/converters/mil/mil/passes/defs/cleanup/const_deduplication.py
imports: import hashlib
  from typing import Dict, List, Tuple
  import numpy
  from coremltools.converters.mil.mil import Block, Var, ListVar, types
  from coremltools.converters.mil.mil.passes.graph_pass import AbstractGraphPass
  from coremltools.converters.mil.mil.passes.helper import block_context_manager
  from coremltools.converters.mil.mil.passes.pass_registry import register_pass
label_desc: Use image node_id 5 for calling the const_deduplication obj's underlying member method code with example usage: obj.find_constants(block) and returns: unique2duplicates