File size: 7,618 Bytes
077989f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# 🛠️ Guia de Instalação - LoRA Trainer Funcional

## 📋 Pré-requisitos

### Sistema Operacional
- **Linux**: Ubuntu 20.04+ (recomendado)
- **Windows**: Windows 10/11 com WSL2
- **macOS**: macOS 12+ (limitado, sem GPU)

### Hardware
- **GPU**: NVIDIA com 6GB+ VRAM (obrigatório)
- **RAM**: 16GB+ (recomendado)
- **Armazenamento**: 20GB+ livres
- **CPU**: Qualquer CPU moderno

### Software
- **Python**: 3.8 a 3.11
- **CUDA**: 11.8 ou 12.1
- **Git**: Para clonar repositórios

## 🚀 Instalação Local

### Método 1: Instalação Completa

```bash
# 1. Clone o repositório
git clone <repository-url>
cd lora_trainer_hf

# 2. Crie ambiente virtual
python -m venv venv
source venv/bin/activate  # Linux/Mac
# ou
venv\Scripts\activate     # Windows

# 3. Instale dependências
pip install -r requirements.txt

# 4. Execute a aplicação
python app.py
```

### Método 2: Instalação com Conda

```bash
# 1. Crie ambiente conda
conda create -n lora_trainer python=3.10
conda activate lora_trainer

# 2. Instale PyTorch com CUDA
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

# 3. Clone e instale
git clone <repository-url>
cd lora_trainer_hf
pip install -r requirements.txt

# 4. Execute
python app.py
```

## 🐳 Instalação com Docker

### Dockerfile Incluído

```bash
# 1. Build da imagem
docker build -t lora-trainer .

# 2. Execute o container
docker run -p 7860:7860 --gpus all lora-trainer
```

### Docker Compose

```yaml
version: '3.8'
services:
  lora-trainer:
    build: .
    ports:
      - "7860:7860"
    volumes:
      - ./data:/tmp/lora_training
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]
```

## ☁️ Deploy no Hugging Face Spaces

### Configuração do Space

1. **Crie um novo Space**:
   - Vá para [huggingface.co/new-space](https://huggingface.co/new-space)
   - Escolha "Gradio" como SDK
   - Selecione hardware com GPU

2. **Configure o Space**:
   ```yaml
   # space_config.yml
   title: LoRA Trainer Funcional
   emoji: 🎨
   colorFrom: blue
   colorTo: purple
   sdk: gradio
   sdk_version: 4.0.0
   app_file: app.py
   pinned: false
   hardware: t4-medium  # ou a100-large
   ```

3. **Upload dos arquivos**:
   - `app.py`
   - `requirements.txt`
   - `README.md`
   - Pasta `sd-scripts/`

### Configuração de Hardware

| Hardware | VRAM | RAM | Recomendado Para |
|----------|------|-----|------------------|
| CPU Basic | 0GB | 16GB | Apenas teste |
| T4 Small | 16GB | 15GB | Projetos pequenos |
| T4 Medium | 16GB | 30GB | Projetos médios |
| A10G Small | 24GB | 30GB | Projetos grandes |
| A100 Large | 40GB | 80GB | Projetos profissionais |

## 🔧 Configuração de Dependências

### Dependências Principais

```txt
# Core ML
torch>=2.0.0
torchvision>=0.15.0
diffusers>=0.21.0
transformers>=4.25.0
accelerate>=0.20.0

# LoRA Training
safetensors>=0.3.0
huggingface-hub>=0.16.0
xformers>=0.0.20
bitsandbytes>=0.41.0

# Interface
gradio>=4.0.0

# Utilities
Pillow>=9.0.0
opencv-python>=4.7.0
numpy>=1.21.0
toml>=0.10.0
tqdm>=4.64.0
```

### Instalação Manual de Dependências

```bash
# PyTorch (ajuste para sua versão CUDA)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

# Diffusers e Transformers
pip install diffusers transformers accelerate

# Otimização
pip install xformers bitsandbytes

# Interface
pip install gradio

# Utilitários
pip install safetensors huggingface-hub Pillow opencv-python numpy toml tqdm
```

## 🐛 Solução de Problemas de Instalação

### Erro: CUDA não encontrado

```bash
# Verifique instalação CUDA
nvidia-smi
nvcc --version

# Reinstale PyTorch com CUDA
pip uninstall torch torchvision torchaudio
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
```

### Erro: xFormers não compatível

```bash
# Instale versão específica
pip install xformers==0.0.20

# Ou compile do código fonte
pip install -U xformers --index-url https://download.pytorch.org/whl/cu118
```

### Erro: Memória insuficiente

```bash
# Aumente swap (Linux)
sudo fallocate -l 8G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

# Configure variáveis de ambiente
export PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:512
```

### Erro: Dependências conflitantes

```bash
# Limpe cache pip
pip cache purge

# Crie ambiente limpo
python -m venv fresh_env
source fresh_env/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
```

## 🔒 Configuração de Segurança

### Variáveis de Ambiente

```bash
# .env
HUGGINGFACE_TOKEN=your_token_here
WANDB_API_KEY=your_wandb_key
CUDA_VISIBLE_DEVICES=0
PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:512
```

### Limitações de Recursos

```python
# No app.py
import resource

# Limite de memória (8GB)
resource.setrlimit(resource.RLIMIT_AS, (8*1024*1024*1024, -1))

# Limite de processos
resource.setrlimit(resource.RLIMIT_NPROC, (100, -1))
```

## 📊 Verificação da Instalação

### Script de Teste

```python
# test_installation.py
import torch
import diffusers
import transformers
import gradio as gr
import safetensors

print("✅ Verificando instalação...")
print(f"Python: {sys.version}")
print(f"PyTorch: {torch.__version__}")
print(f"CUDA disponível: {torch.cuda.is_available()}")
print(f"GPU: {torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'Não disponível'}")
print(f"Diffusers: {diffusers.__version__}")
print(f"Transformers: {transformers.__version__}")
print(f"Gradio: {gr.__version__}")
print("✅ Instalação verificada!")
```

### Teste de GPU

```python
# test_gpu.py
import torch

if torch.cuda.is_available():
    device = torch.device("cuda")
    x = torch.randn(1000, 1000).to(device)
    y = torch.randn(1000, 1000).to(device)
    z = torch.mm(x, y)
    print(f"✅ GPU funcionando: {torch.cuda.get_device_name(0)}")
    print(f"VRAM total: {torch.cuda.get_device_properties(0).total_memory / 1024**3:.1f} GB")
    print(f"VRAM livre: {torch.cuda.memory_reserved(0) / 1024**3:.1f} GB")
else:
    print("❌ GPU não disponível")
```

## 🚀 Otimização de Performance

### Configurações de Sistema

```bash
# Aumentar limites de arquivo
echo "* soft nofile 65536" >> /etc/security/limits.conf
echo "* hard nofile 65536" >> /etc/security/limits.conf

# Otimizar scheduler
echo "performance" > /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
```

### Configurações PyTorch

```python
# No início do app.py
import torch
torch.backends.cudnn.benchmark = True
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
```

## 📝 Logs e Monitoramento

### Configuração de Logs

```python
import logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('lora_trainer.log'),
        logging.StreamHandler()
    ]
)
```

### Monitoramento de Recursos

```bash
# Instalar htop e nvidia-ml-py
pip install nvidia-ml-py3 psutil

# Monitorar em tempo real
watch -n 1 nvidia-smi
```

## 🔄 Atualizações

### Atualizar Dependências

```bash
# Atualizar requirements
pip install --upgrade -r requirements.txt

# Atualizar kohya-ss
cd sd-scripts
git pull origin main
```

### Backup de Configurações

```bash
# Backup de modelos e configurações
tar -czf backup_$(date +%Y%m%d).tar.gz /tmp/lora_training/
```

---

**Nota**: Para suporte adicional, consulte a documentação oficial do kohya-ss e a comunidade Hugging Face.