v14
Browse files- README.md +7 -12
- dataset_fromfolder.py → dataset.py +92 -76
- model_index.json +2 -2
- pipeline_sdxs.py +1 -39
- promo.png +2 -2
- requirements.txt +0 -3
- micro/config.json → result_grid.png +2 -2
- scheduler/scheduler_config.json +2 -2
- text_encoder/config.json +2 -2
- text_projector/config.json → text_encoder/generation_config.json +2 -2
- text_encoder/model.fp16.safetensors +0 -3
- text_encoder/model.safetensors +3 -0
- vae/config-orig-wrong.json → tokenizer/added_tokens.json +2 -2
- tokenizer/chat_template.jinja +89 -0
- tokenizer/merges.txt +0 -0
- tokenizer/special_tokens_map.json +2 -2
- tokenizer/tokenizer.json +2 -2
- tokenizer/tokenizer_config.json +2 -2
- text_projector/model.safetensors → tokenizer/vocab.json +2 -2
- train.py +319 -333
- train_flow_test.py +0 -770
- unet/config.json +2 -2
- unet/diffusion_pytorch_model.fp16.safetensors +0 -3
- {micro → unet}/diffusion_pytorch_model.safetensors +2 -2
- vae/config-Copy1.1json +0 -37
- vae/config.json +2 -2
- vae/diffusion_pytorch_model.fp16.safetensors +0 -3
- result_grid.jpg → vae/diffusion_pytorch_model.safetensors +2 -2
README.md
CHANGED
|
@@ -7,16 +7,15 @@ pipeline_tag: text-to-image
|
|
| 7 |
|
| 8 |
*XS Size, Excess Quality*
|
| 9 |
|
| 10 |
-
At AiArtLab, we strive to create a free, compact
|
| 11 |
|
| 12 |
- We use U-Net for its high efficiency.
|
| 13 |
-
- We have chosen the
|
| 14 |
-
- We
|
| 15 |
-
- The model was trained (~
|
| 16 |
|
| 17 |
### Model Limitations:
|
| 18 |
- Limited concept coverage due to the small dataset.
|
| 19 |
-
- The Image2Image functionality requires further training.
|
| 20 |
|
| 21 |
## Acknowledgments
|
| 22 |
- **[Stan](https://t.me/Stangle)** — Key investor. Thank you for believing in us when others called it madness.
|
|
@@ -26,10 +25,6 @@ At AiArtLab, we strive to create a free, compact (1.7b) and fast (3 sec/image) m
|
|
| 26 |
## Datasets
|
| 27 |
- **[CaptionEmporium](https://huggingface.co/CaptionEmporium)**
|
| 28 |
|
| 29 |
-
## Training budget
|
| 30 |
-
|
| 31 |
-
Around ~$1k for now, but research budget ~$10k
|
| 32 |
-
|
| 33 |
## Donations
|
| 34 |
|
| 35 |
Please contact with us if you may provide some GPU's or money on training
|
|
@@ -42,7 +37,7 @@ BTC: 3JHv9Hb8kEW8zMAccdgCdZGfrHeMhH1rpN
|
|
| 42 |
|
| 43 |
[recoilme](https://t.me/recoilme)
|
| 44 |
|
| 45 |
-
Train status, in progress:
|
| 46 |
|
| 47 |

|
| 48 |
|
|
@@ -79,7 +74,7 @@ def encode_prompt(prompt, negative_prompt, device, dtype):
|
|
| 79 |
negative_prompt,
|
| 80 |
return_tensors="pt",
|
| 81 |
padding="max_length",
|
| 82 |
-
max_length=
|
| 83 |
truncation=True,
|
| 84 |
).to(device)
|
| 85 |
negative_embeddings = text_model.encode_texts(negative_inputs.input_ids, negative_inputs.attention_mask)
|
|
@@ -141,7 +136,7 @@ if __name__ == "__main__":
|
|
| 141 |
scheduler = DDPMScheduler.from_pretrained(pipeid, subfolder="scheduler")
|
| 142 |
|
| 143 |
|
| 144 |
-
height, width =
|
| 145 |
num_inference_steps = 40
|
| 146 |
output_folder, project_name = "samples", "sdxs"
|
| 147 |
latents = generate_latents(
|
|
|
|
| 7 |
|
| 8 |
*XS Size, Excess Quality*
|
| 9 |
|
| 10 |
+
At AiArtLab, we strive to create a free, compact and fast model that can be trained on consumer graphics cards.
|
| 11 |
|
| 12 |
- We use U-Net for its high efficiency.
|
| 13 |
+
- We have chosen the Qwen0.6b wich support 100+ languages.
|
| 14 |
+
- We train new SOTA 16ch Simple VAE, which preserves details and anatomy.
|
| 15 |
+
- The model was trained (~2 month on 4xRTX5090) on approximately 1+ million images with various resolutions and styles, including anime and realistic photos.
|
| 16 |
|
| 17 |
### Model Limitations:
|
| 18 |
- Limited concept coverage due to the small dataset.
|
|
|
|
| 19 |
|
| 20 |
## Acknowledgments
|
| 21 |
- **[Stan](https://t.me/Stangle)** — Key investor. Thank you for believing in us when others called it madness.
|
|
|
|
| 25 |
## Datasets
|
| 26 |
- **[CaptionEmporium](https://huggingface.co/CaptionEmporium)**
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
## Donations
|
| 29 |
|
| 30 |
Please contact with us if you may provide some GPU's or money on training
|
|
|
|
| 37 |
|
| 38 |
[recoilme](https://t.me/recoilme)
|
| 39 |
|
| 40 |
+
Train status, in progress:
|
| 41 |
|
| 42 |

|
| 43 |
|
|
|
|
| 74 |
negative_prompt,
|
| 75 |
return_tensors="pt",
|
| 76 |
padding="max_length",
|
| 77 |
+
max_length=150,
|
| 78 |
truncation=True,
|
| 79 |
).to(device)
|
| 80 |
negative_embeddings = text_model.encode_texts(negative_inputs.input_ids, negative_inputs.attention_mask)
|
|
|
|
| 136 |
scheduler = DDPMScheduler.from_pretrained(pipeid, subfolder="scheduler")
|
| 137 |
|
| 138 |
|
| 139 |
+
height, width = 640, 576
|
| 140 |
num_inference_steps = 40
|
| 141 |
output_folder, project_name = "samples", "sdxs"
|
| 142 |
latents = generate_latents(
|
dataset_fromfolder.py → dataset.py
RENAMED
|
@@ -1,34 +1,34 @@
|
|
| 1 |
# pip install flash-attn --no-build-isolation
|
| 2 |
-
from datasets import Dataset, load_from_disk, concatenate_datasets
|
| 3 |
-
from diffusers import AutoencoderKL
|
| 4 |
-
from torchvision.transforms import Resize, ToTensor, Normalize, Compose, InterpolationMode, Lambda
|
| 5 |
-
from transformers import AutoModel, AutoImageProcessor, AutoTokenizer
|
| 6 |
import torch
|
| 7 |
import os
|
| 8 |
import gc
|
| 9 |
import numpy as np
|
| 10 |
-
from PIL import Image
|
| 11 |
-
from tqdm import tqdm
|
| 12 |
import random
|
| 13 |
import json
|
| 14 |
import shutil
|
| 15 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
from datetime import timedelta
|
| 17 |
|
| 18 |
# ---------------- 1️⃣ Настройки ----------------
|
| 19 |
dtype = torch.float16
|
| 20 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 21 |
batch_size = 5
|
| 22 |
-
min_size = 320 #192
|
| 23 |
-
max_size =
|
| 24 |
-
step = 64
|
| 25 |
-
img_share = 0.05
|
| 26 |
empty_share = 0.05
|
| 27 |
limit = 0
|
| 28 |
-
textemb_full = False
|
| 29 |
# Основная процедура обработки
|
| 30 |
-
folder_path = "/workspace/
|
| 31 |
-
save_path = "/workspace/
|
| 32 |
os.makedirs(save_path, exist_ok=True)
|
| 33 |
|
| 34 |
# Функция для очистки CUDA памяти
|
|
@@ -42,15 +42,32 @@ def clear_cuda_memory():
|
|
| 42 |
# ---------------- 2️⃣ Загрузка моделей ----------------
|
| 43 |
def load_models():
|
| 44 |
print("Загрузка моделей...")
|
| 45 |
-
vae = AutoencoderKL.from_pretrained("/
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# ---------------- 3️⃣ Трансформации ----------------
|
| 55 |
def get_image_transform(min_size=256, max_size=512, step=64):
|
| 56 |
def transform(img, dry_run=False):
|
|
@@ -107,30 +124,49 @@ def get_image_transform(min_size=256, max_size=512, step=64):
|
|
| 107 |
return transform
|
| 108 |
|
| 109 |
# ---------------- 4️⃣ Функции обработки ----------------
|
| 110 |
-
def
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
with torch.inference_mode():
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
|
|
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
text_tokenized = tokenizer(texts, return_tensors="pt", padding="max_length",
|
| 121 |
-
max_length=512,
|
| 122 |
-
truncation=True).to(device)
|
| 123 |
-
text_embeddings = model.encode_texts(text_tokenized.input_ids, text_tokenized.attention_mask)
|
| 124 |
-
return text_embeddings.unsqueeze(1).cpu().numpy()
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
def clean_label(label):
|
| 136 |
label = label.replace("Image 1", "").replace("Image 2", "").replace("Image 3", "").replace("Image 4", "")
|
|
@@ -186,54 +222,34 @@ def encode_to_latents(images, texts):
|
|
| 186 |
|
| 187 |
# Создаём батч
|
| 188 |
batch_tensor = torch.stack(transformed_tensors).to(device, dtype)
|
|
|
|
|
|
|
| 189 |
|
| 190 |
# Кодируем батч
|
| 191 |
with torch.no_grad():
|
| 192 |
posteriors = vae.encode(batch_tensor).latent_dist.mode()
|
| 193 |
-
latents = (posteriors -
|
| 194 |
-
|
| 195 |
-
latents_np = latents.cpu().numpy()
|
| 196 |
-
|
| 197 |
-
# Проверка однородности форм
|
| 198 |
-
base_shape = latents_np.shape[1:] # Форма без батча
|
| 199 |
-
valid_indices = []
|
| 200 |
-
valid_latents = []
|
| 201 |
|
| 202 |
-
|
| 203 |
-
if latent.shape != base_shape:
|
| 204 |
-
print(f"❌ Несоответствие формы в индексе {idx}: {latent.shape} vs {base_shape}")
|
| 205 |
-
continue
|
| 206 |
-
valid_indices.append(idx)
|
| 207 |
-
valid_latents.append(latent)
|
| 208 |
|
| 209 |
-
# Фильтруем данные
|
| 210 |
-
valid_pil = [pil_images[i] for i in valid_indices]
|
| 211 |
-
valid_widths = [widths[i] for i in valid_indices]
|
| 212 |
-
valid_heights = [heights[i] for i in valid_indices]
|
| 213 |
-
|
| 214 |
# Обрабатываем тексты
|
| 215 |
-
text_labels = [clean_label(
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
else:
|
| 220 |
-
model_prompts, text_labels = process_labels_for_guidance(text_labels, empty_share)
|
| 221 |
-
if textemb_full:
|
| 222 |
-
embeddings = encode_texts_batch_full(model_prompts, tokenizer, model)
|
| 223 |
-
else:
|
| 224 |
-
embeddings = encode_texts_batch(model_prompts, tokenizer, model)
|
| 225 |
|
| 226 |
return {
|
| 227 |
-
"vae":
|
| 228 |
"embeddings": embeddings,
|
| 229 |
"text": text_labels,
|
| 230 |
-
"width":
|
| 231 |
-
"height":
|
| 232 |
}
|
| 233 |
|
| 234 |
except Exception as e:
|
| 235 |
print(f"Критическая ошибка в encode_to_latents: {e}")
|
| 236 |
raise
|
|
|
|
| 237 |
|
| 238 |
# ---------------- 5️⃣ Обработка папки с изображениями и текстами ----------------
|
| 239 |
def process_folder(folder_path, limit=None):
|
|
@@ -282,7 +298,7 @@ def process_folder(folder_path, limit=None):
|
|
| 282 |
print(f"Найдено {len(image_paths)} изображений с текстовыми описаниями")
|
| 283 |
return image_paths, text_paths, width, height
|
| 284 |
|
| 285 |
-
def process_in_chunks(image_paths, text_paths, width, height, chunk_size=
|
| 286 |
total_files = len(image_paths)
|
| 287 |
start_time = time.time()
|
| 288 |
chunks = range(0, total_files, chunk_size)
|
|
@@ -373,7 +389,7 @@ image_paths, text_paths, width, height = process_folder(folder_path,limit)
|
|
| 373 |
print(f"Всего найдено {len(image_paths)} изображений")
|
| 374 |
|
| 375 |
# Обработка с чанкованием
|
| 376 |
-
process_in_chunks(image_paths, text_paths, width, height, chunk_size=
|
| 377 |
|
| 378 |
# Объединение чанков в финальный датасет
|
| 379 |
combine_chunks(temp_path, save_path)
|
|
|
|
| 1 |
# pip install flash-attn --no-build-isolation
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
import os
|
| 4 |
import gc
|
| 5 |
import numpy as np
|
|
|
|
|
|
|
| 6 |
import random
|
| 7 |
import json
|
| 8 |
import shutil
|
| 9 |
import time
|
| 10 |
+
|
| 11 |
+
from datasets import Dataset, load_from_disk, concatenate_datasets
|
| 12 |
+
from diffusers import AutoencoderKL,AutoencoderKLWan
|
| 13 |
+
from torchvision.transforms import Resize, ToTensor, Normalize, Compose, InterpolationMode, Lambda
|
| 14 |
+
from transformers import AutoModel, AutoImageProcessor, AutoTokenizer, AutoModelForCausalLM
|
| 15 |
+
from typing import Dict, List, Tuple, Optional, Any
|
| 16 |
+
from PIL import Image
|
| 17 |
+
from tqdm import tqdm
|
| 18 |
from datetime import timedelta
|
| 19 |
|
| 20 |
# ---------------- 1️⃣ Настройки ----------------
|
| 21 |
dtype = torch.float16
|
| 22 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 23 |
batch_size = 5
|
| 24 |
+
min_size = 320 #192 #256 #192
|
| 25 |
+
max_size = 640 #384 #256 #384
|
| 26 |
+
step = 64 #64
|
|
|
|
| 27 |
empty_share = 0.05
|
| 28 |
limit = 0
|
|
|
|
| 29 |
# Основная процедура обработки
|
| 30 |
+
folder_path = "/workspace/d23" #alchemist"
|
| 31 |
+
save_path = "/workspace/d23_640" #"alchemist"
|
| 32 |
os.makedirs(save_path, exist_ok=True)
|
| 33 |
|
| 34 |
# Функция для очистки CUDA памяти
|
|
|
|
| 42 |
# ---------------- 2️⃣ Загрузка моделей ----------------
|
| 43 |
def load_models():
|
| 44 |
print("Загрузка моделей...")
|
| 45 |
+
vae = AutoencoderKL.from_pretrained("AiArtLab/sdxs3d",subfolder="vae",torch_dtype=dtype).to(device).eval()
|
| 46 |
+
|
| 47 |
+
model_name = "Qwen/Qwen3-0.6B"
|
| 48 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 49 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 50 |
+
model_name,
|
| 51 |
+
torch_dtype=dtype,
|
| 52 |
+
device_map=device
|
| 53 |
+
).eval()
|
| 54 |
+
#tokenizer = AutoTokenizer.from_pretrained('Qwen/Qwen3-Embedding-0.6B', padding_side='left')
|
| 55 |
+
#model = AutoModel.from_pretrained('Qwen/Qwen3-Embedding-0.6B').to("cuda")
|
| 56 |
+
return vae, model, tokenizer
|
| 57 |
+
|
| 58 |
+
vae, model, tokenizer = load_models()
|
| 59 |
+
|
| 60 |
+
shift_factor = getattr(vae.config, "shift_factor", 0.0)
|
| 61 |
+
if shift_factor is None:
|
| 62 |
+
shift_factor = 0.0
|
| 63 |
+
|
| 64 |
+
scaling_factor = getattr(vae.config, "scaling_factor", 1.0)
|
| 65 |
+
if scaling_factor is None:
|
| 66 |
+
scaling_factor = 1.0
|
| 67 |
+
|
| 68 |
+
latents_mean = getattr(vae.config, "latents_mean", None)
|
| 69 |
+
latents_std = getattr(vae.config, "latents_std", None)
|
| 70 |
+
|
| 71 |
# ---------------- 3️⃣ Трансформации ----------------
|
| 72 |
def get_image_transform(min_size=256, max_size=512, step=64):
|
| 73 |
def transform(img, dry_run=False):
|
|
|
|
| 124 |
return transform
|
| 125 |
|
| 126 |
# ---------------- 4️⃣ Функции обработки ----------------
|
| 127 |
+
def last_token_pool(last_hidden_states: torch.Tensor,
|
| 128 |
+
attention_mask: torch.Tensor) -> torch.Tensor:
|
| 129 |
+
# Определяем, есть ли left padding
|
| 130 |
+
left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
|
| 131 |
+
if left_padding:
|
| 132 |
+
return last_hidden_states[:, -1]
|
| 133 |
+
else:
|
| 134 |
+
sequence_lengths = attention_mask.sum(dim=1) - 1
|
| 135 |
+
batch_size = last_hidden_states.shape[0]
|
| 136 |
+
return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
|
| 137 |
+
|
| 138 |
+
def encode_texts_batch(texts, tokenizer, model, device="cuda", max_length=150, normalize=False):
|
| 139 |
with torch.inference_mode():
|
| 140 |
+
# Токенизация
|
| 141 |
+
batch = tokenizer(
|
| 142 |
+
texts,
|
| 143 |
+
return_tensors="pt",
|
| 144 |
+
padding="max_length",
|
| 145 |
+
truncation=True,
|
| 146 |
+
max_length=max_length
|
| 147 |
+
).to(device)
|
| 148 |
|
| 149 |
+
# Прогон через модель
|
| 150 |
+
#outputs = model(**batch)
|
| 151 |
|
| 152 |
+
# Пулинг по last token
|
| 153 |
+
#embeddings = last_token_pool(outputs.last_hidden_state, batch["attention_mask"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
+
# L2-нормализация (опционально, обычно нужна для семантического поиска)
|
| 156 |
+
#if normalize:
|
| 157 |
+
# embeddings = F.normalize(embeddings, p=2, dim=1)
|
| 158 |
+
|
| 159 |
+
# Прогон через базовую модель (внутри CausalLM)
|
| 160 |
+
outputs = model.model(**batch, output_hidden_states=True)
|
| 161 |
+
|
| 162 |
+
# Берем последний слой (эмбеддинги всех токенов)
|
| 163 |
+
hidden_states = outputs.hidden_states[-1] # [B, L, D]
|
| 164 |
+
|
| 165 |
+
# Можно применить нормализацию по каждому токену (как в CLIP)
|
| 166 |
+
if normalize:
|
| 167 |
+
hidden_states = F.normalize(hidden_states, p=2, dim=-1)
|
| 168 |
+
|
| 169 |
+
return hidden_states.cpu().numpy() # embeddings.unsqueeze(1).cpu().numpy()
|
| 170 |
|
| 171 |
def clean_label(label):
|
| 172 |
label = label.replace("Image 1", "").replace("Image 2", "").replace("Image 3", "").replace("Image 4", "")
|
|
|
|
| 222 |
|
| 223 |
# Создаём батч
|
| 224 |
batch_tensor = torch.stack(transformed_tensors).to(device, dtype)
|
| 225 |
+
if batch_tensor.ndim==5:
|
| 226 |
+
batch_tensor = batch_tensor.unsqueeze(2) # [B, C, 1, H, W]
|
| 227 |
|
| 228 |
# Кодируем батч
|
| 229 |
with torch.no_grad():
|
| 230 |
posteriors = vae.encode(batch_tensor).latent_dist.mode()
|
| 231 |
+
latents = (posteriors - shift_factor) / scaling_factor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
+
latents_np = latents.to(dtype).cpu().numpy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
# Обрабатываем тексты
|
| 236 |
+
text_labels = [clean_label(text) for text in texts]
|
| 237 |
+
|
| 238 |
+
model_prompts, text_labels = process_labels_for_guidance(text_labels, empty_share)
|
| 239 |
+
embeddings = encode_texts_batch(model_prompts, tokenizer, model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
return {
|
| 242 |
+
"vae": latents_np,
|
| 243 |
"embeddings": embeddings,
|
| 244 |
"text": text_labels,
|
| 245 |
+
"width": widths,
|
| 246 |
+
"height": heights
|
| 247 |
}
|
| 248 |
|
| 249 |
except Exception as e:
|
| 250 |
print(f"Критическая ошибка в encode_to_latents: {e}")
|
| 251 |
raise
|
| 252 |
+
|
| 253 |
|
| 254 |
# ---------------- 5️⃣ Обработка папки с изображениями и текстами ----------------
|
| 255 |
def process_folder(folder_path, limit=None):
|
|
|
|
| 298 |
print(f"Найдено {len(image_paths)} изображений с текстовыми описаниями")
|
| 299 |
return image_paths, text_paths, width, height
|
| 300 |
|
| 301 |
+
def process_in_chunks(image_paths, text_paths, width, height, chunk_size=10000, batch_size=1):
|
| 302 |
total_files = len(image_paths)
|
| 303 |
start_time = time.time()
|
| 304 |
chunks = range(0, total_files, chunk_size)
|
|
|
|
| 389 |
print(f"Всего найдено {len(image_paths)} изображений")
|
| 390 |
|
| 391 |
# Обработка с чанкованием
|
| 392 |
+
process_in_chunks(image_paths, text_paths, width, height, chunk_size=10000, batch_size=batch_size)
|
| 393 |
|
| 394 |
# Объединение чанков в финальный датасет
|
| 395 |
combine_chunks(temp_path, save_path)
|
model_index.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5b101caa6806403e974c161e47a59251ab322afe5ef583bcc7bb40fba0b3a25a
|
| 3 |
+
size 415
|
pipeline_sdxs.py
CHANGED
|
@@ -25,44 +25,6 @@ class SdxsPipeline(DiffusionPipeline):
|
|
| 25 |
unet=unet, scheduler=scheduler
|
| 26 |
)
|
| 27 |
|
| 28 |
-
# Get the model path, which is either provided directly or from internal dict
|
| 29 |
-
model_path = None
|
| 30 |
-
if hasattr(self, '_internal_dict') and self._internal_dict.get('_name_or_path'):
|
| 31 |
-
model_path = self._internal_dict.get('_name_or_path')
|
| 32 |
-
|
| 33 |
-
# Get device and dtype from existing components
|
| 34 |
-
device = "cuda"
|
| 35 |
-
dtype = torch.float16
|
| 36 |
-
|
| 37 |
-
# Always load text_projector, regardless of whether one was provided
|
| 38 |
-
projector_path = None
|
| 39 |
-
|
| 40 |
-
# Try to find projector path
|
| 41 |
-
if model_path and os.path.exists(f"{model_path}/text_projector"):
|
| 42 |
-
projector_path = f"{model_path}/text_projector"
|
| 43 |
-
elif os.path.exists("./text_projector"):
|
| 44 |
-
projector_path = "./text_projector"
|
| 45 |
-
|
| 46 |
-
if projector_path:
|
| 47 |
-
# Create and load projector
|
| 48 |
-
try:
|
| 49 |
-
with open(f"{projector_path}/config.json", "r") as f:
|
| 50 |
-
projector_config = json.load(f)
|
| 51 |
-
|
| 52 |
-
# Create Linear layer with bias=False
|
| 53 |
-
self.text_projector = nn.Linear(
|
| 54 |
-
in_features=projector_config["in_features"],
|
| 55 |
-
out_features=projector_config["out_features"],
|
| 56 |
-
bias=False
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
# Load the state dict using safetensors
|
| 60 |
-
self.text_projector.load_state_dict(load_file(f"{projector_path}/model.safetensors"))
|
| 61 |
-
self.text_projector.to(device=device, dtype=dtype)
|
| 62 |
-
print(f"Successfully loaded text_projector from {projector_path}",device, dtype)
|
| 63 |
-
except Exception as e:
|
| 64 |
-
print(f"Error loading text_projector: {e}")
|
| 65 |
-
|
| 66 |
self.vae_scale_factor = 8
|
| 67 |
|
| 68 |
|
|
@@ -88,7 +50,7 @@ class SdxsPipeline(DiffusionPipeline):
|
|
| 88 |
|
| 89 |
text_inputs = self.tokenizer(
|
| 90 |
prompt, return_tensors="pt", padding="max_length",
|
| 91 |
-
max_length=
|
| 92 |
).to(device)
|
| 93 |
|
| 94 |
# Получаем эмбеддинги
|
|
|
|
| 25 |
unet=unet, scheduler=scheduler
|
| 26 |
)
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
self.vae_scale_factor = 8
|
| 29 |
|
| 30 |
|
|
|
|
| 50 |
|
| 51 |
text_inputs = self.tokenizer(
|
| 52 |
prompt, return_tensors="pt", padding="max_length",
|
| 53 |
+
max_length=150, truncation=True
|
| 54 |
).to(device)
|
| 55 |
|
| 56 |
# Получаем эмбеддинги
|
promo.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
requirements.txt
CHANGED
|
@@ -1,6 +1,3 @@
|
|
| 1 |
-
# torch>=2.6.0
|
| 2 |
-
# torchvision>=0.21.0
|
| 3 |
-
# torchaudio>=2.6.0
|
| 4 |
diffusers>=0.32.2
|
| 5 |
accelerate>=1.5.2
|
| 6 |
datasets>=3.5.0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
diffusers>=0.32.2
|
| 2 |
accelerate>=1.5.2
|
| 3 |
datasets>=3.5.0
|
micro/config.json → result_grid.png
RENAMED
|
File without changes
|
scheduler/scheduler_config.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ad559f5b938d5eac3c1f95f9d96dcfc8d0e4f9780f65755aa0d7e924c0f981c9
|
| 3 |
+
size 482
|
text_encoder/config.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:39738c799f70b98f75dace818f14bfffb0b34fad25d41b80b6236e20a731c740
|
| 3 |
+
size 1359
|
text_projector/config.json → text_encoder/generation_config.json
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d3e057bbca66b92f33a8bdc6a1301014e0e4ab69b3b3fd2e442d9fe0c69f3431
|
| 3 |
+
size 214
|
text_encoder/model.fp16.safetensors
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:107fe15da52fe6d13d877512fa36861d1100534d1b9b88015ad9fd017db095a7
|
| 3 |
-
size 1119825680
|
|
|
|
|
|
|
|
|
|
|
|
text_encoder/model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a3184342876e1e64cd98164dc01aaaf69177a9d0ed80725816e3225977ab2982
|
| 3 |
+
size 1192134784
|
vae/config-orig-wrong.json → tokenizer/added_tokens.json
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c0284b582e14987fbd3d5a2cb2bd139084371ed9acbae488829a1c900833c680
|
| 3 |
+
size 707
|
tokenizer/chat_template.jinja
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{%- if tools %}
|
| 2 |
+
{{- '<|im_start|>system\n' }}
|
| 3 |
+
{%- if messages[0].role == 'system' %}
|
| 4 |
+
{{- messages[0].content + '\n\n' }}
|
| 5 |
+
{%- endif %}
|
| 6 |
+
{{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
|
| 7 |
+
{%- for tool in tools %}
|
| 8 |
+
{{- "\n" }}
|
| 9 |
+
{{- tool | tojson }}
|
| 10 |
+
{%- endfor %}
|
| 11 |
+
{{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
|
| 12 |
+
{%- else %}
|
| 13 |
+
{%- if messages[0].role == 'system' %}
|
| 14 |
+
{{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }}
|
| 15 |
+
{%- endif %}
|
| 16 |
+
{%- endif %}
|
| 17 |
+
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
|
| 18 |
+
{%- for message in messages[::-1] %}
|
| 19 |
+
{%- set index = (messages|length - 1) - loop.index0 %}
|
| 20 |
+
{%- if ns.multi_step_tool and message.role == "user" and message.content is string and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}
|
| 21 |
+
{%- set ns.multi_step_tool = false %}
|
| 22 |
+
{%- set ns.last_query_index = index %}
|
| 23 |
+
{%- endif %}
|
| 24 |
+
{%- endfor %}
|
| 25 |
+
{%- for message in messages %}
|
| 26 |
+
{%- if message.content is string %}
|
| 27 |
+
{%- set content = message.content %}
|
| 28 |
+
{%- else %}
|
| 29 |
+
{%- set content = '' %}
|
| 30 |
+
{%- endif %}
|
| 31 |
+
{%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
|
| 32 |
+
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
|
| 33 |
+
{%- elif message.role == "assistant" %}
|
| 34 |
+
{%- set reasoning_content = '' %}
|
| 35 |
+
{%- if message.reasoning_content is string %}
|
| 36 |
+
{%- set reasoning_content = message.reasoning_content %}
|
| 37 |
+
{%- else %}
|
| 38 |
+
{%- if '</think>' in content %}
|
| 39 |
+
{%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
|
| 40 |
+
{%- set content = content.split('</think>')[-1].lstrip('\n') %}
|
| 41 |
+
{%- endif %}
|
| 42 |
+
{%- endif %}
|
| 43 |
+
{%- if loop.index0 > ns.last_query_index %}
|
| 44 |
+
{%- if loop.last or (not loop.last and reasoning_content) %}
|
| 45 |
+
{{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
|
| 46 |
+
{%- else %}
|
| 47 |
+
{{- '<|im_start|>' + message.role + '\n' + content }}
|
| 48 |
+
{%- endif %}
|
| 49 |
+
{%- else %}
|
| 50 |
+
{{- '<|im_start|>' + message.role + '\n' + content }}
|
| 51 |
+
{%- endif %}
|
| 52 |
+
{%- if message.tool_calls %}
|
| 53 |
+
{%- for tool_call in message.tool_calls %}
|
| 54 |
+
{%- if (loop.first and content) or (not loop.first) %}
|
| 55 |
+
{{- '\n' }}
|
| 56 |
+
{%- endif %}
|
| 57 |
+
{%- if tool_call.function %}
|
| 58 |
+
{%- set tool_call = tool_call.function %}
|
| 59 |
+
{%- endif %}
|
| 60 |
+
{{- '<tool_call>\n{"name": "' }}
|
| 61 |
+
{{- tool_call.name }}
|
| 62 |
+
{{- '", "arguments": ' }}
|
| 63 |
+
{%- if tool_call.arguments is string %}
|
| 64 |
+
{{- tool_call.arguments }}
|
| 65 |
+
{%- else %}
|
| 66 |
+
{{- tool_call.arguments | tojson }}
|
| 67 |
+
{%- endif %}
|
| 68 |
+
{{- '}\n</tool_call>' }}
|
| 69 |
+
{%- endfor %}
|
| 70 |
+
{%- endif %}
|
| 71 |
+
{{- '<|im_end|>\n' }}
|
| 72 |
+
{%- elif message.role == "tool" %}
|
| 73 |
+
{%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
|
| 74 |
+
{{- '<|im_start|>user' }}
|
| 75 |
+
{%- endif %}
|
| 76 |
+
{{- '\n<tool_response>\n' }}
|
| 77 |
+
{{- content }}
|
| 78 |
+
{{- '\n</tool_response>' }}
|
| 79 |
+
{%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
|
| 80 |
+
{{- '<|im_end|>\n' }}
|
| 81 |
+
{%- endif %}
|
| 82 |
+
{%- endif %}
|
| 83 |
+
{%- endfor %}
|
| 84 |
+
{%- if add_generation_prompt %}
|
| 85 |
+
{{- '<|im_start|>assistant\n' }}
|
| 86 |
+
{%- if enable_thinking is defined and enable_thinking is false %}
|
| 87 |
+
{{- '<think>\n\n</think>\n\n' }}
|
| 88 |
+
{%- endif %}
|
| 89 |
+
{%- endif %}
|
tokenizer/merges.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer/special_tokens_map.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:76862e765266b85aa9459767e33cbaf13970f327a0e88d1c65846c2ddd3a1ecd
|
| 3 |
+
size 613
|
tokenizer/tokenizer.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:aeb13307a71acd8fe81861d94ad54ab689df773318809eed3cbe794b4492dae4
|
| 3 |
+
size 11422654
|
tokenizer/tokenizer_config.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:443bfa629eb16387a12edbf92a76f6a6f10b2af3b53d87ba1550adfcf45f7fa0
|
| 3 |
+
size 5404
|
text_projector/model.safetensors → tokenizer/vocab.json
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ca10d7e9fb3ed18575dd1e277a2579c16d108e32f27439684afa0e10b1440910
|
| 3 |
+
size 2776833
|
train.py
CHANGED
|
@@ -8,7 +8,7 @@ from torch.utils.data.distributed import DistributedSampler
|
|
| 8 |
from torch.optim.lr_scheduler import LambdaLR
|
| 9 |
from collections import defaultdict
|
| 10 |
from torch.optim.lr_scheduler import LambdaLR
|
| 11 |
-
from diffusers import UNet2DConditionModel, AutoencoderKL
|
| 12 |
from accelerate import Accelerator
|
| 13 |
from datasets import load_from_disk
|
| 14 |
from tqdm import tqdm
|
|
@@ -26,15 +26,16 @@ import torch.nn.functional as F
|
|
| 26 |
from collections import deque
|
| 27 |
|
| 28 |
# --------------------------- Параметры ---------------------------
|
| 29 |
-
ds_path = "datasets/
|
| 30 |
-
project = "
|
| 31 |
-
batch_size =
|
| 32 |
-
base_learning_rate =
|
| 33 |
-
min_learning_rate =
|
| 34 |
-
num_epochs =
|
| 35 |
# samples/save per epoch
|
| 36 |
-
sample_interval_share =
|
| 37 |
-
use_wandb =
|
|
|
|
| 38 |
save_model = True
|
| 39 |
use_decay = True
|
| 40 |
fbp = False # fused backward pass
|
|
@@ -42,19 +43,19 @@ optimizer_type = "adam8bit"
|
|
| 42 |
torch_compile = False
|
| 43 |
unet_gradient = True
|
| 44 |
clip_sample = False #Scheduler
|
| 45 |
-
fixed_seed =
|
| 46 |
shuffle = True
|
|
|
|
|
|
|
| 47 |
torch.backends.cuda.matmul.allow_tf32 = True
|
| 48 |
torch.backends.cudnn.allow_tf32 = True
|
| 49 |
torch.backends.cuda.enable_mem_efficient_sdp(False)
|
| 50 |
dtype = torch.float32
|
| 51 |
-
save_barrier = 1.
|
| 52 |
-
warmup_percent = 0.
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
betta2 = 0.97
|
| 57 |
-
eps = 1e-6
|
| 58 |
clip_grad_norm = 1.0
|
| 59 |
steps_offset = 0 # Scheduler
|
| 60 |
limit = 0
|
|
@@ -70,7 +71,7 @@ device = accelerator.device
|
|
| 70 |
# Параметры для диффузии
|
| 71 |
n_diffusion_steps = 50
|
| 72 |
samples_to_generate = 12
|
| 73 |
-
guidance_scale =
|
| 74 |
|
| 75 |
# Папки для сохранения результатов
|
| 76 |
generated_folder = "samples"
|
|
@@ -86,16 +87,6 @@ if fixed_seed:
|
|
| 86 |
if torch.cuda.is_available():
|
| 87 |
torch.cuda.manual_seed_all(seed)
|
| 88 |
|
| 89 |
-
# --- Пропорции лоссов и окно медианного нормирования (КОЭФ., не значения) ---
|
| 90 |
-
# CHANGED: добавлен huber и dispersive в пропорции, суммы = 1.0
|
| 91 |
-
loss_ratios = {
|
| 92 |
-
"mse": 0.50,
|
| 93 |
-
"mae": 0.25,
|
| 94 |
-
"huber": 0.20,
|
| 95 |
-
"dispersive": 0.05,
|
| 96 |
-
}
|
| 97 |
-
median_coeff_steps = 256 # за сколько шагов считать медианные коэффициенты
|
| 98 |
-
|
| 99 |
# --------------------------- Параметры LoRA ---------------------------
|
| 100 |
lora_name = ""
|
| 101 |
lora_rank = 32
|
|
@@ -109,116 +100,68 @@ def sample_timesteps_bias(
|
|
| 109 |
progress: float, # [0..1]
|
| 110 |
num_train_timesteps: int, # обычно 1000
|
| 111 |
steps_offset: int = 0,
|
| 112 |
-
device=None
|
|
|
|
| 113 |
) -> torch.Tensor:
|
| 114 |
"""
|
| 115 |
-
Возвращает
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
FIX: исправлена формула alpha/beta (раньше было перевёрнуто).
|
| 120 |
"""
|
| 121 |
-
# Параметры Beta-распределения (FIX: alpha и beta поменяны местами по логике)
|
| 122 |
-
alpha = 1.0 + 2.0 * (1.0 - progress) # при progress=0 -> alpha ~10 (сдвиг к 1.0)
|
| 123 |
-
beta = 1.0 + 2.0 * progress # при progress=0 -> beta ~1
|
| 124 |
-
|
| 125 |
-
samples = torch.distributions.Beta(alpha, beta).sample((batch_size,)).to(device)
|
| 126 |
|
| 127 |
max_idx = num_train_timesteps - 1 - steps_offset
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
return timesteps
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
s = sum(desired_ratios.values())
|
| 136 |
-
self.ratios = {k: (v / s) for k, v in desired_ratios.items()}
|
| 137 |
-
self.buffers = {k: deque(maxlen=window_steps) for k in self.ratios.keys()}
|
| 138 |
-
self.window = window_steps
|
| 139 |
-
|
| 140 |
-
def update_and_total(self, losses: dict):
|
| 141 |
-
"""
|
| 142 |
-
losses: dict ключ->тензор (значения лоссов)
|
| 143 |
-
Поведение:
|
| 144 |
-
- буферим ABS(l) только для активных (ratio>0) лоссов
|
| 145 |
-
- coeff = ratio / median(abs(loss))
|
| 146 |
-
- total = sum(coeff * loss) по активным лоссам
|
| 147 |
-
CHANGED: буферим abs() — чтобы медиана была положительной и не ломала деление.
|
| 148 |
-
"""
|
| 149 |
-
# буферим только активные лоссы
|
| 150 |
-
for k, v in losses.items():
|
| 151 |
-
if k in self.buffers and self.ratios.get(k, 0) > 0:
|
| 152 |
-
self.buffers[k].append(float(v.detach().abs().cpu()))
|
| 153 |
-
|
| 154 |
-
meds = {k: (np.median(self.buffers[k]) if len(self.buffers[k]) > 0 else 1.0) for k in self.buffers}
|
| 155 |
-
coeffs = {k: (self.ratios[k] / max(meds[k], 1e-12)) for k in self.ratios}
|
| 156 |
-
|
| 157 |
-
# суммируем только по активным (ratio>0)
|
| 158 |
-
total = sum(coeffs[k] * losses[k] for k in coeffs if self.ratios.get(k, 0) > 0)
|
| 159 |
-
return total, coeffs, meds
|
| 160 |
-
|
| 161 |
-
# создаём normalizer после определения loss_ratios
|
| 162 |
-
normalizer = MedianLossNormalizer(loss_ratios, median_coeff_steps)
|
| 163 |
-
|
| 164 |
-
class AccelerateDispersiveLoss:
|
| 165 |
-
def __init__(self, accelerator, temperature=0.5, weight=0.5):
|
| 166 |
-
self.accelerator = accelerator
|
| 167 |
-
self.temperature = temperature
|
| 168 |
-
self.weight = weight
|
| 169 |
-
self.activations = []
|
| 170 |
-
self.hooks = []
|
| 171 |
-
|
| 172 |
-
def register_hooks(self, model, target_layer="down_blocks.0"):
|
| 173 |
-
unwrapped_model = self.accelerator.unwrap_model(model)
|
| 174 |
-
print("=== Поиск слоев в unwrapped модели ===")
|
| 175 |
-
for name, module in unwrapped_model.named_modules():
|
| 176 |
-
if target_layer in name:
|
| 177 |
-
hook = module.register_forward_hook(self.hook_fn)
|
| 178 |
-
self.hooks.append(hook)
|
| 179 |
-
print(f"✅ Хук зарегистрирован на: {name}")
|
| 180 |
-
break
|
| 181 |
-
|
| 182 |
-
def hook_fn(self, module, input, output):
|
| 183 |
-
if isinstance(output, tuple):
|
| 184 |
-
activation = output[0]
|
| 185 |
-
else:
|
| 186 |
-
activation = output
|
| 187 |
-
if len(activation.shape) > 2:
|
| 188 |
-
activation = activation.view(activation.shape[0], -1)
|
| 189 |
-
self.activations.append(activation.detach().clone())
|
| 190 |
-
|
| 191 |
-
def compute_dispersive_loss(self):
|
| 192 |
-
if not self.activations:
|
| 193 |
-
return torch.tensor(0.0, requires_grad=True, device=device)
|
| 194 |
-
local_activations = self.activations[-1].float()
|
| 195 |
-
batch_size = local_activations.shape[0]
|
| 196 |
-
if batch_size < 2:
|
| 197 |
-
return torch.tensor(0.0, requires_grad=True, device=device)
|
| 198 |
-
sf = local_activations / torch.norm(local_activations, dim=1, keepdim=True)
|
| 199 |
-
distance = torch.nn.functional.pdist(sf.float(), p=2) ** 2
|
| 200 |
-
exp_neg_dist = torch.exp(-distance / self.temperature) + 1e-5
|
| 201 |
-
dispersive_loss = torch.log(torch.mean(exp_neg_dist))
|
| 202 |
-
return dispersive_loss
|
| 203 |
-
|
| 204 |
-
def clear_activations(self):
|
| 205 |
-
self.activations.clear()
|
| 206 |
-
|
| 207 |
-
def remove_hooks(self):
|
| 208 |
-
for hook in self.hooks:
|
| 209 |
-
hook.remove()
|
| 210 |
-
self.hooks.clear()
|
| 211 |
|
|
|
|
| 212 |
|
| 213 |
# --------------------------- Инициализация WandB ---------------------------
|
| 214 |
-
if
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
# Включение Flash Attention 2/SDPA
|
| 224 |
torch.backends.cuda.enable_flash_sdp(True)
|
|
@@ -228,15 +171,28 @@ gen.manual_seed(seed)
|
|
| 228 |
|
| 229 |
# --------------------------- Загрузка моделей ---------------------------
|
| 230 |
# VAE загружается на CPU для экономии GPU-памяти (как в твоём оригинальном коде)
|
| 231 |
-
vae = AutoencoderKL.from_pretrained("
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
)
|
| 241 |
|
| 242 |
|
|
@@ -343,22 +299,6 @@ def collate_fn_simple(batch):
|
|
| 343 |
embeddings = torch.tensor(np.array([item["embeddings"] for item in batch])).to(device,dtype=dtype)
|
| 344 |
return latents, embeddings
|
| 345 |
|
| 346 |
-
def collate_fn(batch):
|
| 347 |
-
if not batch:
|
| 348 |
-
return [], []
|
| 349 |
-
ref_vae_shape = np.array(batch[0]["vae"]).shape
|
| 350 |
-
ref_embed_shape = np.array(batch[0]["embeddings"]).shape
|
| 351 |
-
valid_latents = []
|
| 352 |
-
valid_embeddings = []
|
| 353 |
-
for item in batch:
|
| 354 |
-
if (np.array(item["vae"]).shape == ref_vae_shape and
|
| 355 |
-
np.array(item["embeddings"]).shape == ref_embed_shape):
|
| 356 |
-
valid_latents.append(item["vae"])
|
| 357 |
-
valid_embeddings.append(item["embeddings"])
|
| 358 |
-
latents = torch.tensor(np.array(valid_latents)).to(device,dtype=dtype)
|
| 359 |
-
embeddings = torch.tensor(np.array(valid_embeddings)).to(device,dtype=dtype)
|
| 360 |
-
return latents, embeddings
|
| 361 |
-
|
| 362 |
batch_sampler = DistributedResolutionBatchSampler(
|
| 363 |
dataset=dataset,
|
| 364 |
batch_size=batch_size,
|
|
@@ -381,11 +321,6 @@ latest_checkpoint = os.path.join(checkpoints_folder, project)
|
|
| 381 |
if os.path.isdir(latest_checkpoint):
|
| 382 |
print("Загружаем UNet из чекпоинта:", latest_checkpoint)
|
| 383 |
unet = UNet2DConditionModel.from_pretrained(latest_checkpoint).to(device=device,dtype=dtype)
|
| 384 |
-
if torch_compile:
|
| 385 |
-
print("compiling")
|
| 386 |
-
torch.set_float32_matmul_precision('high')
|
| 387 |
-
unet = torch.compile(unet)
|
| 388 |
-
print("compiling - ok")
|
| 389 |
if unet_gradient:
|
| 390 |
unet.enable_gradient_checkpointing()
|
| 391 |
unet.set_use_memory_efficient_attention_xformers(False)
|
|
@@ -395,13 +330,6 @@ if os.path.isdir(latest_checkpoint):
|
|
| 395 |
print(f"Ошибка при включении SDPA: {e}")
|
| 396 |
unet.set_use_memory_efficient_attention_xformers(True)
|
| 397 |
|
| 398 |
-
# Создаём hook для dispersive только если нужно
|
| 399 |
-
if loss_ratios.get("dispersive", 0) > 0:
|
| 400 |
-
dispersive_hook = AccelerateDispersiveLoss(
|
| 401 |
-
accelerator=accelerator,
|
| 402 |
-
temperature=dispersive_temperature,
|
| 403 |
-
weight=dispersive_weight
|
| 404 |
-
)
|
| 405 |
else:
|
| 406 |
# FIX: если чекпоинта нет — прекращаем с понятной ошибкой (лучше, чем неожиданные NameError дальше)
|
| 407 |
raise FileNotFoundError(f"UNet checkpoint not found at {latest_checkpoint}. Положи UNet чекпоинт в {latest_checkpoint} или укажи другой путь.")
|
|
@@ -454,25 +382,13 @@ else:
|
|
| 454 |
def create_optimizer(name, params):
|
| 455 |
if name == "adam8bit":
|
| 456 |
return bnb.optim.AdamW8bit(
|
| 457 |
-
params, lr=base_learning_rate, betas=(0.9, betta2), eps=eps, weight_decay=0.
|
| 458 |
percentile_clipping=percentile_clipping
|
| 459 |
)
|
| 460 |
elif name == "adam":
|
| 461 |
return torch.optim.AdamW(
|
| 462 |
params, lr=base_learning_rate, betas=(0.9, 0.999), eps=1e-8, weight_decay=0.01
|
| 463 |
)
|
| 464 |
-
elif name == "lion8bit":
|
| 465 |
-
return bnb.optim.Lion8bit(
|
| 466 |
-
params, lr=base_learning_rate, betas=(0.9, 0.97), weight_decay=0.01,
|
| 467 |
-
percentile_clipping=percentile_clipping
|
| 468 |
-
)
|
| 469 |
-
elif name == "adafactor":
|
| 470 |
-
from transformers import Adafactor
|
| 471 |
-
return Adafactor(
|
| 472 |
-
params, lr=base_learning_rate, scale_parameter=True, relative_step=False,
|
| 473 |
-
warmup_init=False, eps=(1e-30, 1e-3), clip_threshold=1.0,
|
| 474 |
-
beta1=0.9, weight_decay=0.01
|
| 475 |
-
)
|
| 476 |
else:
|
| 477 |
raise ValueError(f"Unknown optimizer: {name}")
|
| 478 |
|
|
@@ -504,23 +420,96 @@ else:
|
|
| 504 |
if torch.isnan(param).any() or torch.isinf(param).any():
|
| 505 |
print(f"[rank {accelerator.process_index}] NaN/Inf in {name}")
|
| 506 |
unet, optimizer, lr_scheduler = accelerator.prepare(unet, optimizer, lr_scheduler)
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 511 |
|
| 512 |
# --------------------------- Фиксированные семплы для генерации ---------------------------
|
| 513 |
fixed_samples = get_fixed_samples_by_resolution(dataset)
|
| 514 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 515 |
@torch.compiler.disable()
|
| 516 |
@torch.no_grad()
|
| 517 |
-
def generate_and_save_samples(fixed_samples_cpu, step):
|
| 518 |
original_model = None
|
| 519 |
try:
|
| 520 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 521 |
vae.to(device=device).eval() # временно подгружаем VAE на GPU для декодинга
|
| 522 |
|
| 523 |
-
scheduler.set_timesteps(n_diffusion_steps)
|
| 524 |
|
| 525 |
all_generated_images = []
|
| 526 |
all_captions = []
|
|
@@ -529,43 +518,66 @@ def generate_and_save_samples(fixed_samples_cpu, step):
|
|
| 529 |
width, height = size
|
| 530 |
sample_latents = sample_latents.to(dtype=dtype, device=device)
|
| 531 |
sample_text_embeddings = sample_text_embeddings.to(dtype=dtype, device=device)
|
| 532 |
-
|
| 533 |
-
|
|
|
|
| 534 |
sample_latents.shape,
|
| 535 |
-
generator=gen,
|
| 536 |
device=device,
|
| 537 |
-
dtype=sample_latents.dtype
|
|
|
|
| 538 |
)
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
text_embeddings_batch = torch.cat([empty_embeddings, sample_text_embeddings], dim=0)
|
| 544 |
-
else:
|
| 545 |
-
text_embeddings_batch = sample_text_embeddings
|
| 546 |
-
|
| 547 |
for t in scheduler.timesteps:
|
| 548 |
-
|
| 549 |
-
if guidance_scale
|
| 550 |
-
latent_model_input = torch.cat([
|
| 551 |
-
else:
|
| 552 |
-
latent_model_input = current_latents
|
| 553 |
|
| 554 |
-
|
| 555 |
-
|
|
|
|
|
|
|
|
|
|
| 556 |
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 560 |
|
| 561 |
-
current_latents = scheduler.step(noise_pred, t, current_latents).prev_sample
|
| 562 |
|
| 563 |
-
|
| 564 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 565 |
|
| 566 |
decoded_fp32 = decoded.to(torch.float32)
|
| 567 |
for img_idx, img_tensor in enumerate(decoded_fp32):
|
| 568 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 569 |
if np.isnan(img).any():
|
| 570 |
print("NaNs found, saving stopped! Step:", step)
|
| 571 |
pil_img = Image.fromarray((img * 255).astype("uint8"))
|
|
@@ -589,7 +601,20 @@ def generate_and_save_samples(fixed_samples_cpu, step):
|
|
| 589 |
wandb.Image(img, caption=f"{all_captions[i]}")
|
| 590 |
for i, img in enumerate(all_generated_images)
|
| 591 |
]
|
| 592 |
-
wandb.log({"generated_images": wandb_images
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 593 |
finally:
|
| 594 |
# вернуть VAE на CPU (как было в твоём коде)
|
| 595 |
vae.to("cpu")
|
|
@@ -603,44 +628,30 @@ def generate_and_save_samples(fixed_samples_cpu, step):
|
|
| 603 |
if accelerator.is_main_process:
|
| 604 |
if save_model:
|
| 605 |
print("Генерация сэмплов до старта обучения...")
|
| 606 |
-
generate_and_save_samples(fixed_samples,0)
|
| 607 |
accelerator.wait_for_everyone()
|
| 608 |
|
| 609 |
# Модифицируем функцию сохранения модели для поддержки LoRA
|
| 610 |
-
def save_checkpoint(unet,variant=""):
|
| 611 |
if accelerator.is_main_process:
|
| 612 |
if lora_name:
|
| 613 |
save_lora_checkpoint(unet)
|
| 614 |
else:
|
| 615 |
-
|
| 616 |
-
|
|
|
|
|
|
|
| 617 |
else:
|
| 618 |
-
|
| 619 |
-
unet = unet.to(dtype=dtype)
|
| 620 |
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
|
|
|
|
|
|
|
|
|
| 624 |
|
| 625 |
-
|
| 626 |
-
if not isinstance(available_ts, torch.Tensor):
|
| 627 |
-
available_ts = torch.tensor(available_ts, device="cpu")
|
| 628 |
-
else:
|
| 629 |
-
available_ts = available_ts.cpu()
|
| 630 |
-
|
| 631 |
-
B = model_outputs.shape[0]
|
| 632 |
-
preds = []
|
| 633 |
-
for i in range(B):
|
| 634 |
-
t_i = int(timesteps_tensor[i].item())
|
| 635 |
-
diffs = torch.abs(available_ts - t_i)
|
| 636 |
-
idx = int(torch.argmin(diffs).item())
|
| 637 |
-
t_for_step = int(available_ts[idx].item())
|
| 638 |
-
model_out_i = model_outputs[i:i+1]
|
| 639 |
-
noisy_latent_i = noisy_latents[i:i+1]
|
| 640 |
-
step_out = scheduler.step(model_out_i, t_for_step, noisy_latent_i)
|
| 641 |
-
preds.append(step_out.pred_original_sample)
|
| 642 |
-
|
| 643 |
-
return torch.cat(preds, dim=0).to(device=device, dtype=dtype)
|
| 644 |
|
| 645 |
# --------------------------- Тренировочный цикл ---------------------------
|
| 646 |
if accelerator.is_main_process:
|
|
@@ -651,61 +662,41 @@ progress_bar = tqdm(total=total_training_steps, disable=not accelerator.is_local
|
|
| 651 |
|
| 652 |
steps_per_epoch = len(dataloader)
|
| 653 |
sample_interval = max(1, steps_per_epoch // sample_interval_share)
|
| 654 |
-
min_loss =
|
| 655 |
|
| 656 |
for epoch in range(start_epoch, start_epoch + num_epochs):
|
| 657 |
batch_losses = []
|
| 658 |
-
batch_tlosses = []
|
| 659 |
batch_grads = []
|
| 660 |
batch_sampler.set_epoch(epoch)
|
| 661 |
accelerator.wait_for_everyone()
|
| 662 |
unet.train()
|
| 663 |
-
print("epoch:",epoch)
|
| 664 |
for step, (latents, embeddings) in enumerate(dataloader):
|
| 665 |
with accelerator.accumulate(unet):
|
| 666 |
if save_model == False and step == 5 :
|
| 667 |
used_gb = torch.cuda.max_memory_allocated() / 1024**3
|
| 668 |
print(f"Шаг {step}: {used_gb:.2f} GB")
|
| 669 |
-
|
|
|
|
| 670 |
noise = torch.randn_like(latents, dtype=latents.dtype)
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
)
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
if loss_ratios.get("dispersive", 0) > 0:
|
| 684 |
-
dispersive_hook.clear_activations()
|
| 685 |
-
|
| 686 |
model_pred = unet(noisy_latents, timesteps, embeddings).sample
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
#
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
mse_loss = F.mse_loss(model_pred.float(),
|
| 693 |
-
losses_dict["mse"] = mse_loss
|
| 694 |
-
losses_dict["mae"] = F.l1_loss(model_pred.float(), target_pred.float())
|
| 695 |
-
|
| 696 |
-
# CHANGED: Huber (smooth_l1) loss added
|
| 697 |
-
losses_dict["huber"] = F.smooth_l1_loss(model_pred.float(), target_pred.float())
|
| 698 |
-
|
| 699 |
-
# === Dispersive loss ===
|
| 700 |
-
if loss_ratios.get("dispersive", 0) > 0:
|
| 701 |
-
disp_raw = dispersive_hook.compute_dispersive_loss().to(device) # может быть отрицательным
|
| 702 |
-
losses_dict["dispersive"] = dispersive_hook.weight * disp_raw
|
| 703 |
-
else:
|
| 704 |
-
losses_dict["dispersive"] = torch.tensor(0.0, device=device)
|
| 705 |
-
|
| 706 |
-
# === Нормализация всех лоссов ===
|
| 707 |
-
abs_for_norm = {k: losses_dict.get(k, torch.tensor(0.0, device=device)) for k in normalizer.ratios.keys()}
|
| 708 |
-
total_loss, coeffs, meds = normalizer.update_and_total(abs_for_norm)
|
| 709 |
|
| 710 |
# Сохраняем для логов (мы сохраняем MSE отдельно — как показатель)
|
| 711 |
batch_losses.append(mse_loss.detach().item())
|
|
@@ -714,7 +705,7 @@ for epoch in range(start_epoch, start_epoch + num_epochs):
|
|
| 714 |
accelerator.wait_for_everyone()
|
| 715 |
|
| 716 |
# Backward
|
| 717 |
-
accelerator.backward(
|
| 718 |
|
| 719 |
if (global_step % 100 == 0) or (global_step % sample_interval == 0):
|
| 720 |
accelerator.wait_for_everyone()
|
|
@@ -729,68 +720,63 @@ for epoch in range(start_epoch, start_epoch + num_epochs):
|
|
| 729 |
lr_scheduler.step()
|
| 730 |
optimizer.zero_grad(set_to_none=True)
|
| 731 |
|
| 732 |
-
|
| 733 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 734 |
|
| 735 |
-
# Логируем метрики
|
| 736 |
-
if accelerator.is_main_process:
|
| 737 |
-
if fbp:
|
| 738 |
-
current_lr = base_learning_rate
|
| 739 |
-
else:
|
| 740 |
-
current_lr = lr_scheduler.get_last_lr()[0]
|
| 741 |
-
batch_tlosses.append(total_loss.detach().item())
|
| 742 |
-
batch_grads.append(grad)
|
| 743 |
-
|
| 744 |
-
# Логируем только активные лоссы (ratio>0)
|
| 745 |
-
active_keys = [k for k, v in loss_ratios.items() if v > 0]
|
| 746 |
-
log_data = {}
|
| 747 |
-
for k in active_keys:
|
| 748 |
-
v = losses_dict.get(k, None)
|
| 749 |
-
if v is None:
|
| 750 |
-
continue
|
| 751 |
-
log_data[f"loss/{k}"] = (v.item() if isinstance(v, torch.Tensor) else float(v))
|
| 752 |
-
|
| 753 |
-
log_data["loss/total"] = float(total_loss.item())
|
| 754 |
-
log_data["loss/lr"] = current_lr
|
| 755 |
-
for k, c in coeffs.items():
|
| 756 |
-
log_data[f"coeff/{k}"] = float(c)
|
| 757 |
-
if use_wandb and accelerator.sync_gradients:
|
| 758 |
-
wandb.log(log_data, step=global_step)
|
| 759 |
-
|
| 760 |
-
# Генерируем сэмплы с заданным интервалом
|
| 761 |
-
if global_step % sample_interval == 0:
|
| 762 |
-
generate_and_save_samples(fixed_samples,global_step)
|
| 763 |
-
last_n = sample_interval
|
| 764 |
-
avg_loss = float(np.mean(batch_losses[-last_n:])) if len(batch_losses) > 0 else 0.0
|
| 765 |
-
avg_tloss = float(np.mean(batch_tlosses[-last_n:])) if len(batch_tlosses) > 0 else 0.0
|
| 766 |
-
avg_grad = float(np.mean(batch_grads[-last_n:])) if len(batch_grads) > 0 else 0.0
|
| 767 |
-
print(f"Эпоха {epoch}, шаг {global_step}, средний лосс: {avg_loss:.6f}, grad: {avg_grad:.6f}")
|
| 768 |
-
|
| 769 |
-
if save_model:
|
| 770 |
-
print("saving:",avg_loss < min_loss*save_barrier)
|
| 771 |
-
if avg_loss < min_loss*save_barrier:
|
| 772 |
-
min_loss = avg_loss
|
| 773 |
-
save_checkpoint(unet)
|
| 774 |
-
if use_wandb:
|
| 775 |
-
avg_data = {}
|
| 776 |
-
avg_data["avg/loss"] = avg_loss
|
| 777 |
-
avg_data["avg/tloss"] = avg_tloss
|
| 778 |
-
avg_data["avg/grad"] = avg_grad
|
| 779 |
-
wandb.log(avg_data, step=global_step)
|
| 780 |
|
| 781 |
if accelerator.is_main_process:
|
| 782 |
-
|
|
|
|
|
|
|
|
|
|
| 783 |
print(f"\nЭпоха {epoch} завершена. Средний лосс: {avg_epoch_loss:.6f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 784 |
if use_wandb:
|
| 785 |
-
wandb.log(
|
|
|
|
|
|
|
| 786 |
|
| 787 |
# Завершение обучения - сохраняем финальную модель
|
| 788 |
-
if loss_ratios.get("dispersive", 0) > 0:
|
| 789 |
-
dispersive_hook.remove_hooks()
|
| 790 |
if accelerator.is_main_process:
|
| 791 |
print("Обучение завершено! Сохраняем финальную модель...")
|
| 792 |
if save_model:
|
| 793 |
save_checkpoint(unet,"fp16")
|
|
|
|
|
|
|
| 794 |
accelerator.free_memory()
|
| 795 |
if torch.distributed.is_initialized():
|
| 796 |
torch.distributed.destroy_process_group()
|
|
|
|
| 8 |
from torch.optim.lr_scheduler import LambdaLR
|
| 9 |
from collections import defaultdict
|
| 10 |
from torch.optim.lr_scheduler import LambdaLR
|
| 11 |
+
from diffusers import UNet2DConditionModel, AutoencoderKLWan,AutoencoderKL
|
| 12 |
from accelerate import Accelerator
|
| 13 |
from datasets import load_from_disk
|
| 14 |
from tqdm import tqdm
|
|
|
|
| 26 |
from collections import deque
|
| 27 |
|
| 28 |
# --------------------------- Параметры ---------------------------
|
| 29 |
+
ds_path = "/workspace/sdxs3d/datasets/mjnj_640"
|
| 30 |
+
project = "unet"
|
| 31 |
+
batch_size = 48
|
| 32 |
+
base_learning_rate = 5e-5
|
| 33 |
+
min_learning_rate = 6e-6
|
| 34 |
+
num_epochs = 40
|
| 35 |
# samples/save per epoch
|
| 36 |
+
sample_interval_share = 4
|
| 37 |
+
use_wandb = False
|
| 38 |
+
use_comet_ml = True
|
| 39 |
save_model = True
|
| 40 |
use_decay = True
|
| 41 |
fbp = False # fused backward pass
|
|
|
|
| 43 |
torch_compile = False
|
| 44 |
unet_gradient = True
|
| 45 |
clip_sample = False #Scheduler
|
| 46 |
+
fixed_seed = True
|
| 47 |
shuffle = True
|
| 48 |
+
comet_ml_api_key = "Agctp26mbqnoYrrlvQuKSTk6r" # Добавлен API ключ для Comet ML
|
| 49 |
+
comet_ml_workspace = "recoilme" # Добавлен workspace для Comet ML
|
| 50 |
torch.backends.cuda.matmul.allow_tf32 = True
|
| 51 |
torch.backends.cudnn.allow_tf32 = True
|
| 52 |
torch.backends.cuda.enable_mem_efficient_sdp(False)
|
| 53 |
dtype = torch.float32
|
| 54 |
+
save_barrier = 1.006
|
| 55 |
+
warmup_percent = 0.005
|
| 56 |
+
percentile_clipping = 99 # 8bit optim
|
| 57 |
+
betta2 = 0.99
|
| 58 |
+
eps = 1e-8
|
|
|
|
|
|
|
| 59 |
clip_grad_norm = 1.0
|
| 60 |
steps_offset = 0 # Scheduler
|
| 61 |
limit = 0
|
|
|
|
| 71 |
# Параметры для диффузии
|
| 72 |
n_diffusion_steps = 50
|
| 73 |
samples_to_generate = 12
|
| 74 |
+
guidance_scale = 4
|
| 75 |
|
| 76 |
# Папки для сохранения результатов
|
| 77 |
generated_folder = "samples"
|
|
|
|
| 87 |
if torch.cuda.is_available():
|
| 88 |
torch.cuda.manual_seed_all(seed)
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
# --------------------------- Параметры LoRA ---------------------------
|
| 91 |
lora_name = ""
|
| 92 |
lora_rank = 32
|
|
|
|
| 100 |
progress: float, # [0..1]
|
| 101 |
num_train_timesteps: int, # обычно 1000
|
| 102 |
steps_offset: int = 0,
|
| 103 |
+
device=None,
|
| 104 |
+
mode: str = "beta", # "beta", "uniform"
|
| 105 |
) -> torch.Tensor:
|
| 106 |
"""
|
| 107 |
+
Возвращает timesteps с разным bias:
|
| 108 |
+
- beta : как раньше (сдвиг в начало или конец в зависимости от progress)
|
| 109 |
+
- normal : около середины (гауссовое распределение)
|
| 110 |
+
- uniform: равномерно по всем timestep’ам
|
|
|
|
| 111 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
max_idx = num_train_timesteps - 1 - steps_offset
|
| 114 |
+
|
| 115 |
+
if mode == "beta":
|
| 116 |
+
alpha = 1.0 + .5 * (1.0 - progress)
|
| 117 |
+
beta = 1.0 + .5 * progress
|
| 118 |
+
samples = torch.distributions.Beta(alpha, beta).sample((batch_size,))
|
| 119 |
+
|
| 120 |
+
elif mode == "uniform":
|
| 121 |
+
samples = torch.rand(batch_size)
|
| 122 |
+
|
| 123 |
+
else:
|
| 124 |
+
raise ValueError(f"Unknown mode: {mode}")
|
| 125 |
+
|
| 126 |
+
timesteps = steps_offset + (samples * max_idx).long().to(device)
|
| 127 |
return timesteps
|
| 128 |
|
| 129 |
+
def logit_normal_samples(shape, mu=0.0, sigma=1.0, device=None, dtype=None):
|
| 130 |
+
normal_samples = torch.normal(mean=mu, std=sigma, size=shape, device=device, dtype=dtype)
|
| 131 |
+
|
| 132 |
+
logit_normal_samples = torch.sigmoid(normal_samples)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
+
return logit_normal_samples
|
| 135 |
|
| 136 |
# --------------------------- Инициализация WandB ---------------------------
|
| 137 |
+
if accelerator.is_main_process:
|
| 138 |
+
if use_wandb:
|
| 139 |
+
wandb.init(project=project+lora_name, config={
|
| 140 |
+
"batch_size": batch_size,
|
| 141 |
+
"base_learning_rate": base_learning_rate,
|
| 142 |
+
"num_epochs": num_epochs,
|
| 143 |
+
"fbp": fbp,
|
| 144 |
+
"optimizer_type": optimizer_type,
|
| 145 |
+
})
|
| 146 |
+
if use_comet_ml:
|
| 147 |
+
from comet_ml import Experiment
|
| 148 |
+
comet_experiment = Experiment(
|
| 149 |
+
api_key=comet_ml_api_key,
|
| 150 |
+
project_name=project,
|
| 151 |
+
workspace=comet_ml_workspace
|
| 152 |
+
)
|
| 153 |
+
# Логируем гиперпараметры в Comet ML
|
| 154 |
+
hyper_params = {
|
| 155 |
+
"batch_size": batch_size,
|
| 156 |
+
"base_learning_rate": base_learning_rate,
|
| 157 |
+
"min_learning_rate": min_learning_rate,
|
| 158 |
+
"num_epochs": num_epochs,
|
| 159 |
+
"n_diffusion_steps": n_diffusion_steps,
|
| 160 |
+
"guidance_scale": guidance_scale,
|
| 161 |
+
"optimizer_type": optimizer_type,
|
| 162 |
+
"mixed_precision": mixed_precision,
|
| 163 |
+
}
|
| 164 |
+
comet_experiment.log_parameters(hyper_params)
|
| 165 |
|
| 166 |
# Включение Flash Attention 2/SDPA
|
| 167 |
torch.backends.cuda.enable_flash_sdp(True)
|
|
|
|
| 171 |
|
| 172 |
# --------------------------- Загрузка моделей ---------------------------
|
| 173 |
# VAE загружается на CPU для экономии GPU-памяти (как в твоём оригинальном коде)
|
| 174 |
+
vae = AutoencoderKL.from_pretrained("AiArtLab/simplevae", subfolder="vae", torch_dtype=dtype).to("cpu").eval()
|
| 175 |
+
|
| 176 |
+
shift_factor = getattr(vae.config, "shift_factor", 0.0)
|
| 177 |
+
if shift_factor is None:
|
| 178 |
+
shift_factor = 0.0
|
| 179 |
+
|
| 180 |
+
scaling_factor = getattr(vae.config, "scaling_factor", 1.0)
|
| 181 |
+
if scaling_factor is None:
|
| 182 |
+
scaling_factor = 1.0
|
| 183 |
+
|
| 184 |
+
latents_mean = getattr(vae.config, "latents_mean", None)
|
| 185 |
+
latents_std = getattr(vae.config, "latents_std", None)
|
| 186 |
+
|
| 187 |
+
from diffusers import FlowMatchEulerDiscreteScheduler
|
| 188 |
+
|
| 189 |
+
# Подстрой под свои параметры
|
| 190 |
+
num_train_timesteps = 1000
|
| 191 |
+
|
| 192 |
+
scheduler = FlowMatchEulerDiscreteScheduler(
|
| 193 |
+
num_train_timesteps=num_train_timesteps,
|
| 194 |
+
#shift=3.0, # пример; подбирается при необходимости
|
| 195 |
+
#use_dynamic_shifting=True
|
| 196 |
)
|
| 197 |
|
| 198 |
|
|
|
|
| 299 |
embeddings = torch.tensor(np.array([item["embeddings"] for item in batch])).to(device,dtype=dtype)
|
| 300 |
return latents, embeddings
|
| 301 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
batch_sampler = DistributedResolutionBatchSampler(
|
| 303 |
dataset=dataset,
|
| 304 |
batch_size=batch_size,
|
|
|
|
| 321 |
if os.path.isdir(latest_checkpoint):
|
| 322 |
print("Загружаем UNet из чекпоинта:", latest_checkpoint)
|
| 323 |
unet = UNet2DConditionModel.from_pretrained(latest_checkpoint).to(device=device,dtype=dtype)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
if unet_gradient:
|
| 325 |
unet.enable_gradient_checkpointing()
|
| 326 |
unet.set_use_memory_efficient_attention_xformers(False)
|
|
|
|
| 330 |
print(f"Ошибка при включении SDPA: {e}")
|
| 331 |
unet.set_use_memory_efficient_attention_xformers(True)
|
| 332 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
else:
|
| 334 |
# FIX: если чекпоинта нет — прекращаем с понятной ошибкой (лучше, чем неожиданные NameError дальше)
|
| 335 |
raise FileNotFoundError(f"UNet checkpoint not found at {latest_checkpoint}. Положи UNet чекпоинт в {latest_checkpoint} или укажи другой путь.")
|
|
|
|
| 382 |
def create_optimizer(name, params):
|
| 383 |
if name == "adam8bit":
|
| 384 |
return bnb.optim.AdamW8bit(
|
| 385 |
+
params, lr=base_learning_rate, betas=(0.9, betta2), eps=eps, weight_decay=0.01,
|
| 386 |
percentile_clipping=percentile_clipping
|
| 387 |
)
|
| 388 |
elif name == "adam":
|
| 389 |
return torch.optim.AdamW(
|
| 390 |
params, lr=base_learning_rate, betas=(0.9, 0.999), eps=1e-8, weight_decay=0.01
|
| 391 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
else:
|
| 393 |
raise ValueError(f"Unknown optimizer: {name}")
|
| 394 |
|
|
|
|
| 420 |
if torch.isnan(param).any() or torch.isinf(param).any():
|
| 421 |
print(f"[rank {accelerator.process_index}] NaN/Inf in {name}")
|
| 422 |
unet, optimizer, lr_scheduler = accelerator.prepare(unet, optimizer, lr_scheduler)
|
| 423 |
+
|
| 424 |
+
if torch_compile:
|
| 425 |
+
print("compiling")
|
| 426 |
+
torch.set_float32_matmul_precision('high')
|
| 427 |
+
torch.backends.cudnn.allow_tf32 = True
|
| 428 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
| 429 |
+
unet = torch.compile(unet)#, mode='max-autotune')
|
| 430 |
+
print("compiling - ok")
|
| 431 |
|
| 432 |
# --------------------------- Фиксированные семплы для генерации ---------------------------
|
| 433 |
fixed_samples = get_fixed_samples_by_resolution(dataset)
|
| 434 |
|
| 435 |
+
def get_negative_embedding(neg_prompt="", batch_size=1):
|
| 436 |
+
"""
|
| 437 |
+
Возвращает эмбеддинг негативного промпта с батчем.
|
| 438 |
+
Загружает модели, вычисляет эмбеддинг, выгружает модели на CPU.
|
| 439 |
+
"""
|
| 440 |
+
import torch
|
| 441 |
+
from transformers import AutoTokenizer, AutoModel
|
| 442 |
+
|
| 443 |
+
# Настройки
|
| 444 |
+
dtype = torch.float16
|
| 445 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 446 |
+
|
| 447 |
+
# Загрузка моделей (если ещё не загружены)
|
| 448 |
+
if not hasattr(get_negative_embedding, "tokenizer"):
|
| 449 |
+
get_negative_embedding.tokenizer = AutoTokenizer.from_pretrained(
|
| 450 |
+
"Qwen/Qwen3-Embedding-0.6B", padding_side="left"
|
| 451 |
+
)
|
| 452 |
+
get_negative_embedding.text_model = AutoModel.from_pretrained(
|
| 453 |
+
"Qwen/Qwen3-Embedding-0.6B"
|
| 454 |
+
).to(device).eval()
|
| 455 |
+
|
| 456 |
+
# Вспомогательная функция для пулинга
|
| 457 |
+
def last_token_pool(last_hidden_states, attention_mask):
|
| 458 |
+
left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
|
| 459 |
+
if left_padding:
|
| 460 |
+
return last_hidden_states[:, -1]
|
| 461 |
+
else:
|
| 462 |
+
sequence_lengths = attention_mask.sum(dim=1) - 1
|
| 463 |
+
batch_size = last_hidden_states.shape[0]
|
| 464 |
+
return last_hidden_states[
|
| 465 |
+
torch.arange(batch_size, device=last_hidden_states.device),
|
| 466 |
+
sequence_lengths
|
| 467 |
+
]
|
| 468 |
+
|
| 469 |
+
# Вычисление эмбеддинга
|
| 470 |
+
def encode_texts(texts, max_length=512):
|
| 471 |
+
with torch.inference_mode():
|
| 472 |
+
toks = get_negative_embedding.tokenizer(
|
| 473 |
+
texts, return_tensors="pt", padding="max_length", truncation=True, max_length=max_length
|
| 474 |
+
).to(device)
|
| 475 |
+
outs = get_negative_embedding.text_model(**toks)
|
| 476 |
+
emb = last_token_pool(outs.last_hidden_state, toks["attention_mask"])
|
| 477 |
+
emb = emb.unsqueeze(1) # Добавляем размерность sequence_length
|
| 478 |
+
return emb
|
| 479 |
+
|
| 480 |
+
# Возвращаем эмбеддинг
|
| 481 |
+
if not neg_prompt:
|
| 482 |
+
hidden_dim = 1024 # Размерность эмбеддинга Qwen3-Embedding-0.6B
|
| 483 |
+
seq_len = 150
|
| 484 |
+
return torch.zeros((batch_size, seq_len, hidden_dim), dtype=dtype, device=device)
|
| 485 |
+
|
| 486 |
+
uncond_emb = encode_texts([neg_prompt]).to(dtype=dtype, device=device)
|
| 487 |
+
uncond_emb = uncond_emb.repeat(batch_size, 1, 1) # Добавляем батч
|
| 488 |
+
|
| 489 |
+
# Выгружаем модели
|
| 490 |
+
if hasattr(get_negative_embedding, "text_model"):
|
| 491 |
+
get_negative_embedding.text_model = get_negative_embedding.text_model.to("cpu")
|
| 492 |
+
if hasattr(get_negative_embedding, "tokenizer"):
|
| 493 |
+
del get_negative_embedding.tokenizer # Освобождаем память
|
| 494 |
+
torch.cuda.empty_cache()
|
| 495 |
+
|
| 496 |
+
return uncond_emb
|
| 497 |
+
|
| 498 |
+
uncond_emb = get_negative_embedding("low quality")
|
| 499 |
+
|
| 500 |
@torch.compiler.disable()
|
| 501 |
@torch.no_grad()
|
| 502 |
+
def generate_and_save_samples(fixed_samples_cpu,empty_embeddings, step):
|
| 503 |
original_model = None
|
| 504 |
try:
|
| 505 |
+
# безопасный unwrap: если компилировано, unwrap не нужен
|
| 506 |
+
if not torch_compile:
|
| 507 |
+
original_model = accelerator.unwrap_model(unet, keep_torch_compile=True).eval()
|
| 508 |
+
else:
|
| 509 |
+
original_model = unet.eval()
|
| 510 |
+
|
| 511 |
vae.to(device=device).eval() # временно подгружаем VAE на GPU для декодинга
|
| 512 |
|
|
|
|
| 513 |
|
| 514 |
all_generated_images = []
|
| 515 |
all_captions = []
|
|
|
|
| 518 |
width, height = size
|
| 519 |
sample_latents = sample_latents.to(dtype=dtype, device=device)
|
| 520 |
sample_text_embeddings = sample_text_embeddings.to(dtype=dtype, device=device)
|
| 521 |
+
|
| 522 |
+
# начальный шум
|
| 523 |
+
latents = torch.randn(
|
| 524 |
sample_latents.shape,
|
|
|
|
| 525 |
device=device,
|
| 526 |
+
dtype=sample_latents.dtype,
|
| 527 |
+
generator=torch.Generator(device=device).manual_seed(seed)
|
| 528 |
)
|
| 529 |
+
|
| 530 |
+
# подготовим timesteps через шедулер
|
| 531 |
+
scheduler.set_timesteps(n_diffusion_steps, device=device)
|
| 532 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 533 |
for t in scheduler.timesteps:
|
| 534 |
+
# guidance: удваиваем батч
|
| 535 |
+
if guidance_scale != 1:
|
| 536 |
+
latent_model_input = torch.cat([latents, latents], dim=0)
|
|
|
|
|
|
|
| 537 |
|
| 538 |
+
# empty_embeddings: [1, 1, hidden_dim] → повторяем по seq_len и batch
|
| 539 |
+
seq_len = sample_text_embeddings.shape[1]
|
| 540 |
+
hidden_dim = sample_text_embeddings.shape[2]
|
| 541 |
+
empty_embeddings_exp = empty_embeddings.expand(-1, seq_len, hidden_dim) # [1, seq_len, hidden_dim]
|
| 542 |
+
empty_embeddings_exp = empty_embeddings_exp.repeat(sample_text_embeddings.shape[0], 1, 1) # [batch, seq_len, hidden_dim]
|
| 543 |
|
| 544 |
+
text_embeddings_batch = torch.cat([empty_embeddings_exp, sample_text_embeddings], dim=0)
|
| 545 |
+
else:
|
| 546 |
+
latent_model_input = latents
|
| 547 |
+
text_embeddings_batch = sample_text_embeddings
|
| 548 |
+
|
| 549 |
+
|
| 550 |
+
|
| 551 |
+
# предсказание потока (velocity)
|
| 552 |
+
model_out = original_model(latent_model_input, t, encoder_hidden_states=text_embeddings_batch)
|
| 553 |
+
flow = getattr(model_out, "sample", model_out)
|
| 554 |
+
|
| 555 |
+
# guidance объединение
|
| 556 |
+
if guidance_scale != 1:
|
| 557 |
+
flow_uncond, flow_cond = flow.chunk(2)
|
| 558 |
+
flow = flow_uncond + guidance_scale * (flow_cond - flow_uncond)
|
| 559 |
+
|
| 560 |
+
# шаг через scheduler
|
| 561 |
+
latents = scheduler.step(flow, t, latents).prev_sample
|
| 562 |
+
|
| 563 |
+
current_latents = latents
|
| 564 |
|
|
|
|
| 565 |
|
| 566 |
+
# Параметры нормализации
|
| 567 |
+
latent_for_vae = current_latents.detach() / scaling_factor + shift_factor
|
| 568 |
+
|
| 569 |
+
decoded = vae.decode(latent_for_vae.to(torch.float32)).sample
|
| 570 |
+
#decoded = decoded[:, :, 0, :, :] # [3, H, W]
|
| 571 |
+
#print(decoded.ndim, decoded.shape)
|
| 572 |
|
| 573 |
decoded_fp32 = decoded.to(torch.float32)
|
| 574 |
for img_idx, img_tensor in enumerate(decoded_fp32):
|
| 575 |
+
|
| 576 |
+
# Форма: [3, H, W] -> преобразуем в [H, W, 3]
|
| 577 |
+
img = (img_tensor / 2 + 0.5).clamp(0, 1).cpu().numpy()
|
| 578 |
+
img = img.transpose(1, 2, 0) # Из [3, H, W] в [H, W, 3]
|
| 579 |
+
|
| 580 |
+
#img = (img_tensor / 2 + 0.5).clamp(0, 1).cpu().numpy().transpose(1, 2, 0)
|
| 581 |
if np.isnan(img).any():
|
| 582 |
print("NaNs found, saving stopped! Step:", step)
|
| 583 |
pil_img = Image.fromarray((img * 255).astype("uint8"))
|
|
|
|
| 601 |
wandb.Image(img, caption=f"{all_captions[i]}")
|
| 602 |
for i, img in enumerate(all_generated_images)
|
| 603 |
]
|
| 604 |
+
wandb.log({"generated_images": wandb_images})
|
| 605 |
+
if use_comet_ml and accelerator.is_main_process:
|
| 606 |
+
for i, img in enumerate(all_generated_images):
|
| 607 |
+
comet_experiment.log_image(
|
| 608 |
+
image_data=img,
|
| 609 |
+
name=f"step_{step}_img_{i}",
|
| 610 |
+
step=step,
|
| 611 |
+
metadata={
|
| 612 |
+
"caption": all_captions[i],
|
| 613 |
+
"width": img.width,
|
| 614 |
+
"height": img.height,
|
| 615 |
+
"global_step": step
|
| 616 |
+
}
|
| 617 |
+
)
|
| 618 |
finally:
|
| 619 |
# вернуть VAE на CPU (как было в твоём коде)
|
| 620 |
vae.to("cpu")
|
|
|
|
| 628 |
if accelerator.is_main_process:
|
| 629 |
if save_model:
|
| 630 |
print("Генерация сэмплов до старта обучения...")
|
| 631 |
+
generate_and_save_samples(fixed_samples,uncond_emb,0)
|
| 632 |
accelerator.wait_for_everyone()
|
| 633 |
|
| 634 |
# Модифицируем функцию сохранения модели для поддержки LoRA
|
| 635 |
+
def save_checkpoint(unet, variant=""):
|
| 636 |
if accelerator.is_main_process:
|
| 637 |
if lora_name:
|
| 638 |
save_lora_checkpoint(unet)
|
| 639 |
else:
|
| 640 |
+
# безопасный unwrap для компилированной модели
|
| 641 |
+
model_to_save = None
|
| 642 |
+
if not torch_compile:
|
| 643 |
+
model_to_save = accelerator.unwrap_model(unet)
|
| 644 |
else:
|
| 645 |
+
model_to_save = unet
|
|
|
|
| 646 |
|
| 647 |
+
if variant != "":
|
| 648 |
+
model_to_save.to(dtype=torch.float16).save_pretrained(
|
| 649 |
+
os.path.join(checkpoints_folder, f"{project}"), variant=variant
|
| 650 |
+
)
|
| 651 |
+
else:
|
| 652 |
+
model_to_save.save_pretrained(os.path.join(checkpoints_folder, f"{project}"))
|
| 653 |
|
| 654 |
+
unet = unet.to(dtype=dtype)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 655 |
|
| 656 |
# --------------------------- Тренировочный цикл ---------------------------
|
| 657 |
if accelerator.is_main_process:
|
|
|
|
| 662 |
|
| 663 |
steps_per_epoch = len(dataloader)
|
| 664 |
sample_interval = max(1, steps_per_epoch // sample_interval_share)
|
| 665 |
+
min_loss = 2.
|
| 666 |
|
| 667 |
for epoch in range(start_epoch, start_epoch + num_epochs):
|
| 668 |
batch_losses = []
|
|
|
|
| 669 |
batch_grads = []
|
| 670 |
batch_sampler.set_epoch(epoch)
|
| 671 |
accelerator.wait_for_everyone()
|
| 672 |
unet.train()
|
| 673 |
+
#print("epoch:",epoch)
|
| 674 |
for step, (latents, embeddings) in enumerate(dataloader):
|
| 675 |
with accelerator.accumulate(unet):
|
| 676 |
if save_model == False and step == 5 :
|
| 677 |
used_gb = torch.cuda.max_memory_allocated() / 1024**3
|
| 678 |
print(f"Шаг {step}: {used_gb:.2f} GB")
|
| 679 |
+
|
| 680 |
+
# шум
|
| 681 |
noise = torch.randn_like(latents, dtype=latents.dtype)
|
| 682 |
+
|
| 683 |
+
# берём t из [0, 1]
|
| 684 |
+
t = torch.rand(latents.shape[0], device=latents.device, dtype=latents.dtype)
|
| 685 |
+
|
| 686 |
+
# интерполяция между x0 и шумом
|
| 687 |
+
noisy_latents = (1.0 - t.view(-1, 1, 1, 1)) * latents + t.view(-1, 1, 1, 1) * noise
|
| 688 |
+
|
| 689 |
+
# делаем integer timesteps для UNet
|
| 690 |
+
timesteps = (t * scheduler.config.num_train_timesteps).long()
|
| 691 |
+
|
| 692 |
+
# предсказание потока (Flow)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 693 |
model_pred = unet(noisy_latents, timesteps, embeddings).sample
|
| 694 |
+
|
| 695 |
+
# таргет — векторное поле (= разность между конечными точками)
|
| 696 |
+
target = noise - latents # или latents - noise?
|
| 697 |
+
|
| 698 |
+
# MSE лосс
|
| 699 |
+
mse_loss = F.mse_loss(model_pred.float(), target.float())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 700 |
|
| 701 |
# Сохраняем для логов (мы сохраняем MSE отдельно — как показатель)
|
| 702 |
batch_losses.append(mse_loss.detach().item())
|
|
|
|
| 705 |
accelerator.wait_for_everyone()
|
| 706 |
|
| 707 |
# Backward
|
| 708 |
+
accelerator.backward(mse_loss)
|
| 709 |
|
| 710 |
if (global_step % 100 == 0) or (global_step % sample_interval == 0):
|
| 711 |
accelerator.wait_for_everyone()
|
|
|
|
| 720 |
lr_scheduler.step()
|
| 721 |
optimizer.zero_grad(set_to_none=True)
|
| 722 |
|
| 723 |
+
if accelerator.sync_gradients:
|
| 724 |
+
global_step += 1
|
| 725 |
+
progress_bar.update(1)
|
| 726 |
+
# Логируем метрики
|
| 727 |
+
if accelerator.is_main_process:
|
| 728 |
+
if fbp:
|
| 729 |
+
current_lr = base_learning_rate
|
| 730 |
+
else:
|
| 731 |
+
current_lr = lr_scheduler.get_last_lr()[0]
|
| 732 |
+
batch_grads.append(grad)
|
| 733 |
+
|
| 734 |
+
log_data = {}
|
| 735 |
+
log_data["loss"] = mse_loss.detach().item()
|
| 736 |
+
log_data["lr"] = current_lr
|
| 737 |
+
log_data["grad"] = grad
|
| 738 |
+
if accelerator.sync_gradients:
|
| 739 |
+
if use_wandb:
|
| 740 |
+
wandb.log(log_data, step=global_step)
|
| 741 |
+
if use_comet_ml:
|
| 742 |
+
comet_experiment.log_metrics(log_data, step=global_step)
|
| 743 |
+
|
| 744 |
+
# Генерируем сэмплы с заданным интервалом
|
| 745 |
+
if global_step % sample_interval == 0:
|
| 746 |
+
generate_and_save_samples(fixed_samples,uncond_emb, global_step)
|
| 747 |
+
last_n = sample_interval
|
| 748 |
+
|
| 749 |
+
if save_model:
|
| 750 |
+
avg_sample_loss = np.mean(batch_losses[-sample_interval:]) if len(batch_losses) > 0 else 0.0
|
| 751 |
+
print("saving:", avg_sample_loss < min_loss * save_barrier, "Avg:", avg_sample_loss)
|
| 752 |
+
if avg_sample_loss is not None and avg_sample_loss < min_loss * save_barrier:
|
| 753 |
+
min_loss = avg_sample_loss
|
| 754 |
+
save_checkpoint(unet)
|
| 755 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 756 |
|
| 757 |
if accelerator.is_main_process:
|
| 758 |
+
# local averages
|
| 759 |
+
avg_epoch_loss = np.mean(batch_losses) if len(batch_losses) > 0 else 0.0
|
| 760 |
+
avg_epoch_grad = np.mean(batch_grads) if len(batch_grads) > 0 else 0.0
|
| 761 |
+
|
| 762 |
print(f"\nЭпоха {epoch} завершена. Средний лосс: {avg_epoch_loss:.6f}")
|
| 763 |
+
log_data_ep = {
|
| 764 |
+
"epoch_loss": avg_epoch_loss,
|
| 765 |
+
"epoch_grad": avg_epoch_grad,
|
| 766 |
+
"epoch": epoch + 1,
|
| 767 |
+
}
|
| 768 |
if use_wandb:
|
| 769 |
+
wandb.log(log_data_ep)
|
| 770 |
+
if use_comet_ml:
|
| 771 |
+
comet_experiment.log_metrics(log_data_ep)
|
| 772 |
|
| 773 |
# Завершение обучения - сохраняем финальную модель
|
|
|
|
|
|
|
| 774 |
if accelerator.is_main_process:
|
| 775 |
print("Обучение завершено! Сохраняем финальную модель...")
|
| 776 |
if save_model:
|
| 777 |
save_checkpoint(unet,"fp16")
|
| 778 |
+
if use_comet_ml:
|
| 779 |
+
comet_experiment.end()
|
| 780 |
accelerator.free_memory()
|
| 781 |
if torch.distributed.is_initialized():
|
| 782 |
torch.distributed.destroy_process_group()
|
train_flow_test.py
DELETED
|
@@ -1,770 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import math
|
| 3 |
-
import torch
|
| 4 |
-
import numpy as np
|
| 5 |
-
import matplotlib.pyplot as plt
|
| 6 |
-
from torch.utils.data import DataLoader, Sampler
|
| 7 |
-
from collections import defaultdict
|
| 8 |
-
from torch.optim.lr_scheduler import LambdaLR
|
| 9 |
-
from diffusers import UNet2DConditionModel, AutoencoderKL, DDPMScheduler
|
| 10 |
-
from accelerate import Accelerator
|
| 11 |
-
from datasets import load_from_disk
|
| 12 |
-
from tqdm import tqdm
|
| 13 |
-
from PIL import Image,ImageOps
|
| 14 |
-
import wandb
|
| 15 |
-
import random
|
| 16 |
-
import gc
|
| 17 |
-
from accelerate.state import DistributedType
|
| 18 |
-
from torch.distributed import broadcast_object_list
|
| 19 |
-
from torch.utils.checkpoint import checkpoint
|
| 20 |
-
from diffusers.models.attention_processor import AttnProcessor2_0
|
| 21 |
-
from datetime import datetime
|
| 22 |
-
import bitsandbytes as bnb
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# region scheduler start
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
#@title scheduler
|
| 29 |
-
# Copyright 2024 The HuggingFace Team. All rights reserved.
|
| 30 |
-
#
|
| 31 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 32 |
-
# you may not use this file except in compliance with the License.
|
| 33 |
-
# You may obtain a copy of the License at
|
| 34 |
-
#
|
| 35 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
| 36 |
-
#
|
| 37 |
-
# Unless required by applicable law or agreed to in writing, software
|
| 38 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 39 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 40 |
-
# See the License for the specific language governing permissions and
|
| 41 |
-
# limitations under the License.
|
| 42 |
-
|
| 43 |
-
# DISCLAIMER: This code is strongly influenced by https://github.com/leffff/euler-scheduler
|
| 44 |
-
|
| 45 |
-
from dataclasses import dataclass
|
| 46 |
-
from typing import Tuple, Any, Optional, Union
|
| 47 |
-
|
| 48 |
-
import torch
|
| 49 |
-
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 50 |
-
from diffusers.utils import BaseOutput
|
| 51 |
-
from diffusers.schedulers.scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
@dataclass
|
| 55 |
-
class FlowMatchingEulerSchedulerOutput(BaseOutput):
|
| 56 |
-
"""
|
| 57 |
-
Output class for the scheduler's `step` function output.
|
| 58 |
-
|
| 59 |
-
Args:
|
| 60 |
-
prev_sample (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images):
|
| 61 |
-
Computed sample `(x_{t-1})` of previous timestep (which in flow-matching notation should be noted as
|
| 62 |
-
`(x_{t+h})`). `prev_sample` should be used as next model input in the denoising loop.
|
| 63 |
-
pred_original_sample (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images):
|
| 64 |
-
The predicted denoised sample `(x_{0})` (which in flow-matching notation should be noted as
|
| 65 |
-
`(x_{1})`) based on the model output from the current timestep.
|
| 66 |
-
`pred_original_sample` can be used to preview progress or for guidance.
|
| 67 |
-
"""
|
| 68 |
-
|
| 69 |
-
prev_sample: torch.Tensor
|
| 70 |
-
pred_original_sample: Optional[torch.Tensor] = None
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
def get_time_coefficients(timestep: torch.Tensor, ndim: int) -> torch.Tensor:
|
| 74 |
-
"""
|
| 75 |
-
Convert timestep to time coefficients.
|
| 76 |
-
Args:
|
| 77 |
-
timestep (`torch.Tensor`): Timestep tensor.
|
| 78 |
-
ndim (`int`): Number of dimensions.
|
| 79 |
-
Returns:
|
| 80 |
-
`torch.Tensor`: Time coefficients.
|
| 81 |
-
"""
|
| 82 |
-
return timestep.reshape((timestep.shape[0], *([1] * (ndim - 1) )))
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
class FlowMatchingEulerScheduler(SchedulerMixin, ConfigMixin):
|
| 86 |
-
"""
|
| 87 |
-
`FlowMatchingEulerScheduler` is a scheduler for training and inferencing Conditional Flow Matching models (CFMs).
|
| 88 |
-
|
| 89 |
-
Flow Matching (FM) is a novel, simulation-free methodology for training Continuous Normalizing Flows (CNFs) by
|
| 90 |
-
regressing vector fields of predetermined conditional probability paths, facilitating scalable training and
|
| 91 |
-
efficient sample generation through the utilization of various probability paths, including Gaussian and
|
| 92 |
-
Optimal Transport (OT) paths, thereby enhancing model performance and generalization capabilities
|
| 93 |
-
|
| 94 |
-
Args:
|
| 95 |
-
num_inference_steps (`int`, defaults to 100):
|
| 96 |
-
The number of steps on inference.
|
| 97 |
-
"""
|
| 98 |
-
|
| 99 |
-
@register_to_config
|
| 100 |
-
def __init__(self, num_inference_steps: int = 100):
|
| 101 |
-
self.timesteps = None
|
| 102 |
-
self.num_inference_steps = None
|
| 103 |
-
self.h = None
|
| 104 |
-
|
| 105 |
-
if num_inference_steps is not None:
|
| 106 |
-
self.set_timesteps(num_inference_steps)
|
| 107 |
-
|
| 108 |
-
@staticmethod
|
| 109 |
-
def add_noise(original_samples: torch.Tensor, noise: torch.Tensor, timestep: torch.Tensor) -> torch.Tensor:
|
| 110 |
-
"""
|
| 111 |
-
Add noise to the given sample
|
| 112 |
-
|
| 113 |
-
Args:
|
| 114 |
-
original_samples (`torch.Tensor`):
|
| 115 |
-
The original sample that is to be noised
|
| 116 |
-
noise (`torch.Tensor`):
|
| 117 |
-
The noise that is used to noise the image
|
| 118 |
-
timestep (`torch.Tensor`):
|
| 119 |
-
Timestep used to create linear interpolation `x_t = t * x_1 + (1 - t) * x_0`.
|
| 120 |
-
Where x_1 is a target distribution, x_0 is a source distribution and t (timestep) ∈ [0, 1]
|
| 121 |
-
"""
|
| 122 |
-
|
| 123 |
-
t = get_time_coefficients(timestep, original_samples.ndim)
|
| 124 |
-
|
| 125 |
-
noised_sample = t * original_samples + (1 - t) * noise
|
| 126 |
-
|
| 127 |
-
return noised_sample
|
| 128 |
-
|
| 129 |
-
def set_timesteps(self, num_inference_steps: int = 100) -> None:
|
| 130 |
-
"""
|
| 131 |
-
Set number of inference steps (Euler intagration steps)
|
| 132 |
-
|
| 133 |
-
Args:
|
| 134 |
-
num_inference_steps (`int`, defaults to 100):
|
| 135 |
-
The number of steps on inference.
|
| 136 |
-
"""
|
| 137 |
-
|
| 138 |
-
self.num_inference_steps = num_inference_steps
|
| 139 |
-
self.h = 1 / num_inference_steps
|
| 140 |
-
self.timesteps = torch.arange(0, 1, self.h)
|
| 141 |
-
|
| 142 |
-
def step(self, model_output: torch.Tensor, timestep: torch.Tensor, sample: torch.Tensor,
|
| 143 |
-
return_dict: bool = True) -> Union[FlowMatchingEulerSchedulerOutput, Tuple]:
|
| 144 |
-
"""
|
| 145 |
-
Predict the sample from the previous timestep by reversing the SDE. This function propagates the diffusion
|
| 146 |
-
process from the learned model outputs (most often the predicted noise).
|
| 147 |
-
|
| 148 |
-
Args:
|
| 149 |
-
model_output (`torch.Tensor`):
|
| 150 |
-
The direct output from learned diffusion model.
|
| 151 |
-
timestep (`float`):
|
| 152 |
-
Timestep used to perform Euler Method `x_t = h * f(x_t, t) + x_{t-1}`.
|
| 153 |
-
Where x_1 is a target distribution, x_0 is a source distribution and t (timestep) ∈ [0, 1]
|
| 154 |
-
sample (`torch.Tensor`):
|
| 155 |
-
A current instance of a sample created by the diffusion process.
|
| 156 |
-
return_dict (`bool`, *optional*, defaults to `True`):
|
| 157 |
-
Whether or not to return a [`~schedulers.scheduling_ddpm.DDPMSchedulerOutput`] or `tuple`.
|
| 158 |
-
|
| 159 |
-
Returns:
|
| 160 |
-
[`~schedulers.scheduling_ddpm.DDPMSchedulerOutput`] or `tuple`:
|
| 161 |
-
If return_dict is `True`, [`~schedulers.scheduling_ddpm.DDPMSchedulerOutput`] is returned, otherwise a
|
| 162 |
-
tuple is returned where the first element is the sample tensor.
|
| 163 |
-
"""
|
| 164 |
-
|
| 165 |
-
step = FlowMatchingEulerSchedulerOutput(
|
| 166 |
-
prev_sample=sample + self.h * model_output,
|
| 167 |
-
pred_original_sample=sample + (1 - get_time_coefficients(timestep, model_output.ndim)) * model_output
|
| 168 |
-
)
|
| 169 |
-
|
| 170 |
-
if return_dict:
|
| 171 |
-
return step
|
| 172 |
-
|
| 173 |
-
return step.prev_sample,
|
| 174 |
-
|
| 175 |
-
@staticmethod
|
| 176 |
-
def get_velocity(original_samples: torch.Tensor, noise: torch.Tensor) -> torch.Tensor:
|
| 177 |
-
"""
|
| 178 |
-
Predict the sample from the previous timestep by reversing the SDE. This function propagates the diffusion
|
| 179 |
-
process from the learned model outputs (most often the predicted noise).
|
| 180 |
-
|
| 181 |
-
Args:
|
| 182 |
-
original_samples (`torch.Tensor`):
|
| 183 |
-
The original sample that is to be noised
|
| 184 |
-
noise (`torch.Tensor`):
|
| 185 |
-
The noise that is used to noise the image
|
| 186 |
-
|
| 187 |
-
Returns:
|
| 188 |
-
`torch.Tensor`
|
| 189 |
-
"""
|
| 190 |
-
|
| 191 |
-
return original_samples - noise
|
| 192 |
-
|
| 193 |
-
@staticmethod
|
| 194 |
-
def scale_model_input(sample: torch.Tensor, timestep: Optional[int] = None) -> torch.Tensor:
|
| 195 |
-
"""
|
| 196 |
-
Ensures interchangeability with schedulers that need to scale the denoising model input depending on the
|
| 197 |
-
current timestep.
|
| 198 |
-
|
| 199 |
-
Args:
|
| 200 |
-
sample (`torch.Tensor`):
|
| 201 |
-
The input sample.
|
| 202 |
-
timestep (`int`, *optional*):
|
| 203 |
-
The current timestep in the diffusion chain.
|
| 204 |
-
|
| 205 |
-
Returns:
|
| 206 |
-
`torch.Tensor`:
|
| 207 |
-
A scaled input sample.
|
| 208 |
-
"""
|
| 209 |
-
|
| 210 |
-
return sample
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
# region scheduler end
|
| 217 |
-
|
| 218 |
-
# --------------------------- Параметры ---------------------------
|
| 219 |
-
save_path = "datasets/768" # "datasets/576" #"datasets/576p2" #"datasets/1152p2" #"datasets/576p2" #"datasets/dataset384_temp" #"datasets/dataset384" #"datasets/imagenet-1kk" #"datasets/siski576" #"datasets/siski384" #"datasets/siski64" #"datasets/mnist"
|
| 220 |
-
batch_size = 30 #26 #45 #11 #45 #555 #35 #7
|
| 221 |
-
base_learning_rate = 4e-6 #9.5e-7 #9e-7 #2e-6 #1e-6 #9e-7 #1e-6 #2e-6 #1e-6 #2e-6 #6e-6 #2e-6 #8e-7 #6e-6 #2e-5 #4e-5 #3e-5 #5e-5 #8e-5
|
| 222 |
-
min_learning_rate = 2.5e-5 #2e-5
|
| 223 |
-
num_epochs = 1 #2 #36 #18
|
| 224 |
-
project = "sdxs"
|
| 225 |
-
<<<<<<< HEAD
|
| 226 |
-
use_wandb = True
|
| 227 |
-
save_model = True
|
| 228 |
-
=======
|
| 229 |
-
use_wandb = False
|
| 230 |
-
save_model = False
|
| 231 |
-
>>>>>>> d0c94e4 (sdxxxs)
|
| 232 |
-
limit = 0 #200000 #0
|
| 233 |
-
checkpoints_folder = ""
|
| 234 |
-
|
| 235 |
-
# Параметры для диффузии
|
| 236 |
-
n_diffusion_steps = 40
|
| 237 |
-
samples_to_generate = 12
|
| 238 |
-
guidance_scale = 5
|
| 239 |
-
sample_interval_share = 25 # samples/save per epoch
|
| 240 |
-
|
| 241 |
-
# Папки для сохранения результатов
|
| 242 |
-
generated_folder = "samples"
|
| 243 |
-
os.makedirs(generated_folder, exist_ok=True)
|
| 244 |
-
|
| 245 |
-
# Настройка seed для воспроизводимости
|
| 246 |
-
current_date = datetime.now()
|
| 247 |
-
seed = int(current_date.strftime("%Y%m%d"))
|
| 248 |
-
fixed_seed = True
|
| 249 |
-
if fixed_seed:
|
| 250 |
-
torch.manual_seed(seed)
|
| 251 |
-
np.random.seed(seed)
|
| 252 |
-
random.seed(seed)
|
| 253 |
-
if torch.cuda.is_available():
|
| 254 |
-
torch.cuda.manual_seed_all(seed)
|
| 255 |
-
|
| 256 |
-
# --------------------------- Параметры LoRA ---------------------------
|
| 257 |
-
# pip install peft
|
| 258 |
-
lora_name = "" #"nusha" # Имя для сохранения/загрузки LoRA адаптеров
|
| 259 |
-
lora_rank = 32 # Ранг LoRA (чем меньше, тем компактнее модель)
|
| 260 |
-
lora_alpha = 64 # Альфа параметр LoRA, определ��ющий масштаб
|
| 261 |
-
|
| 262 |
-
print("init")
|
| 263 |
-
# Включение Flash Attention 2/SDPA
|
| 264 |
-
torch.backends.cuda.enable_flash_sdp(True)
|
| 265 |
-
# --------------------------- Инициализация Accelerator --------------------
|
| 266 |
-
dtype = torch.bfloat16
|
| 267 |
-
accelerator = Accelerator(mixed_precision="bf16")
|
| 268 |
-
device = accelerator.device
|
| 269 |
-
gen = torch.Generator(device=device)
|
| 270 |
-
gen.manual_seed(seed)
|
| 271 |
-
|
| 272 |
-
# --------------------------- Инициализация WandB ---------------------------
|
| 273 |
-
if use_wandb and accelerator.is_main_process:
|
| 274 |
-
wandb.init(project=project+lora_name, config={
|
| 275 |
-
"batch_size": batch_size,
|
| 276 |
-
"base_learning_rate": base_learning_rate,
|
| 277 |
-
"num_epochs": num_epochs,
|
| 278 |
-
"n_diffusion_steps": n_diffusion_steps,
|
| 279 |
-
"samples_to_generate": samples_to_generate,
|
| 280 |
-
"dtype": str(dtype)
|
| 281 |
-
})
|
| 282 |
-
|
| 283 |
-
# --------------------------- Загрузка датасета ---------------------------
|
| 284 |
-
class ResolutionBatchSampler(Sampler):
|
| 285 |
-
"""Сэмплер, который группирует примеры по одинаковым размерам"""
|
| 286 |
-
def __init__(self, dataset, batch_size, shuffle=True, drop_last=False):
|
| 287 |
-
self.dataset = dataset
|
| 288 |
-
self.batch_size = batch_size
|
| 289 |
-
self.shuffle = shuffle
|
| 290 |
-
self.drop_last = drop_last
|
| 291 |
-
|
| 292 |
-
# Группируем примеры по размерам
|
| 293 |
-
self.size_groups = defaultdict(list)
|
| 294 |
-
|
| 295 |
-
try:
|
| 296 |
-
widths = dataset["width"]
|
| 297 |
-
heights = dataset["height"]
|
| 298 |
-
except KeyError:
|
| 299 |
-
widths = [0] * len(dataset)
|
| 300 |
-
heights = [0] * len(dataset)
|
| 301 |
-
|
| 302 |
-
for i, (w, h) in enumerate(zip(widths, heights)):
|
| 303 |
-
size = (w, h)
|
| 304 |
-
self.size_groups[size].append(i)
|
| 305 |
-
|
| 306 |
-
# Печатаем статистику по размерам
|
| 307 |
-
print(f"Найдено {len(self.size_groups)} уникальных размеров:")
|
| 308 |
-
for size, indices in sorted(self.size_groups.items(), key=lambda x: len(x[1]), reverse=True):
|
| 309 |
-
width, height = size
|
| 310 |
-
print(f" {width}x{height}: {len(indices)} примеров")
|
| 311 |
-
|
| 312 |
-
# Формируем батчи
|
| 313 |
-
self.reset()
|
| 314 |
-
|
| 315 |
-
def reset(self):
|
| 316 |
-
"""Сбрасывает и перемешивает индексы"""
|
| 317 |
-
self.batches = []
|
| 318 |
-
|
| 319 |
-
for size, indices in self.size_groups.items():
|
| 320 |
-
if self.shuffle:
|
| 321 |
-
indices_copy = indices.copy()
|
| 322 |
-
random.shuffle(indices_copy)
|
| 323 |
-
else:
|
| 324 |
-
indices_copy = indices
|
| 325 |
-
|
| 326 |
-
# Разбиваем на батчи
|
| 327 |
-
for i in range(0, len(indices_copy), self.batch_size):
|
| 328 |
-
batch_indices = indices_copy[i:i + self.batch_size]
|
| 329 |
-
|
| 330 |
-
# Пропускаем неполные батчи если drop_last=True
|
| 331 |
-
if self.drop_last and len(batch_indices) < self.batch_size:
|
| 332 |
-
continue
|
| 333 |
-
|
| 334 |
-
self.batches.append(batch_indices)
|
| 335 |
-
|
| 336 |
-
# Перемешиваем батчи между собой
|
| 337 |
-
if self.shuffle:
|
| 338 |
-
random.shuffle(self.batches)
|
| 339 |
-
|
| 340 |
-
def __iter__(self):
|
| 341 |
-
self.reset() # Сбрасываем и перемешиваем в начале каждой эпохи
|
| 342 |
-
return iter(self.batches)
|
| 343 |
-
|
| 344 |
-
def __len__(self):
|
| 345 |
-
return len(self.batches)
|
| 346 |
-
|
| 347 |
-
# Функция для выборки фиксированных семплов по размерам
|
| 348 |
-
def get_fixed_samples_by_resolution(dataset, samples_per_group=1):
|
| 349 |
-
"""Выбирает фиксированные семплы для каждого уникального разрешения"""
|
| 350 |
-
# Группируем по размерам
|
| 351 |
-
size_groups = defaultdict(list)
|
| 352 |
-
try:
|
| 353 |
-
widths = dataset["width"]
|
| 354 |
-
heights = dataset["height"]
|
| 355 |
-
except KeyError:
|
| 356 |
-
widths = [0] * len(dataset)
|
| 357 |
-
heights = [0] * len(dataset)
|
| 358 |
-
for i, (w, h) in enumerate(zip(widths, heights)):
|
| 359 |
-
size = (w, h)
|
| 360 |
-
size_groups[size].append(i)
|
| 361 |
-
|
| 362 |
-
# Выбираем фиксированные примеры из каждой группы
|
| 363 |
-
fixed_samples = {}
|
| 364 |
-
for size, indices in size_groups.items():
|
| 365 |
-
# Определяем сколько семплов брать из этой группы
|
| 366 |
-
n_samples = min(samples_per_group, len(indices))
|
| 367 |
-
if len(size_groups)==1:
|
| 368 |
-
n_samples = samples_to_generate
|
| 369 |
-
if n_samples == 0:
|
| 370 |
-
continue
|
| 371 |
-
|
| 372 |
-
# Выбираем случайные индексы
|
| 373 |
-
sample_indices = random.sample(indices, n_samples)
|
| 374 |
-
samples_data = [dataset[idx] for idx in sample_indices]
|
| 375 |
-
|
| 376 |
-
# Собираем данные
|
| 377 |
-
latents = torch.tensor(np.array([item["vae"] for item in samples_data]), dtype=dtype).to(device)
|
| 378 |
-
embeddings = torch.tensor(np.array([item["embeddings"] for item in samples_data]), dtype=dtype).to(device)
|
| 379 |
-
texts = [item["text"] for item in samples_data]
|
| 380 |
-
|
| 381 |
-
# Сохраняем для этого размера
|
| 382 |
-
fixed_samples[size] = (latents, embeddings, texts)
|
| 383 |
-
|
| 384 |
-
print(f"Создано {len(fixed_samples)} групп фиксированных семплов по разрешениям")
|
| 385 |
-
return fixed_samples
|
| 386 |
-
|
| 387 |
-
if limit > 0:
|
| 388 |
-
dataset = load_from_disk(save_path).select(range(limit))
|
| 389 |
-
else:
|
| 390 |
-
dataset = load_from_disk(save_path)
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
def collate_fn(batch):
|
| 395 |
-
# Преобразуем список в тензоры и перемещаем на девайс
|
| 396 |
-
latents = torch.tensor(np.array([item["vae"] for item in batch]), dtype=dtype).to(device)
|
| 397 |
-
embeddings = torch.tensor(np.array([item["embeddings"] for item in batch]), dtype=dtype).to(device)
|
| 398 |
-
return latents, embeddings
|
| 399 |
-
|
| 400 |
-
# Используем наш ResolutionBatchSampler
|
| 401 |
-
batch_sampler = ResolutionBatchSampler(dataset, batch_size=batch_size, shuffle=True)
|
| 402 |
-
dataloader = DataLoader(dataset, batch_sampler=batch_sampler)#, collate_fn=collate_fn)
|
| 403 |
-
|
| 404 |
-
print("Total samples",len(dataloader))
|
| 405 |
-
dataloader = accelerator.prepare(dataloader)
|
| 406 |
-
|
| 407 |
-
# --------------------------- Загрузка моделей ---------------------------
|
| 408 |
-
# VAE загружается на CPU для экономии GPU-памяти
|
| 409 |
-
vae = AutoencoderKL.from_pretrained("AuraDiffusion/16ch-vae").to("cpu", dtype=dtype)
|
| 410 |
-
|
| 411 |
-
# DDPMScheduler с V_Prediction и Zero-SNR
|
| 412 |
-
# scheduler = DDPMScheduler(
|
| 413 |
-
# num_train_timesteps=1000, # Полный график шагов для обучения
|
| 414 |
-
# prediction_type="v_prediction", # V-Prediction
|
| 415 |
-
# rescale_betas_zero_snr=True, # Включение Zero-SNR
|
| 416 |
-
# timestep_spacing="leading", # Добавляем улучшенное распределение шагов
|
| 417 |
-
# steps_offset=1 # Избегаем проблем с нулевым timestep
|
| 418 |
-
# )
|
| 419 |
-
|
| 420 |
-
# Flow Matching
|
| 421 |
-
scheduler = FlowMatchingEulerScheduler(
|
| 422 |
-
<<<<<<< HEAD
|
| 423 |
-
num_train_timesteps=1000,
|
| 424 |
-
=======
|
| 425 |
-
# num_train_timesteps=1000,
|
| 426 |
-
>>>>>>> d0c94e4 (sdxxxs)
|
| 427 |
-
)
|
| 428 |
-
|
| 429 |
-
# Инициализация переменных для возобновления обучения
|
| 430 |
-
start_epoch = 0
|
| 431 |
-
global_step = 0
|
| 432 |
-
|
| 433 |
-
# Расчёт общего количества шагов
|
| 434 |
-
total_training_steps = (len(dataloader) * num_epochs)
|
| 435 |
-
# Get the world size
|
| 436 |
-
world_size = accelerator.state.num_processes
|
| 437 |
-
print(f"World Size: {world_size}")
|
| 438 |
-
|
| 439 |
-
# Опция загрузки модели из последнего чекпоинта (если существует)
|
| 440 |
-
latest_checkpoint = os.path.join(checkpoints_folder, project)
|
| 441 |
-
if os.path.isdir(latest_checkpoint):
|
| 442 |
-
print("Загружаем UNet из чекпоинта:", latest_checkpoint)
|
| 443 |
-
unet = UNet2DConditionModel.from_pretrained(latest_checkpoint).to(device, dtype=dtype)
|
| 444 |
-
unet.enable_gradient_checkpointing()
|
| 445 |
-
unet.set_use_memory_efficient_attention_xformers(False) # отключаем xformers
|
| 446 |
-
try:
|
| 447 |
-
unet.set_attn_processor(AttnProcessor2_0()) # Используем стандартный AttnProcessor
|
| 448 |
-
print("SDPA включен через set_attn_processor.")
|
| 449 |
-
except Exception as e:
|
| 450 |
-
print(f"Ошибка при включении SDPA: {e}")
|
| 451 |
-
print("Попытка использовать enable_xformers_memory_efficient_attention.")
|
| 452 |
-
unet.set_use_memory_efficient_attention_xformers(True)
|
| 453 |
-
|
| 454 |
-
if lora_name:
|
| 455 |
-
print(f"--- Настройка LoRA через PEFT (Rank={lora_rank}, Alpha={lora_alpha}) ---")
|
| 456 |
-
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
|
| 457 |
-
from peft.tuners.lora import LoraModel
|
| 458 |
-
import os
|
| 459 |
-
# 1. Замораживаем все параметры UNet
|
| 460 |
-
unet.requires_grad_(False)
|
| 461 |
-
print("Параметры базового UNet заморожены.")
|
| 462 |
-
|
| 463 |
-
# 2. Создаем конфигурацию LoRA
|
| 464 |
-
lora_config = LoraConfig(
|
| 465 |
-
r=lora_rank,
|
| 466 |
-
lora_alpha=lora_alpha,
|
| 467 |
-
target_modules=["to_q", "to_k", "to_v", "to_out.0"],
|
| 468 |
-
)
|
| 469 |
-
unet.add_adapter(lora_config)
|
| 470 |
-
|
| 471 |
-
# 3. Оборачиваем UNet в PEFT-модель
|
| 472 |
-
from peft import get_peft_model
|
| 473 |
-
|
| 474 |
-
peft_unet = get_peft_model(unet, lora_config)
|
| 475 |
-
|
| 476 |
-
# 4. Получаем параметры для оптимизации
|
| 477 |
-
params_to_optimize = list(p for p in peft_unet.parameters() if p.requires_grad)
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
# 5. Выводим информацию о количестве параметров
|
| 481 |
-
if accelerator.is_main_process:
|
| 482 |
-
lora_params_count = sum(p.numel() for p in params_to_optimize)
|
| 483 |
-
total_params_count = sum(p.numel() for p in unet.parameters())
|
| 484 |
-
print(f"Количество обучаемых параметров (LoRA): {lora_params_count:,}")
|
| 485 |
-
print(f"Общее количество параметров UNet: {total_params_count:,}")
|
| 486 |
-
|
| 487 |
-
# 6. Путь для сохранения
|
| 488 |
-
lora_save_path = os.path.join("lora", lora_name)
|
| 489 |
-
os.makedirs(lora_save_path, exist_ok=True)
|
| 490 |
-
|
| 491 |
-
# 7. Функция для сохранения
|
| 492 |
-
def save_lora_checkpoint(model):
|
| 493 |
-
if accelerator.is_main_process:
|
| 494 |
-
print(f"Сохраняем LoRA адаптеры в {lora_save_path}")
|
| 495 |
-
from peft.utils.save_and_load import get_peft_model_state_dict
|
| 496 |
-
# Получаем state_dict только LoRA
|
| 497 |
-
lora_state_dict = get_peft_model_state_dict(model)
|
| 498 |
-
|
| 499 |
-
# Сохраняем веса
|
| 500 |
-
torch.save(lora_state_dict, os.path.join(lora_save_path, "adapter_model.bin"))
|
| 501 |
-
|
| 502 |
-
# Сохраняем конфиг
|
| 503 |
-
model.peft_config["default"].save_pretrained(lora_save_path)
|
| 504 |
-
# SDXL must be compatible
|
| 505 |
-
from diffusers import StableDiffusionXLPipeline
|
| 506 |
-
StableDiffusionXLPipeline.save_lora_weights(lora_save_path, lora_state_dict)
|
| 507 |
-
|
| 508 |
-
# --------------------------- Оптимизатор ---------------------------
|
| 509 |
-
# Определяем параметры для оптимизации
|
| 510 |
-
if lora_name:
|
| 511 |
-
# Если используется LoRA, оптимизируем только параметры LoRA
|
| 512 |
-
trainable_params = [p for p in unet.parameters() if p.requires_grad]
|
| 513 |
-
else:
|
| 514 |
-
# Иначе оптимизируем все параметры
|
| 515 |
-
trainable_params = list(unet.parameters())
|
| 516 |
-
|
| 517 |
-
# [1] Создаем словарь оптимизаторов (fused backward)
|
| 518 |
-
optimizer_dict = {
|
| 519 |
-
p: bnb.optim.AdamW8bit(
|
| 520 |
-
[p], # Каждый параметр получает свой оптимизатор
|
| 521 |
-
lr=base_learning_rate,
|
| 522 |
-
betas=(0.9, 0.999),
|
| 523 |
-
weight_decay=1e-5,
|
| 524 |
-
eps=1e-8
|
| 525 |
-
) for p in trainable_params
|
| 526 |
-
}
|
| 527 |
-
|
| 528 |
-
# [2] Определяем hook для применения оптимизатора сразу после накопления градиента
|
| 529 |
-
def optimizer_hook(param):
|
| 530 |
-
optimizer_dict[param].step()
|
| 531 |
-
optimizer_dict[param].zero_grad(set_to_none=True)
|
| 532 |
-
|
| 533 |
-
# [3] Регистрируем hook для trainable параметров модели
|
| 534 |
-
for param in trainable_params:
|
| 535 |
-
param.register_post_accumulate_grad_hook(optimizer_hook)
|
| 536 |
-
|
| 537 |
-
# Подготовка через Accelerator
|
| 538 |
-
unet, optimizer = accelerator.prepare(unet, optimizer_dict)
|
| 539 |
-
|
| 540 |
-
# --------------------------- Фиксированные семплы для генерации ---------------------------
|
| 541 |
-
# Примеры фиксированных семплов по размерам
|
| 542 |
-
fixed_samples = get_fixed_samples_by_resolution(dataset)
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
@torch.no_grad()
|
| 546 |
-
def generate_and_save_samples(fixed_samples,step):
|
| 547 |
-
"""
|
| 548 |
-
Генерирует семплы для каждого из разрешений и сохраняет их.
|
| 549 |
-
|
| 550 |
-
Args:
|
| 551 |
-
step: Текущий шаг обучения
|
| 552 |
-
fixed_samples: Словарь, где ключи - размеры (width, height),
|
| 553 |
-
а значения - кортежи (latents, embeddings)
|
| 554 |
-
"""
|
| 555 |
-
try:
|
| 556 |
-
original_model = accelerator.unwrap_model(unet)
|
| 557 |
-
# Перемещаем VAE на device для семплирования
|
| 558 |
-
vae.to(accelerator.device, dtype=dtype)
|
| 559 |
-
|
| 560 |
-
# Устанавливаем количество diffusion шагов
|
| 561 |
-
scheduler.set_timesteps(n_diffusion_steps)
|
| 562 |
-
|
| 563 |
-
all_generated_images = []
|
| 564 |
-
size_info = [] # Для хранения информации о размере для каждого изображения
|
| 565 |
-
all_captions = []
|
| 566 |
-
|
| 567 |
-
# Проходим по всем группам размеров
|
| 568 |
-
for size, (sample_latents, sample_text_embeddings, sample_text) in fixed_samples.items():
|
| 569 |
-
width, height = size
|
| 570 |
-
size_info.append(f"{width}x{height}")
|
| 571 |
-
#print(f"Генерация {sample_latents.shape[0]} изображений размером {width}x{height}")
|
| 572 |
-
|
| 573 |
-
# Инициализируем латенты случайным шумом для этой группы
|
| 574 |
-
noise = torch.randn(
|
| 575 |
-
sample_latents.shape,
|
| 576 |
-
generator=gen,
|
| 577 |
-
device=sample_latents.device,
|
| 578 |
-
dtype=sample_latents.dtype
|
| 579 |
-
)
|
| 580 |
-
|
| 581 |
-
# Начинаем с шума
|
| 582 |
-
current_latents = noise.clone()
|
| 583 |
-
|
| 584 |
-
# Подготовка текстовых эмбеддингов для guidance
|
| 585 |
-
if guidance_scale > 0:
|
| 586 |
-
empty_embeddings = torch.zeros_like(sample_text_embeddings)
|
| 587 |
-
text_embeddings = torch.cat([empty_embeddings, sample_text_embeddings], dim=0)
|
| 588 |
-
else:
|
| 589 |
-
text_embeddings = sample_text_embeddings
|
| 590 |
-
|
| 591 |
-
# Генерация изображений
|
| 592 |
-
for t in scheduler.timesteps:
|
| 593 |
-
# Подготовка входных данных для UNet
|
| 594 |
-
t = t.unsqueeze(dim=0).to(device) # Добавляем размерность для батча
|
| 595 |
-
if guidance_scale > 0:
|
| 596 |
-
latent_model_input = torch.cat([current_latents] * 2)
|
| 597 |
-
latent_model_input = scheduler.scale_model_input(latent_model_input, t)
|
| 598 |
-
else:
|
| 599 |
-
latent_model_input = scheduler.scale_model_input(current_latents, t)
|
| 600 |
-
|
| 601 |
-
# Предсказание шума
|
| 602 |
-
noise_pred = original_model(latent_model_input, t, text_embeddings).sample
|
| 603 |
-
|
| 604 |
-
# Применение guidance scale
|
| 605 |
-
if guidance_scale > 0:
|
| 606 |
-
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
|
| 607 |
-
noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
|
| 608 |
-
|
| 609 |
-
# Обновление латентов
|
| 610 |
-
current_latents = scheduler.step(noise_pred, t, current_latents).prev_sample
|
| 611 |
-
|
| 612 |
-
# Декодирование через VAE
|
| 613 |
-
latent = (current_latents.detach() / vae.config.scaling_factor) + vae.config.shift_factor
|
| 614 |
-
latent = latent.to(accelerator.device, dtype=dtype)
|
| 615 |
-
decoded = vae.decode(latent).sample
|
| 616 |
-
|
| 617 |
-
# Преобразуем тензоры в PIL-изображения и сохраняем
|
| 618 |
-
for img_idx, img_tensor in enumerate(decoded):
|
| 619 |
-
img = (img_tensor.to(torch.float32) / 2 + 0.5).clamp(0, 1).cpu().numpy().transpose(1, 2, 0)
|
| 620 |
-
pil_img = Image.fromarray((img * 255).astype("uint8"))
|
| 621 |
-
# Определяем максимальные ширину и высоту
|
| 622 |
-
max_width = max(size[0] for size in fixed_samples.keys())
|
| 623 |
-
max_height = max(size[1] for size in fixed_samples.keys())
|
| 624 |
-
max_width = max(255,max_width)
|
| 625 |
-
max_height = max(255,max_height)
|
| 626 |
-
|
| 627 |
-
# Добавляем padding, чтобы изображение стало размером max_width x max_height
|
| 628 |
-
padded_img = ImageOps.pad(pil_img, (max_width, max_height), color='white')
|
| 629 |
-
|
| 630 |
-
all_generated_images.append(padded_img)
|
| 631 |
-
|
| 632 |
-
caption_text = sample_text[img_idx][:200] if img_idx < len(sample_text) else ""
|
| 633 |
-
all_captions.append(caption_text)
|
| 634 |
-
|
| 635 |
-
# Сохраняем с информацией о размере в имени файла
|
| 636 |
-
save_path = f"{generated_folder}/{project}_{width}x{height}_{img_idx}.jpg"
|
| 637 |
-
pil_img.save(save_path, "JPEG", quality=96)
|
| 638 |
-
|
| 639 |
-
# Отправляем изображения на WandB с информацией о размере
|
| 640 |
-
if use_wandb and accelerator.is_main_process:
|
| 641 |
-
wandb_images = [
|
| 642 |
-
wandb.Image(img, caption=f"{all_captions[i]}")
|
| 643 |
-
for i, img in enumerate(all_generated_images)
|
| 644 |
-
]
|
| 645 |
-
wandb.log({"generated_images": wandb_images, "global_step": step})
|
| 646 |
-
|
| 647 |
-
finally:
|
| 648 |
-
# Гарантированное перемещение VAE обратно на CPU
|
| 649 |
-
vae.to("cpu")
|
| 650 |
-
if original_model is not None:
|
| 651 |
-
del original_model
|
| 652 |
-
# Очистка всех тензоров
|
| 653 |
-
for var in list(locals().keys()):
|
| 654 |
-
if isinstance(locals()[var], torch.Tensor):
|
| 655 |
-
del locals()[var]
|
| 656 |
-
torch.cuda.empty_cache()
|
| 657 |
-
gc.collect()
|
| 658 |
-
|
| 659 |
-
# --------------------------- Генерация сэмплов перед обучением ---------------------------
|
| 660 |
-
if accelerator.is_main_process:
|
| 661 |
-
if save_model:
|
| 662 |
-
print("Генерация сэмплов до старта обучения...")
|
| 663 |
-
generate_and_save_samples(fixed_samples,0)
|
| 664 |
-
|
| 665 |
-
# Модифицируем функцию сохранения модели для поддержки LoRA
|
| 666 |
-
def save_checkpoint(unet):
|
| 667 |
-
if accelerator.is_main_process:
|
| 668 |
-
if lora_name:
|
| 669 |
-
# Сохраняем только LoRA адаптеры
|
| 670 |
-
save_lora_checkpoint(unet)
|
| 671 |
-
else:
|
| 672 |
-
# Сохраняем полную модель
|
| 673 |
-
accelerator.unwrap_model(unet).save_pretrained(os.path.join(checkpoints_folder, f"{project}"))
|
| 674 |
-
|
| 675 |
-
# --------------------------- Тренировочный цикл ---------------------------
|
| 676 |
-
# Для логирования среднего лосса каждые % эпохи
|
| 677 |
-
if accelerator.is_main_process:
|
| 678 |
-
print(f"Total steps per GPU: {total_training_steps}")
|
| 679 |
-
print(f"[GPU {accelerator.process_index}] Total steps: {total_training_steps}")
|
| 680 |
-
|
| 681 |
-
epoch_loss_points = []
|
| 682 |
-
progress_bar = tqdm(total=total_training_steps, disable=not accelerator.is_local_main_process, desc="Training", unit="step")
|
| 683 |
-
|
| 684 |
-
# Определяем интервал для сэмплирования и логирования в пределах эпохи (10% эпохи)
|
| 685 |
-
steps_per_epoch = len(dataloader)
|
| 686 |
-
sample_interval = max(1, steps_per_epoch // sample_interval_share)
|
| 687 |
-
|
| 688 |
-
# Начинаем с указанной эпохи (полезно при возобновлении)
|
| 689 |
-
for epoch in range(start_epoch, start_epoch + num_epochs):
|
| 690 |
-
batch_losses = []
|
| 691 |
-
unet.train()
|
| 692 |
-
|
| 693 |
-
for step, (latents, embeddings) in enumerate(dataloader):
|
| 694 |
-
with accelerator.accumulate(unet):
|
| 695 |
-
if save_model == False and step == 3 :
|
| 696 |
-
used_gb = torch.cuda.max_memory_allocated() / 1024**3
|
| 697 |
-
print(f"Шаг {step}: {used_gb:.2f} GB")
|
| 698 |
-
# Forward pass
|
| 699 |
-
noise = torch.randn_like(latents)
|
| 700 |
-
|
| 701 |
-
timesteps = torch.randint(
|
| 702 |
-
0,
|
| 703 |
-
1000,
|
| 704 |
-
(latents.shape[0],),
|
| 705 |
-
device=device
|
| 706 |
-
) / 1000 # Кастим в float
|
| 707 |
-
|
| 708 |
-
# Добавляем шум к латентам
|
| 709 |
-
noisy_latents = scheduler.add_noise(latents, noise, timesteps)
|
| 710 |
-
|
| 711 |
-
# Получаем предсказание шума
|
| 712 |
-
noise_pred = unet(noisy_latents, timesteps, embeddings).sample #.to(dtype=torch.bfloat16)
|
| 713 |
-
|
| 714 |
-
# Используем целевое значение v_prediction
|
| 715 |
-
target = scheduler.get_velocity(latents, noise)
|
| 716 |
-
|
| 717 |
-
# Считаем лосс
|
| 718 |
-
loss = torch.nn.functional.mse_loss(noise_pred.float(), target.float())
|
| 719 |
-
|
| 720 |
-
# Делаем backward через Accelerator
|
| 721 |
-
accelerator.backward(loss)
|
| 722 |
-
|
| 723 |
-
# Увеличиваем счетчик глобальных шагов
|
| 724 |
-
global_step += 1
|
| 725 |
-
|
| 726 |
-
# Обновляем прогресс-бар
|
| 727 |
-
progress_bar.update(1)
|
| 728 |
-
|
| 729 |
-
# Логируем метрики
|
| 730 |
-
if accelerator.is_main_process:
|
| 731 |
-
current_lr = base_learning_rate
|
| 732 |
-
batch_losses.append(loss.detach().item())
|
| 733 |
-
|
| 734 |
-
# Логируем в Wandb
|
| 735 |
-
if use_wandb:
|
| 736 |
-
wandb.log({
|
| 737 |
-
"loss": loss.detach().item(),
|
| 738 |
-
"learning_rate": current_lr,
|
| 739 |
-
"epoch": epoch,
|
| 740 |
-
"global_step": global_step
|
| 741 |
-
})
|
| 742 |
-
|
| 743 |
-
# Генерируем сэмплы с заданным интервалом
|
| 744 |
-
if global_step % sample_interval == 0:
|
| 745 |
-
if save_model:
|
| 746 |
-
save_checkpoint(unet)
|
| 747 |
-
|
| 748 |
-
generate_and_save_samples(fixed_samples,global_step)
|
| 749 |
-
|
| 750 |
-
# Выводим текущий лосс
|
| 751 |
-
avg_loss = np.mean(batch_losses[-sample_interval:])
|
| 752 |
-
#print(f"Эпоха {epoch}, шаг {global_step}, средний лосс: {avg_loss:.6f}, LR: {current_lr:.8f}")
|
| 753 |
-
if use_wandb:
|
| 754 |
-
wandb.log({"intermediate_loss": avg_loss})
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
# По окончании эпохи
|
| 758 |
-
if accelerator.is_main_process:
|
| 759 |
-
avg_epoch_loss = np.mean(batch_losses)
|
| 760 |
-
print(f"\nЭпоха {epoch} завершена. Средний лосс: {avg_epoch_loss:.6f}")
|
| 761 |
-
if use_wandb:
|
| 762 |
-
wandb.log({"epoch_loss": avg_epoch_loss, "epoch": epoch+1})
|
| 763 |
-
|
| 764 |
-
# Завершение обучения - сохраняем финальную модель
|
| 765 |
-
if accelerator.is_main_process:
|
| 766 |
-
print("Обучение завершено! Сохраняем финальную модель...")
|
| 767 |
-
# Сохраняем основную модель
|
| 768 |
-
#if save_model:
|
| 769 |
-
save_checkpoint(unet)
|
| 770 |
-
print("Готово!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unet/config.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:636ed60d2a73b31fe88849234ff578446cd018092fdc9210575893551220bfc3
|
| 3 |
+
size 1887
|
unet/diffusion_pytorch_model.fp16.safetensors
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:756e18a3addbf8b5b8d1fabc9bae9e03d710318eb9698346ba3ed70b4e07af72
|
| 3 |
-
size 3929714960
|
|
|
|
|
|
|
|
|
|
|
|
{micro → unet}/diffusion_pytorch_model.safetensors
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b8ec1c74913276f691c838b5a85cd65939c4437c0ce1741f69c8c8931dd39112
|
| 3 |
+
size 3092571208
|
vae/config-Copy1.1json
DELETED
|
@@ -1,37 +0,0 @@
|
|
| 1 |
-
{
|
| 2 |
-
"_class_name": "AutoencoderKL",
|
| 3 |
-
"_diffusers_version": "0.30.0.dev0",
|
| 4 |
-
"act_fn": "silu",
|
| 5 |
-
"block_out_channels": [
|
| 6 |
-
128,
|
| 7 |
-
256,
|
| 8 |
-
512,
|
| 9 |
-
512
|
| 10 |
-
],
|
| 11 |
-
"down_block_types": [
|
| 12 |
-
"DownEncoderBlock2D",
|
| 13 |
-
"DownEncoderBlock2D",
|
| 14 |
-
"DownEncoderBlock2D",
|
| 15 |
-
"DownEncoderBlock2D"
|
| 16 |
-
],
|
| 17 |
-
"force_upcast": false,
|
| 18 |
-
"in_channels": 3,
|
| 19 |
-
"latent_channels": 16,
|
| 20 |
-
"latents_mean": [0.2539, 0.1431, 0.1484, -0.3048, -0.0985, -0.162, 0.1403, 0.2034, -0.1419, 0.2646, 0.0655, 0.0061, 0.1555, 0.0506, 0.0129, -0.1948],
|
| 21 |
-
"latents_std": [0.8123, 0.7376, 0.7354, 1.1827, 0.8387, 0.8735, 0.8705, 0.8142, 0.8076, 0.7409, 0.7655, 0.8731, 0.8087, 0.7058, 0.8087, 0.7615],
|
| 22 |
-
"layers_per_block": 2,
|
| 23 |
-
"mid_block_add_attention": false,
|
| 24 |
-
"norm_num_groups": 32,
|
| 25 |
-
"out_channels": 3,
|
| 26 |
-
"sample_size": 1024,
|
| 27 |
-
"scaling_factor": 1,
|
| 28 |
-
"shift_factor": 0,
|
| 29 |
-
"up_block_types": [
|
| 30 |
-
"UpDecoderBlock2D",
|
| 31 |
-
"UpDecoderBlock2D",
|
| 32 |
-
"UpDecoderBlock2D",
|
| 33 |
-
"UpDecoderBlock2D"
|
| 34 |
-
],
|
| 35 |
-
"use_post_quant_conv": true,
|
| 36 |
-
"use_quant_conv": true
|
| 37 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vae/config.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ca78c49c02cf8afd8f022b99418bec3e4da9882cfed036c3664897af6d4589f0
|
| 3 |
+
size 801
|
vae/diffusion_pytorch_model.fp16.safetensors
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:a27d33cadf456598eee3cf5638ba3e8e01dd020a5f9c80c1a7b162bf97096701
|
| 3 |
-
size 163460798
|
|
|
|
|
|
|
|
|
|
|
|
result_grid.jpg → vae/diffusion_pytorch_model.safetensors
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2ff1aa1cce9dcf003c7eb23548fdb62101fe9bdeedf30aa4a897b91a6ef13f93
|
| 3 |
+
size 167669678
|