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
相关推荐
if时光重来8 小时前
kingbase数据库指定数据表自增id重置
数据库·python·sql
赵谨言8 小时前
基于OpenCV的人脸五官识别系统研究
大数据·开发语言·经验分享·python
Elnaij8 小时前
从C++开始的编程生活(15)——模板知识补充
开发语言·c++
gaize12138 小时前
如何配置一个!P地址和子网掩码?
开发语言·php
学习3人组8 小时前
docker运行报错启动守护进程
linux·运维·centos
csbysj20208 小时前
广度优先遍历与最短路径
开发语言
Elnaij8 小时前
从C++开始的编程生活(16)——继承
开发语言·c++
2401_841495648 小时前
【自然语言处理】处理 GBK 编码汉字的算法设计
人工智能·python·自然语言处理·校验·文件读写·gbk编码与解码·批量过滤