Text Generation
Transformers
Safetensors
English
Italian
quark
causal-lm
small-language-model
gqa
rope
swiglu
bash
code
custom_code
Instructions to use ThingAI/ARK-72M with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ThingAI/ARK-72M with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="ThingAI/ARK-72M", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("ThingAI/ARK-72M", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use ThingAI/ARK-72M with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "ThingAI/ARK-72M" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ThingAI/ARK-72M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/ThingAI/ARK-72M
- SGLang
How to use ThingAI/ARK-72M 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 "ThingAI/ARK-72M" \ --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": "ThingAI/ARK-72M", "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 "ThingAI/ARK-72M" \ --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": "ThingAI/ARK-72M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use ThingAI/ARK-72M with Docker Model Runner:
docker model run hf.co/ThingAI/ARK-72M
fix: dtype cast in SDPA + tie_weights() esplicito
Browse files- modeling_quark.py +14 -7
modeling_quark.py
CHANGED
|
@@ -46,8 +46,8 @@ class RotaryEmbedding(nn.Module):
|
|
| 46 |
T = q.size(2)
|
| 47 |
if T > self._max:
|
| 48 |
self._build_cache(T)
|
| 49 |
-
cos = self.cos_cache[:, :, :T, :]
|
| 50 |
-
sin = self.sin_cache[:, :, :T, :]
|
| 51 |
return q * cos + self._rotate_half(q) * sin, k * cos + self._rotate_half(k) * sin
|
| 52 |
|
| 53 |
|
|
@@ -75,6 +75,10 @@ class GroupedQueryAttention(nn.Module):
|
|
| 75 |
if self.n_groups > 1:
|
| 76 |
k = k.repeat_interleave(self.n_groups, dim=1)
|
| 77 |
v = v.repeat_interleave(self.n_groups, dim=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
out = F.scaled_dot_product_attention(
|
| 79 |
q, k, v, attn_mask=None,
|
| 80 |
dropout_p=self.drop if self.training else 0.0,
|
|
@@ -110,7 +114,7 @@ class TransformerBlock(nn.Module):
|
|
| 110 |
|
| 111 |
|
| 112 |
class QuarkPreTrainedModel(PreTrainedModel):
|
| 113 |
-
config_class
|
| 114 |
base_model_prefix = "model"
|
| 115 |
supports_gradient_checkpointing = False
|
| 116 |
|
|
@@ -153,6 +157,11 @@ class QuarkForCausalLM(QuarkPreTrainedModel):
|
|
| 153 |
def set_output_embeddings(self, value):
|
| 154 |
self.lm_head = value
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
def forward(
|
| 157 |
self,
|
| 158 |
input_ids = None,
|
|
@@ -160,9 +169,9 @@ class QuarkForCausalLM(QuarkPreTrainedModel):
|
|
| 160 |
labels = None,
|
| 161 |
**kwargs,
|
| 162 |
):
|
| 163 |
-
x
|
| 164 |
for layer in self.layers:
|
| 165 |
-
x
|
| 166 |
x = self.norm(x)
|
| 167 |
logits = self.lm_head(x)
|
| 168 |
|
|
@@ -179,7 +188,6 @@ class QuarkForCausalLM(QuarkPreTrainedModel):
|
|
| 179 |
@torch.no_grad()
|
| 180 |
def generate_text(self, input_ids, max_new_tokens=200, temperature=0.7,
|
| 181 |
top_p=0.9, eos_token_id=None):
|
| 182 |
-
"""Generazione semplice senza dipendere da .generate() HF."""
|
| 183 |
ctx = input_ids.clone()
|
| 184 |
for _ in range(max_new_tokens):
|
| 185 |
out = self(ctx[:, -self.config.max_seq_len:])
|
|
@@ -187,7 +195,6 @@ class QuarkForCausalLM(QuarkPreTrainedModel):
|
|
| 187 |
if temperature > 0:
|
| 188 |
logits /= temperature
|
| 189 |
probs = F.softmax(logits, dim=-1)
|
| 190 |
-
# top-p
|
| 191 |
sorted_p, sorted_i = torch.sort(probs, descending=True)
|
| 192 |
cum_p = torch.cumsum(sorted_p, dim=-1)
|
| 193 |
remove = cum_p - sorted_p > top_p
|
|
|
|
| 46 |
T = q.size(2)
|
| 47 |
if T > self._max:
|
| 48 |
self._build_cache(T)
|
| 49 |
+
cos = self.cos_cache[:, :, :T, :].to(dtype=q.dtype)
|
| 50 |
+
sin = self.sin_cache[:, :, :T, :].to(dtype=q.dtype)
|
| 51 |
return q * cos + self._rotate_half(q) * sin, k * cos + self._rotate_half(k) * sin
|
| 52 |
|
| 53 |
|
|
|
|
| 75 |
if self.n_groups > 1:
|
| 76 |
k = k.repeat_interleave(self.n_groups, dim=1)
|
| 77 |
v = v.repeat_interleave(self.n_groups, dim=1)
|
| 78 |
+
# Forza dtype uniforme prima di SDPA
|
| 79 |
+
dtype = q.dtype
|
| 80 |
+
k = k.to(dtype)
|
| 81 |
+
v = v.to(dtype)
|
| 82 |
out = F.scaled_dot_product_attention(
|
| 83 |
q, k, v, attn_mask=None,
|
| 84 |
dropout_p=self.drop if self.training else 0.0,
|
|
|
|
| 114 |
|
| 115 |
|
| 116 |
class QuarkPreTrainedModel(PreTrainedModel):
|
| 117 |
+
config_class = QuarkConfig
|
| 118 |
base_model_prefix = "model"
|
| 119 |
supports_gradient_checkpointing = False
|
| 120 |
|
|
|
|
| 157 |
def set_output_embeddings(self, value):
|
| 158 |
self.lm_head = value
|
| 159 |
|
| 160 |
+
def tie_weights(self):
|
| 161 |
+
"""HF chiama questo metodo dopo il caricamento dei pesi."""
|
| 162 |
+
if self.config.tie_word_embeddings:
|
| 163 |
+
self.lm_head.weight = self.embed_tokens.weight
|
| 164 |
+
|
| 165 |
def forward(
|
| 166 |
self,
|
| 167 |
input_ids = None,
|
|
|
|
| 169 |
labels = None,
|
| 170 |
**kwargs,
|
| 171 |
):
|
| 172 |
+
x = self.embed_tokens(input_ids)
|
| 173 |
for layer in self.layers:
|
| 174 |
+
x = layer(x, attention_mask=attention_mask)
|
| 175 |
x = self.norm(x)
|
| 176 |
logits = self.lm_head(x)
|
| 177 |
|
|
|
|
| 188 |
@torch.no_grad()
|
| 189 |
def generate_text(self, input_ids, max_new_tokens=200, temperature=0.7,
|
| 190 |
top_p=0.9, eos_token_id=None):
|
|
|
|
| 191 |
ctx = input_ids.clone()
|
| 192 |
for _ in range(max_new_tokens):
|
| 193 |
out = self(ctx[:, -self.config.max_seq_len:])
|
|
|
|
| 195 |
if temperature > 0:
|
| 196 |
logits /= temperature
|
| 197 |
probs = F.softmax(logits, dim=-1)
|
|
|
|
| 198 |
sorted_p, sorted_i = torch.sort(probs, descending=True)
|
| 199 |
cum_p = torch.cumsum(sorted_p, dim=-1)
|
| 200 |
remove = cum_p - sorted_p > top_p
|