Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ci:

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.11.2
rev: v0.16.0
hooks:
# Run the linter.
- id: ruff
Expand All @@ -18,7 +18,7 @@ repos:
types_or: [python, pyi, jupyter]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
rev: v6.0.0
hooks:
- id: trailing-whitespace
exclude_types:
Expand Down
14 changes: 7 additions & 7 deletions src/animatediff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ def get_dir(dirname: str = "data") -> Path:


__all__ = [
"__version__",
"version_tuple",
"PACKAGE",
"PACKAGE_ROOT",
"HF_HOME",
"HF_HUB_CACHE",
"PACKAGE",
"PACKAGE_ROOT",
"__version__",
"cli",
"console",
"err_console",
"generate",
"get_dir",
"models",
"pipelines",
"rife",
"utils",
"cli",
"generate",
"schedulers",
"settings",
"utils",
"version_tuple",
]
18 changes: 9 additions & 9 deletions src/animatediff/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from datetime import datetime
from pathlib import Path
from typing import Annotated, Optional
from typing import Annotated

import torch
import typer
Expand Down Expand Up @@ -57,8 +57,8 @@
rife_app = None

# mildly cursed globals to allow for reuse of the pipeline if we're being called as a module
pipeline: Optional[AnimationPipeline] = None
last_model_path: Optional[Path] = None
pipeline: AnimationPipeline | None = None
last_model_path: Path | None = None


def version_callback(value: bool):
Expand Down Expand Up @@ -125,7 +125,7 @@ def generate(
),
] = 16,
context: Annotated[
Optional[int],
int | None,
typer.Option(
"--context",
"-C",
Expand All @@ -137,7 +137,7 @@ def generate(
),
] = None,
overlap: Annotated[
Optional[int],
int | None,
typer.Option(
"--overlap",
"-O",
Expand All @@ -149,7 +149,7 @@ def generate(
),
] = None,
stride: Annotated[
Optional[int],
int | None,
typer.Option(
"--stride",
"-S",
Expand Down Expand Up @@ -229,7 +229,7 @@ def generate(
),
] = False,
version: Annotated[
Optional[bool],
bool | None,
typer.Option(
"--version",
"-v",
Expand Down Expand Up @@ -387,7 +387,7 @@ def convert(
),
] = ...,
out_dir: Annotated[
Optional[Path],
Path | None,
typer.Option(
"--out-dir",
"-o",
Expand Down Expand Up @@ -417,7 +417,7 @@ def merge(
),
] = ...,
out_dir: Annotated[
Optional[Path],
Path | None,
typer.Option(
"--out-dir",
"-o",
Expand Down
9 changes: 4 additions & 5 deletions src/animatediff/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import re
from os import PathLike
from pathlib import Path
from typing import Optional, Union

import torch
from diffusers import AutoencoderKL, StableDiffusionPipeline
Expand All @@ -26,7 +25,7 @@


def create_pipeline(
base_model: Union[str, PathLike] = default_base_path,
base_model: str | PathLike = default_base_path,
model_config: ModelConfig = ...,
infer_config: InferenceConfig = ...,
use_xformers: bool = True,
Expand Down Expand Up @@ -136,8 +135,8 @@ def create_pipeline(

def run_inference(
pipeline: AnimationPipeline,
prompt: Optional[str] = None,
prompt_map: Optional[dict[int, str]] = None,
prompt: str | None = None,
prompt_map: dict[int, str] | None = None,
n_prompt: str = ...,
seed: int = -1,
steps: int = 25,
Expand Down Expand Up @@ -186,7 +185,7 @@ def run_inference(
# Trim and clean up the prompt for filename use
prompt_str = prompt or next(iter(prompt_map.values()))
prompt_tags = [re_clean_prompt.sub("", tag).strip().replace(" ", "-") for tag in prompt_str.split(",")]
prompt_str = "_".join((prompt_tags[:6]))
prompt_str = "_".join(prompt_tags[:6])

# generate the output filename and save the video
out_str = f"{idx:02d}_{seed}_{prompt_str}"[:250]
Expand Down
32 changes: 16 additions & 16 deletions src/animatediff/models/attention.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Adapted from https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention.py

from dataclasses import dataclass
from typing import Any, Dict, Optional
from typing import Any

import torch
from diffusers.configuration_utils import ConfigMixin, register_to_config
Expand All @@ -25,14 +25,14 @@ def __init__(
self,
num_attention_heads: int = 16,
attention_head_dim: int = 88,
in_channels: Optional[int] = None,
in_channels: int | None = None,
num_layers: int = 1,
dropout: float = 0.0,
norm_num_groups: int = 32,
cross_attention_dim: Optional[int] = None,
cross_attention_dim: int | None = None,
attention_bias: bool = False,
activation_fn: str = "geglu",
num_embeds_ada_norm: Optional[int] = None,
num_embeds_ada_norm: int | None = None,
use_linear_projection: bool = False,
only_cross_attention: bool = False,
upcast_attention: bool = False,
Expand Down Expand Up @@ -86,11 +86,11 @@ def __init__(
def forward(
self,
hidden_states: torch.Tensor,
encoder_hidden_states: Optional[torch.Tensor] = None,
timestep: Optional[torch.LongTensor] = None,
cross_attention_kwargs: Dict[str, Any] = None,
attention_mask: Optional[torch.Tensor] = None,
encoder_attention_mask: Optional[torch.Tensor] = None,
encoder_hidden_states: torch.Tensor | None = None,
timestep: torch.LongTensor | None = None,
cross_attention_kwargs: dict[str, Any] = None,
attention_mask: torch.Tensor | None = None,
encoder_attention_mask: torch.Tensor | None = None,
return_dict: bool = True,
) -> tuple[Tensor] | Transformer3DModelOutput:
# validate input dim
Expand Down Expand Up @@ -180,9 +180,9 @@ def __init__(
num_attention_heads: int,
attention_head_dim: int,
dropout: float = 0.0,
cross_attention_dim: Optional[int] = None,
cross_attention_dim: int | None = None,
activation_fn: str = "geglu",
num_embeds_ada_norm: Optional[int] = None,
num_embeds_ada_norm: int | None = None,
attention_bias: bool = False,
only_cross_attention: bool = False,
upcast_attention: bool = False,
Expand Down Expand Up @@ -262,11 +262,11 @@ def __init__(
def forward(
self,
hidden_states: torch.FloatTensor,
attention_mask: Optional[torch.FloatTensor] = None,
encoder_hidden_states: Optional[torch.FloatTensor] = None,
encoder_attention_mask: Optional[torch.FloatTensor] = None,
timestep: Optional[torch.LongTensor] = None,
cross_attention_kwargs: Dict[str, Any] = None,
attention_mask: torch.FloatTensor | None = None,
encoder_hidden_states: torch.FloatTensor | None = None,
encoder_attention_mask: torch.FloatTensor | None = None,
timestep: torch.LongTensor | None = None,
cross_attention_kwargs: dict[str, Any] = None,
video_length=None,
):
# SparseCausal-Attention
Expand Down
30 changes: 14 additions & 16 deletions src/animatediff/models/clip.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional, Tuple, Union

import torch
from transformers.modeling_outputs import BaseModelOutput, BaseModelOutputWithPooling
from transformers.models.clip import CLIPTextConfig, CLIPTextModel
Expand Down Expand Up @@ -27,14 +25,14 @@ class CLIPSkipTextTransformer(CLIPTextTransformer):
@replace_return_docstrings(output_type=BaseModelOutputWithPooling, config_class=CLIPTextConfig)
def forward(
self,
input_ids: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.Tensor] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
input_ids: torch.Tensor | None = None,
attention_mask: torch.Tensor | None = None,
position_ids: torch.Tensor | None = None,
output_attentions: bool | None = None,
output_hidden_states: bool | None = None,
return_dict: bool | None = None,
clip_skip: int = 1,
) -> Union[Tuple, BaseModelOutputWithPooling]:
) -> tuple | BaseModelOutputWithPooling:
r"""
Returns:

Expand Down Expand Up @@ -122,14 +120,14 @@ def __init__(self, config: CLIPTextConfig):
@replace_return_docstrings(output_type=BaseModelOutputWithPooling, config_class=CLIPTextConfig)
def forward(
self,
input_ids: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.Tensor] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
input_ids: torch.Tensor | None = None,
attention_mask: torch.Tensor | None = None,
position_ids: torch.Tensor | None = None,
output_attentions: bool | None = None,
output_hidden_states: bool | None = None,
return_dict: bool | None = None,
clip_skip: int = 1,
) -> Union[Tuple, BaseModelOutputWithPooling]:
) -> tuple | BaseModelOutputWithPooling:
r"""
Returns:

Expand Down
7 changes: 3 additions & 4 deletions src/animatediff/models/motion_module.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import math
from dataclasses import dataclass
from typing import Optional

import torch
from diffusers.models.attention import Attention, FeedForward
Expand Down Expand Up @@ -125,8 +124,8 @@ def __init__(
def forward(
self,
hidden_states: Tensor,
encoder_hidden_states: Optional[Tensor] = None,
attention_mask: Optional[Tensor] = None,
encoder_hidden_states: Tensor | None = None,
attention_mask: Tensor | None = None,
):
assert hidden_states.dim() == 5, (
f"Expected hidden_states to have ndim=5, but got ndim={hidden_states.dim()}."
Expand Down Expand Up @@ -251,7 +250,7 @@ class VersatileAttention(Attention):
def __init__(
self,
attention_mode: str = None,
cross_frame_attention_mode: Optional[str] = None,
cross_frame_attention_mode: str | None = None,
temporal_position_encoding: bool = False,
temporal_position_encoding_max_len: int = 24,
*args,
Expand Down
5 changes: 2 additions & 3 deletions src/animatediff/models/resnet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Adapted from https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/resnet.py

from typing import Optional

import torch
import torch.nn.functional as F
Expand All @@ -24,7 +23,7 @@ def __init__(
channels: int,
use_conv: bool = False,
use_conv_transpose: bool = False,
out_channels: Optional[int] = None,
out_channels: int | None = None,
name="conv",
):
super().__init__()
Expand Down Expand Up @@ -75,7 +74,7 @@ def __init__(
self,
channels: int,
use_conv: bool = False,
out_channels: Optional[int] = None,
out_channels: int | None = None,
padding: int = 1,
name="conv",
):
Expand Down
Loading