> 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/tgi.md).

# TGI (Text Generation Inference)

Clore.ai GPUs पर प्रोडक्शन LLM सर्विंग के लिए HuggingFace Text Generation Inference (TGI) चलाएँ

Text Generation Inference (TGI) HuggingFace का production-grade LLM serving framework है, जिसे high-throughput और low-latency inference के लिए डिज़ाइन किया गया है। यह Flash Attention 2, continuous batching, PagedAttention, और tensor parallelism को out of the box सपोर्ट करता है — जिससे यह CLORE.AI GPU servers पर बड़े language models को scale पर deploy करने के लिए go-to solution बन जाता है।

{% 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 (Flash Attention के लिए Ampere+) | A100, H100, RTX 4090 |

{% hint style="info" %}
Flash Attention 2 के लिए Ampere architecture या उससे नया (RTX 3000+, A100, H100) चाहिए। पुराने GPUs के लिए, TGI अपने आप standard attention पर वापस चला जाएगा।
{% endhint %}

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

**Docker इमेज:** `ghcr.io/huggingface/text-generation-inference:latest`

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

**Environment Variables:**

| वेरिएबल            | उदाहरण                               | विवरण                                    |
| ------------------ | ------------------------------------ | ---------------------------------------- |
| `MODEL_ID`         | `mistralai/Mistral-7B-Instruct-v0.3` | HuggingFace model ID                     |
| `HF_TOKEN`         | `hf_xxx...`                          | HuggingFace token (gated models के लिए)  |
| `NUM_SHARD`        | `2`                                  | tensor parallelism के लिए GPUs की संख्या |
| `MAX_INPUT_LENGTH` | `4096`                               | अधिकतम input tokens                      |
| `MAX_TOTAL_TOKENS` | `8192`                               | अधिकतम input + output tokens             |
| `QUANTIZE`         | `bitsandbytes-nf4`                   | Quantization method                      |

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

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

पर जाएँ [CLORE.AI मार्केटप्लेस](https://clore.ai/marketplace) और servers को इनके आधार पर फ़िल्टर करें:

* 7B models (full precision) के लिए VRAM ≥ 24 GB
* 7B models (4-bit quantization) के लिए VRAM ≥ 12 GB
* 70B models (full precision, single GPU) के लिए VRAM ≥ 80 GB

### 2. SSH के माध्यम से कनेक्ट करें

आपका order confirm होने के बाद, अपने CLORE.AI dashboard से SSH details का उपयोग करके अपने server से कनेक्ट करें:

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

या अपने CLORE.AI order panel से Web Terminal का उपयोग करें।

### 3. TGI Docker Image खींचें

```bash
docker pull ghcr.io/huggingface/text-generation-inference:latest
```

### 4. Model के साथ TGI लॉन्च करें

**Basic launch (Mistral 7B):**

```bash
docker run -d \\
  --name tgi \
  --gpus all \\
  --shm-size 1g \
  -p 8080:80 \
  -v /root/models:/data \
  -e MODEL_ID=mistralai/Mistral-7B-Instruct-v0.3 \
  ghcr.io/huggingface/text-generation-inference:latest \
  --model-id mistralai/Mistral-7B-Instruct-v0.3 \
  --max-input-length 4096 \
  --max-total-tokens 8192
```

**HuggingFace token के साथ (Llama 3 जैसे gated models के लिए):**

```bash
docker run -d \\
  --name tgi \
  --gpus all \\
  --shm-size 1g \
  -p 8080:80 \
  -v /root/models:/data \
  -e HUGGING_FACE_HUB_TOKEN=hf_your_token_here \
  ghcr.io/huggingface/text-generation-inference:latest \
  --model-id meta-llama/Meta-Llama-3-8B-Instruct \
  --max-input-length 8192 \
  --max-total-tokens 16384
```

**4-bit quantization के साथ (कम VRAM के लिए):**

```bash
docker run -d \\
  --name tgi \
  --gpus all \\
  --shm-size 1g \
  -p 8080:80 \
  -v /root/models:/data \
  ghcr.io/huggingface/text-generation-inference:latest \
  --model-id mistralai/Mixtral-8x7B-Instruct-v0.1 \
  --quantize bitsandbytes-nf4 \
  --max-input-length 4096 \
  --max-total-tokens 8192
```

**Multi-GPU tensor parallelism (70B models के लिए):**

```bash
docker run -d \\
  --name tgi \
  --gpus all \\
  --shm-size 2g \
  -p 8080:80 \
  -v /root/models:/data \
  ghcr.io/huggingface/text-generation-inference:latest \
  --model-id meta-llama/Meta-Llama-3-70B-Instruct \
  --num-shard 2 \
  --max-input-length 8192 \
  --max-total-tokens 16384
```

### 5. जाँच करें कि सर्वर चल रहा है

```bash
# लॉग्स जांचें
docker logs -f tgi

# "Connected" संदेश का इंतज़ार करें, फिर test करें:
curl http://localhost:8080/health
```

अपेक्षित प्रतिक्रिया: `{"status":"ok"}`

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

अपने CLORE.AI order panel में, आप अपना देखेंगे `http_pub` port 8080 के लिए URL। इससे SSH tunneling के बिना browser/API access मिलता है:

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

***

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

### उदाहरण 1: Basic Text Generation

```bash
curl http://localhost:8080/generate \
  -X POST \
  -H 'Content-Type: application/json' \
  -d '{
    "inputs": "फ़्रांस की राजधानी क्या है?",
    "parameters": {
      "max_new_tokens": 100,
      "temperature": 0.7
    }
  }'
```

### उदाहरण 2: Chat Completions (OpenAI-compatible)

TGI OpenAI chat completions API format को सपोर्ट करता है:

```bash
curl http://localhost:8080/v1/chat/completions \\
  -X POST \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "tgi",
    "messages": [
      {"role": "system", "content": "आप एक सहायक सहायक हैं."},
      {"role": "user", "content": "Quantum entanglement को सरल शब्दों में समझाइए।"}
    ],
    "max_tokens": 512,
    "temperature": 0.8,
    "stream": false
  }'
```

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

```bash
curl http://localhost:8080/generate_stream \
  -X POST \
  -H 'Content-Type: application/json' \
  -d '{
    "inputs": "Fibonacci numbers की गणना करने के लिए एक Python function लिखिए:",
    "parameters": {
      "max_new_tokens": 300,
      "temperature": 0.2
    }
  }' \
  --no-buffer
```

### उदाहरण 4: Python Client

```python
from huggingface_hub import InferenceClient

# अपने CLORE.AI http_pub URL से बदलें
client = InferenceClient(model="http://localhost:8080")

# सरल जनरेशन
response = client.text_generation(
    "फ़्रेंच में अनुवाद करें: Hello, how are you?",
    max_new_tokens=100,
    temperature=0.7,
)
print(response)

# चैट
for token in client.chat_completion(
    messages=[{"role": "user", "content": "Machine learning क्या है?"}],
    max_tokens=200,
    stream=True,
):
    print(token.choices[0].delta.content, end="", flush=True)
```

### उदाहरण 5: Batch Requests

```python
import requests

BASE_URL = "http://localhost:8080"  # या आपका CLORE.AI http_pub URL

prompts = [
    "फ़्रेंच क्रांति को 3 वाक्यों में संक्षेप करें।",
    "GPU computing पर एक haiku लिखिए।",
    "C++ की तुलना में Rust के मुख्य लाभ क्या हैं?",
]

results = []
for prompt in prompts:
    response = requests.post(
        f"{BASE_URL}/generate",
        json={"inputs": prompt, "parameters": {"max_new_tokens": 150}},
    )
    results.append(response.json()["generated_text"])

for prompt, result in zip(prompts, results):
    print(f"Prompt: {prompt}\nAnswer: {result}\n{'-'*50}")
```

***

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

### मुख्य CLI Parameters

| पैरामीटर                    | डिफ़ॉल्ट | विवरण                                           |
| --------------------------- | -------- | ----------------------------------------------- |
| `--model-id`                | आवश्यक   | HuggingFace model ID या local path              |
| `--num-shard`               | 1        | GPU shards की संख्या (tensor parallelism)       |
| `--max-concurrent-requests` | 128      | अधिकतम एक साथ अनुरोध                            |
| `--max-input-length`        | 1024     | अधिकतम input token लंबाई                        |
| `--max-total-tokens`        | 2048     | अधिकतम input + output tokens                    |
| `--max-batch-total-tokens`  | auto     | प्रति batch अधिकतम tokens                       |
| `--quantize`                | none     | Quantization: `bitsandbytes-nf4`, `gptq`, `awq` |
| `--dtype`                   | auto     | `float16`, `bfloat16`                           |
| `--trust-remote-code`       | false    | कस्टम model code की अनुमति दें                  |
| `--port`                    | 80       | Server port                                     |

### Local Model का उपयोग

यदि आपके पास local रूप से डाउनलोड किया हुआ model है:

```bash
docker run -d \\
  --name tgi \
  --gpus all \\
  --shm-size 1g \
  -p 8080:80 \
  -v /path/to/your/model:/model \
  ghcr.io/huggingface/text-generation-inference:latest \
  --model-id /model
```

### AWQ Quantization (NF4 से तेज़)

```bash
docker run -d \\
  --name tgi \
  --gpus all \\
  --shm-size 1g \
  -p 8080:80 \
  -v /root/models:/data \
  ghcr.io/huggingface/text-generation-inference:latest \
  --model-id casperhansen/mistral-7b-instruct-v0.2-awq \
  --quantize awq
```

***

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

### 1. Flash Attention 2 सक्षम करें

Flash Attention 2 Ampere+ GPUs (RTX 3000+, A100, H100) पर अपने आप सक्षम हो जाता है। किसी अतिरिक्त configuration की आवश्यकता नहीं है।

### 2. Max Batch Size समायोजित करें

High-throughput scenarios के लिए, batch size बढ़ाएँ:

```bash
--max-batch-total-tokens 32000 \
--max-waiting-tokens 20
```

### 3. Ampere+ GPUs पर bfloat16 का उपयोग करें

```bash
--dtype bfloat16
```

यह float16 की तुलना में अधिक numerically stable है और modern GPUs पर समान रूप से perform करता है।

### 4. Models को Persistent Storage में पहले डाउनलोड करें

```bash
# Server पर, TGI शुरू करने से पहले pre-download करें
pip install huggingface_hub
python -c "
from huggingface_hub import snapshot_download
snapshot_download('mistralai/Mistral-7B-Instruct-v0.3', local_dir='/root/models/mistral-7b')
"
```

फिर restart पर दोबारा डाउनलोड से बचने के लिए local path mount करें।

### 5. GPU Memory Management

RTX 3090/4090 (24GB VRAM) के लिए:

```bash
# float16 में 7B model बिल्कुल फिट बैठता है
--max-total-tokens 8192

# 13B model के लिए quantization चाहिए
--quantize bitsandbytes-nf4
--max-total-tokens 4096
```

### 6. Speculative Decoding

छोटे models को draft के रूप में उपयोग करके तेज़ generation के लिए:

```bash
--speculate 4  # speculative tokens की संख्या
```

***

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

### समस्या: "CUDA out of memory"

```
त्रुटि: CUDA out of memory. X GiB allocate करने की कोशिश की गई
```

**समाधान:** कम करें `--max-total-tokens` या quantization सक्षम करें:

```bash
--quantize bitsandbytes-nf4
--max-total-tokens 4096
```

### समस्या: Model डाउनलोड धीमा है

**समाधान:** HuggingFace mirror का उपयोग करें या pre-download करें:

```bash
# Mirror सेट करें
-e HF_ENDPOINT=https://hf-mirror.com
```

### समस्या: http\_pub के माध्यम से server accessible नहीं है

**समाधान:** सुनिश्चित करें कि port 8080 सही ढंग से mapped है। TGI अंदरूनी रूप से port 80 पर सुनता है, लेकिन आप इसे बाहरी रूप से 8080 पर map करते हैं:

```bash
-p 8080:80  # host:container
```

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

कुछ models (जैसे Falcon, Phi) को custom code की आवश्यकता होती है:

```bash
--trust-remote-code
```

### समस्या: पहली प्रतिक्रिया धीमी है

पहला request model को VRAM में लोड करता है। यह सामान्य है। बाद के requests तेज़ होंगे।

```bash
# लोडिंग प्रगति जाँचें
docker logs -f tgi | grep -E "Connected|Error|Loading"
```

### समस्या: Container तुरंत exit हो जाता है

```bash
# त्रुटियों की जाँच करें
docker logs tgi

# सामान्य सुधार: shared memory बढ़ाएँ
--shm-size 2g
```

***

## लिंक्स

* [GitHub](https://github.com/huggingface/text-generation-inference)
* [दस्तावेज़ीकरण](https://huggingface.co/docs/text-generation-inference)
* [Docker Hub / GHCR](https://github.com/huggingface/text-generation-inference/pkgs/container/text-generation-inference)
* [समर्थित मॉडल](https://huggingface.co/docs/text-generation-inference/supported_models)
* [CLORE.AI मार्केटप्लेस](https://clore.ai/marketplace)

***

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

| उपयोग-प्रकरण        | अनुशंसित GPU     | Clore.ai पर अनुमानित लागत |
| ------------------- | ---------------- | ------------------------- |
| विकास/परीक्षण       | RTX 3090 (24GB)  | $0.07–0.21/gpu/hr         |
| Production (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/tgi.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.
