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

在Kaggle中使用HuggingFace遭遇SafeTensorError的I/O内存问题解决方法

解决Kaggle中HuggingFace模型加载的SafeTensorError及空间限制问题
  • 加载量化模型压缩空间
    用bitsandbytes实现4bit/8bit量化,大幅降低模型存储空间需求,同时避免完整权重下载失败。先安装依赖:

    !pip install bitsandbytes accelerate
    

    加载模型代码:

    from transformers import AutoModelForCasualLM, AutoTokenizer
    model = AutoModelForCasualLM.from_pretrained(
        "你的模型名称",
        load_in_4bit=True,
        device_map="auto",
        trust_remote_code=True
    )
    tokenizer = AutoTokenizer.from_pretrained("你的模型名称")
    
  • 跳过SafeTensors,加载PyTorch格式权重
    如果是SafeTensors文件损坏或下载不全导致错误,强制加载传统PyTorch权重:

    model = AutoModelForCasualLM.from_pretrained(
        "你的模型名称",
        use_safetensors=False,
        device_map="auto"
    )
    

    注意:部分模型仅提供SafeTensors格式,此方法可能不适用。

  • 清理缓存释放空间
    Kaggle临时目录可能堆积缓存文件,先清理再尝试下载:

    !rm -rf /tmp/huggingface_cache/*
    

    也可自定义缓存目录到空间更充足的位置:

    model = AutoModelForCasualLM.from_pretrained(
        "你的模型名称",
        cache_dir="/kaggle/working/my_custom_cache"
    )
    
  • 低内存模式加载模型
    启用低内存加载选项,减少CPU内存占用,避免因空间不足中断加载:

    model = AutoModelForCasualLM.from_pretrained(
        "你的模型名称",
        low_cpu_mem_usage=True,
        device_map="auto"
    )
    

内容的提问来源于stack exchange,提问作者Sankarshan Acharya

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.01 13:14:53