Repository: huggingface/transformers
File: src/transformers/utils/import_utils.py (Python)

Code before:
        device_info = torch.musa.get_device_properties(torch.musa.current_device())
        if f"{device_info.major}{device_info.minor}" >= "22":
            return True
        return False
    if not torch.cuda.is_available() or torch.version.cuda is None:
        return False
    if torch.cuda.get_device_properties(torch.cuda.current_device()).major < 8:
        return False
    return True


@lru_cache
def enable_tf32(enable: bool) -> None:
    """
    Set TF32 mode using the appropriate PyTorch API.
    For PyTorch 2.9+, uses the new fp32_precision API.
    For older versions, uses the legacy allow_tf32 flags.
    Args:
        enable: Whether to enable TF32 mode
    """
    import torch

    pytorch_version = version.parse(get_torch_version())
    if pytorch_version >= version.parse("2.9.0"):
        precision_mode = "tf32" if enable else "ieee"
        torch.backends.cuda.matmul.fp32_precision = precision_mode
        torch.backends.cudnn.fp32_precision = precision_mode
    else:
        torch.backends.cuda.matmul.allow_tf32 = enable
        torch.backends.cudnn.allow_tf32 = enable


@lru_cache
def is_torch_flex_attn_available() -> bool:
    return is_torch_available() and version.parse(get_torch_version()) >= version.parse("2.5.0")


@lru_cache
def is_kenlm_available() -> bool:
    return _is_package_available("kenlm")


@lru_cache
def is_kernels_available() -> bool:
    return _is_package_available("kernels")


@lru_cache
def is_cv2_available() -> bool:
    return _is_package_available("cv2")


Review comment (suggestion):
One thing I just noticed in the docs - can we also set `torch.backends.fp32_precision = precision_mode`? The docs are a bit unclear but I think that's the "global" setting.

Code after:
        device_info = torch.musa.get_device_properties(torch.musa.current_device())
        if f"{device_info.major}{device_info.minor}" >= "22":
            return True
        return False
    if not torch.cuda.is_available() or torch.version.cuda is None:
        return False
    if torch.cuda.get_device_properties(torch.cuda.current_device()).major < 8:
        return False
    return True


@lru_cache
def enable_tf32(enable: bool) -> None:
    """
    Set TF32 mode using the appropriate PyTorch API.
    For PyTorch 2.9+, uses the new fp32_precision API.
    For older versions, uses the legacy allow_tf32 flags.
    Args:
        enable: Whether to enable TF32 mode
    """
    import torch

    pytorch_version = version.parse(get_torch_version())
    if pytorch_version >= version.parse("2.9.0"):
        precision_mode = "tf32" if enable else "ieee"
        torch.backends.fp32_precision = precision_mode
    else:
        if is_torch_musa_available():
            torch.backends.mudnn.allow_tf32 = enable
        else:
            torch.backends.cuda.matmul.allow_tf32 = enable
            torch.backends.cudnn.allow_tf32 = enable


@lru_cache
def is_torch_flex_attn_available() -> bool:
    return is_torch_available() and version.parse(get_torch_version()) >= version.parse("2.5.0")


@lru_cache
def is_kenlm_available() -> bool:
    return _is_package_available("kenlm")


@lru_cache
def is_kernels_available() -> bool:
    return _is_package_available("kernels")


@lru_cache
def is_cv2_available() -> bool: