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

# DeepSpeed Training

Microsoft DeepSpeed के साथ बड़े मॉडल को कुशलतापूर्वक प्रशिक्षित करें।

{% 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>`

## DeepSpeed क्या है?

DeepSpeed सक्षम करता है:

* ऐसे मॉडल को प्रशिक्षित करना जो GPU मेमोरी में फिट नहीं होते
* मल्टी-GPU और मल्टी-नोड प्रशिक्षण
* ZeRO अनुकूलन (मेमोरी दक्षता)
* मिक्स्ड प्रिसिजन प्रशिक्षण

## ZeRO चरण

| चरण           | मेमोरी बचत                  | स्पीड          |
| ------------- | --------------------------- | -------------- |
| ZeRO-1        | ऑप्टिमाइज़र स्टेट्स विभाजित | तेज़           |
| ZeRO-2        | + ग्रैडिएंट्स विभाजित       | संतुलित        |
| ZeRO-3        | + पैरामीटर विभाजित          | अधिकतम बचत     |
| ZeRO-Infinity | CPU/NVMe ऑफलोड              | सबसे बड़े मॉडल |

## त्वरित तैनाती

**Docker इमेज:**

```
pytorch/pytorch:2.5.1-cuda12.4-cudnn9-devel
```

**पोर्ट:**

```
22/tcp
```

**कमांड:**

```bash
pip install deepspeed transformers datasets accelerate
```

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

```bash
pip install deepspeed

# इंस्टॉलेशन सत्यापित करें
ds_report
```

## बेसिक प्रशिक्षण

### DeepSpeed कॉन्फ़िग

**ds\_config.json:**

```json
{
    "train_batch_size": 32,
    "gradient_accumulation_steps": 4,
    "optimizer": {
        "type": "AdamW",
        "params": {
            "lr": 1e-4,
            "betas": [0.9, 0.999],
            "eps": 1e-8,
            "weight_decay": 0.01
        }
    },
    "scheduler": {
        "type": "WarmupLR",
        "params": {
            "warmup_min_lr": 0,
            "warmup_max_lr": 1e-4,
            "warmup_num_steps": 100
        }
    },
    "fp16": {
        "enabled": true,
        "loss_scale": 0,
        "initial_scale_power": 16
    },
    "zero_optimization": {
        "stage": 2,
        "contiguous_gradients": true,
        "overlap_comm": true
    }
}
```

### प्रशिक्षण स्क्रिप्ट

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

# आरंभ करें
model = AutoModelForCausalLM.from_pretrained("gpt2")
tokenizer = AutoTokenizer.from_pretrained("gpt2")

# DeepSpeed प्रारंभिकरण
model_engine, optimizer, _, _ = deepspeed.initialize(
    model=model,
    model_parameters=model.parameters(),
    config="ds_config.json"
)

# प्रशिक्षण लूप
for epoch in range(num_epochs):
    for batch in dataloader:
        inputs = tokenizer(batch["text"], return_tensors="pt", padding=True, truncation=True)
        inputs = {k: v.to(model_engine.device) for k, v in inputs.items()}

        outputs = model_engine(**inputs, labels=inputs["input_ids"])
        loss = outputs.loss

        model_engine.backward(loss)
        model_engine.step()
```

## ZeRO स्टेज 2 कॉन्फ़िग

```json
{
    "train_batch_size": "auto",
    "gradient_accumulation_steps": "auto",
    "gradient_clipping": 1.0,
    "fp16": {
        "enabled": true
    },
    "zero_optimization": {
        "stage": 2,
        "allgather_partitions": true,
        "allgather_bucket_size": 2e8,
        "reduce_scatter": true,
        "reduce_bucket_size": 2e8,
        "overlap_comm": true
    }
}
```

## ZeRO स्टेज 3 कॉन्फ़िग

बड़े मॉडलों के लिए:

```json
{
    "train_batch_size": "auto",
    "gradient_accumulation_steps": "auto",
    "fp16": {
        "enabled": true
    },
    "zero_optimization": {
        "stage": 3,
        "offload_optimizer": {
            "device": "cpu",
            "pin_memory": true
        },
        "offload_param": {
            "device": "cpu",
            "pin_memory": true
        },
        "overlap_comm": true,
        "contiguous_gradients": true,
        "sub_group_size": 1e9,
        "reduce_bucket_size": "auto",
        "stage3_prefetch_bucket_size": "auto",
        "stage3_param_persistence_threshold": "auto",
        "stage3_max_live_parameters": 1e9,
        "stage3_max_reuse_distance": 1e9,
        "stage3_gather_16bit_weights_on_model_save": true
    }
}
```

## Hugging Face Transformers के साथ

### Trainer इंटीग्रेशन

```python
from transformers import Trainer, TrainingArguments

training_args = TrainingArguments(
    output_dir="./output",
    per_device_train_batch_size=4,
    gradient_accumulation_steps=4,
    learning_rate=1e-4,
    num_train_epochs=3,
    fp16=True,
    deepspeed="ds_config.json",
    logging_steps=10,
    save_steps=500,
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_dataset,
    tokenizer=tokenizer,
)

trainer.train()
```

## मल्टी-GPU प्रशिक्षण

### लॉन्च कमांड

```bash

# सिंगल नोड, 4 GPUs
deepspeed --num_gpus=4 train.py --deepspeed ds_config.json

# विशेष GPUs
deepspeed --include="localhost:0,1,2,3" train.py --deepspeed ds_config.json
```

### torchrun के साथ

```bash
torchrun --nproc_per_node=4 train.py --deepspeed ds_config.json
```

## मल्टी-नोड प्रशिक्षण

### हॉस्टफाइल

**hostfile:**

```
node1 slots=4
node2 slots=4
```

### लॉन्च

```bash
deepspeed --hostfile=hostfile train.py --deepspeed ds_config.json
```

### SSH सेटअप

```bash

# नोड्स के बीच पासवर्डलेस SSH सुनिश्चित करें
ssh-keygen -t rsa
ssh-copy-id user@node2
```

## मेमोरी-एफिशिएंट कॉन्फ़िग्स

### 24GB GPU पर 7B मॉडल

```json
{
    "bf16": {"enabled": true},
    "zero_optimization": {
        "stage": 3,
        "offload_optimizer": {"device": "cpu"},
        "offload_param": {"device": "cpu"}
    },
    "gradient_checkpointing": true,
    "train_micro_batch_size_per_gpu": 1,
    "gradient_accumulation_steps": 16
}
```

### 24GB GPU पर 13B मॉडल

```json
{
    "bf16": {"enabled": true},
    "zero_optimization": {
        "stage": 3,
        "offload_optimizer": {"device": "cpu"},
        "offload_param": {"device": "cpu"},
        "stage3_param_persistence_threshold": 0
    },
    "gradient_checkpointing": true,
    "train_micro_batch_size_per_gpu": 1,
    "gradient_accumulation_steps": 32
}
```

## ग्रैडिएंट चेकपॉइंटिंग

एक्टिवेशन को फिर से गणना करके मेमोरी बचाएं:

```python
from transformers import AutoModelForCausalLM

model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf")
model.gradient_checkpointing_enable()
```

## चेकपॉइंट्स सेव और लोड करें

### सेव

```python

# DeepSpeed चेकपॉइंटिंग संभालता है
model_engine.save_checkpoint("./checkpoints", tag="step_1000")
```

### लोड

```python
model_engine.load_checkpoint("./checkpoints", tag="step_1000")
```

### HuggingFace फ़ॉर्मेट में सेव करें

```python

# DeepSpeed चेकपॉइंट को HF फॉर्मेट में कनवर्ट करें
from deepspeed.utils.zero_to_fp32 import get_fp32_state_dict_from_zero_checkpoint

state_dict = get_fp32_state_dict_from_zero_checkpoint("./checkpoints/step_1000")
model.load_state_dict(state_dict)
model.save_pretrained("./hf_model")
```

## मॉनिटरिंग

### TensorBoard

```json
{
    "tensorboard": {
        "enabled": true,
        "output_path": "./logs",
        "job_name": "training_run"
    }
}
```

### Weights & Biases

```json
{
    "wandb": {
        "enabled": true,
        "project": "my_project"
    }
}
```

## सामान्य समस्याएँ

### आउट ऑफ़ मेमोरी

```json
// आज़माएं:
{
    "zero_optimization": {
        "stage": 3,
        "offload_optimizer": {"device": "cpu"},
        "offload_param": {"device": "cpu"}
    },
    "train_micro_batch_size_per_gpu": 1
}
```

### धीमा प्रशिक्षण

* CPU ऑफलोडिंग कम करें
* बैच साइज बढ़ाएं
* ZeRO स्टेज 3 की बजाय स्टेज 2 का उपयोग करें

### NCCL त्रुटियाँ

```bash

# एनवायरनमेंट वेरिएबल सेट करें
export NCCL_DEBUG=INFO
export NCCL_IB_DISABLE=1
```

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

| सुझाव                            | प्रभाव        |
| -------------------------------- | ------------- |
| fp16 की बजाय bf16 का उपयोग करें  | बेहतर स्थिरता |
| ग्रैडिएंट चेकपॉइंटिंग सक्षम करें | कम मेमोरी     |
| बैच साइज को ट्यून करें           | बेहतर थ्रूपुट |
| NVMe ऑफलोड का उपयोग करें         | बड़े मॉडल     |

## प्रदर्शन तुलना

| मॉडल | GPUs    | ZeRO स्टेज | प्रशिक्षण गति      |
| ---- | ------- | ---------- | ------------------ |
| 7B   | 1x A100 | ZeRO-3     | \~1000 टोकंस/सेकंड |
| 7B   | 4x A100 | ZeRO-2     | \~4000 टोकंस/सेकंड |
| 13B  | 4x A100 | ZeRO-3     | \~2000 टोकंस/सेकंड |
| 70B  | 8x A100 | ZeRO-3     | \~800 टोकंस/सेकंड  |

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

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

सामान्य 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** टोकन के साथ
* विभिन्न प्रदाताओं के बीच कीमतों की तुलना करें

## अगले कदम

* [LLMs को फाइन-ट्यून करें](/guides/guides_v2-hi/training/finetune-llm.md) - LoRA प्रशिक्षण
* vLLM इनफरेंस - प्रशिक्षित मॉडल को डिप्लॉय करें
* [Hugging Face गाइड](/guides/guides_v2-hi/training/huggingface-transformers.md) - Transformers लाइब्रेरी


---

# 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/training/deepspeed-training.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.
