Skip to content

Add random block wise mask from Pixio#1919

Open
gabrielfruet wants to merge 6 commits into
lightly-ai:masterfrom
gabrielfruet:feature/random-block-wise-mask
Open

Add random block wise mask from Pixio#1919
gabrielfruet wants to merge 6 commits into
lightly-ai:masterfrom
gabrielfruet:feature/random-block-wise-mask

Conversation

@gabrielfruet

Copy link
Copy Markdown
Contributor

#1894

This is part of a series of PR's to implement the Pixio paper.

  • Tests added
  • Documentation referencing the author
  • Using same API as the other masking functions

The only difference is the num_prefix_tokens vs mask_class_token. Since Pixio relies on register tokens, the mask_class_token was not suitable for choosing whether to mask or not the REG/CLS tokens.

…n masking

Based on Pixio's MAE implementation.

Masks patches in coherent blocks rather than individually for spatial
coherence in image patch masking.
Tests cover:
- Basic functionality and index coverage
- Class token preservation
- Block alignment verification
- Various mask ratios and block sizes
- Device handling (CPU/CUDA)
- Error cases (non-square, invalid block size)
- Determinism and batch consistency
@codecov

codecov Bot commented May 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.43590% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.25%. Comparing base (c792812) to head (8b10f9d).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
lightly/models/utils.py 97.43% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1919      +/-   ##
==========================================
+ Coverage   86.19%   86.25%   +0.06%     
==========================================
  Files         169      169              
  Lines        7071     7110      +39     
==========================================
+ Hits         6095     6133      +38     
- Misses        976      977       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new token-index masking utility to support Pixio-style block-wise masking (masking contiguous patch blocks instead of individual tokens), along with unit tests to validate expected behavior and prefix-token handling (CLS/REG tokens).

Changes:

  • Introduce random_block_wise_mask(...) in lightly/models/utils.py to generate keep/mask indices using block granularity and num_prefix_tokens.
  • Add a dedicated test suite in tests/models/test_ModelUtils.py covering prefix tokens, block sizes, mask ratios, determinism, and device placement.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
lightly/models/utils.py Adds the new block-wise masking function returning (idx_keep, idx_mask) indices.
tests/models/test_ModelUtils.py Adds unit tests for random_block_wise_mask behavior, error cases, determinism, and device handling.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lightly/models/utils.py Outdated
Comment thread lightly/models/utils.py

if num_patches <= 0:
raise ValueError(
"Sequence length must be greater than prefix tokens (1 if not masking class token)."
Comment thread lightly/models/utils.py
Comment on lines +689 to +691
if num_prefix_tokens > 0:
noise[:, 0] = -1

Comment thread lightly/models/utils.py
.expand(batch_size, -1)
)
idx_keep = torch.cat([prefix_indices, patch_indices], dim=1)
idx_keep = torch.sort(torch.unique(idx_keep, dim=1), dim=1)[0]
Comment on lines +1183 to +1184
assert idx_keep.shape[1] == idx_keep[0].shape[0]
assert idx_mask.shape[1] == idx_mask[0].shape[0]
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@lorinczszabolcs lorinczszabolcs mentioned this pull request Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants