gbharti/finance-alpaca
Viewer • Updated • 68.9k • 2.49k • 146
How to use seong67360/Qwen2.5-7B-Instruct_v4 with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="seong67360/Qwen2.5-7B-Instruct_v4")
messages = [
{"role": "user", "content": "Who are you?"},
]
pipe(messages) # Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("seong67360/Qwen2.5-7B-Instruct_v4")
model = AutoModelForCausalLM.from_pretrained("seong67360/Qwen2.5-7B-Instruct_v4")
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]:]))How to use seong67360/Qwen2.5-7B-Instruct_v4 with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "seong67360/Qwen2.5-7B-Instruct_v4"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "seong67360/Qwen2.5-7B-Instruct_v4",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'docker model run hf.co/seong67360/Qwen2.5-7B-Instruct_v4
How to use seong67360/Qwen2.5-7B-Instruct_v4 with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "seong67360/Qwen2.5-7B-Instruct_v4" \
--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": "seong67360/Qwen2.5-7B-Instruct_v4",
"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 "seong67360/Qwen2.5-7B-Instruct_v4" \
--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": "seong67360/Qwen2.5-7B-Instruct_v4",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'How to use seong67360/Qwen2.5-7B-Instruct_v4 with Docker Model Runner:
docker model run hf.co/seong67360/Qwen2.5-7B-Instruct_v4
이 저장소는 Amazon SageMaker를 사용하여 Qwen 2.5 7B Instruct 모델을 파인튜닝하는 코드를 포함하고 있습니다. 이 프로젝트는 대규모 언어 모델의 효율적인 파인튜닝을 위해 QLoRA(Quantized Low-Rank Adaptation)를 사용합니다.
pip install torch transformers accelerate
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
# CUDA 사용 가능 여부 확인
if torch.cuda.is_available():
print(f"Using GPU: {torch.cuda.get_device_name(0)}")
else:
print("Warning: CUDA not available, using CPU")
# 모델과 토크나이저 로드
model = AutoModelForCausalLM.from_pretrained(
"seong67360/Qwen2.5-7B-Instruct_v4",
device_map="auto",
trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(
"seong67360/Qwen2.5-7B-Instruct_v4",
trust_remote_code=True
)
# 대화 예시
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is quantum computing?"}
]
# 응답 생성
response = model.chat(tokenizer, messages)
print(response)
GPU 메모리가 제한된 경우, 8비트 또는 4비트 양자화를 사용할 수 있습니다:
# 8비트 양자화
model = AutoModelForCausalLM.from_pretrained(
"seong67360/Qwen2.5-7B-Instruct_v4",
device_map="auto",
trust_remote_code=True,
load_in_8bit=True
)
# 또는 4비트 양자화
model = AutoModelForCausalLM.from_pretrained(
"seong67360/Qwen2.5-7B-Instruct_v4",
device_map="auto",
trust_remote_code=True,
load_in_4bit=True
)
response = model.chat(
tokenizer,
messages,
temperature=0.7, # 높을수록 더 창의적인 응답
top_p=0.9, # 샘플링에 사용될 누적 확률의 임계값
max_new_tokens=512, # 생성할 최대 토큰 수
repetition_penalty=1.1 # 반복 방지를 위한 페널티 (1.0 이상)
)
.
├── scripts/
│ ├── train.py
│ ├── tokenization_qwen2.py
│ ├── requirements.txt
│ └── bootstrap.sh
├── sagemaker_train.py
└── README.md
프로젝트에서 사용하는 주요 의존성:
Qwen/Qwen2.5-7B-Instruct{
'epochs': 3,
'per_device_train_batch_size': 4,
'gradient_accumulation_steps': 8,
'learning_rate': 1e-5,
'max_steps': 1000,
'bf16': True,
'max_length': 2048,
'gradient_checkpointing': True,
'optim': 'adamw_torch',
'lr_scheduler_type': 'cosine',
'warmup_ratio': 0.1,
'weight_decay': 0.01,
'max_grad_norm': 0.3
}
학습 환경은 분산 학습 및 메모리 관리를 위한 최적화로 구성되어 있습니다:
환경 준비:
requirements.txt 생성bootstrap.sh 생성모델 로딩:
데이터셋 처리:
학습:
학습 과정에서 다음 메트릭을 추적합니다:
구현에는 포괄적인 오류 처리 및 로깅이 포함되어 있습니다:
python sagemaker_train.py
프로젝트는 다음 기능이 포함된 Qwen2 토크나이저의 커스텀 구현(tokenization_qwen2.py)을 포함합니다: