You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyTorch降级至1.13.0后出现AttributeError: module 'torch' has no attribute 'version'求助

问题:PyTorch 1.13.0加载Falcon-7B时出现AttributeError: module 'torch' has no attribute 'version'

背景

因CUDA 11.3与PyTorch 2.2.2不兼容,将PyTorch降级至1.13.0以适配transformers库,但运行加载Falcon-7B模型的代码时触发上述错误。

环境信息

OS: Ubuntu 18.04 LTS
CUDA: 11.3
GPU: NVIDIA P5000 Quadro
IDE: Jupyter Notebook
Environment: VirtualEnv (venv)

运行代码

# 导入所需库
import torch

from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig

# 指定Falcon模型名称
model_name = "ybelkada/falcon-7b-sharded-bf16"

# 配置BitsAndBytes量化参数
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16,
)

# 加载带量化配置的Falcon模型
model = AutoModelForCausalLM.from_pretrained(
model_name,
quantization_config=bnb_config,
trust_remote_code=True
)

# 禁用模型缓存
model.config.use_cache = False

错误信息

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/FYP_Chatbot/test02/src/myenv-test002-02/lib/python3.9/site-packages/torch/cuda/__init__.py:242, in _lazy_init()
    241 try:
--> 242     queued_call()
    243 except Exception as e:

File ~/FYP_Chatbot/test02/src/myenv-test002-02/lib/python3.9/site-packages/torch/cuda/__init__.py:122, in _check_capability()
    116 old_gpu_warn = """
    117 Found GPU%d %s which is of cuda capability %d.%d.
    118 PyTorch no longer supports this GPU because it is too old.
    119 The minimum cuda capability supported by this library is %d.%d.
    120 """
--> 122 if torch.version.cuda is not None:  # on ROCm we don't want this check
    123     CUDA_VERSION = torch._C._cuda_getCompiledVersion()

AttributeError: module 'torch' has no attribute 'version'

The above exception was the direct cause of the following exception:

DeferredCudaCallError                     Traceback (most recent call last)
Cell In[10], line 17
     10 bnb_config = BitsAndBytesConfig(
     11 load_in_4bit=True,
     12 bnb_4bit_quant_type="nf4",
     13 bnb_4bit_compute_dtype=torch.float16,
     14 )
     16 # Loading the Falcon model with quantization configuration
---> 17 model = AutoModelForCausalLM.from_pretrained(
     18 model_name,
     19 quantization_config=bnb_config,
     20 trust_remote_code=True
     21 )
     23 # Disabling cache usage in the model configuration
     24 model.config.use_cache = False

File ~/FYP_Chatbot/test02/src/myenv-test002-02/lib/python3.9/site-packages/transformers/models/auto/auto_factory.py:563, in _BaseAutoModelClass.from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
    561 elif type(config) in cls._model_mapping.keys():
    562     model_class = _get_model_class(config, cls._model_mapping)
--> 563     return model_class.from_pretrained(
    564         pretrained_model_name_or_path, *model_args, config=config, **hub_kwargs, **kwargs
    565     )
    566 raise ValueError(
    567     f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
    568     f"Model type should be one of {', '.join(c.__name__ for c in cls._model_mapping.keys())}."
    569 )

File ~/FYP_Chatbot/test02/src/myenv-test002-02/lib/python3.9/site-packages/transformers/modeling_utils.py:3053, in PreTrainedModel.from_pretrained(cls, pretrained_model_name_or_path, config, cache_dir, ignore_mismatched_sizes, force_download, local_files_only, token, revision, use_safetensors, *model_args, **kwargs)
   3049 hf_quantizer.validate_environment(
   3050     torch_dtype=torch_dtype, from_tf=from_tf, from_flax=from_flax, device_map=device_map
   3051 )
   3052 torch_dtype = hf_quantizer.update_torch_dtype(torch_dtype)
-> 3053 device_map = hf_quantizer.update_device_map(device_map)
   3055 # Force-set to `True` for more mem efficiency
   3056 if low_cpu_mem_usage is None:

File ~/FYP_Chatbot/test02/src/myenv-test002-02/lib/python3.9/site-packages/transformers/quantizers/quantizer_bnb_4bit.py:246, in Bnb4BitHfQuantizer.update_device_map(self, device_map)
    244 def update_device_map(self, device_map):
    245     if device_map is None:
--> 246         device_map = {"": torch.cuda.current_device()}
    247         logger.info(
    248             "The device_map was not initialized. "
    249             "Setting device_map to {'':torch.cuda.current_device()}. "
    250             "If you want to use the model for inference, please set device_map ='auto' "
    251         )
    252     return device_map

File ~/FYP_Chatbot/test02/src/myenv-test002-02/lib/python3.9/site-packages/torch/cuda/__init__.py:552, in current_device()
    550 def current_device() -> int:
    551     r"""Returns the index of a currently selected device."""
-> 552     _lazy_init()
    553     return torch._C._cuda_getDevice()

File ~/FYP_Chatbot/test02/src/myenv-test002-02/lib/python3.9/site-packages/torch/cuda/__init__.py:246, in _lazy_init()
    243         except Exception as e:
    244             msg = (f"CUDA call failed lazily at initialization with error: {str(e)}\n\n"
    245                    f"CUDA call was originally invoked at:\n\n{orig_traceback}")
--> 246             raise DeferredCudaCallError(msg) from e
    247 finally:
    248     delattr(_tls, 'is_initializing')

DeferredCudaCallError: CUDA call failed lazily at initialization with error: module 'torch' has no attribute 'version'

环境依赖包

accelerate==0.29.1 
bitsandbytes==0.43.0 
datasets==2.18.0 
einops==0.7.0 
fsspec==2023.10.0 
peft @ git+https://github.com/huggingface/peft.git@26726bf1ddee6ca75ed4e1bfd292094526707a78 
torch==1.13.0 
transformers==4.39.3 
trl==0.8.1 
wandb==0.16.6

NVIDIA显卡信息(nvidia-smi)

Sat Apr  6 22:40:45 2024       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.182.03   Driver Version: 470.182.03   CUDA Version: 11.4     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  Quadro P5000        Off  | 00000000:01:00.0  On |                  Off |
| 27%   44C    P8     6W / 180W |    295MiB / 16275MiB |      3%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A       938      G   /usr/lib/xorg/Xorg                103MiB |
|    0   N/A  N/A      1150      G   /usr/bin/gnome-shell               37MiB |
|    0   N/A  N/A      1986      G   /usr/lib/firefox/firefox          150MiB |
+-----------------------------------------------------------------------------+

nvcc版本

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Sun_Mar_21_19:15:46_PDT_2021
Cuda compilation tools, release 11.3, V11.3.58
Build cuda_11.3.r11.3/compiler.29745058_0

解决方法

1. 修复PyTorch安装

该错误多因PyTorch安装不完整、CUDA组件缺失导致。先验证安装状态:

import torch
print(torch.__version__)
print(torch.cuda.is_available())

若输出异常,卸载后重新安装适配CUDA 11.3的PyTorch 1.13.0:

pip uninstall torch -y
pip install torch==1.13.0+cu113 torchvision==0.14.0+cu113 torchaudio==0.13.0 --extra-index-url https://download.pytorch.org/whl/cu113

2. 降级transformers版本

当前transformers 4.39.3与PyTorch 1.13.0兼容性差,降级至适配版本:

pip install transformers==4.26.1

3. 调整量化配置参数

PyTorch 1.13.0对nf4量化支持有限,修改BitsAndBytes配置:

bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="fp4",
    bnb_4bit_compute_dtype=torch.float32,
)

4. 显式指定设备映射

加载模型时设置device_map='auto',避免自动获取CUDA设备时触发错误:

model = AutoModelForCausalLM.from_pretrained(
    model_name,
    quantization_config=bnb_config,
    trust_remote_code=True,
    device_map='auto'
)

内容的提问来源于stack exchange,提问作者Muhammad Omar Farooq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 14:20:46