Skip to content

Open-Sora: Democratizing Efficient Video Production for All

Python

LeCam Loss working

Shen-Chenhui486593f13783
diff --git a/opensora/models/vae/README.md b/opensora/models/vae/README.md
index daa16f9..4e5f6f7 100644
--- a/opensora/models/vae/README.md
+++ b/opensora/models/vae/README.md
@@ -67,3 +67,9 @@ clone the repo, go to the branch, then do `pip install .`
CUDA_VISIBLE_DEVICES7 torchrun --master_port=29510 --nnodes=1 --nproc_per_node=1 scripts/train-vae-v2.py configs/vae_magvit_v2/train/17x128x128.py --data-path /home/shenchenhui/data/pexels/train.csv
```
+### 2.3 Inference
+
+
+### 2.4 Data
+
+* `/home/data/sora_data/pixabay/raw/data/split-0`
\ No newline at end of file
diff --git a/opensora/models/vae/vae_3d_v2.py b/opensora/models/vae/vae_3d_v2.py
index c3ceb07..39e687a 100644
--- a/opensora/models/vae/vae_3d_v2.py
+++ b/opensora/models/vae/vae_3d_v2.py
@@ -60,8 +60,8 @@ def exists(v):
# ============== Generator Adversarial Loss Functions ==============
def lecam_reg(real_pred, fake_pred, ema_real_pred, ema_fake_pred):
assert real_pred.ndim == 0 and ema_fake_pred.ndim == 0
- lecam_loss = np.mean(np.power(nn.ReLU(real_pred - ema_fake_pred), 2))
- lecam_loss += np.mean(np.power(nn.ReLU(ema_real_pred - fake_pred), 2))
+ lecam_loss = torch.mean(torch.pow(nn.ReLU()(real_pred - ema_fake_pred), 2))
+ lecam_loss += torch.mean(torch.pow(nn.ReLU()(ema_real_pred - fake_pred), 2))
return lecam_loss
def gradient_penalty_fn(images, output):
@@ -1120,10 +1120,15 @@ class AdversarialLoss(nn.Module):
return weighted_gen_loss
class LeCamEMA:
- def __init__(self, decay=0.999):
+ def __init__(
+ self,
+ decay=0.999,
+ dtype=torch.bfloat16,
+ device="cpu"
+ ):
self.decay = decay
- self.ema_real = torch.tensor(0.0)
- self.ema_fake = torch.tensor(0.0)
+ self.ema_real = torch.tensor(0.0).to(device, dtype)
+ self.ema_fake = torch.tensor(0.0).to(device, dtype)
def update(self, ema_real, ema_fake):
self.ema_real = self.ema_real * self.decay + ema_real * (1-self.decay)
@@ -1192,8 +1197,8 @@ class DiscriminatorLoss(nn.Module):
lecam_loss = 0.0
if self.lecam_loss_weight is not None and self.lecam_loss_weight > 0.0:
- real_pred = np.mean(real_logits)
- fake_pred = np.mean(fake_logits)
+ real_pred = torch.mean(real_logits)
+ fake_pred = torch.mean(fake_logits)
lecam_loss = lecam_reg(real_pred, fake_pred,
lecam_ema_real,
lecam_ema_fake)
diff --git a/scripts/train-vae-v2.py b/scripts/train-vae-v2.py
index 82ba8d6..0b19998 100644
--- a/scripts/train-vae-v2.py
+++ b/scripts/train-vae-v2.py
@@ -267,7 +267,7 @@ def main():
# lecam_ema_real = torch.tensor(0.0)
# lecam_ema_fake = torch.tensor(0.0)
- lecam_ema = LeCamEMA(decay=cfg.ema_decay)
+ lecam_ema = LeCamEMA(decay=cfg.ema_decay, dtype=dtype, device=device)
for epoch in range(start_epoch, cfg.epochs):
dataloader.sampler.set_epoch(epoch)
@@ -401,8 +401,8 @@ def main():
# SCH: TODO: is this written properly like this for moving average? e.g. distributed training etc.
# lecam_ema_real = lecam_ema_real * cfg.ema_decay + (1 - cfg.ema_decay) * torch.mean(real_logits.clone().detach())
# lecam_ema_fake = lecam_ema_fake * cfg.ema_decay + (1 - cfg.ema_decay) * torch.mean(fake_logits.clone().detach())
- ema_real = torch.mean(real_logits.clone().detach())
- ema_fake = torch.mean(fake_logits.clone().detach())
+ ema_real = torch.mean(real_logits.clone().detach()).to(device, dtype)
+ ema_fake = torch.mean(fake_logits.clone().detach()).to(device, dtype)
all_reduce_mean(ema_real)
all_reduce_mean(ema_fake)
lecam_ema.update(ema_real, ema_fake)