Instructions to use optimum/gpt2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use optimum/gpt2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="optimum/gpt2")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("optimum/gpt2") model = AutoModelForCausalLM.from_pretrained("optimum/gpt2") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use optimum/gpt2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "optimum/gpt2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "optimum/gpt2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/optimum/gpt2
- SGLang
How to use optimum/gpt2 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 "optimum/gpt2" \ --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": "optimum/gpt2", "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 "optimum/gpt2" \ --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": "optimum/gpt2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use optimum/gpt2 with Docker Model Runner:
docker model run hf.co/optimum/gpt2
Update README.md
Browse files
README.md
CHANGED
|
@@ -43,6 +43,8 @@ You can use the raw model for text generation or fine-tune it to a downstream ta
|
|
| 43 |
|
| 44 |
Here is how to use the ONNX models of gpt2 to get the features of a given text:
|
| 45 |
|
|
|
|
|
|
|
| 46 |
```python
|
| 47 |
from transformers import AutoTokenizer, pipeline
|
| 48 |
from optimum.onnxruntime import ORTModelForCausalLM
|
|
@@ -54,3 +56,19 @@ onnx_gen = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
|
| 54 |
text = "My name is Philipp and I live in Germany."
|
| 55 |
gen = onnx_gen(text)
|
| 56 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
Here is how to use the ONNX models of gpt2 to get the features of a given text:
|
| 45 |
|
| 46 |
+
Example using transformers.pipelines:
|
| 47 |
+
|
| 48 |
```python
|
| 49 |
from transformers import AutoTokenizer, pipeline
|
| 50 |
from optimum.onnxruntime import ORTModelForCausalLM
|
|
|
|
| 56 |
text = "My name is Philipp and I live in Germany."
|
| 57 |
gen = onnx_gen(text)
|
| 58 |
```
|
| 59 |
+
|
| 60 |
+
Example of text generation:
|
| 61 |
+
|
| 62 |
+
```python
|
| 63 |
+
from transformers import AutoTokenizer
|
| 64 |
+
from optimum.onnxruntime import ORTModelForCausalLM
|
| 65 |
+
import torch
|
| 66 |
+
|
| 67 |
+
tokenizer = AutoTokenizer.from_pretrained("optimum/gpt2")
|
| 68 |
+
model = ORTModelForCausalLM.from_pretrained("optimum/gpt2")
|
| 69 |
+
|
| 70 |
+
inputs = tokenizer("My name is Arthur and I live in", return_tensors="pt")
|
| 71 |
+
|
| 72 |
+
gen_tokens = model.generate(**inputs,do_sample=True,temperature=0.9, min_length=20,max_length=20)
|
| 73 |
+
tokenizer.batch_decode(gen_tokens)
|
| 74 |
+
```
|