-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Add SeedVR2 support (CORE-6) #14424
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
pollockjj
wants to merge
8
commits into
Comfy-Org:master
Choose a base branch
from
pollockjj:seedvr2-native-support-v5
base: master
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.
Open
Add SeedVR2 support (CORE-6) #14424
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cd18c44
Add SeedVR2 model support
pollockjj a7ea0c2
Add SeedVR2 VAE support
pollockjj d54ce3d
Add SeedVR2 workflow nodes
pollockjj 0fdbc5d
Add SeedVR2 core coverage
pollockjj bed0cd2
Add SeedVR2 VAE coverage
pollockjj 7050bdc
Add SeedVR2 node coverage
pollockjj cfb9c31
Add SeedVR2 sampler coverage
pollockjj ad04a61
Merge branch 'master' into seedvr2-native-support-v5
pollockjj 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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import torch | ||
|
|
||
| from comfy.ldm.modules import attention as _attention | ||
|
|
||
|
|
||
| def _var_attention_qkv(q, k, v, heads, skip_reshape): | ||
| if skip_reshape: | ||
| return q, k, v, q.shape[-1] | ||
| total_tokens, embed_dim = q.shape | ||
| head_dim = embed_dim // heads | ||
| return ( | ||
| q.view(total_tokens, heads, head_dim), | ||
| k.view(k.shape[0], heads, head_dim), | ||
| v.view(v.shape[0], heads, head_dim), | ||
| head_dim, | ||
| ) | ||
|
|
||
|
|
||
| def _var_attention_output(out, heads, head_dim, skip_output_reshape): | ||
| if skip_output_reshape: | ||
| return out | ||
| return out.reshape(-1, heads * head_dim) | ||
|
|
||
|
|
||
| def _validate_split_cu_seqlens(name, cu_seqlens, token_count): | ||
| if cu_seqlens.dtype not in (torch.int32, torch.int64): | ||
| raise ValueError(f"{name} must use an integer dtype") | ||
| if cu_seqlens.ndim != 1 or cu_seqlens.numel() < 2: | ||
| raise ValueError(f"{name} must be a 1D tensor with at least two offsets") | ||
| if cu_seqlens[0].item() != 0: | ||
| raise ValueError(f"{name} must start at 0") | ||
| if (cu_seqlens[1:] <= cu_seqlens[:-1]).any().item(): | ||
| raise ValueError(f"{name} must be strictly increasing") | ||
| if cu_seqlens[-1].item() != token_count: | ||
| raise ValueError(f"{name} does not match token count") | ||
|
|
||
|
|
||
| def _split_indices(cu_seqlens): | ||
| return cu_seqlens[1:-1].to(device="cpu", dtype=torch.long) | ||
|
|
||
|
|
||
| def var_attention_optimized_split(q, k, v, heads, cu_seqlens_q, cu_seqlens_k, *args, skip_reshape=False, skip_output_reshape=False, **kwargs): | ||
| q, k, v, head_dim = _var_attention_qkv(q, k, v, heads, skip_reshape) | ||
|
|
||
| _validate_split_cu_seqlens("cu_seqlens_q", cu_seqlens_q, q.shape[0]) | ||
| _validate_split_cu_seqlens("cu_seqlens_k", cu_seqlens_k, k.shape[0]) | ||
| if cu_seqlens_k[-1].item() != v.shape[0]: | ||
| raise ValueError("cu_seqlens_k does not match v token count") | ||
|
|
||
| q_split_indices = _split_indices(cu_seqlens_q) | ||
| k_split_indices = _split_indices(cu_seqlens_k) | ||
| q_splits = torch.tensor_split(q, q_split_indices, dim=0) | ||
| k_splits = torch.tensor_split(k, k_split_indices, dim=0) | ||
| v_splits = torch.tensor_split(v, k_split_indices, dim=0) | ||
| if len(q_splits) != len(k_splits) or len(q_splits) != len(v_splits): | ||
| raise ValueError("cu_seqlens_q and cu_seqlens_k must describe the same sequence count") | ||
|
|
||
| out = [] | ||
| for q_i, k_i, v_i in zip(q_splits, k_splits, v_splits): | ||
| q_i = q_i.permute(1, 0, 2).unsqueeze(0) | ||
| k_i = k_i.permute(1, 0, 2).unsqueeze(0) | ||
| v_i = v_i.permute(1, 0, 2).unsqueeze(0) | ||
| out_dtype = q_i.dtype | ||
| if _attention.optimized_attention is _attention.attention_sage and q_i.dtype not in (torch.float16, torch.bfloat16): | ||
| q_i = q_i.to(torch.bfloat16) | ||
| k_i = k_i.to(torch.bfloat16) | ||
| v_i = v_i.to(torch.bfloat16) | ||
| out_i = _attention.optimized_attention(q_i, k_i, v_i, heads, skip_reshape=True, skip_output_reshape=True) | ||
| if out_i.dtype != out_dtype: | ||
| out_i = out_i.to(out_dtype) | ||
| out.append(out_i.squeeze(0).permute(1, 0, 2)) | ||
|
|
||
| out = torch.cat(out, dim=0) | ||
| return _var_attention_output(out, heads, head_dim, skip_output_reshape) | ||
|
|
||
|
|
||
| optimized_var_attention = var_attention_optimized_split |
Oops, something went wrong.
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.
Guard the new frequency-shift parameter.
downscale_freq_shiftnow feeds the divisor directly. Values>= embedding_dim // 2make the frequency scale undefined or inverted, so this helper can return broken timestep embeddings instead of failing fast.Suggested guard
def get_timestep_embedding(timesteps, embedding_dim, flip_sin_to_cos=False, downscale_freq_shift=1): assert len(timesteps.shape) == 1 half_dim = embedding_dim // 2 + if half_dim > 0 and downscale_freq_shift >= half_dim: + raise ValueError("downscale_freq_shift must be smaller than embedding_dim // 2") emb = math.log(10000) / (half_dim - downscale_freq_shift)📝 Committable suggestion
🤖 Prompt for AI Agents