Instructions to use zai-org/glm-4-9b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use zai-org/glm-4-9b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="zai-org/glm-4-9b", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("zai-org/glm-4-9b", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use zai-org/glm-4-9b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "zai-org/glm-4-9b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "zai-org/glm-4-9b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/zai-org/glm-4-9b
- SGLang
How to use zai-org/glm-4-9b 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 "zai-org/glm-4-9b" \ --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": "zai-org/glm-4-9b", "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 "zai-org/glm-4-9b" \ --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": "zai-org/glm-4-9b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use zai-org/glm-4-9b with Docker Model Runner:
docker model run hf.co/zai-org/glm-4-9b
Fix tensor shape error
#7
by hiyouga - opened
- modeling_chatglm.py +4 -7
modeling_chatglm.py
CHANGED
|
@@ -253,15 +253,12 @@ class CoreAttention(torch.nn.Module):
|
|
| 253 |
# This is actually dropping out entire tokens to attend to, which might
|
| 254 |
# seem a bit unusual, but is taken from the original Transformer paper.
|
| 255 |
attention_probs = self.attention_dropout(attention_probs)
|
| 256 |
-
# =========================
|
| 257 |
-
# Context layer. [sq, b, hp]
|
| 258 |
-
# =========================
|
| 259 |
-
|
| 260 |
-
# value_layer -> context layer.
|
| 261 |
-
# [sk, b, np, hn] --> [b, np, sq, hn]
|
| 262 |
|
|
|
|
|
|
|
|
|
|
| 263 |
# context layer shape: [b, np, sq, hn]
|
| 264 |
-
output_size = (value_layer.size(
|
| 265 |
# change view [b * np, sk, hn]
|
| 266 |
value_layer = value_layer.view(output_size[0] * output_size[1], value_layer.size(2), -1)
|
| 267 |
# change view [b * np, sq, sk]
|
|
|
|
| 253 |
# This is actually dropping out entire tokens to attend to, which might
|
| 254 |
# seem a bit unusual, but is taken from the original Transformer paper.
|
| 255 |
attention_probs = self.attention_dropout(attention_probs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
+
# query layer shape: [b * np, sq, hn]
|
| 258 |
+
# value layer shape: [b, np, sk, hn]
|
| 259 |
+
# attention shape: [b, np, sq, sk]
|
| 260 |
# context layer shape: [b, np, sq, hn]
|
| 261 |
+
output_size = (value_layer.size(0), value_layer.size(1), query_layer.size(1), value_layer.size(3))
|
| 262 |
# change view [b * np, sk, hn]
|
| 263 |
value_layer = value_layer.view(output_size[0] * output_size[1], value_layer.size(2), -1)
|
| 264 |
# change view [b * np, sq, sk]
|