-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Support for finetuning Hunyuan Video-1.5 model #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pthombre
wants to merge
12
commits into
main
Choose a base branch
from
pranav/hunyuan_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,162
−76
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b36ed97
Adding support for hunyuan finetuning
pthombre c9f5f29
Ensuring that activation checkpointing is gated with a flag
pthombre 54c7e6e
Make the flow matching pipeline logic model agnostic
pthombre 47d86e9
Adding copyright to dataset processing file
pthombre 3450391
Linting fixes
pthombre c3e3bb3
Fix linting
pthombre 5a58e83
lintfix
pablo-garay aa475ee
lintfix
pablo-garay 23bbea0
Update automodel dependencies
pthombre 33ea6fc
Remove unused import
pthombre 375bba6
Setting the minimum diffusers package version
pthombre ede8fca
Merge branch 'main' into pranav/hunyuan_support
pthombre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """ | ||
| Model adapters for FlowMatching Pipeline. | ||
|
|
||
| This module provides model-specific adapters that decouple the flow matching | ||
| logic from model-specific implementation details. | ||
|
|
||
| Available Adapters: | ||
| - ModelAdapter: Abstract base class for all adapters | ||
| - HunyuanAdapter: For HunyuanVideo 1.5 style models | ||
| - SimpleAdapter: For simple transformer models (e.g., Wan) | ||
|
|
||
| Usage: | ||
| from automodel.flow_matching.adapters import HunyuanAdapter, SimpleAdapter | ||
|
|
||
| # Or import the base class to create custom adapters | ||
| from automodel.flow_matching.adapters import ModelAdapter | ||
| """ | ||
|
|
||
| from .base import FlowMatchingContext, ModelAdapter | ||
| from .hunyuan import HunyuanAdapter | ||
| from .simple import SimpleAdapter | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "FlowMatchingContext", | ||
| "ModelAdapter", | ||
| "HunyuanAdapter", | ||
| "SimpleAdapter", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """ | ||
| Base classes and data structures for model adapters. | ||
|
|
||
| This module defines the abstract ModelAdapter class and the FlowMatchingContext | ||
| dataclass used to pass data between the pipeline and adapters. | ||
| """ | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from dataclasses import dataclass | ||
| from typing import Any, Dict | ||
|
|
||
| import torch | ||
| import torch.nn as nn | ||
|
|
||
|
|
||
| @dataclass | ||
| class FlowMatchingContext: | ||
| """ | ||
| Context object passed to model adapters containing all necessary data. | ||
|
|
||
| This provides a clean interface for adapters to access the data they need | ||
| without coupling to the batch dictionary structure. | ||
|
|
||
| Attributes: | ||
| noisy_latents: [B, C, F, H, W] - Noisy latents after interpolation | ||
| video_latents: [B, C, F, H, W] - Original clean latents | ||
| timesteps: [B] - Sampled timesteps | ||
| sigma: [B] - Sigma values | ||
| task_type: "t2v" or "i2v" | ||
| data_type: "video" or "image" | ||
| device: Device for tensor operations | ||
| dtype: Data type for tensor operations | ||
| batch: Original batch dictionary (for model-specific data) | ||
| """ | ||
|
|
||
| # Core tensors | ||
| noisy_latents: torch.Tensor | ||
| video_latents: torch.Tensor | ||
| timesteps: torch.Tensor | ||
| sigma: torch.Tensor | ||
|
|
||
| # Task info | ||
| task_type: str | ||
| data_type: str | ||
|
|
||
| # Device/dtype | ||
| device: torch.device | ||
| dtype: torch.dtype | ||
|
|
||
| # Original batch (for model-specific data) | ||
| batch: Dict[str, Any] | ||
|
|
||
|
|
||
| class ModelAdapter(ABC): | ||
| """ | ||
| Abstract base class for model-specific forward pass logic. | ||
|
|
||
| Implement this class to add support for new model architectures | ||
| without modifying the FlowMatchingPipeline. | ||
|
|
||
| The adapter pattern decouples the flow matching logic from model-specific | ||
| details like input preparation and forward pass conventions. | ||
|
|
||
| Example: | ||
| class MyCustomAdapter(ModelAdapter): | ||
| def prepare_inputs(self, context: FlowMatchingContext) -> Dict[str, Any]: | ||
| return { | ||
| "x": context.noisy_latents, | ||
| "t": context.timesteps, | ||
| "cond": context.batch["my_conditioning"], | ||
| } | ||
|
|
||
| def forward(self, model: nn.Module, inputs: Dict[str, Any]) -> torch.Tensor: | ||
| return model(**inputs) | ||
|
|
||
| pipeline = FlowMatchingPipelineV2(model_adapter=MyCustomAdapter()) | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def prepare_inputs(self, context: FlowMatchingContext) -> Dict[str, Any]: | ||
| """ | ||
| Prepare model-specific inputs from the context. | ||
|
|
||
| Args: | ||
| context: FlowMatchingContext containing all necessary data | ||
|
|
||
| Returns: | ||
| Dictionary of inputs to pass to the model's forward method | ||
| """ | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def forward(self, model: nn.Module, inputs: Dict[str, Any]) -> torch.Tensor: | ||
| """ | ||
| Execute the model forward pass. | ||
|
|
||
| Args: | ||
| model: The model to call | ||
| inputs: Dictionary of inputs from prepare_inputs() | ||
|
|
||
| Returns: | ||
| Model prediction tensor | ||
| """ | ||
| pass | ||
|
|
||
| def get_condition_latents(self, latents: torch.Tensor, task_type: str) -> torch.Tensor: | ||
| """ | ||
| Generate conditional latents based on task type. | ||
|
|
||
| Override this method if your model uses a different conditioning scheme. | ||
| Default implementation adds a channel for conditioning mask. | ||
|
|
||
| Args: | ||
| latents: Input latents [B, C, F, H, W] | ||
| task_type: Task type ("t2v" or "i2v") | ||
|
|
||
| Returns: | ||
| Conditional latents [B, C+1, F, H, W] | ||
| """ | ||
| b, c, f, h, w = latents.shape | ||
| cond = torch.zeros([b, c + 1, f, h, w], device=latents.device, dtype=latents.dtype) | ||
|
|
||
| if task_type == "t2v": | ||
| return cond | ||
| elif task_type == "i2v": | ||
| cond[:, :-1, :1] = latents[:, :, :1] | ||
| cond[:, -1, 0] = 1 | ||
| return cond | ||
| else: | ||
| raise ValueError(f"Unsupported task type: {task_type}") | ||
|
|
||
| def post_process_prediction(self, model_pred: torch.Tensor) -> torch.Tensor: | ||
| """ | ||
| Post-process model prediction if needed. | ||
|
|
||
| Override this for models that return extra outputs or need transformation. | ||
|
|
||
| Args: | ||
| model_pred: Raw model output | ||
|
|
||
| Returns: | ||
| Processed prediction tensor | ||
| """ | ||
| if isinstance(model_pred, tuple): | ||
| return model_pred[0] | ||
| return model_pred | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this context limited to just flow matching? if i'm not missing something EDM pipeline requires the same set of attributes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not specifically. But since I've implemented only flow matching thought to call it this.