# 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)
   * यदि आवश्यक हो तो एनवायरनमेंट वेरिएबल जोड़ें
   * स्टार्टअप कमांड दर्ज करें
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.5.1-cuda12.4-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": "Explain how neural networks learn."}
]

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": "Summarize in one sentence: AI is transforming industries."}]
```

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

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

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

# 24GB VRAM में फिट करने के लिए 4-bit का उपयोग करें
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": "Write a haiku about programming"}
    ],
    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": "Tell me a short story"}],
    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="Temperature")],
    title="Gemma 2 Chat"
)

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 = [
    "Explain gravity in one sentence.",
    "What is photosynthesis?",
    "Define machine learning.",
    "What is the speed of light?"
]

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-bit) | RTX 4090 | \~30       |

## तुलना

| मॉडल         | MMLU  | गुणवत्ता   | स्पीड |
| ------------ | ----- | ---------- | ----- |
| Gemma-2-9B   | 71.3% | बहुत अच्छा | तेज़  |
| Llama-3.1-8B | 69.4% | अच्छा      | तेज़  |
| Mistral-7B   | 62.5% | अच्छा      | तेज़  |

## समस्याओं का निवारण

{% hint style="danger" %}
**CUDA में आउट ऑफ मेमोरी**
{% endhint %}

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

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

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

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

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

### Tokenizer चेतावनियाँ

* 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) *वर्तमान दरों के लिए।*

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

* उपयोग करें **स्पॉट** लचीले वर्कलोड के लिए मार्केट (अक्सर 30-50% सस्ता)
* भुगतान करें **CLORE** टोकन के साथ
* विभिन्न प्रदाताओं के बीच कीमतों की तुलना करें

## अगले कदम

* Llama 3.2 - Meta का मॉडल
* Qwen2.5 - Alibaba का मॉडल
* vLLM Inference - प्रोडक्शन सर्विंग


---

# Agent Instructions: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
