Text-to-Audio
Transformers
Safetensors
qadit
feature-extraction
diffusion
dit
audio
educational
research
custom_code
Instructions to use QuarkML/QaDiT-160 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use QuarkML/QaDiT-160 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-to-audio", model="QuarkML/QaDiT-160", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("QuarkML/QaDiT-160", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload folder using huggingface_hub
Browse files- README.md +1 -1
- modelling_qadit.py +35 -7
README.md
CHANGED
|
@@ -33,7 +33,7 @@ from transformers import AutoModel
|
|
| 33 |
import torch
|
| 34 |
|
| 35 |
model = AutoModel.from_pretrained(
|
| 36 |
-
"
|
| 37 |
trust_remote_code=True,
|
| 38 |
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 39 |
|
|
|
|
| 33 |
import torch
|
| 34 |
|
| 35 |
model = AutoModel.from_pretrained(
|
| 36 |
+
"USER/qadit", # or a local export folder
|
| 37 |
trust_remote_code=True,
|
| 38 |
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 39 |
|
modelling_qadit.py
CHANGED
|
@@ -353,19 +353,46 @@ class DiffusionScheduler:
|
|
| 353 |
self.T = num_train_steps
|
| 354 |
self.ln_mean = logit_normal_mean
|
| 355 |
self.ln_std = logit_normal_std
|
|
|
|
| 356 |
if schedule != "cosine":
|
| 357 |
raise ValueError(f"unknown schedule: {schedule}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
s = 0.008
|
| 359 |
-
steps = torch.arange(
|
| 360 |
-
|
|
|
|
|
|
|
| 361 |
abar = (f / f[0]).clamp(1e-5, 1.0)
|
| 362 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
|
| 364 |
def to(self, device) -> "DiffusionScheduler":
|
| 365 |
-
|
| 366 |
-
return self
|
| 367 |
|
| 368 |
def _gather(self, t: torch.Tensor):
|
|
|
|
|
|
|
| 369 |
abar = self.alpha_bar.to(t.device)[t]
|
| 370 |
return abar.sqrt().view(-1, 1, 1, 1), (1 - abar).sqrt().view(-1, 1, 1, 1)
|
| 371 |
|
|
@@ -390,6 +417,7 @@ class DiffusionScheduler:
|
|
| 390 |
device: Union[str, torch.device] = "cpu",
|
| 391 |
generator: Optional[torch.Generator] = None,
|
| 392 |
) -> torch.Tensor:
|
|
|
|
| 393 |
B = shape[0]
|
| 394 |
z = torch.randn(shape, device=device, generator=generator)
|
| 395 |
times = torch.linspace(self.T - 1, 0, num_steps, device=device).long()
|
|
@@ -411,8 +439,8 @@ class DiffusionScheduler:
|
|
| 411 |
break
|
| 412 |
|
| 413 |
t_next = times[i + 1].expand(B)
|
| 414 |
-
abar_next = self.alpha_bar
|
| 415 |
-
abar_now = self.alpha_bar
|
| 416 |
sigma = eta * torch.sqrt(
|
| 417 |
(1 - abar_next) / (1 - abar_now) * (1 - abar_now / abar_next)
|
| 418 |
)
|
|
|
|
| 353 |
self.T = num_train_steps
|
| 354 |
self.ln_mean = logit_normal_mean
|
| 355 |
self.ln_std = logit_normal_std
|
| 356 |
+
self.schedule = schedule
|
| 357 |
if schedule != "cosine":
|
| 358 |
raise ValueError(f"unknown schedule: {schedule}")
|
| 359 |
+
# May be created on the meta device under HF's init_empty_weights();
|
| 360 |
+
# materialize_real() / to() rebuilds a real CPU/CUDA table before use.
|
| 361 |
+
self.alpha_bar = self._build_alpha_bar(self.T)
|
| 362 |
+
|
| 363 |
+
@staticmethod
|
| 364 |
+
def _build_alpha_bar(num_train_steps: int, device=None) -> torch.Tensor:
|
| 365 |
+
device = torch.device(device) if device is not None else torch.device("cpu")
|
| 366 |
+
# Force a concrete device — never allocate on "meta".
|
| 367 |
+
if device.type == "meta":
|
| 368 |
+
device = torch.device("cpu")
|
| 369 |
s = 0.008
|
| 370 |
+
steps = torch.arange(
|
| 371 |
+
num_train_steps + 1, dtype=torch.float64, device=device
|
| 372 |
+
)
|
| 373 |
+
f = torch.cos((steps / num_train_steps + s) / (1 + s) * math.pi / 2) ** 2
|
| 374 |
abar = (f / f[0]).clamp(1e-5, 1.0)
|
| 375 |
+
return abar[1:].float()
|
| 376 |
+
|
| 377 |
+
def _is_meta(self) -> bool:
|
| 378 |
+
t = self.alpha_bar
|
| 379 |
+
return bool(getattr(t, "is_meta", False) or t.device.type == "meta")
|
| 380 |
+
|
| 381 |
+
def materialize_real(self, device=None) -> "DiffusionScheduler":
|
| 382 |
+
"""Rebuild alpha_bar if it was left on the meta device by from_pretrained."""
|
| 383 |
+
target = torch.device(device) if device is not None else torch.device("cpu")
|
| 384 |
+
if target.type == "meta":
|
| 385 |
+
target = torch.device("cpu")
|
| 386 |
+
if self._is_meta() or self.alpha_bar.device != target:
|
| 387 |
+
self.alpha_bar = self._build_alpha_bar(self.T, device="cpu").to(target)
|
| 388 |
+
return self
|
| 389 |
|
| 390 |
def to(self, device) -> "DiffusionScheduler":
|
| 391 |
+
return self.materialize_real(device)
|
|
|
|
| 392 |
|
| 393 |
def _gather(self, t: torch.Tensor):
|
| 394 |
+
if self._is_meta():
|
| 395 |
+
self.materialize_real(t.device)
|
| 396 |
abar = self.alpha_bar.to(t.device)[t]
|
| 397 |
return abar.sqrt().view(-1, 1, 1, 1), (1 - abar).sqrt().view(-1, 1, 1, 1)
|
| 398 |
|
|
|
|
| 417 |
device: Union[str, torch.device] = "cpu",
|
| 418 |
generator: Optional[torch.Generator] = None,
|
| 419 |
) -> torch.Tensor:
|
| 420 |
+
self.materialize_real(device)
|
| 421 |
B = shape[0]
|
| 422 |
z = torch.randn(shape, device=device, generator=generator)
|
| 423 |
times = torch.linspace(self.T - 1, 0, num_steps, device=device).long()
|
|
|
|
| 439 |
break
|
| 440 |
|
| 441 |
t_next = times[i + 1].expand(B)
|
| 442 |
+
abar_next = self.alpha_bar[t_next].view(-1, 1, 1, 1)
|
| 443 |
+
abar_now = self.alpha_bar[t].view(-1, 1, 1, 1)
|
| 444 |
sigma = eta * torch.sqrt(
|
| 445 |
(1 - abar_next) / (1 - abar_now) * (1 - abar_now / abar_next)
|
| 446 |
)
|