Text Generation
Transformers
Safetensors
English
pragya_e2
small-language-model
from-scratch
causal-lm
decoder-only
gqa
rope
swiglu
exclusive-gated-attention
engram
kv-cache
custom_code
Instructions to use RuVI-AI/Pragya-EXP2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use RuVI-AI/Pragya-EXP2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="RuVI-AI/Pragya-EXP2", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("RuVI-AI/Pragya-EXP2", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use RuVI-AI/Pragya-EXP2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "RuVI-AI/Pragya-EXP2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "RuVI-AI/Pragya-EXP2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/RuVI-AI/Pragya-EXP2
- SGLang
How to use RuVI-AI/Pragya-EXP2 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 "RuVI-AI/Pragya-EXP2" \ --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": "RuVI-AI/Pragya-EXP2", "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 "RuVI-AI/Pragya-EXP2" \ --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": "RuVI-AI/Pragya-EXP2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use RuVI-AI/Pragya-EXP2 with Docker Model Runner:
docker model run hf.co/RuVI-AI/Pragya-EXP2
Update modeling_pragya.py
Browse files- modeling_pragya.py +24 -5
modeling_pragya.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import dataclasses
|
| 2 |
|
| 3 |
import torch
|
|
|
|
| 4 |
from transformers import GenerationMixin, PreTrainedModel
|
| 5 |
from transformers.modeling_outputs import CausalLMOutputWithPast
|
| 6 |
|
|
@@ -86,7 +87,8 @@ class PragyaForCausalLM(PreTrainedModel, GenerationMixin):
|
|
| 86 |
def get_output_embeddings(self):
|
| 87 |
return self.model.lm_head
|
| 88 |
|
| 89 |
-
def forward(self, input_ids=None, attention_mask=None, labels=None,
|
|
|
|
| 90 |
if attention_mask is not None and not bool(torch.all(attention_mask == 1)):
|
| 91 |
# The model has no attention-mask support. Padded batches would silently
|
| 92 |
# attend to pad tokens, so refuse rather than return wrong results.
|
|
@@ -94,10 +96,27 @@ class PragyaForCausalLM(PreTrainedModel, GenerationMixin):
|
|
| 94 |
"attention_mask with padding is not supported. Run one sequence at a time "
|
| 95 |
"or pass unpadded batches of equal length."
|
| 96 |
)
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
def prepare_inputs_for_generation(self, input_ids, **kwargs):
|
| 103 |
# No KV cache: GPT keeps its own cache format, so every step runs on the full sequence.
|
|
|
|
| 1 |
import dataclasses
|
| 2 |
|
| 3 |
import torch
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
from transformers import GenerationMixin, PreTrainedModel
|
| 6 |
from transformers.modeling_outputs import CausalLMOutputWithPast
|
| 7 |
|
|
|
|
| 87 |
def get_output_embeddings(self):
|
| 88 |
return self.model.lm_head
|
| 89 |
|
| 90 |
+
def forward(self, input_ids=None, attention_mask=None, labels=None,
|
| 91 |
+
logits_to_keep=0, **kwargs):
|
| 92 |
if attention_mask is not None and not bool(torch.all(attention_mask == 1)):
|
| 93 |
# The model has no attention-mask support. Padded batches would silently
|
| 94 |
# attend to pad tokens, so refuse rather than return wrong results.
|
|
|
|
| 96 |
"attention_mask with padding is not supported. Run one sequence at a time "
|
| 97 |
"or pass unpadded batches of equal length."
|
| 98 |
)
|
| 99 |
+
if labels is not None:
|
| 100 |
+
# Training path: GPT computes the loss itself.
|
| 101 |
+
logits, loss = self.model(input_ids, targets=labels)
|
| 102 |
+
return CausalLMOutputWithPast(loss=loss, logits=logits)
|
| 103 |
+
|
| 104 |
+
# Inference path. logits_to_keep=0 means logits for every position (B, T, V),
|
| 105 |
+
# which lm-evaluation-harness and loglikelihood scoring need. logits_to_keep=k
|
| 106 |
+
# returns the last k positions. Hidden states are captured after ln_f, and the
|
| 107 |
+
# projection is the same F.linear(x, lm_head.weight) GPT uses internally.
|
| 108 |
+
captured = {}
|
| 109 |
+
handle = self.model.transformer.ln_f.register_forward_hook(
|
| 110 |
+
lambda module, inp, out: captured.__setitem__("x", out))
|
| 111 |
+
try:
|
| 112 |
+
self.model(input_ids)
|
| 113 |
+
finally:
|
| 114 |
+
handle.remove()
|
| 115 |
+
x = captured["x"]
|
| 116 |
+
if logits_to_keep:
|
| 117 |
+
x = x[:, -logits_to_keep:, :]
|
| 118 |
+
logits = F.linear(x, self.model.lm_head.weight)
|
| 119 |
+
return CausalLMOutputWithPast(logits=logits)
|
| 120 |
|
| 121 |
def prepare_inputs_for_generation(self, input_ids, **kwargs):
|
| 122 |
# No KV cache: GPT keeps its own cache format, so every step runs on the full sequence.
|