Instructions to use AiArtLab/sdxs-2b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use AiArtLab/sdxs-2b with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("AiArtLab/sdxs-2b", dtype=torch.bfloat16, device_map="cuda") prompt = "sdxs-2b" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Draw Things
- DiffusionBee
bf16
Browse files
pipeline_sdxs.py
CHANGED
|
@@ -5,36 +5,14 @@ from typing import List, Union, Optional, Tuple
|
|
| 5 |
from dataclasses import dataclass
|
| 6 |
import logging
|
| 7 |
import math
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
from transformers import logging as transformers_logging
|
| 10 |
from diffusers import DiffusionPipeline
|
| 11 |
from diffusers.utils import BaseOutput
|
| 12 |
-
from tqdm import tqdm
|
| 13 |
-
import warnings
|
| 14 |
-
import requests
|
| 15 |
-
from io import BytesIO
|
| 16 |
-
|
| 17 |
-
def pad_to_match(tensor1, tensor2):
|
| 18 |
-
"""Дополняет нулями короткий тензор, чтобы размерности seq_len совпадали."""
|
| 19 |
-
len1, len2 = tensor1.shape[1], tensor2.shape[1]
|
| 20 |
-
if len1 == len2:
|
| 21 |
-
return tensor1, tensor2
|
| 22 |
-
if len1 < len2:
|
| 23 |
-
padding = torch.zeros(tensor1.shape[0], len2 - len1, tensor1.shape[2], device=tensor1.device, dtype=tensor1.dtype)
|
| 24 |
-
return torch.cat([tensor1, padding], dim=1), tensor2
|
| 25 |
-
else:
|
| 26 |
-
padding = torch.zeros(tensor2.shape[0], len1 - len2, tensor2.shape[2], device=tensor2.device, dtype=tensor2.dtype)
|
| 27 |
-
return tensor1, torch.cat([tensor2, padding], dim=1)
|
| 28 |
-
|
| 29 |
-
from PIL import Image
|
| 30 |
-
import requests
|
| 31 |
-
from io import BytesIO
|
| 32 |
-
from typing import Union, Tuple
|
| 33 |
-
|
| 34 |
-
from PIL import Image
|
| 35 |
-
import requests
|
| 36 |
-
from io import BytesIO
|
| 37 |
-
from typing import Union, Tuple
|
| 38 |
|
| 39 |
|
| 40 |
@dataclass
|
|
@@ -43,7 +21,7 @@ class SdxsPipelineOutput(BaseOutput):
|
|
| 43 |
prompt: Optional[Union[str, List[str]]] = None
|
| 44 |
|
| 45 |
class SdxsPipeline(DiffusionPipeline):
|
| 46 |
-
MAX_TEXT_TOKENS =
|
| 47 |
|
| 48 |
def __init__(self, vae, text_encoder, tokenizer, transformer, scheduler):
|
| 49 |
super().__init__()
|
|
@@ -156,7 +134,7 @@ class SdxsPipeline(DiffusionPipeline):
|
|
| 156 |
do_sample=True,
|
| 157 |
temperature=temperature,
|
| 158 |
top_p=0.95, # Вместо top_k используем top_p
|
| 159 |
-
repetition_penalty=1.
|
| 160 |
pad_token_id=self.tokenizer.pad_token_id,
|
| 161 |
)
|
| 162 |
|
|
@@ -168,7 +146,7 @@ class SdxsPipeline(DiffusionPipeline):
|
|
| 168 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 169 |
)[0]
|
| 170 |
|
| 171 |
-
# 3. ВАЖНО: Вырезаем блок размышлений <think
|
| 172 |
# Если модель думала, она выдаст мысль, а потом ответ. Нам нужен только ответ.
|
| 173 |
if "</think" in output_text:
|
| 174 |
output_text = output_text.split("</think", 1)[1].strip()
|
|
@@ -252,7 +230,7 @@ class SdxsPipeline(DiffusionPipeline):
|
|
| 252 |
negative_prompt: Optional[Union[str, List[str]]] = None,
|
| 253 |
height: int = 1152,
|
| 254 |
width: int = 768,
|
| 255 |
-
num_inference_steps: int =
|
| 256 |
guidance_scale: float = 4.0,
|
| 257 |
num_images_per_prompt: int = 1,
|
| 258 |
seed: Optional[int] = None,
|
|
@@ -403,10 +381,10 @@ class SdxsPipeline(DiffusionPipeline):
|
|
| 403 |
else:
|
| 404 |
text_embeddings = prompt_embeds
|
| 405 |
|
| 406 |
-
# 2. Prepare Timesteps (Bridging Diffusers Scheduler + Karras EDM)
|
| 407 |
# Ищем sigma_max/min в конфиге шедулера (если ты их туда добавил),
|
| 408 |
# если их там нет — используем константы Cosmos по умолчанию.
|
| 409 |
-
sigma_max = getattr(self.scheduler.config, "sigma_max",
|
| 410 |
sigma_min = getattr(self.scheduler.config, "sigma_min", 0.002)
|
| 411 |
|
| 412 |
# Создаем наше любимое экспоненциальное убывание для лучшей детализации
|
|
|
|
| 5 |
from dataclasses import dataclass
|
| 6 |
import logging
|
| 7 |
import math
|
| 8 |
+
import requests
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
import warnings
|
| 11 |
+
from tqdm import tqdm
|
| 12 |
|
| 13 |
from transformers import logging as transformers_logging
|
| 14 |
from diffusers import DiffusionPipeline
|
| 15 |
from diffusers.utils import BaseOutput
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
@dataclass
|
|
|
|
| 21 |
prompt: Optional[Union[str, List[str]]] = None
|
| 22 |
|
| 23 |
class SdxsPipeline(DiffusionPipeline):
|
| 24 |
+
MAX_TEXT_TOKENS = 250
|
| 25 |
|
| 26 |
def __init__(self, vae, text_encoder, tokenizer, transformer, scheduler):
|
| 27 |
super().__init__()
|
|
|
|
| 134 |
do_sample=True,
|
| 135 |
temperature=temperature,
|
| 136 |
top_p=0.95, # Вместо top_k используем top_p
|
| 137 |
+
repetition_penalty=1.15, # Снижено с 1.15 (presence_penalty в HF заменяется этим)
|
| 138 |
pad_token_id=self.tokenizer.pad_token_id,
|
| 139 |
)
|
| 140 |
|
|
|
|
| 146 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 147 |
)[0]
|
| 148 |
|
| 149 |
+
# 3. ВАЖНО: Вырезаем блок размышлений <think>...</think> из финального ответа
|
| 150 |
# Если модель думала, она выдаст мысль, а потом ответ. Нам нужен только ответ.
|
| 151 |
if "</think" in output_text:
|
| 152 |
output_text = output_text.split("</think", 1)[1].strip()
|
|
|
|
| 230 |
negative_prompt: Optional[Union[str, List[str]]] = None,
|
| 231 |
height: int = 1152,
|
| 232 |
width: int = 768,
|
| 233 |
+
num_inference_steps: int = 35,
|
| 234 |
guidance_scale: float = 4.0,
|
| 235 |
num_images_per_prompt: int = 1,
|
| 236 |
seed: Optional[int] = None,
|
|
|
|
| 381 |
else:
|
| 382 |
text_embeddings = prompt_embeds
|
| 383 |
|
| 384 |
+
# 2. Prepare Timesteps (Bridging Diffusers Scheduler + Karras EDM)
|
| 385 |
# Ищем sigma_max/min в конфиге шедулера (если ты их туда добавил),
|
| 386 |
# если их там нет — используем константы Cosmos по умолчанию.
|
| 387 |
+
sigma_max = getattr(self.scheduler.config, "sigma_max", 10.0)
|
| 388 |
sigma_min = getattr(self.scheduler.config, "sigma_min", 0.002)
|
| 389 |
|
| 390 |
# Создаем наше любимое экспоненциальное убывание для лучшей детализации
|
transformer/diffusion_pytorch_model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 3912877104
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:07d6f1315358e1da06e51b1b023781bce56151d08169679d1212426ceb6e0b82
|
| 3 |
size 3912877104
|