Text Generation
Transformers
Safetensors
English
Chinese
qwen3
ksa
ksa-v2
custom_code
long-context
conversational
text-generation-inference
Instructions to use OpenOneRec/KSA-V2-4B-base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use OpenOneRec/KSA-V2-4B-base with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="OpenOneRec/KSA-V2-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 AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("OpenOneRec/KSA-V2-4B-base", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("OpenOneRec/KSA-V2-4B-base", trust_remote_code=True, device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=256) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use OpenOneRec/KSA-V2-4B-base with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "OpenOneRec/KSA-V2-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": "OpenOneRec/KSA-V2-4B-base", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/OpenOneRec/KSA-V2-4B-base
- SGLang
How to use OpenOneRec/KSA-V2-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 "OpenOneRec/KSA-V2-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": "OpenOneRec/KSA-V2-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 "OpenOneRec/KSA-V2-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": "OpenOneRec/KSA-V2-4B-base", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use OpenOneRec/KSA-V2-4B-base with Docker Model Runner:
docker model run hf.co/OpenOneRec/KSA-V2-4B-base
Download sparse_decode_gather.py from OpenOneRec/KSA-V2-4B-base: direct link, hf CLI and curl.
- Browser
- Download file 4.23 kB
-
https://huggingface.co/OpenOneRec/KSA-V2-4B-base/resolve/main/sparse_decode_gather.py
- Command line
-
hf download hf://OpenOneRec/KSA-V2-4B-base/sparse_decode_gather.py
-
curl -L -o sparse_decode_gather.py https://huggingface.co/OpenOneRec/KSA-V2-4B-base/resolve/main/sparse_decode_gather.py
4.23 kB
| """Request-owned, raw-bit-preserving summary-indexer decode materialization.""" | |
| import torch | |
| import triton | |
| import triton.language as tl | |
| def _gather(K, V, Blocks, OK, OV, Mask, Candidates, Safe, | |
| L, H: tl.constexpr, D: tl.constexpr, | |
| N: tl.constexpr, W: tl.constexpr, R: tl.constexpr, | |
| START, KS0: tl.constexpr, KS1: tl.constexpr, | |
| KS2: tl.constexpr, KS3: tl.constexpr, | |
| VS0: tl.constexpr, VS1: tl.constexpr, VS2: tl.constexpr, | |
| VS3: tl.constexpr, BS0: tl.constexpr, BS2: tl.constexpr, | |
| TILE: tl.constexpr, DIM: tl.constexpr): | |
| bh = tl.program_id(0) | |
| b, h = bh // H, bh % H | |
| t = tl.program_id(1) * TILE + tl.arange(0, TILE) | |
| d = tl.arange(0, DIM) | |
| remote = t < N * R | |
| block = tl.load(Blocks + b * BS0 + (t // R) * BS2, | |
| mask=remote & (t < W), other=-1).to(tl.int64) | |
| index = tl.where(remote, block * R + t % R, START + t - N * R) | |
| valid = tl.where(remote, (block >= 0) & (index < L), | |
| (index >= 0) & (index < L)) | |
| candidate = tl.where(valid, index, L) | |
| safe = tl.minimum(candidate, L - 1) | |
| k = tl.load(K + b * KS0 + h * KS1 + safe[:, None] * KS2 + d[None, :] * KS3, | |
| mask=(t[:, None] < W) & (d[None, :] < D), other=0) | |
| v = tl.load(V + b * VS0 + h * VS1 + safe[:, None] * VS2 + d[None, :] * VS3, | |
| mask=(t[:, None] < W) & (d[None, :] < D), other=0) | |
| out = (bh * W + t[:, None]) * D + d[None, :] | |
| tl.store(OK + out, k, mask=(t[:, None] < W) & (d[None, :] < D)) | |
| tl.store(OV + out, v, mask=(t[:, None] < W) & (d[None, :] < D)) | |
| if h == 0: | |
| tl.store(Mask + b * W + t, valid, mask=t < W) | |
| tl.store(Candidates + b * W + t, candidate, mask=t < W) | |
| tl.store(Safe + b * W + t, safe, mask=t < W) | |
| def fused_summary_decode_gather(key, value, selected, *, raw_block_size, | |
| local_window_tokens, local_alignment_tokens, | |
| workspace): | |
| """Write every output slot, including masked slots, on the input device. | |
| Masked K/V equal the reference's last-live-token copy. Exact compact shapes | |
| and contiguous strides preserve SDPA dispatch. Storage is cache-local; | |
| shape/dtype/device changes replace it. Returned views live until next call. | |
| """ | |
| if key.shape != value.shape or key.ndim != 4: | |
| raise ValueError('decode K/V must share [B,H,L,D]') | |
| b, h, length, dim = key.shape | |
| if selected.ndim != 3 or selected.shape[:2] != (b, 1): | |
| raise ValueError('decode selected blocks must have [B,1,K] layout') | |
| alignment = raw_block_size if local_alignment_tokens is None else local_alignment_tokens | |
| if raw_block_size <= 0 or local_window_tokens <= 0 or alignment <= 0 or alignment % raw_block_size: | |
| raise ValueError('invalid block/window/alignment') | |
| if length == 0: | |
| raise ValueError('decode requires live K/V') | |
| n = selected.shape[-1] | |
| width = n * raw_block_size + local_window_tokens + alignment - 1 | |
| signature = (b, h, width, dim, key.dtype, key.device) | |
| if workspace.get('signature') != signature: | |
| workspace.clear() | |
| workspace.update(signature=signature, | |
| key=torch.empty((b,h,width,dim),dtype=key.dtype,device=key.device), | |
| value=torch.empty((b,h,width,dim),dtype=value.dtype,device=value.device), | |
| mask=torch.empty((b,1,1,width),dtype=torch.bool,device=key.device), | |
| candidates=torch.empty((b,width),dtype=torch.int64,device=key.device), | |
| safe=torch.empty((b,width),dtype=torch.int64,device=key.device)) | |
| start = max(length-local_window_tokens,0) // alignment * alignment | |
| with torch.cuda.device(key.device): | |
| _gather[(b*h,triton.cdiv(width,16))]( | |
| key,value,selected,workspace['key'],workspace['value'],workspace['mask'], | |
| workspace['candidates'],workspace['safe'],length,h,dim,n,width,raw_block_size,start, | |
| *key.stride(),*value.stride(),selected.stride(0),selected.stride(2), | |
| 16,triton.next_power_of_2(dim),num_warps=4) | |
| return workspace['key'],workspace['value'],workspace['mask'] | |