def find_constants(blocks: List[Block]) -> Dict[Var, List[Var]]:
        """
        Given a list of blocks, return all constants in the blocks 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 block in blocks:
            for op in list(block.operations):
                if op.op_type != "const":
                    continue

                constant_var = op.outputs[0]
                if isinstance(constant_var, ListVar):
                    continue

                if not const_deduplication.should_be_deduplicated(constant_var.val):
                    continue

                shape = constant_var.shape
                dtype = constant_var.dtype
                value = constant_var.val

                hash = hashlib.sha1(
                    np.ascontiguousarray(value.reshape(-1)[: const_deduplication.LENGTH_OF_HASHKEY])
                ).hexdigest()
                if hasattr(op, "weight_key"):
                    key = (op.weight_key, dtype, shape, hash)
                else:
                    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=const_deduplication.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

        Given a list of blocks, return all constants in the blocks 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, ...],
             ...
            }
        
coremltools/converters/mil/mil/passes/defs/cleanup/const_deduplication.py