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
相关推荐
A__tao18 小时前
Elasticsearch Mapping 一键生成 Java 实体类(支持嵌套 + 自动过滤注释)
java·python·elasticsearch
研究点啥好呢18 小时前
Github热门项目推荐 | 创建你的像素风格!
c++·python·node.js·github·开源软件
lly20240618 小时前
C 标准库 - `<stdio.h>`
开发语言
沫璃染墨18 小时前
C++ string 从入门到精通:构造、迭代器、容量接口全解析
c语言·开发语言·c++
jwn99918 小时前
Laravel6.x核心特性全解析
开发语言·php·laravel
迷藏49418 小时前
**发散创新:基于Rust实现的开源合规权限管理框架设计与实践**在现代软件架构中,**权限控制(RBAC)** 已成为保障
java·开发语言·python·rust·开源
lifewange18 小时前
Linux ps 进程查看命令详解
linux·运维·服务器
功德+n18 小时前
Linux下安装与配置Docker完整详细步骤
linux·运维·服务器·开发语言·docker·centos
明日清晨19 小时前
python扫码登录dy
开发语言·python