Skip to content

Add regression test for Sequential sublayer in custom train_step#22273

Open
DeborahOlaboye wants to merge 3 commits intokeras-team:masterfrom
DeborahOlaboye:fix/sequential-sublayer-in-train-step-regression-test
Open

Add regression test for Sequential sublayer in custom train_step#22273
DeborahOlaboye wants to merge 3 commits intokeras-team:masterfrom
DeborahOlaboye:fix/sequential-sublayer-in-train-step-regression-test

Conversation

@DeborahOlaboye
Copy link

Description

Calling a Sequential model from a custom train_step during model.fit() previously caused:

ValueError: A KerasTensor cannot be used as input to a TensorFlow function.

This happened because _symbolic_build was tracing train_step with KerasTensor inputs.
When train_step passed those tensors to a Sequential sublayer (e.g., a data augmentation
pipeline), TF's internal conversion (__tf_tensor__) raised the error.

Changes

Adds a regression test to trainer_test.py to prevent this bug from silently returning
in future refactors:

  • SequentialSublayerInTrainStepModel — a model that holds a Sequential sublayer
    (Rescaling) called only inside train_step, not in call(), mirroring the
    exact pattern from the issue.
  • test_sequential_sublayer_in_custom_train_step — runs model.fit() and asserts
    no KerasTensor-related error is raised.

Testing

KERAS_BACKEND=torch pytest keras/src/trainers/trainer_test.py \
  -k test_sequential_sublayer_in_custom_train_step -xvs

Closes #18459     

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @DeborahOlaboye, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a regression test to address a previously identified bug where calling a Sequential sublayer from within a custom train_step in Keras would incorrectly receive KerasTensor inputs, leading to a ValueError. The new test ensures that this specific scenario now executes without error, safeguarding against future regressions.

Highlights

  • Regression Test Added: Introduced a new test case, test_sequential_sublayer_in_custom_train_step, to prevent a ValueError related to KerasTensor when a Sequential sublayer is used within a custom train_step during model.fit().

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • keras/src/trainers/trainer_test.py
    • Defined SequentialSublayerInTrainStepModel, a new model class designed to replicate the bug scenario by calling a Sequential sublayer exclusively within its train_step method.
    • Implemented test_sequential_sublayer_in_custom_train_step, a regression test that instantiates and trains SequentialSublayerInTrainStepModel to confirm the absence of the KerasTensor error.
    • Added a conditional skip for the JAX backend in the new test, as its train_step signature differs and is not affected by the original issue.
Activity
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@google-cla
Copy link

google-cla bot commented Feb 24, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request introduces a regression test for a specific issue where a Sequential sublayer within a custom train_step would incorrectly receive KerasTensors, leading to a ValueError. The changes include a new model class SequentialSublayerInTrainStepModel and a corresponding test method test_sequential_sublayer_in_custom_train_step to ensure this bug does not reappear. The implementation looks correct and directly addresses the reported issue by verifying that the model.fit() call completes without errors. The JAX backend is appropriately skipped due to its different train_step signature, which is a good consideration for backend compatibility.

Comment on lines +108 to +113
def train_step(self, data):
x, y = data[0], data[1]
# Before the fix for #18459, `x` was a KerasTensor here,
# causing an error when passed to the Sequential sublayer.
x = self.sublayer(x, training=True)
return super().train_step((x, y))
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The comment Before the fix for #18459, x was a KerasTensor here, causing an error when passed to the Sequential sublayer. is helpful for understanding the context of the fix. However, since the fix is already applied, this comment could be rephrased to describe the current behavior or removed if the code itself is self-explanatory. The goal of comments should be to explain why something is done, not just what was fixed, especially for regression tests. This aligns with the principle of minimizing cognitive load (Repository Style Guide, line 48) by keeping documentation concise and relevant to the current state.

Suggested change
def train_step(self, data):
x, y = data[0], data[1]
# Before the fix for #18459, `x` was a KerasTensor here,
# causing an error when passed to the Sequential sublayer.
x = self.sublayer(x, training=True)
return super().train_step((x, y))
def train_step(self, data):
x, y = data[0], data[1]
x = self.sublayer(x, training=True)
return super().train_step((x, y))

@codecov-commenter
Copy link

codecov-commenter commented Feb 24, 2026

Codecov Report

❌ Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.04%. Comparing base (0ddf962) to head (59cf91a).
⚠️ Report is 96 commits behind head on master.

Files with missing lines Patch % Lines
keras/src/trainers/trainer.py 66.66% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #22273       +/-   ##
===========================================
+ Coverage   71.44%   83.04%   +11.59%     
===========================================
  Files         594      596        +2     
  Lines       65029    66800     +1771     
  Branches    10174    10403      +229     
===========================================
+ Hits        46461    55475     +9014     
+ Misses      16105     8683     -7422     
- Partials     2463     2642      +179     
Flag Coverage Δ
keras 82.87% <77.77%> (+11.54%) ⬆️
keras-jax 60.51% <11.11%> (-0.93%) ⬇️
keras-numpy 54.75% <0.00%> (-0.88%) ⬇️
keras-openvino 50.04% <0.00%> (?)
keras-tensorflow 61.82% <77.77%> (?)
keras-torch 60.58% <44.44%> (-0.97%) ⬇️

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.

Adds a regression test to verify that calling a Sequential sublayer
from a custom train_step during model.fit() does not incorrectly pass
KerasTensors to the sublayer, which previously caused:
"A KerasTensor cannot be used as input to a TensorFlow function."

The test uses a model that holds a Sequential sublayer (Rescaling) that
is only called inside train_step (not in call()), mirroring the pattern
reported in the issue.
@DeborahOlaboye DeborahOlaboye force-pushed the fix/sequential-sublayer-in-train-step-regression-test branch from 381373b to 316d355 Compare February 24, 2026 13:06
Copy link
Collaborator

@hertschuh hertschuh left a comment

Choose a reason for hiding this comment

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

@DeborahOlaboye

I'm confused about this, it doesn't include a bug fix.

To be clear, I don't think the bug is just the result of using Sequential in a sublayer with a custom train_step. The root cause of the bug still needs to be determined.

@DeborahOlaboye
Copy link
Author

@DeborahOlaboye

I'm confused about this, it doesn't include a bug fix.

To be clear, I don't think the bug is just the result of using Sequential in a sublayer with a custom train_step. The root cause of the bug still needs to be determined.

@hertschuh, thanks for the review. You're right, the previous commit only added a regression test without a bug fix. I've now identified the root cause and added a fix.

Root cause: when a Sequential sublayer is only used inside a custom
`train_step` (not in `call()`), it is not built during `_symbolic_build`,
which only traces `call()`. For the TF backend without a distribute
strategy, `_maybe_symbolic_build()` previously returned early without
calling `_symbolic_build` at all. This caused the Sequential sublayer to
be built lazily during the first `tf.function` trace of `train_step`.
`Sequential.build()` creates KerasTensors and calls `compute_output_spec`,
which for TF creates a nested `FuncGraph` inside the already-active
`tf.function` context. When TF then tries to use a KerasTensor as a real
tensor, it hits `KerasTensor.__tf_tensor__` which raises:
  "A KerasTensor cannot be used as input to a TensorFlow function."

Fix (two parts):
1. `_symbolic_build` (trainer.py): after tracing `call()`, iterate over
   all sublayers and pre-build any that are still unbuilt by running
   `compute_output_spec` on the symbolic input `x`.
2. TF `_maybe_symbolic_build` (tensorflow/trainer.py): instead of always
   deferring when no distribute strategy is set, only defer when all
   layers are already built. If unbuilt sublayers exist, call
   `_symbolic_build` so they are built before `tf.function` traces
   `train_step`.

Also updates the regression test docstrings to accurately describe the
root cause (keras-teamgh-18459).
@DeborahOlaboye DeborahOlaboye force-pushed the fix/sequential-sublayer-in-train-step-regression-test branch from 62c9532 to 59cf91a Compare March 13, 2026 22:45
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