> For the complete documentation index, see [llms.txt](https://docs.clore.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clore.ai/guides/guides_v2-hi/language-models/sglang.md).

# SGLang

Clore.ai GPUs पर RadixAttention के साथ उच्च-प्रदर्शन LLM सर्विंग के लिए SGLang डिप्लॉय करें

SGLang (Structured Generation Language) एक उच्च-प्रदर्शन LLM सर्विंग फ्रेमवर्क है, जिसे LMSYS टीम ने विकसित किया है, जो Vicuna और Chatbot Arena पर अपने काम के लिए जानी जाती है। इसमें KV cache sharing के लिए RadixAttention, कुशल MoE (Mixture of Experts) समर्थन, और OpenAI-संगत API शामिल है — जिससे यह CLORE.AI GPU सर्वरों पर उपलब्ध सबसे तेज़ ओपन-सोर्स inference engines में से एक बन जाता है।

{% hint style="success" %}
सभी उदाहरण GPU सर्वरों पर चलाए जा सकते हैं, जिन्हें किराए पर लिया गया हो [CLORE.AI मार्केटप्लेस](https://clore.ai/marketplace).
{% endhint %}

## सर्वर आवश्यकताएँ

| पैरामीटर | न्यूनतम                    | अनुशंसित             |
| -------- | -------------------------- | -------------------- |
| RAM      | 16 GB                      | 32 GB+               |
| VRAM     | 8 GB                       | 24 GB+               |
| डिस्क    | 50 GB                      | 200 GB+              |
| GPU      | NVIDIA Turing+ (RTX 2000+) | A100, H100, RTX 4090 |

{% hint style="info" %}
SGLang Ampere+ GPUs पर FlashInfer सक्षम होने पर सबसे अच्छा प्रदर्शन देता है। Mixtral या DeepSeek जैसे MoE मॉडलों के लिए multi-GPU सेटअप की अनुशंसा की जाती है।
{% endhint %}

## CLORE.AI पर त्वरित परिनियोजन

**Docker इमेज:** `lmsysorg/sglang:latest`

**पोर्ट्स:** `22/tcp`, `30000/http`

**Environment Variables:**

| वेरिएबल                | उदाहरण      | विवरण                                 |
| ---------------------- | ----------- | ------------------------------------- |
| `HF_TOKEN`             | `hf_xxx...` | gated models के लिए HuggingFace token |
| `CUDA_VISIBLE_DEVICES` | `0,1`       | उपयोग की जाने वाली GPUs               |

## चरण-दर-चरण सेटअप

### 1. CLORE.AI पर GPU Server किराए पर लें

देखें [CLORE.AI मार्केटप्लेस](https://clore.ai/marketplace) और एक server चुनें:

* **7B मॉडल**: न्यूनतम 16 GB VRAM (RTX 4080, A10)
* **13B मॉडल**: 24 GB VRAM (RTX 3090, RTX 4090, A5000)
* **70B मॉडल**: 80 GB+ VRAM (A100 80GB) या multi-GPU
* **MoE मॉडल (Mixtral 8x7B)**: 48 GB VRAM या 2× 24 GB

### 2. अपने Server में SSH करें

```bash
ssh -p <PORT> root@<SERVER_IP>
```

### 3. SGLang Docker Image Pull करें

```bash
docker pull lmsysorg/sglang:latest
```

### 4. SGLang Server लॉन्च करें

**बेसिक लॉन्च (Llama 3.1 8B):**

```bash
docker run -d \\
  --name sglang \\
  --gpus all \\
  --shm-size 16g \\
  --ipc host \\
  -p 30000:30000 \\
  -v /root/models:/root/.cache/huggingface \\
  lmsysorg/sglang:latest \\
  python3 -m sglang.launch_server \\
    --model-path meta-llama/Meta-Llama-3.1-8B-Instruct \\
    --host 0.0.0.0 \\
    --port 30000
```

**HuggingFace token के साथ:**

```bash
docker run -d \\
  --name sglang \\
  --gpus all \\
  --shm-size 16g \\
  --ipc host \\
  -p 30000:30000 \\
  -v /root/models:/root/.cache/huggingface \\
  -e HF_TOKEN=hf_your_token_here \\
  lmsysorg/sglang:latest \\
  python3 -m sglang.launch_server \\
    --model-path meta-llama/Meta-Llama-3.1-8B-Instruct \\
    --host 0.0.0.0 \\
    --port 30000 \\
    --dtype bfloat16
```

**multi-GPU पर Qwen2.5 72B:**

```bash
docker run -d \\
  --name sglang \\
  --gpus all \\
  --shm-size 32g \\
  --ipc host \\
  -p 30000:30000 \\
  -v /root/models:/root/.cache/huggingface \\
  lmsysorg/sglang:latest \\
  python3 -m sglang.launch_server \\
    --model-path Qwen/Qwen2.5-72B-Instruct \\
    --host 0.0.0.0 \\
    --port 30000 \\
    --tp 2 \\
    --dtype bfloat16
```

**DeepSeek-V2 (MoE model):**

```bash
docker run -d \\
  --name sglang \\
  --gpus all \\
  --shm-size 32g \\
  --ipc host \\
  -p 30000:30000 \\
  -v /root/models:/root/.cache/huggingface \\
  lmsysorg/sglang:latest \\
  python3 -m sglang.launch_server \\
    --model-path deepseek-ai/DeepSeek-V2-Lite-Chat \\
    --host 0.0.0.0 \\
    --port 30000 \\
    --trust-remote-code \
    --tp 1
```

### 5. Server Health जांचें

```bash
# लॉग देखें
docker logs -f sglang

# Health check (model को load होने में लगभग 2-3 मिनट प्रतीक्षा करें)
curl http://localhost:30000/health

# मॉडल जानकारी प्राप्त करें
curl http://localhost:30000/get_model_info
```

### 6. CLORE.AI Proxy के माध्यम से बाहर से पहुँचें

आपका CLORE.AI dashboard एक `http_pub` port 30000 के लिए URL प्रदान करता है:

```
https://<order-id>-30000.clore.ai/
```

किसी भी OpenAI-संगत client में इस URL को अपना base URL के रूप में उपयोग करें।

***

## उपयोग के उदाहरण

### उदाहरण 1: OpenAI-संगत Chat Completions

```bash
curl http://localhost:30000/v1/chat/completions \\
  -X POST \\
  -H 'Content-Type: application/json' \\
  -d '{
    "model": "meta-llama/Meta-Llama-3.1-8B-Instruct",
    "messages": [
      {"role": "system", "content": "आप एक सहायक coding assistant हैं."},
      {"role": "user", "content": "Python में quicksort implementation लिखें."}
    ],
    "max_tokens": 512,
    "temperature": 0.2
  }'
```

### उदाहरण 2: Streaming Response

```bash
curl http://localhost:30000/v1/chat/completions \\
  -X POST \\
  -H 'Content-Type: application/json' \\
  -d '{
    "model": "meta-llama/Meta-Llama-3.1-8B-Instruct",
    "messages": [
      {"role": "user", "content": "Transformer attention कैसे काम करता है, समझाइए."}
    ],
    "max_tokens": 800,
    "stream": true
  }' \\
  --no-buffer
```

### उदाहरण 3: Python OpenAI Client

```python
from openai import OpenAI

# अपने CLORE.AI SGLang server की ओर इंगित करें
client = OpenAI(
    base_url="http://localhost:30000/v1",
    api_key="none",  # SGLang को डिफ़ॉल्ट रूप से auth की आवश्यकता नहीं होती
)

response = client.chat.completions.create(
    model="meta-llama/Meta-Llama-3.1-8B-Instruct",
    messages=[
        {"role": "system", "content": "आप एक data science expert हैं."},
        {"role": "user", "content": "Gradient boosting क्या है?"},
    ],
    max_tokens=400,
    temperature=0.7,
)

print(response.choices[0].message.content)
```

### उदाहरण 4: SGLang Native API के साथ Batch Inference

SGLang का native API अतिरिक्त नियंत्रण प्रदान करता है:

```python
import requests

# Completions उत्पन्न करें
response = requests.post(
    "http://localhost:30000/generate",
    json={
        "text": "AI का भविष्य है",
        "sampling_params": {
            "max_new_tokens": 200,
            "temperature": 0.8,
            "top_p": 0.95,
        },
    },
)
print(response.json()["text"])
```

### उदाहरण 5: सीमित JSON आउटपुट

SGLang संरचित आउटपुट generation का समर्थन करता है:

```python
import requests

schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer"},
        "city": {"type": "string"},
    },
    "required": ["name", "age", "city"],
}

response = requests.post(
    "http://localhost:30000/generate",
    json={
        "text": "जानकारी निकालें: John Smith, 35 years old, lives in New York.",
        "sampling_params": {
            "max_new_tokens": 100,
            "temperature": 0.0,
        },
        "json_schema": schema,
    },
)
print(response.json()["text"])
# आउटपुट: {"name": "John Smith", "age": 35, "city": "New York"}
```

***

## कॉन्फ़िगरेशन

### मुख्य लॉन्च पैरामीटर

| पैरामीटर                | डिफ़ॉल्ट      | विवरण                                         |
| ----------------------- | ------------- | --------------------------------------------- |
| `--model-path`          | आवश्यक        | HuggingFace model ID या local path            |
| `--host`                | `127.0.0.1`   | Bind host (उपयोग करें `0.0.0.0` बाहरी के लिए) |
| `--port`                | `30000`       | Server port                                   |
| `--tp`                  | `1`           | Tensor parallelism degree (num GPUs)          |
| `--dp`                  | `1`           | Data parallelism degree                       |
| `--dtype`               | `auto`        | `float16`, `bfloat16`, `float32`              |
| `--mem-fraction-static` | `0.88`        | KV cache के लिए VRAM का अंश                   |
| `--max-prefill-tokens`  | auto          | एक prefill चरण में अधिकतम tokens              |
| `--context-length`      | मॉडल max      | अधिकतम context length को override करें        |
| `--trust-remote-code`   | false         | custom model code की अनुमति दें               |
| `--quantization`        | none          | `awq`, `gptq`, `fp8`                          |
| `--load-format`         | `auto`        | `auto`, `pt`, `safetensors`                   |
| `--tokenizer-path`      | model के समान | Custom tokenizer path                         |

### Quantization विकल्प

**AWQ (गति के लिए अनुशंसित):**

```bash
python3 -m sglang.launch_server \\
  --model-path casperhansen/mistral-7b-instruct-v0.2-awq \\
  --quantization awq \\
  --host 0.0.0.0 \\
  --port 30000
```

**FP8 (H100/A100 के लिए):**

```bash
python3 -m sglang.launch_server \\
  --model-path meta-llama/Meta-Llama-3.1-8B-Instruct \\
  --quantization fp8 \
  --host 0.0.0.0 \\
  --port 30000
```

***

## प्रदर्शन सुझाव

### 1. RadixAttention — मुख्य लाभ

SGLang का RadixAttention साझा prompt prefixes के लिए KV cache को स्वचालित रूप से पुन: उपयोग करता है। यह विशेष रूप से इनके लिए शक्तिशाली है:

* लंबे system prompts वाले chatbots
* बार-बार दोहराए जाने वाले context वाले RAG applications
* एक ही prefix साझा करने वाले Batch API calls

कोई अतिरिक्त configuration आवश्यक नहीं — यह हमेशा सक्षम रहता है।

### 2. KV Cache Size बढ़ाएँ

```bash
--mem-fraction-static 0.90  # KV cache के लिए VRAM का 90% उपयोग करें
```

बहुत अधिक न जाएँ — model weights के लिए जगह छोड़ें।

### 3. लंबे contexts के लिए Chunked Prefill

```bash
--chunked-prefill-size 4096  # लंबे prompts को chunks में process करें
```

### 4. FlashInfer Backend सक्षम करें

SGLang उपलब्ध होने पर FlashInfer का स्वचालित रूप से उपयोग करता है (Ampere+ GPUs):

```bash
--attention-backend flashinfer
```

### 5. Multi-GPU Tensor Parallelism

उन मॉडलों के लिए जो एक single GPU पर fit नहीं होते:

```bash
--tp 4  # 4 GPUs का उपयोग करें
```

प्रत्येक GPU के पास model के एक shard के लिए पर्याप्त VRAM होना चाहिए।

### 6. Throughput बनाम Latency के लिए Tune करें

**कम latency (single user):**

```bash
--max-running-requests 4
```

**उच्च throughput (कई users):**

```bash
--max-running-requests 64 \\
--schedule-policy lpm  # Longest Prefix Match scheduling
```

***

## समस्या निवारण

### समस्या: "torch.cuda.OutOfMemoryError"

```
torch.cuda.OutOfMemoryError: CUDA मेमोरी समाप्त
```

**समाधान:** मेमोरी fraction कम करें या quantization का उपयोग करें:

```bash
--mem-fraction-static 0.80
# या
--quantization awq
```

### समस्या: Server शुरू नहीं होता (लोडिंग पर अटक जाता है)

```bash
# CUDA उपलब्धता जांचें
docker exec -it sglang nvidia-smi

# model download progress जांचें
docker logs -f sglang 2>&1 | tail -50
```

### समस्या: "trust\_remote\_code required"

जोड़ें `--trust-remote-code` custom architectures (DeepSeek, Falcon, आदि) वाले मॉडलों के लिए launch command में।

### समस्या: MoE models पर धीमी generation

MoE models (Mixtral, DeepSeek) memory-bandwidth bound होते हैं। सुनिश्चित करें कि आप उपयोग कर रहे हैं:

```bash
--dtype bfloat16  # MoE के लिए float16 से बेहतर
--tp 2            # उपलब्ध होने पर GPUs में विभाजित करें
```

### समस्या: Context length errors

```bash
# context length override करें
--context-length 32768
```

### समस्या: Port 30000 accessible नहीं है

सत्यापित करें कि port आपकी CLORE.AI order configuration में exposed है। अपने order dashboard में http\_pub URL जांचें, localhost नहीं।

***

## लिंक्स

* [GitHub](https://github.com/sgl-project/sglang)
* [दस्तावेज़ीकरण](https://sgl-project.github.io/start/install.html)
* [Docker Hub](https://hub.docker.com/r/lmsysorg/sglang)
* [समर्थित मॉडल](https://github.com/sgl-project/sglang?tab=readme-ov-file#supported-models)
* [CLORE.AI मार्केटप्लेस](https://clore.ai/marketplace)

***

## Clore.ai GPU अनुशंसाएँ

| उपयोग-प्रकरण     | अनुशंसित GPU     | Clore.ai पर अनुमानित लागत |
| ---------------- | ---------------- | ------------------------- |
| विकास/परीक्षण    | RTX 3090 (24GB)  | $0.07–0.21/gpu/hr         |
| उत्पादन (7B–13B) | RTX 4090 (24GB)  | $0.14–0.42/gpu/hr         |
| बड़े मॉडल (70B+) | A100 80GB / H100 | \~$1.04/gpu/hr            |

> 💡 इस गाइड के सभी उदाहरण [Clore.ai](https://clore.ai/marketplace) GPU servers पर तैनात किए जा सकते हैं। उपलब्ध GPUs ब्राउज़ करें और घंटे के हिसाब से किराए पर लें — कोई प्रतिबद्धता नहीं, पूर्ण root access.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-hi/language-models/sglang.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
