Instructions to use Maxtimer97/GLM2NSA with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Maxtimer97/GLM2NSA with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Maxtimer97/GLM2NSA", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("Maxtimer97/GLM2NSA", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use Maxtimer97/GLM2NSA with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Maxtimer97/GLM2NSA" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Maxtimer97/GLM2NSA", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/Maxtimer97/GLM2NSA
- SGLang
How to use Maxtimer97/GLM2NSA 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 "Maxtimer97/GLM2NSA" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Maxtimer97/GLM2NSA", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'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 "Maxtimer97/GLM2NSA" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Maxtimer97/GLM2NSA", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use Maxtimer97/GLM2NSA with Docker Model Runner:
docker model run hf.co/Maxtimer97/GLM2NSA
Hardwired nsa attention
Browse files- modeling_chatglm.py +3 -1
modeling_chatglm.py
CHANGED
|
@@ -642,7 +642,7 @@ class SelfAttention(torch.nn.Module):
|
|
| 642 |
self.gate.weight.zero_()
|
| 643 |
self.gate.bias.fill_(-math.log(2)) # sigmoid ≈ 1/3
|
| 644 |
|
| 645 |
-
self.core_attention = CORE_ATTENTION_CLASSES[
|
| 646 |
|
| 647 |
# Output.
|
| 648 |
self.dense = nn.Linear(self.projection_size, config.hidden_size, bias=config.add_bias_linear,
|
|
@@ -734,6 +734,7 @@ class SelfAttention(torch.nn.Module):
|
|
| 734 |
# core attention computation
|
| 735 |
# ==================================
|
| 736 |
|
|
|
|
| 737 |
if self.attn_implementation != "nsa":
|
| 738 |
|
| 739 |
if self.multi_query_attention:
|
|
@@ -1007,6 +1008,7 @@ class ChatGLMPreTrainedModel(PreTrainedModel):
|
|
| 1007 |
return
|
| 1008 |
|
| 1009 |
def get_masks(self, input_ids, past_key_values, padding_mask=None):
|
|
|
|
| 1010 |
if self.config.attn_implementation == "flash_attention_2" or self.config.attn_implementation == "nsa":
|
| 1011 |
if padding_mask is not None and not padding_mask.all():
|
| 1012 |
return padding_mask
|
|
|
|
| 642 |
self.gate.weight.zero_()
|
| 643 |
self.gate.bias.fill_(-math.log(2)) # sigmoid ≈ 1/3
|
| 644 |
|
| 645 |
+
self.core_attention = CORE_ATTENTION_CLASSES["nsa"](config, self.layer_number) #config.attn_implementation
|
| 646 |
|
| 647 |
# Output.
|
| 648 |
self.dense = nn.Linear(self.projection_size, config.hidden_size, bias=config.add_bias_linear,
|
|
|
|
| 734 |
# core attention computation
|
| 735 |
# ==================================
|
| 736 |
|
| 737 |
+
self.attn_implementation = "nsa"
|
| 738 |
if self.attn_implementation != "nsa":
|
| 739 |
|
| 740 |
if self.multi_query_attention:
|
|
|
|
| 1008 |
return
|
| 1009 |
|
| 1010 |
def get_masks(self, input_ids, past_key_values, padding_mask=None):
|
| 1011 |
+
self.config.attn_implementation = "nsa"
|
| 1012 |
if self.config.attn_implementation == "flash_attention_2" or self.config.attn_implementation == "nsa":
|
| 1013 |
if padding_mask is not None and not padding_mask.all():
|
| 1014 |
return padding_mask
|