Instructions to use microsoft/phi-2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/phi-2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="microsoft/phi-2")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2") model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2") - Inference
- Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use microsoft/phi-2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "microsoft/phi-2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/phi-2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/microsoft/phi-2
- SGLang
How to use microsoft/phi-2 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 "microsoft/phi-2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/phi-2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'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 "microsoft/phi-2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/phi-2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use microsoft/phi-2 with Docker Model Runner:
docker model run hf.co/microsoft/phi-2
Target modules {'out_proj', 'Wqkv'} is not found in the phi-2 model how can I fix this error?
from transformers import BitsAndBytesConfig
Quantization + LoRA = QLoRA
bnb_4bit = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16
)
bnb_8bit = BitsAndBytesConfig(
load_in_8bit=True,
)
model = AutoModelForCausalLM.from_pretrained(
model_id,
quantization_config=bnb_4bit, # 4๋นํธ or 8๋นํธ ์ค ์ ํ
device_map="auto",
trust_remote_code=True
)
model
#next code
from peft import LoraConfig, get_peft_model
config = LoraConfig(
r=8,
lora_alpha=32,
lora_dropout=0.05,
bias="none",
task_type="CAUSAL_LM",
target_modules=["Wqkv", "out_proj", ]
)
model = get_peft_model(model, config)
model.print_trainable_parameters()
this is my error message
ValueError Traceback (most recent call last)
in <cell line: 13>()
11 )
12
---> 13 model = get_peft_model(model, config)
14 model.print_trainable_parameters()
5 frames
/usr/local/lib/python3.10/dist-packages/peft/tuners/tuners_utils.py in inject_adapter(self, model, adapter_name)
303
304 if not is_target_modules_in_base_model:
--> 305 raise ValueError(
306 f"Target modules {peft_config.target_modules} not found in the base model. "
307 f"Please check the target modules and try again."
ValueError: Target modules {'out_proj', 'Wqkv'} not found in the base model. Please check the target modules and try again.
This code was worked few months ago, but now Is doesn't work. How should I change the target modules?
changetarget_modules from ["Wqkv", "out_proj", ] to ["q_proj", "k_proj", "v_proj", "dense"] would solve the problem.
change
target_modulesfrom["Wqkv", "out_proj", ]to["q_proj", "k_proj", "v_proj", "dense"]would solve the problem.
thanks!