autodl(linux)环境下载git-lfs等工具及使用

发布于:2025-05-01 ⋅ 阅读:(11) ⋅ 点赞:(0)

一、git-lfs工具下载

#初始化git.lfs命令
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
sudo apt-get install git-lfs
git lfs install

二、 huggingface-cli工具下载及使用

Linux设置huggingface的镜像:

export HF_ENDPOINT=https://hf-mirror.com
pip install -U huggingface_hub

使用:

// 下载全部文件并保存到指定位置时,添加--local-dir参数,此时将保存至./Llama-2-13b-chat-hf/中
huggingface-cli download --resume-download meta-llama/Llama-2-13b-chat-hf --local-dir Llama-2-13b-chat-hf


// 下载多个文件时,再添加具体文件名即可
huggingface-cli download meta-llama/Llama-2-13b-chat-hf config.json model-00001-of-00003.safetensors --local-dir Llama-2-13b-chat-hf

// 下载多个文件并排除一些文件可使用--include和--exclude命令
huggingface-cli download meta-llama/Llama-2-13b-chat-hf --include "*.safetensors" --exclude "*.bin"

// 需要 huggingface token 时 (Gated Repo),添加--token参数
huggingface-cli download meta-llama/Llama-2-13b-chat-hf --include "*.safetensors" --exclude "*.bin" --token hf_****

        测试下载模型是否正常运行的代码:

import transformers
import torch

# 切换为你下载的模型文件目录, 这里的demo是Llama-3-8B-Instruct
# 如果是其他模型,比如qwen,chatglm,请使用其对应的官方demo
model_id = "/media/codingma/LLM/llama3/Meta-Llama-3-8B-Instruct"

pipeline = transformers.pipeline(
    "text-generation",
    model=model_id,
    model_kwargs={"torch_dtype": torch.bfloat16},
    device_map="auto",
)

messages = [
    {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
    {"role": "user", "content": "Who are you?"},
]

prompt = pipeline.tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True
)

terminators = [
    pipeline.tokenizer.eos_token_id,
    pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
]

outputs = pipeline(
    prompt,
    max_new_tokens=256,
    eos_token_id=terminators,
    do_sample=True,
    temperature=0.6,
    top_p=0.9,
)
print(outputs[0]["generated_text"][len(prompt):])