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

# Gemma 2

Clore.ai GPUs पर Google के Gemma 2 मॉडल कुशलतापूर्वक चलाएँ

{% hint style="info" %}
**नया संस्करण उपलब्ध है!** Google ने जारी किया [**Gemma 3**](/guides/guides_v2-hi/language-models/gemma3.md) मार्च 2025 में — 27B मॉडल Llama 3.1 405B को पछाड़ता है और मूल मल्टीमॉडल समर्थन जोड़ता है। अपग्रेड करने पर विचार करें।
{% endhint %}

कुशल इन्फरेंस के लिए Google के Gemma 2 मॉडल चलाएँ।

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

## CLORE.AI पर किराए पर लेना

1. विज़िट करें [CLORE.AI मार्केटप्लेस](https://clore.ai/marketplace)
2. GPU प्रकार, VRAM और कीमत के अनुसार फ़िल्टर करें
3. चुनें **ऑन-डिमांड** (स्थिर दर) या **स्पॉट** (बोली मूल्य)
4. अपना ऑर्डर कॉन्फ़िगर करें:
   * Docker इमेज चुनें
   * पोर्ट सेट करें (SSH के लिए TCP, वेब UI के लिए HTTP)
   * यदि आवश्यक हो तो environment variables जोड़ें
   * स्टार्टअप कमांड दर्ज करें
5. भुगतान चुनें: **CLORE**, **BTC**या **USDT/USDC**
6. ऑर्डर बनाएं और डिप्लॉयमेंट की प्रतीक्षा करें

### अपने सर्वर तक पहुँचें

* कनेक्शन विवरण यहाँ खोजें **मेरे ऑर्डर**
* वेब इंटरफ़ेस: HTTP पोर्ट URL का उपयोग करें
* SSH: `ssh -p <port> root@<proxy-address>`

## Gemma 2 क्या है?

Google का Gemma 2 प्रदान करता है:

* 2B से 27B पैरामीटर तक के मॉडल
* आकार के अनुपात में उत्कृष्ट प्रदर्शन
* निर्देशों का मजबूत पालन
* कुशल आर्किटेक्चर

## मॉडल वेरिएंट

| मॉडल        | पैरामीटर | VRAM | संदर्भ |
| ----------- | -------- | ---- | ------ |
| Gemma-2-2B  | 2B       | 3GB  | 8K     |
| Gemma-2-9B  | 9B       | 12GB | 8K     |
| Gemma-2-27B | 27B      | 32GB | 8K     |

## त्वरित डिप्लॉय

**Docker इमेज:**

```
pytorch/pytorch:2.11.0-cuda12.8-cudnn9-runtime
```

**पोर्ट:**

```
22/tcp
8000/http
```

**कमांड:**

```bash
pip install vllm && \\
vllm serve google/gemma-2-9b-it --port 8000
```

## अपनी सेवा तक पहुँचना

डिप्लॉयमेंट के बाद, अपना `http_pub` URL यहाँ **मेरे ऑर्डर**:

1. पर जाएँ **मेरे ऑर्डर** पेज
2. अपने ऑर्डर पर क्लिक करें
3. खोजें `http_pub` URL (उदा., `abc123.clorecloud.net`)

उपयोग करें `https://YOUR_HTTP_PUB_URL` की बजाय `localhost` नीचे दिए गए उदाहरणों में।

## Ollama का उपयोग

```bash

# Gemma 2 चलाएँ
ollama run gemma2

# विशिष्ट आकार
ollama run gemma2:2b
ollama run gemma2:9b
ollama run gemma2:27b
```

## इंस्टॉलेशन

```bash
pip install transformers accelerate torch
```

## मूल उपयोग

```python
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

model_id = "google/gemma-2-9b-it"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.bfloat16,
    device_map="auto"
)

messages = [
    {"role": "user", "content": "समझाएँ कि न्यूरल नेटवर्क कैसे सीखते हैं।"}
]

inputs = tokenizer.apply_chat_template(
    messages,
    return_tensors="pt",
    add_generation_prompt=True
).to("cuda")

outputs = model.generate(
    inputs,
    max_new_tokens=512,
    temperature=0.7,
    do_sample=True
)

response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
print(response)
```

## Gemma 2 2B (हल्का)

एज/मोबाइल परिनियोजन के लिए:

```python
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

model_id = "google/gemma-2-2b-it"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.float16,
    device_map="auto"
)

# सरल कार्यों के लिए तेज़ इन्फरेंस
messages = [{"role": "user", "content": "एक वाक्य में सारांश दें: AI उद्योगों को बदल रही है।"}]
```

## Gemma 2 27B (सर्वोत्तम गुणवत्ता)

```python
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
import torch

model_id = "google/gemma-2-27b-it"

# 24GB VRAM में फिट करने के लिए 4-बिट का उपयोग करें
quantization_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_compute_dtype=torch.bfloat16
)

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    quantization_config=quantization_config,
    device_map="auto"
)
```

## vLLM सर्वर

```bash
vllm serve google/gemma-2-9b-it \
    --port 8000 \
    --dtype bfloat16 \
    --max-model-len 8192
```

### OpenAI-संगत API

```python
from openai import OpenAI

client = OpenAI(base_url="http://localhost:8000/v1", api_key="x")

response = client.chat.completions.create(
    model="google/gemma-2-9b-it",
    messages=[
        {"role": "user", "content": "प्रोग्रामिंग पर एक हाइकु लिखें"}
    ],
    temperature=0.8
)

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

## स्ट्रीमिंग

```python
from openai import OpenAI

client = OpenAI(base_url="http://localhost:8000/v1", api_key="x")

stream = client.chat.completions.create(
    model="google/gemma-2-9b-it",
    messages=[{"role": "user", "content": "मुझे एक छोटी कहानी सुनाएँ"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
```

## Gradio इंटरफ़ेस

```python
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

model_id = "google/gemma-2-9b-it"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id, torch_dtype=torch.bfloat16, device_map="auto"
)

def chat(message, history, temperature):
    messages = []
    for h in history:
        messages.append({"role": "user", "content": h[0]})
        messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    inputs = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True).to("cuda")
    outputs = model.generate(inputs, max_new_tokens=512, temperature=temperature, do_sample=True)

    return tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)

demo = gr.ChatInterface(
    fn=chat,
    additional_inputs=[gr.Slider(0.1, 1.5, value=0.7, label="तापमान")],
    title="Gemma 2 चैट"
)

demo.launch(server_name="0.0.0.0", server_port=7860)
```

## बैच प्रोसेसिंग

```python
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

model_id = "google/gemma-2-9b-it"
tokenizer = AutoTokenizer.from_pretrained(model_id, padding_side="left")
tokenizer.pad_token = tokenizer.eos_token

model = AutoModelForCausalLM.from_pretrained(
    model_id, torch_dtype=torch.bfloat16, device_map="auto"
)

prompts = [
    "गुरुत्वाकर्षण को एक वाक्य में समझाएँ।",
    "प्रकाश संश्लेषण क्या है?",
    "मशीन लर्निंग को परिभाषित करें।",
    "प्रकाश की गति क्या है?"
]

messages_batch = [[{"role": "user", "content": p}] for p in prompts]

inputs = tokenizer.apply_chat_template(
    messages_batch,
    return_tensors="pt",
    padding=True,
    add_generation_prompt=True
).to("cuda")

outputs = model.generate(inputs, max_new_tokens=128, pad_token_id=tokenizer.pad_token_id)

for i, output in enumerate(outputs):
    response = tokenizer.decode(output, skip_special_tokens=True)
    print(f"Q: {prompts[i]}")
    print(f"A: {response.split('<start_of_turn>model')[-1].strip()}\n")
```

## प्रदर्शन

| मॉडल                | GPU      | टोकन/सेकंड |
| ------------------- | -------- | ---------- |
| Gemma-2-2B          | RTX 3060 | \~100      |
| Gemma-2-9B          | RTX 3090 | \~60       |
| Gemma-2-9B          | RTX 4090 | \~85       |
| Gemma-2-27B         | A100     | \~45       |
| Gemma-2-27B (4-बिट) | RTX 4090 | \~30       |

## तुलना

| मॉडल         | MMLU  | गुणवत्ता | गति  |
| ------------ | ----- | -------- | ---- |
| Gemma-2-9B   | 71.3% | उत्कृष्ट | तेज़ |
| Llama-3.1-8B | 69.4% | अच्छा    | तेज़ |
| Mistral-7B   | 62.5% | अच्छा    | तेज़ |

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

{% hint style="danger" %}
**CUDA out of memory**
{% endhint %}

27B के लिए - BitsAndBytesConfig के साथ 4-बिट क्वांटाइज़ेशन का उपयोग करें - \`max\_new\_tokens\` कम करें - GPU कैश साफ़ करें: \`torch.cuda.empty\_cache()\`

### धीमी जनरेशन

* उत्पादन परिनियोजन के लिए vLLM का उपयोग करें
* Flash Attention सक्षम करें
* तेज़ इन्फरेंस के लिए 9B मॉडल आज़माएँ

### आउटपुट गुणवत्ता संबंधी समस्याएँ

* निर्देश-ट्यून किया गया संस्करण उपयोग करें (`-it` प्रत्यय)
* तापमान समायोजित करें (0.7-0.9 अनुशंसित)
* संदर्भ के लिए सिस्टम प्रॉम्प्ट जोड़ें

### टोकनाइज़र चेतावनियाँ

* transformers को नवीनतम संस्करण में अपडेट करें
* उपयोग करें `padding_side="left"` बैच इन्फरेंस के लिए

## लागत का अनुमान

CLORE.AI मार्केटप्लेस की सामान्य दरें (2024 तक):

| GPU       | प्रति घंटा दर | प्रति दिन दर | 4-घंटे का सत्र |
| --------- | ------------- | ------------ | -------------- |
| RTX 3060  | \~$0.03       | \~$0.70      | \~$0.12        |
| RTX 3090  | \~$0.06       | \~$1.50      | \~$0.25        |
| RTX 4090  | \~$0.10       | \~$2.30      | \~$0.40        |
| A100 40GB | \~$0.17       | \~$4.00      | \~$0.70        |
| A100 80GB | \~$0.25       | \~$6.00      | \~$1.00        |

*मूल्य प्रदाता और मांग के अनुसार बदलते हैं। देखें* [*CLORE.AI मार्केटप्लेस*](https://clore.ai/marketplace) *वर्तमान दरों के लिए।*

**पैसे बचाएँ:**

* का उपयोग करें **स्पॉट** बाधित किए जा सकने वाले कार्य के लिए मार्केट — लगभग एक-तिहाई सर्वरों की स्पॉट कीमत ऑन-डिमांड से कम होती है (मध्य \~13% छूट), बाकी उससे मेल खाते हैं
* से भुगतान करें **CLORE** टोकन
* विभिन्न प्रदाताओं के बीच कीमतों की तुलना करें

## अगले चरण

* Llama 3.2 - Meta का मॉडल
* Qwen2.5 - अलीबाबा का मॉडल
* vLLM इंफ़ेरेंस - प्रोडक्शन सर्विंग


---

# 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/gemma2.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.
