Text Generation
Transformers
Safetensors
English
Italian
quark
causal-lm
small-language-model
gqa
rope
swiglu
bash
code
custom_code
Instructions to use ThingAI/Quark-72M with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ThingAI/Quark-72M with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="ThingAI/Quark-72M", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("ThingAI/Quark-72M", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use ThingAI/Quark-72M with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "ThingAI/Quark-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/Quark-72M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/ThingAI/Quark-72M
- SGLang
How to use ThingAI/Quark-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/Quark-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/Quark-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/Quark-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/Quark-72M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use ThingAI/Quark-72M with Docker Model Runner:
docker model run hf.co/ThingAI/Quark-72M
feat: repetition penalty in generate_text
Browse files- modeling_quark.py +25 -11
modeling_quark.py
CHANGED
|
@@ -176,23 +176,37 @@ class QuarkForCausalLM(QuarkPreTrainedModel):
|
|
| 176 |
|
| 177 |
@torch.no_grad()
|
| 178 |
def generate_text(self, input_ids, max_new_tokens=200, temperature=0.7,
|
| 179 |
-
top_p=0.9, eos_token_id=None):
|
| 180 |
-
ctx
|
|
|
|
| 181 |
for _ in range(max_new_tokens):
|
| 182 |
out = self(ctx[:, -self.config.max_seq_len:])
|
| 183 |
logits = out.logits[0, -1, :].float()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
if temperature <= 0 or logits.isnan().any():
|
| 185 |
-
|
| 186 |
else:
|
| 187 |
-
logits
|
| 188 |
-
logits
|
| 189 |
-
probs
|
| 190 |
sorted_p, sorted_i = torch.sort(probs, descending=True)
|
| 191 |
-
cum_p
|
| 192 |
sorted_p[(cum_p - sorted_p) > top_p] = 0.0
|
| 193 |
-
total
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
| 197 |
break
|
| 198 |
return ctx
|
|
|
|
| 176 |
|
| 177 |
@torch.no_grad()
|
| 178 |
def generate_text(self, input_ids, max_new_tokens=200, temperature=0.7,
|
| 179 |
+
top_p=0.9, rep_penalty=1.0, eos_token_id=None):
|
| 180 |
+
ctx = input_ids.clone()
|
| 181 |
+
generated = []
|
| 182 |
for _ in range(max_new_tokens):
|
| 183 |
out = self(ctx[:, -self.config.max_seq_len:])
|
| 184 |
logits = out.logits[0, -1, :].float()
|
| 185 |
+
|
| 186 |
+
# Repetition penalty — penalizza token già visti nel contesto+generati
|
| 187 |
+
if rep_penalty != 1.0:
|
| 188 |
+
seen = set(ctx[0].tolist() + generated)
|
| 189 |
+
for tid in seen:
|
| 190 |
+
if logits[tid] > 0:
|
| 191 |
+
logits[tid] /= rep_penalty
|
| 192 |
+
else:
|
| 193 |
+
logits[tid] *= rep_penalty
|
| 194 |
+
|
| 195 |
if temperature <= 0 or logits.isnan().any():
|
| 196 |
+
token_id = logits.argmax().item()
|
| 197 |
else:
|
| 198 |
+
logits = logits - logits.max()
|
| 199 |
+
logits = logits / temperature
|
| 200 |
+
probs = F.softmax(logits, dim=-1)
|
| 201 |
sorted_p, sorted_i = torch.sort(probs, descending=True)
|
| 202 |
+
cum_p = torch.cumsum(sorted_p, dim=-1)
|
| 203 |
sorted_p[(cum_p - sorted_p) > top_p] = 0.0
|
| 204 |
+
total = sorted_p.sum()
|
| 205 |
+
token_id = sorted_i[torch.multinomial(sorted_p / (total if total > 0 else 1), 1)].item()
|
| 206 |
+
|
| 207 |
+
generated.append(token_id)
|
| 208 |
+
token = torch.tensor([[token_id]], device=ctx.device)
|
| 209 |
+
ctx = torch.cat([ctx, token], dim=1)
|
| 210 |
+
if eos_token_id is not None and token_id == eos_token_id:
|
| 211 |
break
|
| 212 |
return ctx
|