Video-to-Video
Diffusers
English
Chinese
video-editing
streaming
quantization
int8
text-stream-stripping
Instructions to use kylinfires/Joy-face_change_optimized with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use kylinfires/Joy-face_change_optimized with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("kylinfires/Joy-face_change_optimized", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
Download training/src/models/mmdit/dit/modulate_layers.py from kylinfires/Joy-face_change_optimized: direct link, hf CLI and curl.
- Browser
- Download file 2.82 kB
-
https://huggingface.co/kylinfires/Joy-face_change_optimized/resolve/main/training/src/models/mmdit/dit/modulate_layers.py
- Command line
-
hf download hf://kylinfires/Joy-face_change_optimized/training/src/models/mmdit/dit/modulate_layers.py
-
curl -L -o modulate_layers.py https://huggingface.co/kylinfires/Joy-face_change_optimized/resolve/main/training/src/models/mmdit/dit/modulate_layers.py
2.82 kB
| import torch | |
| import torch.nn as nn | |
| def load_modulation( | |
| modulate_type: str, | |
| hidden_size: int, | |
| factor: int, | |
| act_layer=nn.SiLU, | |
| dtype=None, | |
| device=None): | |
| factory_kwargs = {"dtype": dtype, "device": device} | |
| if modulate_type == 'wanx': | |
| return ModulateWan(hidden_size, factor, **factory_kwargs) | |
| elif modulate_type == 'adaLN': | |
| return ModulateDiT(hidden_size, factor, act_layer, **factory_kwargs) | |
| elif modulate_type == 'jdx': | |
| return ModulateX(factor) | |
| else: | |
| raise ValueError( | |
| f"Unknown modulation type: {modulate_type}.") | |
| class ModulateDiT(nn.Module): | |
| """Modulation layer for DiT.""" | |
| def __init__( | |
| self, | |
| hidden_size: int, | |
| factor: int, | |
| act_layer: nn.SiLU, | |
| dtype=None, | |
| device=None, | |
| ): | |
| factory_kwargs = {"dtype": dtype, "device": device} | |
| super().__init__() | |
| self.factor = factor | |
| self.act = act_layer() | |
| self.linear = nn.Linear( | |
| hidden_size, factor * hidden_size, bias=True, **factory_kwargs | |
| ) | |
| nn.init.zeros_(self.linear.weight) | |
| nn.init.zeros_(self.linear.bias) | |
| def forward(self, x: torch.Tensor) -> torch.Tensor: | |
| return self.linear(self.act(x)).chunk(self.factor, dim=-1) | |
| class ModulateWan(nn.Module): | |
| """Modulation layer for WanX.""" | |
| def __init__(self, hidden_size: int, factor: int, dtype=None, device=None): | |
| super().__init__() | |
| self.factor = factor | |
| self.modulate_table = nn.Parameter( | |
| torch.randn(1, factor, hidden_size, | |
| dtype=dtype, device=device) / hidden_size**0.5, | |
| requires_grad=True | |
| ) | |
| def forward(self, x: torch.Tensor) -> torch.Tensor: | |
| if len(x.shape) != 3: | |
| x = x.unsqueeze(1) | |
| return [o.squeeze(1) for o in (self.modulate_table + x).chunk(self.factor, dim=1)] | |
| class ModulateX(nn.Module): | |
| """Modulation layer for WanX.""" | |
| def __init__(self, factor: int): | |
| super().__init__() | |
| self.factor = factor | |
| def forward(self, x: torch.Tensor) -> torch.Tensor: | |
| if len(x.shape) != 3: | |
| x = x.unsqueeze(1) | |
| return [o.squeeze(1) for o in x.chunk(self.factor, dim=1)] | |
| def modulate(x, shift=None, scale=None): | |
| """Apply shift and scale to the hidden states.""" | |
| if scale is None and shift is None: | |
| return x | |
| elif shift is None: | |
| return x * (1 + scale.unsqueeze(1)) | |
| elif scale is None: | |
| return x + shift.unsqueeze(1) | |
| else: | |
| return x * (1 + scale.unsqueeze(1)) + shift.unsqueeze(1) | |
| def apply_gate(x, gate=None): | |
| """Apply an element-wise gate to the residual branch output.""" | |
| if gate is None: | |
| return x | |
| return x * gate.unsqueeze(1) | |