[kernel] Extract VSA utility functions into fastvideo_kernel - #1408
Conversation
Merge Protections🔴 1 of 1 protections blocking · waiting on 👀 reviews and 🤖 CI
🔴 PR merge requirementsWaiting for
This rule is failing.
|
There was a problem hiding this comment.
Code Review
This pull request introduces standalone VSA metadata utilities in vsa_utils.py along with comprehensive unit tests in test_vsa_utils.py and exports them in the package initialization. A critical issue was identified in vsa_utils.py where @functools.lru_cache is applied to get_non_pad_index. Since this function accepts a torch.Tensor which is unhashable, this will raise a TypeError at runtime and must be removed.
| @functools.lru_cache(maxsize=10) | ||
| def get_non_pad_index( | ||
| variable_block_sizes: torch.LongTensor, | ||
| max_block_size: int, | ||
| ) -> torch.LongTensor: |
There was a problem hiding this comment.
Applying @functools.lru_cache to get_non_pad_index will raise a TypeError: unhashable type: 'Tensor' at runtime because variable_block_sizes is a torch.Tensor, which is mutable and not hashable in PyTorch.
Since get_non_pad_index is a lightweight operation and is only called once per metadata construction, caching it is unnecessary and causes immediate failures. Please remove the @functools.lru_cache decorator from this function.
def get_non_pad_index(
variable_block_sizes: torch.LongTensor,
max_block_size: int,
) -> torch.LongTensor:
alexzms
left a comment
There was a problem hiding this comment.
Quick question on the dict keys returned by build_vsa_metadata() in fastvideo-kernel/python/fastvideo_kernel/vsa_utils.py:
- The dict uses
tile_indicesandreverse_tile_indices. - The framework
VideoSparseAttentionMetadatadataclass infastvideo/attention/backends/video_sparse_attn.py(lines 138–139) uses the longer namestile_partition_indicesandreverse_tile_partition_indices.
Since the follow-up PR will swap the framework over to importing from here, was there a reason to shorten the names? If not, aligning them now (using tile_partition_indices / reverse_tile_partition_indices in the returned dict too) would let the follow-up be a clean import ... as ... swap rather than touching every call site that destructures the metadata.
Happy to be wrong if you meant this as a cleaner external-facing API — just want to flag it before merge.
|
Good catch! You're right — aligning the dict keys now avoids unnecessary churn in the follow-up PR. Updated Tests updated and passing. |
|
This PR has merge conflicts with the base branch. Please rebase: git fetch origin main
git rebase origin/main
# Resolve any conflicts, then:
git push --force-with-lease |
Move tile-partition index helpers and variable-block-size computations from the fastvideo framework layer into the standalone kernel package, enabling external users to call VSA without depending on the full fastvideo framework. New module: fastvideo_kernel/vsa_utils.py - get_tile_partition_indices - get_reverse_tile_partition_indices - construct_variable_block_sizes - get_non_pad_index - build_vsa_metadata (convenience one-call entry point) Closes hao-ai-lab#782
- Rename build_vsa_metadata() keys to tile_partition_indices / reverse_tile_partition_indices to match VideoSparseAttentionMetadata - Remove @lru_cache from get_non_pad_index (torch.Tensor is unhashable) - Update tests accordingly
330d206 to
381c87e
Compare
Summary
fastvideo/attention/backends/video_sparse_attn.pyinto standalonefastvideo_kernel/vsa_utils.pybuild_vsa_metadata()convenience entry point for one-call metadata constructionpip install fastvideo-kernelis now self-contained)Motivation
The VSA kernel lives in
fastvideo_kernel(independently installable), but calling it requires helper functions buried in thefastvideoframework layer. This forces external users to install the entire framework just for metadata construction. See #782.Changes
fastvideo_kernel/vsa_utils.pyVSA_TILE_SIZEconstant (~147 lines)fastvideo_kernel/__init__.pytests/test_vsa_utils.pyDesign decisions
tile_size:construct_variable_block_sizesacceptstile_sizeas argument instead of hardcodingVSA_TILE_SIZE, enabling non-default tile configurationslru_cacheon pure functions: index computations are deterministic for a given shape — caching avoids redundant recomputationfastvideois not installed)Test plan
pytest fastvideo-kernel/tests/test_vsa_utils.py -v— 20 tests, all pass (CPU-only)Closes #782