From 2003da34f88f1cf5e0d1b3311873700785bef65c Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Fri, 12 Sep 2025 03:13:15 -0700 Subject: [PATCH] importlib_version --- unsloth/__init__.py | 7 +++++-- unsloth/models/_utils.py | 2 ++ unsloth/models/llama.py | 7 +------ unsloth/models/vision.py | 7 +------ 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/unsloth/__init__.py b/unsloth/__init__.py index 5529e4eeef..8255e505a8 100644 --- a/unsloth/__init__.py +++ b/unsloth/__init__.py @@ -14,7 +14,7 @@ import warnings, importlib, sys from packaging.version import Version -import os, re, subprocess, inspect +import os, re, subprocess, inspect, functools import numpy as np # Fix some issues before importing other packages @@ -69,9 +69,12 @@ except Exception as exception: raise exception pass +@functools.cache def is_hip(): return bool(getattr(getattr(torch, "version", None), "hip", None)) +pass +@functools.cache def get_device_type(): if hasattr(torch, "cuda") and torch.cuda.is_available(): if is_hip(): @@ -83,6 +86,7 @@ def get_device_type(): pass DEVICE_TYPE : str = get_device_type() +@functools.cache def get_device_count(): if DEVICE_TYPE in ("cuda", "hip"): return torch.cuda.device_count() @@ -137,7 +141,6 @@ pass import importlib.util from pathlib import Path from importlib.metadata import version as importlib_version -from packaging.version import Version from .import_fixes import fix_xformers_performance_issue fix_xformers_performance_issue(); del fix_xformers_performance_issue; from .import_fixes import fix_vllm_aimv2_issue diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py index e3b70d93a5..e3ac56ac83 100644 --- a/unsloth/models/_utils.py +++ b/unsloth/models/_utils.py @@ -24,6 +24,7 @@ __all__ = [ "xformers_attention", "xformers_version", "__version__", + "importlib_version", "HAS_FLASH_ATTENTION", "HAS_FLASH_ATTENTION_SOFTCAPPING", "USE_MODELSCOPE", @@ -81,6 +82,7 @@ import re import functools import warnings, subprocess, re, inspect, psutil, os, math from unsloth_zoo.utils import Version +from importlib.metadata import version as importlib_version from unsloth import DEVICE_TYPE, DEVICE_COUNT from unsloth_zoo.tokenizer_utils import ( diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py index af3ab8d3da..f7a53d05fd 100644 --- a/unsloth/models/llama.py +++ b/unsloth/models/llama.py @@ -19,7 +19,7 @@ import functools from typing import Any, Dict, Optional, Tuple, List, Union from ._utils import * from ._utils import patch_unsloth_smart_gradient_checkpointing -from ._utils import __version__ +from ._utils import __version__, importlib_version from ._utils import move_to_device from ._utils import _prepare_model_for_qat from torch.nn.functional import scaled_dot_product_attention @@ -1867,23 +1867,18 @@ class FastLlamaModel: gpu_stats = torch.cuda.get_device_properties(0) gpu_version = torch.version.cuda gpu_stats_snippet = f"CUDA: {gpu_stats.major}.{gpu_stats.minor}. CUDA Toolkit: {gpu_version}." - - from importlib.metadata import version as importlib_version try: vllm_version = f" vLLM: {importlib_version('vllm')}." except: vllm_version = "" elif DEVICE_TYPE == "hip": gpu_stats = torch.cuda.get_device_properties(0) gpu_version = torch.version.hip gpu_stats_snippet = f"ROCm Toolkit: {gpu_version}." - - from importlib.metadata import version as importlib_version try: vllm_version = f" vLLM: {importlib_version('vllm')}." except: vllm_version = "" elif DEVICE_TYPE == "xpu": gpu_stats = torch.xpu.get_device_properties(0) gpu_version = torch.version.xpu gpu_stats_snippet = f"Intel Toolkit: {gpu_version}." - try: vllm_version = f" vLLM: {importlib_version('vllm')}." except: vllm_version = "" else: diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py index 6c986a3bd3..7a07a8d0fd 100644 --- a/unsloth/models/vision.py +++ b/unsloth/models/vision.py @@ -28,7 +28,7 @@ pass from ..kernels import ( post_patch_loss_function, ) -from ._utils import __version__ +from ._utils import __version__, importlib_version from ._utils import * from ..save import patch_saving_functions from peft import LoraConfig, TaskType, get_peft_model as _get_peft_model @@ -277,23 +277,18 @@ class FastBaseModel: gpu_stats = torch.cuda.get_device_properties(0) gpu_version = torch.version.cuda gpu_stats_snippet = f"CUDA: {gpu_stats.major}.{gpu_stats.minor}. CUDA Toolkit: {gpu_version}." - - from importlib.metadata import version as importlib_version try: vllm_version = f" vLLM: {importlib_version('vllm')}." except: vllm_version = "" elif DEVICE_TYPE == "hip": gpu_stats = torch.cuda.get_device_properties(0) gpu_version = torch.version.hip gpu_stats_snippet = f"ROCm Toolkit: {gpu_version}." - - from importlib.metadata import version as importlib_version try: vllm_version = f" vLLM: {importlib_version('vllm')}." except: vllm_version = "" elif DEVICE_TYPE == "xpu": gpu_stats = torch.xpu.get_device_properties(0) gpu_version = torch.version.xpu gpu_stats_snippet = f"Intel Toolkit: {gpu_version}." - # TODO: After adding vLLM support for XPU, changed this vllm_version = "" else: