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 autogptq compat
#4
by Qubitium - opened
- modeling_chatglm.py +10 -10
modeling_chatglm.py
CHANGED
|
@@ -603,17 +603,17 @@ class GLMTransformer(torch.nn.Module):
|
|
| 603 |
layer_ret = torch.utils.checkpoint.checkpoint(
|
| 604 |
layer,
|
| 605 |
hidden_states,
|
| 606 |
-
attention_mask,
|
| 607 |
-
rotary_pos_emb,
|
| 608 |
-
kv_caches[index],
|
| 609 |
-
use_cache,
|
| 610 |
use_reentrant=False
|
| 611 |
)
|
| 612 |
else:
|
| 613 |
layer_ret = layer(
|
| 614 |
hidden_states,
|
| 615 |
-
attention_mask,
|
| 616 |
-
rotary_pos_emb,
|
| 617 |
kv_cache=kv_caches[index],
|
| 618 |
use_cache=use_cache
|
| 619 |
)
|
|
@@ -722,7 +722,7 @@ class ChatGLMModel(ChatGLMPreTrainedModel):
|
|
| 722 |
config.hidden_size // config.num_attention_heads if config.kv_channels is None else config.kv_channels
|
| 723 |
)
|
| 724 |
|
| 725 |
-
self.rotary_pos_emb = RotaryEmbedding(rotary_dim // 2, rope_ratio=config.rope_ratio, original_impl=config.original_rope,
|
| 726 |
device=device, dtype=config.torch_dtype)
|
| 727 |
self.encoder = init_method(GLMTransformer, config, **init_kwargs)
|
| 728 |
self.output_layer = init_method(nn.Linear, config.hidden_size, config.padded_vocab_size, bias=False,
|
|
@@ -738,8 +738,8 @@ class ChatGLMModel(ChatGLMPreTrainedModel):
|
|
| 738 |
self,
|
| 739 |
input_ids,
|
| 740 |
position_ids: Optional[torch.Tensor] = None,
|
| 741 |
-
attention_mask: Optional[torch.
|
| 742 |
-
full_attention_mask: Optional[torch.
|
| 743 |
past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
|
| 744 |
inputs_embeds: Optional[torch.Tensor] = None,
|
| 745 |
use_cache: Optional[bool] = None,
|
|
@@ -1204,4 +1204,4 @@ class ChatGLMForSequenceClassification(ChatGLMPreTrainedModel):
|
|
| 1204 |
past_key_values=transformer_outputs.past_key_values,
|
| 1205 |
hidden_states=transformer_outputs.hidden_states,
|
| 1206 |
attentions=transformer_outputs.attentions,
|
| 1207 |
-
)
|
|
|
|
| 603 |
layer_ret = torch.utils.checkpoint.checkpoint(
|
| 604 |
layer,
|
| 605 |
hidden_states,
|
| 606 |
+
attention_mask=attention_mask,
|
| 607 |
+
rotary_pos_emb=rotary_pos_emb,
|
| 608 |
+
kv_cache=kv_caches[index],
|
| 609 |
+
use_cache=use_cache,
|
| 610 |
use_reentrant=False
|
| 611 |
)
|
| 612 |
else:
|
| 613 |
layer_ret = layer(
|
| 614 |
hidden_states,
|
| 615 |
+
attention_mask=attention_mask,
|
| 616 |
+
rotary_pos_emb=rotary_pos_emb,
|
| 617 |
kv_cache=kv_caches[index],
|
| 618 |
use_cache=use_cache
|
| 619 |
)
|
|
|
|
| 722 |
config.hidden_size // config.num_attention_heads if config.kv_channels is None else config.kv_channels
|
| 723 |
)
|
| 724 |
|
| 725 |
+
self.rotary_pos_emb = RotaryEmbedding(rotary_dim // 2, rope_ratio=config.rope_ratio, original_impl=config.original_rope,
|
| 726 |
device=device, dtype=config.torch_dtype)
|
| 727 |
self.encoder = init_method(GLMTransformer, config, **init_kwargs)
|
| 728 |
self.output_layer = init_method(nn.Linear, config.hidden_size, config.padded_vocab_size, bias=False,
|
|
|
|
| 738 |
self,
|
| 739 |
input_ids,
|
| 740 |
position_ids: Optional[torch.Tensor] = None,
|
| 741 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 742 |
+
full_attention_mask: Optional[torch.Tensor] = None,
|
| 743 |
past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
|
| 744 |
inputs_embeds: Optional[torch.Tensor] = None,
|
| 745 |
use_cache: Optional[bool] = None,
|
|
|
|
| 1204 |
past_key_values=transformer_outputs.past_key_values,
|
| 1205 |
hidden_states=transformer_outputs.hidden_states,
|
| 1206 |
attentions=transformer_outputs.attentions,
|
| 1207 |
+
)
|