Meta-Llama-3.1-8B-bnb-4bit 下载加载

Meta-Llama-3.1-8B-bnb-4bit 加载:

python 复制代码
from huggingface_hub import InferenceClient
from torch import nn
from transformers import (
    AutoModel,
    AutoProcessor,
    AutoTokenizer,
    AutoModelForCausalLM,
    PreTrainedTokenizer,
    PreTrainedTokenizerFast,
    BitsAndBytesConfig,
)
from pathlib import Path
import torch
from PIL import Image
import os


# ===============================
# CLIP (SigLIP)
# # ===============================
# model_id = "google/siglip-so400m-patch14-384"
# CLIP_PATH = download_hg_model(model_id, "clip")

# clip_processor = AutoProcessor.from_pretrained(
#     CLIP_PATH,
#     trust_remote_code=True
# )

# clip_model = AutoModel.from_pretrained(
#     CLIP_PATH,
#     trust_remote_code=True
# )

# clip_model = clip_model.vision_model
# clip_model.eval()
# clip_model.requires_grad_(False)
# clip_model.to("cuda")


# ===============================
# LLM (LLaMA 3.1 4bit)
# ===============================
MODEL_PATH = "/data/lbg/models/textoon/ComfyUI/models/LLM/Meta-Llama-3.1-8B-bnb-4bit"

tokenizer = AutoTokenizer.from_pretrained(
    MODEL_PATH,
    trust_remote_code=True,
    use_fast=True
)

assert isinstance(
    tokenizer, (PreTrainedTokenizer, PreTrainedTokenizerFast)
), f"Tokenizer is of type {type(tokenizer)}"


# ⭐ 2️⃣ bitsandbytes 4bit 配置(关键)
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.float16,
    bnb_4bit_use_double_quant=True,
)

# ⭐ 3️⃣ 正确加载 4bit 模型
text_model = AutoModelForCausalLM.from_pretrained(
    MODEL_PATH,
    quantization_config=bnb_config,
    device_map="auto",
    trust_remote_code=True,
)

text_model.eval()
bash 复制代码
pip install bitsandbytes
相关推荐
孟健17 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞19 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽1 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
chlk1231 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑1 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件1 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
敏编程1 天前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪1 天前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook1 天前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
深紫色的三北六号1 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移