In @nodes.py , replace padtensor with torch.nn.functional.pad. --- Content from…
- User
In @nodes.py , replace
pad_tensorwithtorch.nn.functional.pad. --- Content from referenced files --- Content from @nodes.py: import functools from unittest.mock import patchimport torch from torch.nn import functional as F from comfy.ldm.modules.attention import optimized_attention, wrap_attn
from .attn_mask import MaskMap, RadialAttention
_initialized = False _original_functions = {} if not _initialized: _original_functions["orig_attention"] = optimized_attention _initialized = True
@functools.cache def get_radial_attn_func(video_token_num, num_frame, block_size, decay_factor): mask_map = MaskMap(video_token_num, num_frame)
code@torch.compiler.disable() @wrap_attn def radial_attn_func(q, k, v, heads, mask=None, attn_precision=None, skip_reshape=False, skip_output_reshape=False, **kwargs): if q.shape != k.shape: # This is cross attn. Fallback to the original attn. orig_attention = _original_functions["orig_attention"] return orig_attention(q, k, v, heads, mask=mask, attn_precision=attn_precision, skip_reshape=skip_reshape, skip_output_reshape=skip_output_reshape, **kwargs) # attn_precision is unused assert mask is None if skip_reshape: # (batch_size, num_heads, seq_len, head_dim) -> (batch_size * seq_len, num_heads, head_dim) b, _, orig_seq_len, head_dim = q.shape q, k, v = map(lambda t: t.permute(0, 2, 1, 3).reshape(-1, heads, head_dim), (q, k, v)) else: # (batch_size, seq_len, num_heads * head_dim) -> (batch_size * seq_len, num_heads, head_dim) b, orig_seq_len, head_dim = q.shape head_dim //= heads q, k, v = map(lambda t: t.view(-1, heads, head_dim), (q, k, v)) padded_len = b * video_token_num if q.shape[0] != padded_len: def pad_tensor(tensor): padding = torch.zeros(padded_len - tensor.shape[0], tensor.shape[1], tensor.shape[2], device=tensor.device, dtype=tensor.dtype) return torch.cat([tensor, padding], dim=0) q, k, v = map(pad_tensor, (q, k, v)) out = RadialAttention( q, k, v, mask_map=mask_map, sparsity_type="radial", block_size=block_size, decay_factor=decay_factor, model_type="wan", pre_defined_mask=None, use_sage_attention=True, ) out = out[: b * orig_seq_len, :, :] if skip_output_reshape: # (batch_size * seq_len, num_heads, head_dim) -> (batch_size, num_heads, seq_len, head_dim) out = out.reshape(b, orig_seq_len, heads, head_dim).permute(0, 2, 1, 3) else: # (batch_size * seq_len, num_heads, head_dim) -> (batch_size, seq_len, num_heads * head_dim) out = out.reshape(b, -1, heads * head_dim) return out return radial_attn_func Assistant
I will replace the local
pad_tensorfunction withtorch.nn.functional.pad(aliased asF.pad) innodes.py.Assistant
I have replaced the
pad_tensorfunction withtorch.nn.functional.padinnodes.py.Assistant
The
pad_tensorfunction has been successfully replaced withtorch.nn.functional.padinnodes.py.