matsuo-llm-course
Collection
15 items • Updated
How to use miya-99999/matsuo_llm_exp02 with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="miya-99999/matsuo_llm_exp02")
messages = [
{"role": "user", "content": "Who are you?"},
]
pipe(messages) # pip install -U transformers accelerate
# Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("miya-99999/matsuo_llm_exp02")
model = AutoModelForCausalLM.from_pretrained("miya-99999/matsuo_llm_exp02", device_map="auto")
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=256)
print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:]))How to use miya-99999/matsuo_llm_exp02 with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "miya-99999/matsuo_llm_exp02"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "miya-99999/matsuo_llm_exp02",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'docker model run hf.co/miya-99999/matsuo_llm_exp02
How to use miya-99999/matsuo_llm_exp02 with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "miya-99999/matsuo_llm_exp02" \
--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": "miya-99999/matsuo_llm_exp02",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'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 "miya-99999/matsuo_llm_exp02" \
--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": "miya-99999/matsuo_llm_exp02",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'How to use miya-99999/matsuo_llm_exp02 with Docker Model Runner:
docker model run hf.co/miya-99999/matsuo_llm_exp02
このモデルは東京大学松尾・岩澤研究室のLLM講座2024の課題のために作られたものです。
model_name = "miya-99999/matsuo_llm_exp002"
device = "cuda" if torch.cuda.is_available() else "cpu"
tokenizer = AutoTokenizer.from_pretrained(model_name, token=HF_TOKEN)
model = AutoModelForCausalLM.from_pretrained(
model_name,
device_map=device,
token=HF_TOKEN,
torch_dtype=torch.bfloat16,
use_cache=True
)
chat = [
{"role": "user", "content": "こんにちは。いい天気ですね。"},
]
tokenized_input = tokenizer.apply_chat_template(chat, add_generation_prompt=True, tokenize=True, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
tokenized_input,
max_new_tokens=512,
do_sample=False,
repetition_penalty=1.2,
pad_token_id=tokenizer.pad_token_id,
eos_token_id=[1,107],
)[0]
output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)