def resave_model(
    model_path: Union[str, Path],
    new_model_path: Union[str, Path],
    force_external_data: bool = False,
    saved_external_files: Optional[dict[str, str]] = None,
) -> bool:
    """Resave the model along with external data files.

    :param model_path: Path to the original model file.
    :param new_model_path: Path to the new model file.
    :param force_external_data: If True, force the model to be saved with external data.
    :param saved_external_files: A dictionary of original file paths to new file names for external data files.
        Reuse the same file name if the the original file path is already in the dictionary.
        Else, the new file name will be <new_model_path>.data and this dictionary will be updated with the new
        file name.
    :return: True if the model has external data, False otherwise.
    """
    saved_external_files = {} if saved_external_files is None else saved_external_files

    model_path = Path(model_path).resolve()
    new_model_path = Path(new_model_path).resolve()
    assert new_model_path.suffix == ".onnx", "new_model_path must be .onnx file"
    new_model_path.parent.mkdir(parents=True, exist_ok=True)

    # copy over context binary files
    has_cb_files = copy_context_bin_files(model_path, new_model_path.parent, saved_cb_files=saved_external_files)

    external_file_names = get_external_data_file_names(model_path)

    if not external_file_names:
        if force_external_data:
            # save the model with single external data file
            model_proto_to_file(onnx.load(model_path), new_model_path, {"save_as_external_data": True})
            return True

        # no external data, so we can just copy the model
        hardlink_copy_file(model_path, new_model_path)
        return has_cb_files or False

    if len(external_file_names) > 1:
        # save the model with single external data file
        model_proto_to_file(onnx.load(model_path), new_model_path, {"save_as_external_data": True})
        return True

    external_file_path = str(model_path.parent / external_file_names[0])
    if external_file_path in saved_external_files:
        # already saved, model will refer to the same file
        new_external_file_name = saved_external_files[external_file_path]
    else:
        new_external_file_name = f"{new_model_path.name}.data"
        # copy the external data file to the new location
        hardlink_copy_file(external_file_path, new_model_path.parent / new_external_file_name)
        # update the saved external files mapping
        saved_external_files[external_file_path] = new_external_file_name

    # change the external data location and save the model file
    model_proto = onnx.load(model_path, load_external_data=False)
    change_external_data_location(model_proto, new_external_file_name)
    model_proto_to_file(model_proto, new_model_path)
    return True
Resave the model along with external data files.

    :param model_path: Path to the original model file.
    :param new_model_path: Path to the new model file.
    :param force_external_data: If True, force the model to be saved with external data.
    :param saved_external_files: A dictionary of original file paths to new file names for external data files.
        Reuse the same file name if the the original file path is already in the dictionary.
        Else, the new file name will be <new_model_path>.data and this dictionary will be updated with the new
        file name.
    :return: True if the model has external data, False otherwise.
    
olive/passes/onnx/common.py