def _make_second_chunk_prog(prog: _mil.Program, op_idx: int) -> _mil.Program:
    """Build second chunk by rebuilding a pristine MIL Program from MLModel"""
    block = prog.functions["main"]
    block.opset_version = _ct.target.iOS16

    # First chunk outputs are second chunk inputs (e.g. skip connections)
    boundary_vars = _get_first_chunk_outputs(block, op_idx)

    # This op will not be included in this program. Its output var will be made into an input
    boundary_op = block.operations[op_idx]

    # Add all boundary ops as inputs
    with block:
        for var in boundary_vars:
            new_placeholder = _Placeholder(
                sym_shape=var.shape,
                dtype=var.dtype if var.dtype != _mil.types.fp16 else _mil.types.fp32,
                name=var.name,
            )

            block._input_dict[new_placeholder.outputs[0].name] = new_placeholder.outputs[0]

            block.function_inputs = tuple(block._input_dict.values())
            new_var = None
            if var.dtype == _mil.types.fp16:
                new_var = _mb.cast(x=new_placeholder.outputs[0], dtype="fp16", before_op=var.op)
            else:
                new_var = new_placeholder.outputs[0]

            block.replace_uses_of_var_after_op(
                anchor_op=boundary_op,
                old_var=var,
                new_var=new_var,
                # This is needed if the program contains "constexpr_*" ops. In normal cases, there are stricter
                # rules for removing them, and their presence may prevent replacing this var.
                # However in this case, since we want to remove all the ops in chunk 1, we can safely
                # set this to True.
                force_replace=True,
            )

    _PASS_REGISTRY["common::dead_code_elimination"](prog)

    # Remove any unused inputs
    new_input_dict = _OrderedDict()
    for k, v in block._input_dict.items():
        if len(v.child_ops) > 0:
            new_input_dict[k] = v
    block._input_dict = new_input_dict
    block.function_inputs = tuple(block._input_dict.values())

    return prog
Build second chunk by rebuilding a pristine MIL Program from MLModel
coremltools/models/utils.py