-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Batch size #1205
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
ahmeddawy
wants to merge
5
commits into
modelscope:main
Choose a base branch
from
ahmeddawy:batch-size
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.
+383
−27
Open
Batch size #1205
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
27c354d
enable batch size
ahmeddawy 6a5a0fe
update bash
ahmeddawy 1cb14f7
Adding eval videos and losses print
ahmeddawy edce953
Resolve stash conflict in Wan2.1-VACE-1.3B lora train script
ahmeddawy 2a790e9
save best eval ckpt
ahmeddawy 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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,10 +1,127 @@ | ||||||
| import os, torch | ||||||
| import numpy as np | ||||||
| from tqdm import tqdm | ||||||
| from accelerate import Accelerator | ||||||
| from .training_module import DiffusionTrainingModule | ||||||
| from .logger import ModelLogger | ||||||
|
|
||||||
|
|
||||||
| def _pad_frames(frames, target_frames): | ||||||
| if target_frames is None: | ||||||
| return frames | ||||||
| if len(frames) >= target_frames: | ||||||
| return frames[:target_frames] | ||||||
| if len(frames) == 0: | ||||||
| raise ValueError("Cannot pad empty frame list.") | ||||||
| pad_frame = frames[-1] | ||||||
| return frames + [pad_frame] * (target_frames - len(frames)) | ||||||
|
|
||||||
|
|
||||||
| def _frame_to_tensor(frame, min_value=-1.0, max_value=1.0): | ||||||
| if isinstance(frame, torch.Tensor): | ||||||
| tensor = frame | ||||||
| if tensor.dim() == 3 and tensor.shape[0] not in (1, 3): | ||||||
| tensor = tensor.permute(2, 0, 1) | ||||||
| return tensor | ||||||
| array = np.array(frame, dtype=np.float32) | ||||||
| tensor = torch.from_numpy(array).permute(2, 0, 1) | ||||||
| tensor = tensor * ((max_value - min_value) / 255.0) + min_value | ||||||
| return tensor | ||||||
|
|
||||||
|
|
||||||
| def _frames_to_tensor(frames, min_value=-1.0, max_value=1.0): | ||||||
| frame_tensors = [_frame_to_tensor(frame, min_value=min_value, max_value=max_value) for frame in frames] | ||||||
| return torch.stack(frame_tensors, dim=1) | ||||||
|
|
||||||
|
|
||||||
| def _collate_batch(batch, data_file_keys, num_frames): | ||||||
| if len(batch) == 1: | ||||||
| return batch[0] | ||||||
| single_frame_keys = {"reference_image", "vace_reference_image"} | ||||||
| output = {} | ||||||
| keys = batch[0].keys() | ||||||
| for key in keys: | ||||||
| values = [sample.get(key) for sample in batch] | ||||||
| if key in data_file_keys: | ||||||
| is_mask = "mask" in key | ||||||
| min_value = 0.0 if is_mask else -1.0 | ||||||
| max_value = 1.0 if is_mask else 1.0 | ||||||
| if any(value is None for value in values): | ||||||
| raise ValueError(f"Missing key '{key}' in one or more batch samples.") | ||||||
| if key in single_frame_keys: | ||||||
| frames = [] | ||||||
| for value in values: | ||||||
| if isinstance(value, list): | ||||||
| if len(value) == 0: | ||||||
| raise ValueError(f"Key '{key}' has empty frame list.") | ||||||
| frames.append(value[0]) | ||||||
| else: | ||||||
| frames.append(value) | ||||||
| tensors = [_frame_to_tensor(frame, min_value=min_value, max_value=max_value) for frame in frames] | ||||||
| output[key] = torch.stack(tensors, dim=0) | ||||||
| else: | ||||||
| tensors = [] | ||||||
| for value in values: | ||||||
| if isinstance(value, list): | ||||||
|
Contributor
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. Similar to the previous comment, this
Suggested change
|
||||||
| padded = _pad_frames(value, num_frames) | ||||||
| tensors.append(_frames_to_tensor(padded, min_value=min_value, max_value=max_value)) | ||||||
| elif isinstance(value, torch.Tensor): | ||||||
| tensors.append(value) | ||||||
| else: | ||||||
| raise ValueError(f"Unsupported value type for key '{key}': {type(value)}") | ||||||
| output[key] = torch.stack(tensors, dim=0) | ||||||
| else: | ||||||
| output[key] = values | ||||||
| return output | ||||||
|
|
||||||
|
|
||||||
| def run_validation( | ||||||
| accelerator: Accelerator, | ||||||
| dataset: torch.utils.data.Dataset, | ||||||
| model: DiffusionTrainingModule, | ||||||
| num_workers: int, | ||||||
| batch_size: int, | ||||||
| data_file_keys: list[str], | ||||||
| num_frames: int, | ||||||
| max_batches: int = None, | ||||||
| ): | ||||||
| if dataset is None: | ||||||
| return None | ||||||
| if batch_size > 1: | ||||||
| dataloader = torch.utils.data.DataLoader( | ||||||
| dataset, | ||||||
| batch_size=batch_size, | ||||||
| shuffle=False, | ||||||
| collate_fn=lambda batch: _collate_batch(batch, data_file_keys, num_frames), | ||||||
| num_workers=num_workers, | ||||||
| ) | ||||||
| else: | ||||||
| dataloader = torch.utils.data.DataLoader(dataset, shuffle=False, collate_fn=lambda x: x[0], num_workers=num_workers) | ||||||
| dataloader = accelerator.prepare(dataloader) | ||||||
| was_training = model.training | ||||||
| model.eval() | ||||||
| losses = [] | ||||||
| with torch.no_grad(): | ||||||
| for step, data in enumerate(tqdm(dataloader, desc="Eval")): | ||||||
| if max_batches is not None and step >= max_batches: | ||||||
| break | ||||||
| if dataset.load_from_cache: | ||||||
| loss = model({}, inputs=data) | ||||||
| else: | ||||||
| loss = model(data) | ||||||
| loss = loss.detach().float() | ||||||
| loss = accelerator.gather(loss) | ||||||
| losses.append(loss.flatten()) | ||||||
| if was_training: | ||||||
| model.train() | ||||||
| if not losses: | ||||||
| return None | ||||||
| mean_loss = torch.cat(losses).mean().item() | ||||||
| if accelerator.is_main_process: | ||||||
| print(f"Eval loss: {mean_loss:.6f}") | ||||||
| return mean_loss | ||||||
|
|
||||||
|
|
||||||
| def launch_training_task( | ||||||
| accelerator: Accelerator, | ||||||
| dataset: torch.utils.data.Dataset, | ||||||
|
|
@@ -15,6 +132,7 @@ def launch_training_task( | |||||
| num_workers: int = 1, | ||||||
| save_steps: int = None, | ||||||
| num_epochs: int = 1, | ||||||
| val_dataset: torch.utils.data.Dataset = None, | ||||||
| args = None, | ||||||
| ): | ||||||
| if args is not None: | ||||||
|
|
@@ -23,27 +141,88 @@ def launch_training_task( | |||||
| num_workers = args.dataset_num_workers | ||||||
| save_steps = args.save_steps | ||||||
| num_epochs = args.num_epochs | ||||||
| batch_size = args.batch_size | ||||||
| data_file_keys = args.data_file_keys.split(",") | ||||||
| num_frames = getattr(args, "num_frames", None) | ||||||
| val_num_workers = args.val_dataset_num_workers | ||||||
| val_batch_size = args.val_batch_size or batch_size | ||||||
| val_data_file_keys = (args.val_data_file_keys or args.data_file_keys).split(",") | ||||||
| eval_every_n_epochs = args.eval_every_n_epochs | ||||||
| eval_max_batches = args.eval_max_batches | ||||||
| else: | ||||||
| batch_size = 1 | ||||||
| data_file_keys = [] | ||||||
| num_frames = None | ||||||
| val_num_workers = 0 | ||||||
| val_batch_size = 1 | ||||||
| val_data_file_keys = [] | ||||||
| eval_every_n_epochs = 0 | ||||||
| eval_max_batches = None | ||||||
|
|
||||||
| optimizer = torch.optim.AdamW(model.trainable_modules(), lr=learning_rate, weight_decay=weight_decay) | ||||||
| scheduler = torch.optim.lr_scheduler.ConstantLR(optimizer) | ||||||
| dataloader = torch.utils.data.DataLoader(dataset, shuffle=True, collate_fn=lambda x: x[0], num_workers=num_workers) | ||||||
| if batch_size > 1: | ||||||
| dataloader = torch.utils.data.DataLoader( | ||||||
| dataset, | ||||||
| batch_size=batch_size, | ||||||
| shuffle=True, | ||||||
| collate_fn=lambda batch: _collate_batch(batch, data_file_keys, num_frames), | ||||||
| num_workers=num_workers, | ||||||
| ) | ||||||
| else: | ||||||
| dataloader = torch.utils.data.DataLoader(dataset, shuffle=True, collate_fn=lambda x: x[0], num_workers=num_workers) | ||||||
|
|
||||||
| model, optimizer, dataloader, scheduler = accelerator.prepare(model, optimizer, dataloader, scheduler) | ||||||
|
|
||||||
| best_val_loss = None | ||||||
| for epoch_id in range(num_epochs): | ||||||
| epoch_loss_sum = None | ||||||
| epoch_steps = 0 | ||||||
| for data in tqdm(dataloader): | ||||||
| with accelerator.accumulate(model): | ||||||
| optimizer.zero_grad() | ||||||
| if dataset.load_from_cache: | ||||||
| loss = model({}, inputs=data) | ||||||
| else: | ||||||
| loss = model(data) | ||||||
| loss_value = loss.detach().float() | ||||||
| if epoch_loss_sum is None: | ||||||
| epoch_loss_sum = loss_value | ||||||
| else: | ||||||
| epoch_loss_sum = epoch_loss_sum + loss_value | ||||||
| epoch_steps += 1 | ||||||
| accelerator.backward(loss) | ||||||
| optimizer.step() | ||||||
| model_logger.on_step_end(accelerator, model, save_steps) | ||||||
| scheduler.step() | ||||||
| if epoch_loss_sum is None: | ||||||
| epoch_loss_sum = torch.tensor(0.0, device=accelerator.device) | ||||||
| steps_tensor = torch.tensor(float(epoch_steps), device=epoch_loss_sum.device) | ||||||
| loss_stats = torch.stack([epoch_loss_sum, steps_tensor]).unsqueeze(0) | ||||||
| gathered_stats = accelerator.gather(loss_stats) | ||||||
| if accelerator.is_main_process: | ||||||
| total_loss = gathered_stats[:, 0].sum().item() | ||||||
| total_steps = gathered_stats[:, 1].sum().item() | ||||||
| avg_loss = total_loss / total_steps if total_steps > 0 else float("nan") | ||||||
| print(f"Train loss (epoch {epoch_id}): {avg_loss:.6f}") | ||||||
| if save_steps is None: | ||||||
| model_logger.on_epoch_end(accelerator, model, epoch_id) | ||||||
| if val_dataset is not None and eval_every_n_epochs > 0 and (epoch_id + 1) % eval_every_n_epochs == 0: | ||||||
| val_loss = run_validation( | ||||||
| accelerator, | ||||||
| val_dataset, | ||||||
| model, | ||||||
| val_num_workers, | ||||||
| val_batch_size, | ||||||
| val_data_file_keys, | ||||||
| num_frames, | ||||||
| max_batches=eval_max_batches, | ||||||
| ) | ||||||
| if val_loss is not None and (best_val_loss is None or val_loss < best_val_loss): | ||||||
| best_val_loss = val_loss | ||||||
| if accelerator.is_main_process: | ||||||
| print(f"New best eval loss: {best_val_loss:.6f}. Saving best checkpoint.") | ||||||
| model_logger.save_model(accelerator, model, "best.safetensors") | ||||||
| model_logger.on_training_end(accelerator, model, save_steps) | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
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
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.
The check
isinstance(value, list)is too restrictive. It will likely fail for custom sequence-like objects such asVideoDatareturned by the dataset, causing an error during batch collation. To make this more robust, you should check for the generalSequencetype fromcollections.abcand explicitly exclude strings.You'll need to add
from collections.abc import Sequenceat the top of the file.