Text Generation
Transformers
PyTorch
English
gpt2
text-generation-inference
backpack
backpackmodel
custom_code
Instructions to use stanfordnlp/backpack-gpt2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use stanfordnlp/backpack-gpt2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="stanfordnlp/backpack-gpt2", trust_remote_code=True)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("stanfordnlp/backpack-gpt2", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("stanfordnlp/backpack-gpt2", trust_remote_code=True) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use stanfordnlp/backpack-gpt2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "stanfordnlp/backpack-gpt2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "stanfordnlp/backpack-gpt2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/stanfordnlp/backpack-gpt2
- SGLang
How to use stanfordnlp/backpack-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 "stanfordnlp/backpack-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": "stanfordnlp/backpack-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 "stanfordnlp/backpack-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": "stanfordnlp/backpack-gpt2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use stanfordnlp/backpack-gpt2 with Docker Model Runner:
docker model run hf.co/stanfordnlp/backpack-gpt2
Set more precise shape to the attention weights and outputs
#1
by ivanzhouyq - opened
modeling_backpack_gpt2.py
CHANGED
|
@@ -101,13 +101,14 @@ class BackpackWeightNetwork(nn.Module):
|
|
| 101 |
super().__init__()
|
| 102 |
self.n_embd = embed_dim
|
| 103 |
self.num_senses = num_senses
|
| 104 |
-
self.
|
|
|
|
| 105 |
self.softmax_scale = None
|
| 106 |
|
| 107 |
def forward(self, encoded):
|
| 108 |
b, s, d = encoded.shape
|
| 109 |
encoded = self.c_attn(encoded) # (b, s, 2*d)
|
| 110 |
-
encoded = encoded.reshape(b, s, 2, self.num_senses,
|
| 111 |
batch_size, seqlen = encoded.shape[0], encoded.shape[1]
|
| 112 |
|
| 113 |
# compute scores & mask
|
|
|
|
| 101 |
super().__init__()
|
| 102 |
self.n_embd = embed_dim
|
| 103 |
self.num_senses = num_senses
|
| 104 |
+
self.embed_per_sense = embed_dim // num_senses
|
| 105 |
+
self.c_attn = nn.Linear(embed_dim, 2 * num_senses * self.embed_per_sense)
|
| 106 |
self.softmax_scale = None
|
| 107 |
|
| 108 |
def forward(self, encoded):
|
| 109 |
b, s, d = encoded.shape
|
| 110 |
encoded = self.c_attn(encoded) # (b, s, 2*d)
|
| 111 |
+
encoded = encoded.reshape(b, s, 2, self.num_senses, self.embed_per_sense) #(b, s, 2, nv, d//nv)
|
| 112 |
batch_size, seqlen = encoded.shape[0], encoded.shape[1]
|
| 113 |
|
| 114 |
# compute scores & mask
|