Text Generation
Transformers
TensorBoard
Safetensors
llama
trl
sft
conversational
text-generation-inference
8-bit precision
bitsandbytes
Instructions to use stuser2023/Llama2-7b-finetuned with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use stuser2023/Llama2-7b-finetuned with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="stuser2023/Llama2-7b-finetuned") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("stuser2023/Llama2-7b-finetuned") model = AutoModelForCausalLM.from_pretrained("stuser2023/Llama2-7b-finetuned") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use stuser2023/Llama2-7b-finetuned with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "stuser2023/Llama2-7b-finetuned" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "stuser2023/Llama2-7b-finetuned", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/stuser2023/Llama2-7b-finetuned
- SGLang
How to use stuser2023/Llama2-7b-finetuned with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "stuser2023/Llama2-7b-finetuned" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "stuser2023/Llama2-7b-finetuned", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "stuser2023/Llama2-7b-finetuned" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "stuser2023/Llama2-7b-finetuned", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use stuser2023/Llama2-7b-finetuned with Docker Model Runner:
docker model run hf.co/stuser2023/Llama2-7b-finetuned
Model Card for Model ID
2023 AIA LLM課程範例
- 基礎模型: huggingface平台的Llama-2-7b-chat-hf ( https://huggingface.co/meta-llama/Llama-2-7b-chat-hf)
- 採用8bits精度載入模型權重 (load_in_8bit=True)
- 使用peft library的LoRA訓練方式,進行fine-tuning:
lora_alpha = 16
lora_dropout = 0.1
lora_r = 8
資料集
(語料包括:英,中,日,韓) HF連結: https://huggingface.co/datasets/timdettmers/openassistant-guanaco
訓練環境
使用google colab 免費資源(GPU: T4, 15GB)
執行範例
1.先確認所需library
#確認安裝所需套件
!pip install -q -U trl transformers accelerate git+https://github.com/huggingface/peft.git
#LlamaTokenizer requires the SentencePiece library
!pip install sentencepiece
2.下載模型
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
model_name = "stuser2023/Llama2-7b-finetuned"
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
model = AutoModelForCausalLM.from_pretrained(
model_name,
quantization_config=quantization_config,
device_map={'': 0}, # 設定使用的設備,此處指定為 GPU 0
trust_remote_code=True,
)
model.config.use_cache = False
model=model.eval() #把Dropout功能關掉
3.進行推論(文字生成)
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path, trust_remote_code=True, padding=True)
tokenizer.pad_token = tokenizer.eos_token
input_ids = tokenizer(['<s>Human: 在未來的2040年,人類社會將進入\n</s><s>Assistant: '], return_tensors="pt",add_special_tokens=False).input_ids.to('cuda')
generate_input = {
"input_ids":input_ids,
"max_new_tokens":384,
"do_sample":True,
"top_k":50,
"top_p":0.95,
"temperature":0.3,
"repetition_penalty":1.3,
"eos_token_id":tokenizer.eos_token_id,
"bos_token_id":tokenizer.bos_token_id,
"pad_token_id":tokenizer.pad_token_id
}
generate_ids = model.generate(**generate_input)
text = tokenizer.decode(generate_ids[0])
print(text)
目前的生成效果
'''
### Human:在未來的2040年,人類社會將進入一個新的時代。
### Assistant:人類社會在2040年進入一個新的時代,這個時代是一個高度技術和人工智慧的時代。
人類將使用自動駕駛汽車和處理資料,並且人類將使用人工智慧來解決問題。
人類將使用數位貨幣來儲存和交換貨幣,並且人類將使用雷射和激光來傳遞資料。
人類將使用創新的技術來改善生活。
人類將使用處理資料和數位貨幣來儲存和交換資料。
人類將使用雷射和激光來傳遞資料。
人類將使用創新的技術來改善生活。
人類將使用處理資料和數位貨幣來儲存和交換資料
'''
- Downloads last month
- 4