-
Notifications
You must be signed in to change notification settings - Fork 189
[Preprocess] [feat] Support HunyuanVideo Model #754
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
base: main
Are you sure you want to change the base?
Conversation
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.
Summary of Changes
Hello @JiayiZhangA, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces support for the HunyuanVideo model within the FastVideo framework, primarily by adding new preprocessing pipelines and a utility script. It enables the preparation of datasets for both text-to-video and image-to-video tasks using the Hunyuan model, alongside minor improvements to data validation and dataset loading mechanisms.
Highlights
- Hunyuan Preprocessing Script: A new bash script (
preprocess_hunyuan_data_t2v.sh) is added to facilitate the preprocessing of Hunyuan text-to-video datasets, defining model paths, dataset locations, and various video parameters. - Dedicated Hunyuan Pipelines: New Python classes (
PreprocessPipelineI2VandPreprocessPipelineT2V) are introduced to define specific preprocessing workflows for Hunyuan models, handling stages like text transformation, prompt encoding, and video encoding for both image-to-video and text-to-video tasks. - Robust Data Validation: The data validation logic in preprocessing components has been enhanced to correctly handle cases where the 'fps' key might be absent from the input batch, improving the robustness of the data pipeline.
- Dataset Loading Enhancement: The dataset loading mechanism has been refined to explicitly specify the 'data' field when loading training datasets, ensuring accurate data retrieval for the preprocessing workflow.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
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.
Code Review
This pull request introduces support for Hunyuan, including preprocessing pipelines for text-to-video and image-to-video tasks. The changes include a new preprocessing script, new pipeline definitions, and updates to the data validation and loading logic.
My review focuses on improving script robustness, reducing code duplication in the new pipelines for better maintainability, and removing leftover debugging code. Overall, the changes look good and add valuable functionality.
| #!/bin/bash | ||
|
|
||
| GPU_NUM=1 # 2,4,8 | ||
| MODEL_PATH="hunyuanvideo-community/HunyuanVideo" | ||
| DATASET_PATH="/FastVideo/data/mini_i2v_dataset/crush-smol_raw" | ||
| OUTPUT_DIR="/FastVideo/data/mini_i2v_dataset/crush-smol_processed_t2v_hunyuan/" | ||
|
|
||
| torchrun --nproc_per_node=$GPU_NUM \ | ||
| -m fastvideo.pipelines.preprocess.v1_preprocessing_new \ | ||
| --model_path $MODEL_PATH \ | ||
| --mode preprocess \ | ||
| --workload_type t2v \ | ||
| --preprocess.dataset_path $DATASET_PATH \ | ||
| --preprocess.dataset_output_dir $OUTPUT_DIR \ | ||
| --preprocess.preprocess_video_batch_size 2 \ | ||
| --preprocess.dataloader_num_workers 0 \ | ||
| --preprocess.max_height 480 \ | ||
| --preprocess.max_width 832 \ | ||
| --preprocess.num_frames 77 \ | ||
| --preprocess.train_fps 16 \ | ||
| --preprocess.samples_per_file 8 \ | ||
| --preprocess.flush_frequency 8 \ | ||
| --preprocess.video_length_tolerance_range 5 |
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.
For improved robustness and to prevent potential issues with paths containing spaces or special characters, it's recommended to:
- Add
set -euo pipefailat the beginning of the script. This will make the script exit on any error, treat unset variables as an error, and fail if any command in a pipeline fails. - Double-quote all variable expansions to prevent word splitting and globbing.
These changes will make the script more reliable and safer to run.
#!/bin/bash
set -euo pipefail
GPU_NUM=1 # 2,4,8
MODEL_PATH="hunyuanvideo-community/HunyuanVideo"
DATASET_PATH="/FastVideo/data/mini_i2v_dataset/crush-smol_raw"
OUTPUT_DIR="/FastVideo/data/mini_i2v_dataset/crush-smol_processed_t2v_hunyuan/"
torchrun --nproc_per_node="$GPU_NUM" \
-m fastvideo.pipelines.preprocess.v1_preprocessing_new \
--model_path "$MODEL_PATH" \
--mode preprocess \
--workload_type t2v \
--preprocess.dataset_path "$DATASET_PATH" \
--preprocess.dataset_output_dir "$OUTPUT_DIR" \
--preprocess.preprocess_video_batch_size 2 \
--preprocess.dataloader_num_workers 0 \
--preprocess.max_height 480 \
--preprocess.max_width 832 \
--preprocess.num_frames 77 \
--preprocess.train_fps 16 \
--preprocess.samples_per_file 8 \
--preprocess.flush_frequency 8 \
--preprocess.video_length_tolerance_range 5| print("-------------------------------") | ||
| print(batch) |
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.
| class PreprocessPipelineI2V(ComposedPipelineBase): | ||
| _required_config_modules = [ | ||
| "image_encoder", "image_processor", "text_encoder", "tokenizer", "vae" | ||
| ] | ||
|
|
||
| def create_pipeline_stages(self, fastvideo_args: FastVideoArgs): | ||
| assert fastvideo_args.preprocess_config is not None | ||
| self.add_stage(stage_name="text_transform_stage", | ||
| stage=TextTransformStage( | ||
| cfg_uncondition_drop_rate=fastvideo_args. | ||
| preprocess_config.training_cfg_rate, | ||
| seed=fastvideo_args.preprocess_config.seed, | ||
| )) | ||
| self.add_stage(stage_name="prompt_encoding_stage", | ||
| stage=TextEncodingStage( | ||
| text_encoders=[self.get_module("text_encoder")], | ||
| tokenizers=[self.get_module("tokenizer")], | ||
| )) | ||
| self.add_stage( | ||
| stage_name="video_transform_stage", | ||
| stage=VideoTransformStage( | ||
| train_fps=fastvideo_args.preprocess_config.train_fps, | ||
| num_frames=fastvideo_args.preprocess_config.num_frames, | ||
| max_height=fastvideo_args.preprocess_config.max_height, | ||
| max_width=fastvideo_args.preprocess_config.max_width, | ||
| do_temporal_sample=fastvideo_args.preprocess_config. | ||
| do_temporal_sample, | ||
| )) | ||
| if (self.get_module("image_encoder") is not None | ||
| and self.get_module("image_processor") is not None): | ||
| self.add_stage( | ||
| stage_name="image_encoding_stage", | ||
| stage=ImageEncodingStage( | ||
| image_encoder=self.get_module("image_encoder"), | ||
| image_processor=self.get_module("image_processor"), | ||
| )) | ||
| self.add_stage(stage_name="image_vae_encoding_stage", | ||
| stage=ImageVAEEncodingStage( | ||
| vae=self.get_module("vae"), )) | ||
| self.add_stage(stage_name="video_encoding_stage", | ||
| stage=EncodingStage(vae=self.get_module("vae"), )) | ||
|
|
||
|
|
||
| class PreprocessPipelineT2V(ComposedPipelineBase): | ||
| _required_config_modules = ["text_encoder", "tokenizer", "vae"] | ||
|
|
||
| def create_pipeline_stages(self, fastvideo_args: FastVideoArgs): | ||
| assert fastvideo_args.preprocess_config is not None | ||
| self.add_stage(stage_name="text_transform_stage", | ||
| stage=TextTransformStage( | ||
| cfg_uncondition_drop_rate=fastvideo_args. | ||
| preprocess_config.training_cfg_rate, | ||
| seed=fastvideo_args.preprocess_config.seed, | ||
| )) | ||
| self.add_stage(stage_name="prompt_encoding_stage", | ||
| stage=TextEncodingStage( | ||
| text_encoders=[self.get_module("text_encoder")], | ||
| tokenizers=[self.get_module("tokenizer")], | ||
| )) | ||
| self.add_stage( | ||
| stage_name="video_transform_stage", | ||
| stage=VideoTransformStage( | ||
| train_fps=fastvideo_args.preprocess_config.train_fps, | ||
| num_frames=fastvideo_args.preprocess_config.num_frames, | ||
| max_height=fastvideo_args.preprocess_config.max_height, | ||
| max_width=fastvideo_args.preprocess_config.max_width, | ||
| do_temporal_sample=fastvideo_args.preprocess_config. | ||
| do_temporal_sample, | ||
| )) | ||
| self.add_stage(stage_name="video_encoding_stage", | ||
| stage=EncodingStage(vae=self.get_module("vae"), )) |
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.
The classes PreprocessPipelineI2V and PreprocessPipelineT2V share a significant amount of duplicated code. To improve maintainability and reduce redundancy, you can refactor the common logic into a helper function or a base class.
Here's a suggested refactoring using a helper function to build the common pipeline stages.
def _add_base_stages(pipeline: ComposedPipelineBase, fastvideo_args: FastVideoArgs):
"""Helper to add common preprocessing stages."""
assert fastvideo_args.preprocess_config is not None
pipeline.add_stage(stage_name="text_transform_stage",
stage=TextTransformStage(
cfg_uncondition_drop_rate=fastvideo_args.
preprocess_config.training_cfg_rate,
seed=fastvideo_args.preprocess_config.seed,
))
pipeline.add_stage(stage_name="prompt_encoding_stage",
stage=TextEncodingStage(
text_encoders=[pipeline.get_module("text_encoder")],
tokenizers=[pipeline.get_module("tokenizer")],
))
pipeline.add_stage(
stage_name="video_transform_stage",
stage=VideoTransformStage(
train_fps=fastvideo_args.preprocess_config.train_fps,
num_frames=fastvideo_args.preprocess_config.num_frames,
max_height=fastvideo_args.preprocess_config.max_height,
max_width=fastvideo_args.preprocess_config.max_width,
do_temporal_sample=fastvideo_args.preprocess_config.
do_temporal_sample,
))
class PreprocessPipelineI2V(ComposedPipelineBase):
_required_config_modules = [
"image_encoder", "image_processor", "text_encoder", "tokenizer", "vae"
]
def create_pipeline_stages(self, fastvideo_args: FastVideoArgs):
_add_base_stages(self, fastvideo_args)
if (self.get_module("image_encoder") is not None
and self.get_module("image_processor") is not None):
self.add_stage(
stage_name="image_encoding_stage",
stage=ImageEncodingStage(
image_encoder=self.get_module("image_encoder"),
image_processor=self.get_module("image_processor"),
))
self.add_stage(stage_name="image_vae_encoding_stage",
stage=ImageVAEEncodingStage(
vae=self.get_module("vae"), ))
self.add_stage(stage_name="video_encoding_stage",
stage=EncodingStage(vae=self.get_module("vae"), ))
class PreprocessPipelineT2V(ComposedPipelineBase):
_required_config_modules = ["text_encoder", "tokenizer", "vae"]
def create_pipeline_stages(self, fastvideo_args: FastVideoArgs):
_add_base_stages(self, fastvideo_args)
self.add_stage(stage_name="video_encoding_stage",
stage=EncodingStage(vae=self.get_module("vae"), ))|
|
||
| def _validate_data_type(self, batch: dict[str, Any]) -> bool: | ||
| """Validate basic validity of data items""" | ||
| print("-------------------------------") |
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.
remove
|
please rebase |
| # training dataset | ||
| training_dataset = load_dataset(preprocess_config.dataset_path, | ||
| split="train") | ||
| split="train",field="data") |
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.
Please change field to a member of PreprocessConfig to specify it in cli rather than hardcoding it
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.
Looks like this isn't rebased correctly, with many fake changes to irrelevant files. Please rebase or merge main
|
|
||
| # Save to JSON file | ||
| output_file = folder_path / output_name | ||
| print(folder_path,output_file,output_name) |
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.
remove this
| from datasets import Dataset, Video, load_dataset | ||
|
|
||
| from fastvideo.configs.configs import DatasetType, PreprocessConfig | ||
| from fastvideo.distributed.parallel_state import get_world_rank, get_world_size |
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.
There are some issues with your rebase. You should align your code with our implementation(accept both) instead of only use your code(accept current). Please revert this.
| return written_count | ||
|
|
||
|
|
||
| def build_dataset(preprocess_config: PreprocessConfig, split: str, |
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.
revert this
| self.add_component("raw_data_validator", raw_data_validator) | ||
|
|
||
| # training dataset | ||
| training_dataset = build_dataset(preprocess_config, |
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.
revert this
No description provided.