Skip to content

Ensures mask kwarg is used for output mask propagation #21449

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

Merged
merged 1 commit into from
Jul 3, 2025

Conversation

JyotinderSingh
Copy link
Collaborator

This fix addresses a bug in the Layer class where an explicitly passed mask keyword argument was ignored during the computation of the output mask for subsequent layers.

The Problem

Currently, when a layer is called with an explicit mask (e.g., layer(inputs, mask=my_mask)), the framework correctly uses this mask for the layer's internal call() computation. However, when preparing to propagate a mask to the next layer, the framework incorrectly ignores this explicit mask. Instead, it only checks for a _keras_mask attribute attached directly to the input tensor.

If the input tensor has no such attribute, the output mask is computed as None, effectively breaking the mask propagation chain.

Solution

The fix modifies how the previous_mask variable is determined within the Layer.__call__ method. The logic has been updated to prioritize the explicitly passed mask keyword argument.

The new order of operations is:

  1. Check if a mask was provided in kwargs. If yes, use it as previous_mask.
  2. If no explicit mask is found, fall back to checking for a _keras_mask attribute on the first input tensor.

This ensures that the mask actually used by the layer is the same one used to compute the mask for its output, restoring correct and consistent mask propagation behavior.

@codecov-commenter
Copy link

codecov-commenter commented Jul 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.71%. Comparing base (ff17868) to head (d827a25).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #21449   +/-   ##
=======================================
  Coverage   82.71%   82.71%           
=======================================
  Files         565      565           
  Lines       55255    55257    +2     
  Branches     8614     8615    +1     
=======================================
+ Hits        45703    45705    +2     
  Misses       7443     7443           
  Partials     2109     2109           
Flag Coverage Δ
keras 82.52% <100.00%> (+<0.01%) ⬆️
keras-jax 63.34% <100.00%> (+<0.01%) ⬆️
keras-numpy 58.52% <100.00%> (+<0.01%) ⬆️
keras-openvino 33.78% <100.00%> (+<0.01%) ⬆️
keras-tensorflow 63.75% <100.00%> (+<0.01%) ⬆️
keras-torch 63.36% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

@JyotinderSingh JyotinderSingh changed the title Ensures mask keyword argument is used for output mask propagation Ensures mask kwarg is used for output mask propagation Jul 3, 2025
@JyotinderSingh JyotinderSingh requested a review from hertschuh July 3, 2025 09:03
Copy link
Collaborator

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the fix!

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Jul 3, 2025
@fchollet fchollet merged commit b855c42 into keras-team:master Jul 3, 2025
7 checks passed
@google-ml-butler google-ml-butler bot removed awaiting review ready to pull Ready to be merged into the codebase kokoro:force-run labels Jul 3, 2025
@JyotinderSingh JyotinderSingh deleted the mask_propagation branch July 4, 2025 01:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants