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
How to finetune Phi2 using RoPE and QLoRA for long text summary generation?
I want to increase Phi2 context length from 2048---->5k tokens, So that I can finetune this model on my custom dataset (approx 5000 tokens per sample) using QLoRA.
I heard about RoPE but couldn't find any documentation or code to increase the context length by finetuning.
model = AutoModelForCausalLM.from_pretrained(
model_name,
quantization_config=bnb_config,
torch_dtype="auto",
device_map=device_map,
rope_scaling={"type": "linear", "factor": 3}, # Is this enough
use_cache=True,
use_flash_attention_2=False,
)
Also, how to verify that my rotatory_embedding has changed ?
Please Help
not correct use this one bro ......
print(model.config)
model.config.rope_scaling = {"type": "linear", "factor": 3}
print(model.config)
Did @ramkrish120595 suggestion work for you @parikshit1619 ? I'm exploring the possibility of fine-tuning a Phi2 model myself but without extending the context length WELL beyond 2k it's useless. Did you successfully FT Phi2 using RoPE? What was your length?
hi , I am using dynamic ROPE scaling technique.
model.config.rope_scaling = {"type": "dynamic", "factor": 8.0} ### context length extend up to 16k. It is working successfully for me.
if you want extend the context length in FT you can use linear ROPE scaling technique.
model.config.rope_scaling = {"type": "linear", "factor": 8.0} ### context length extend up to 16k.