-
Notifications
You must be signed in to change notification settings - Fork 133
[WIP][tests] add precomputation tests #234
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
72d3000
add precomputation tests
sayakpaul d91476d
update
sayakpaul 9ba2aff
updates
sayakpaul 348fe81
changes
sayakpaul 3dda221
quality
sayakpaul 8841dbd
remove do_not_run_validation.
sayakpaul ee368a1
make get_memory_stat method leaner.
sayakpaul d63fbcf
reset memory utils.
sayakpaul 5529264
sync util.
sayakpaul fdab36b
Merge branch 'main' into add-precompute-tests
sayakpaul 1f61911
updates
sayakpaul 4422071
Merge branch 'main' into add-precompute-tests
sayakpaul c432f39
typo.
sayakpaul 3609fdf
resolve imports.
sayakpaul 8faac47
updates
sayakpaul 19356bb
update ltx
sayakpaul 5909c21
updates
sayakpaul 778d077
updates
sayakpaul 0f8b4bb
updates
sayakpaul 9595803
updates
sayakpaul 2c7f758
fixes
sayakpaul a91368d
Merge branch 'main' into add-precompute-tests
sayakpaul 846a0fb
Merge branch 'main' into add-precompute-tests
sayakpaul ef42b05
Merge branch 'main' into add-precompute-tests
sayakpaul c745d22
fixes
sayakpaul 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
import torch | ||
from diffusers import AutoencoderKLCogVideoX, CogVideoXDDIMScheduler, CogVideoXPipeline, CogVideoXTransformer3DModel | ||
from PIL import Image | ||
from transformers import T5EncoderModel, T5Tokenizer | ||
from transformers import T5EncoderModel, T5Tokenizer, AutoTokenizer | ||
|
||
from .utils import prepare_rotary_positional_embeddings | ||
|
||
|
@@ -15,7 +15,10 @@ def load_condition_models( | |
cache_dir: Optional[str] = None, | ||
**kwargs, | ||
): | ||
tokenizer = T5Tokenizer.from_pretrained(model_id, subfolder="tokenizer", revision=revision, cache_dir=cache_dir) | ||
try: | ||
tokenizer = T5Tokenizer.from_pretrained(model_id, subfolder="tokenizer", revision=revision, cache_dir=cache_dir) | ||
except: | ||
tokenizer = AutoTokenizer.from_pretrained(model_id, subfolder="tokenizer", revision=revision, cache_dir=cache_dir) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not super proud of this but we cannot do |
||
text_encoder = T5EncoderModel.from_pretrained( | ||
model_id, subfolder="text_encoder", torch_dtype=text_encoder_dtype, revision=revision, cache_dir=cache_dir | ||
) | ||
|
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
Empty file.
Empty file.
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,31 @@ | ||
import sys | ||
from pathlib import Path | ||
|
||
current_file = Path(__file__).resolve() | ||
root_dir = current_file.parents[3] | ||
sys.path.append(str(root_dir)) | ||
|
||
from ..test_trainers_common import TrainerTestMixin | ||
from typing import Tuple | ||
from finetrainers import Args | ||
import unittest | ||
|
||
# Copied for now. | ||
def parse_resolution_bucket(resolution_bucket: str) -> Tuple[int, ...]: | ||
return tuple(map(int, resolution_bucket.split("x"))) | ||
|
||
|
||
|
||
class CogVideoXTester(unittest.TestCase, TrainerTestMixin): | ||
model_name = "cogvideox" | ||
|
||
def get_training_args(self): | ||
args = Args() | ||
args.model_name = self.model_name | ||
args.training_type = "lora" | ||
args.pretrained_model_name_or_path = "finetrainers/dummy-cogvideox" | ||
args.data_root = "" # will be set from the tester method. | ||
args.video_resolution_buckets = [parse_resolution_bucket("9x16x16")] | ||
args.precompute_conditions = True | ||
args.do_not_run_validation = True | ||
return args |
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,63 @@ | ||
import sys | ||
from pathlib import Path | ||
|
||
current_file = Path(__file__).resolve() | ||
root_dir = current_file.parents[1] | ||
sys.path.append(str(root_dir)) | ||
|
||
|
||
from finetrainers import Trainer | ||
from finetrainers.utils.file_utils import string_to_filename | ||
from finetrainers.constants import PRECOMPUTED_DIR_NAME, PRECOMPUTED_CONDITIONS_DIR_NAME, PRECOMPUTED_LATENTS_DIR_NAME | ||
from huggingface_hub import snapshot_download | ||
import tempfile | ||
import glob | ||
import os | ||
|
||
class TrainerTestMixin: | ||
model_name = None | ||
|
||
def get_training_args(self): | ||
raise NotImplementedError | ||
|
||
def download_dataset_txt_format(self, cache_dir): | ||
path = snapshot_download(repo_id="finetrainers/dummy-disney-dataset", repo_type="dataset", cache_dir=cache_dir) | ||
return path | ||
|
||
def test_precomputation_txt_format(self): | ||
# Here we assume the dataset is formatted like: | ||
# https://huggingface.co/datasets/Wild-Heart/Disney-VideoGeneration-Dataset/tree/main | ||
training_args = self.get_training_args() | ||
|
||
with tempfile.TemporaryDirectory() as tmpdir: | ||
# Prepare remaining args. | ||
training_args.data_root = Path(self.download_dataset_txt_format(cache_dir=tmpdir)) | ||
|
||
training_args.video_column = "videos.txt" | ||
training_args.caption_column = "prompt.txt" | ||
with open(f"{training_args.data_root}/{training_args.video_column}", "r", encoding="utf-8") as file: | ||
video_paths = [training_args.data_root.joinpath(line.strip()) for line in file.readlines() if len(line.strip()) > 0] | ||
|
||
# Initialize trainer. | ||
training_args.output_dir = tmpdir | ||
trainer = Trainer(training_args) | ||
training_args = trainer.args | ||
|
||
# Perform precomputations. | ||
trainer.prepare_dataset() | ||
trainer.prepare_models() | ||
trainer.prepare_precomputations() | ||
|
||
cleaned_model_id = string_to_filename(training_args.pretrained_model_name_or_path) | ||
precomputation_dir = ( | ||
Path(training_args.data_root) / f"{training_args.model_name}_{cleaned_model_id}_{PRECOMPUTED_DIR_NAME}" | ||
) | ||
|
||
# Checks. | ||
conditions_dir = precomputation_dir / PRECOMPUTED_CONDITIONS_DIR_NAME | ||
latents_dir = precomputation_dir / PRECOMPUTED_LATENTS_DIR_NAME | ||
assert os.path.exists(precomputation_dir), f"Precomputation wasn't successful. Couldn't find the precomputed dir: {os.listdir(training_args.data_root)=}\n" | ||
assert os.path.exists(conditions_dir), f"conditions dir ({str(conditions_dir)}) doesn't exist" | ||
assert os.path.exists(latents_dir), f"latents dir ({str(latents_dir)}) doesn't exist" | ||
assert len(video_paths) == len([p for p in conditions_dir.glob("*.pt")]) | ||
assert len(video_paths) == len([p for p in latents_dir.glob("*.pt")]) |
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.
Uh oh!
There was an error while loading. Please reload this page.