Instructions to use AjayRangoji/AR-4B-base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use AjayRangoji/AR-4B-base with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="AjayRangoji/AR-4B-base", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("AjayRangoji/AR-4B-base", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use AjayRangoji/AR-4B-base with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "AjayRangoji/AR-4B-base" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "AjayRangoji/AR-4B-base", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/AjayRangoji/AR-4B-base
- SGLang
How to use AjayRangoji/AR-4B-base 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 "AjayRangoji/AR-4B-base" \ --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": "AjayRangoji/AR-4B-base", "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 "AjayRangoji/AR-4B-base" \ --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": "AjayRangoji/AR-4B-base", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use AjayRangoji/AR-4B-base with Docker Model Runner:
docker model run hf.co/AjayRangoji/AR-4B-base
Download training_code/ar/model/attention.py from AjayRangoji/AR-4B-base: direct link, hf CLI and curl.
- Browser
- Download file 2.3 kB
-
https://huggingface.co/AjayRangoji/AR-4B-base/resolve/main/training_code/ar/model/attention.py
- Command line
-
hf download hf://AjayRangoji/AR-4B-base/training_code/ar/model/attention.py
-
curl -L -o attention.py https://huggingface.co/AjayRangoji/AR-4B-base/resolve/main/training_code/ar/model/attention.py
2.3 kB
| import torch | |
| import torch.nn as nn | |
| from torch.nn.attention.flex_attention import flex_attention | |
| from .rmsnorm import RMSNorm | |
| from .rope import apply_rope | |
| # compile FlexAttention once at import; without compile it falls back | |
| # to a very slow reference kernel | |
| flex_attention = torch.compile(flex_attention) | |
| class GroupedQueryAttention(nn.Module): | |
| """Grouped-query attention with RoPE, QK-norm, and a caller-supplied mask. | |
| Query heads are split into groups that each share one key/value head, | |
| shrinking the KV cache with almost no loss in quality. The block_mask | |
| is built outside the model (causal + same-document) and simply passed in. | |
| """ | |
| def __init__(self, dim: int, n_heads: int, n_kv_heads: int, head_dim: int): | |
| super().__init__() | |
| self.n_heads = n_heads | |
| self.n_kv_heads = n_kv_heads | |
| self.head_dim = head_dim | |
| # projections: Q is full width, K/V are narrower (fewer heads) | |
| self.q_proj = nn.Linear(dim, n_heads * head_dim, bias=False) | |
| self.k_proj = nn.Linear(dim, n_kv_heads * head_dim, bias=False) | |
| self.v_proj = nn.Linear(dim, n_kv_heads * head_dim, bias=False) | |
| self.o_proj = nn.Linear(n_heads * head_dim, dim, bias=False) | |
| # QK-norm: normalize each head's Q and K before attention (stability) | |
| self.q_norm = RMSNorm(head_dim) | |
| self.k_norm = RMSNorm(head_dim) | |
| def forward(self, x, cos, sin, block_mask): | |
| B, T, _ = x.shape | |
| # project, then split into heads: (B, heads, T, head_dim) | |
| q = self.q_proj(x).view(B, T, self.n_heads, self.head_dim).transpose(1, 2) | |
| k = self.k_proj(x).view(B, T, self.n_kv_heads, self.head_dim).transpose(1, 2) | |
| v = self.v_proj(x).view(B, T, self.n_kv_heads, self.head_dim).transpose(1, 2) | |
| # QK-norm, then rotate Q and K by their positions | |
| q = apply_rope(self.q_norm(q), cos, sin) | |
| k = apply_rope(self.k_norm(k), cos, sin) | |
| # FlexAttention handles GQA internally; no KV expansion required, | |
| # which saves memory vs the old repeat_interleave path | |
| out = flex_attention(q, k, v, block_mask=block_mask, enable_gqa=True) | |
| # merge heads back together and project out | |
| out = out.transpose(1, 2).contiguous().view(B, T, -1) | |
| return self.o_proj(out) |