def load_weights(path: Union[str, Path], file_format: Optional[WeightsFileFormat] = None, framework: str = "numpy"):
    """Load weights from a file.

    :param path: Path to the file.
    :param file_format: Format of the file. If None, will try to infer from the file extension.
    :param framework: Framework to load the weights into. Supported values are "pt" (pytorch) and "numpy".
    :return: Weights.
    """
    path = Path(path)
    if not path.exists():
        raise FileNotFoundError(f"File not found: {path}")

    if file_format is not None:
        pass
    elif path.suffix.startswith(".pt"):
        file_format = "pt"
    elif path.suffix.startswith(".np"):
        file_format = "numpy"
    elif path.suffix == ".safetensors":
        file_format = "safetensors"
    elif path.suffix == ".onnx_adapter":
        file_format = "onnx_adapter"
    else:
        raise ValueError(f"Unknown file format for {path}. Please provide file_format.")

    # validate file_format
    file_format = WeightsFileFormat(file_format)

    weights = None
    if file_format == WeightsFileFormat.PT:
        import torch

        weights = torch.load(path, weights_only=True)
        if framework == "numpy":
            weights = {k: v.numpy() for k, v in weights.items()}
    elif file_format == WeightsFileFormat.NUMPY:
        import numpy as np

        weights = dict(np.load(path))
        if framework == "pt":
            import torch

            weights = {k: torch.from_numpy(v) for k, v in weights.items()}
    elif file_format == WeightsFileFormat.SAFETENSORS:
        from safetensors import safe_open

        weights = {}
        with safe_open(path, framework=framework, device="cpu") as f:
            for key in f.keys():  # noqa: SIM118
                weights[key] = f.get_tensor(key)
    elif file_format == WeightsFileFormat.ONNX_ADAPTER:
        import numpy as np
        import onnxruntime as ort
        from packaging import version

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

        adapter_format = ort.AdapterFormat.read_adapter(str(path))
        # need to do np.copy since the .numpy is bound to the ort.OrtValue
        # after returning the weights, adapter_format will be deleted so the numpy will be invalid
        weights = {k: np.copy(v.numpy()) for k, v in adapter_format.get_parameters().items()}
        if framework == "pt":
            import torch

            weights = {k: torch.from_numpy(v) for k, v in weights.items()}

    return weights
Load weights from a file.

    :param path: Path to the file.
    :param file_format: Format of the file. If None, will try to infer from the file extension.
    :param framework: Framework to load the weights into. Supported values are "pt" (pytorch) and "numpy".
    :return: Weights.
    
olive/common/utils.py