def save_weights(weights: dict, path: Union[str, Path], file_format: WeightsFileFormat = WeightsFileFormat.NUMPY):
    """Save the weights to a file.

    :param weights: Dictionary of numpy arrays.
    :param path: Path to save the weights. Might or might not include the file extension.
    :param file_format: Format to save the weights in.
    :return: Path to the saved file.
    """
    # validate file_format
    file_format = WeightsFileFormat(file_format)

    suffix = ".npz" if file_format == WeightsFileFormat.NUMPY else f".{file_format}"
    path = str(path)
    if not path.endswith(suffix):
        # do this this instead of using Path.with_suffix because it will remove periods in the path
        path += suffix
    path = Path(path)
    path.parent.mkdir(parents=True, exist_ok=True)

    if file_format == WeightsFileFormat.PT:
        import torch

        weights = {k: torch.from_numpy(v) for k, v in weights.items()}
        torch.save(weights, path)
    elif file_format == WeightsFileFormat.NUMPY:
        import numpy as np

        np.savez(path, **weights)
    elif file_format == WeightsFileFormat.SAFETENSORS:
        from safetensors.numpy import save_file

        save_file(weights, path)
    elif file_format == WeightsFileFormat.ONNX_ADAPTER:
        import onnxruntime as ort
        from packaging import version

        if version.parse(ort.__version__) < version.parse("1.20"):
            raise ValueError("Saving ONNX adapter files is only supported in ONNX Runtime >= 1.20")

        adapter_format = ort.AdapterFormat()
        # TODO(jambayk): Add model and adapter version
        adapter_format.set_parameters({k: ort.OrtValue.ortvalue_from_numpy(v) for k, v in weights.items()})
        adapter_format.export_adapter(str(path))

    return path
Save the weights to a file.

    :param weights: Dictionary of numpy arrays.
    :param path: Path to save the weights. Might or might not include the file extension.
    :param file_format: Format to save the weights in.
    :return: Path to the saved file.
    
olive/common/utils.py