def _deduplicate_const_across_functions(self, prog: Program) -> None:
        """
        When there are duplicated consts across functions, we cannot create a common const op to be shared.
        Instead, we set the weight_id to the consts, to allow them share the same blob file value when lowering into milproto.
        """
        # We first make sure that consts are deduplicated within each function,
        # to make sure we can maximize the weight sharing.
        self.apply(prog)

        # check no weight_id is set yet in the program
        for block in prog.functions.values():
            for op in block.operations:
                if op.op_type != "const":
                    continue
                if op.weight_id is not None:
                    raise ValueError(f"const op {op.name} already has weight_id {op.weight_id}")

        # deduplication across functions
        blocks = list(prog.functions.values())

        try:
            # Locally be very aggressive for identifying duplicated consts
            # to make sure they are shared across functions, as failure to do 
            # so may result in weight unsharing after pre-compilation.
            # This is typically true for ANE and 4bits + 6bits palettization lut values
            # which are below the 100 threshold, and end up unsharing the lut indices as well
            # due to operator fusion.
            old_threshold = self.const_threshold
            self.const_threshold = 1
            unique2duplicates_const = self.find_constants(blocks)
        finally:
            self.const_threshold = old_threshold

        for i, (k, v) in enumerate(unique2duplicates_const.items()):
            if len(v) == 0:
                continue
            # There could be cases where two functions are pointing to the same block
            all_vars = [k] + list(v)
            all_vars = list(set(all_vars))
            for duplicate in all_vars:
                duplicate.op.weight_id = str(i)

        When there are duplicated consts across functions, we cannot create a common const op to be shared.
        Instead, we set the weight_id to the consts, to allow them share the same blob file value when lowering into milproto.
        
coremltools/converters/mil/mil/passes/defs/cleanup/const_deduplication.py