Skip to content

Support Keras multi-backend in tinyAES tutorial#311

Merged
kralka merged 4 commits intogoogle:mainfrom
wsxrdv:multibackend
Feb 20, 2026
Merged

Support Keras multi-backend in tinyAES tutorial#311
kralka merged 4 commits intogoogle:mainfrom
wsxrdv:multibackend

Conversation

@wsxrdv
Copy link
Collaborator

@wsxrdv wsxrdv commented Feb 20, 2026

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @wsxrdv, 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 enhances the tinyAES tutorial by introducing support for multiple Keras backends. It adapts the data loading and preprocessing mechanisms to seamlessly integrate with both TensorFlow and JAX, ensuring the tutorial remains functional and relevant across different Keras configurations. The changes allow the tutorial to dynamically select the appropriate data pipeline based on the detected backend.

Highlights

  • Keras Multi-Backend Support: Implemented conditional data loading and processing based on the active Keras backend (TensorFlow or JAX) within the tinyAES tutorial.
  • New Batch Processing Function: Added a process_batch function to handle batch-wise data preparation, specifically for the JAX backend, converting raw traces and sub-byte inputs into the format expected by the neural network.
  • Dataset Adaptation: Modified the training and validation dataset creation to use as_tfdataset with process_record for TensorFlow and as_numpy_iterator_rust_batched with process_batch for JAX.

🧠 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
  • docs/tutorials/sca/tiny_aes.py
    • Added a new function process_batch for handling batch-wise data processing.
    • Modified the train function to conditionally create training and validation datasets based on the Keras backend (TensorFlow or JAX).
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.

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 support for Keras multi-backend in the tinyAES tutorial, specifically adding a process_batch function and modifying the train function to use a match statement for different Keras backends (TensorFlow and JAX). This change improves the flexibility and compatibility of the tutorial with various Keras configurations. The code is generally well-structured and follows good practices.

Comment on lines +200 to +214
def process_batch(batch: dict[str, Any]) -> tuple[Any, dict[str, Any]]:
"""Processing of a batch of records. The input is a dictionary of string
and tensor, the output of this function is a tuple the neural network's
input (trace) and a dictionary of one-hot encoded expected outputs.
"""
# The first neural network was using just the first half of the trace:
inputs = batch["trace1"]
outputs = {
"sub_bytes_in_0":
keras.ops.one_hot(
batch["sub_bytes_in"][:, 0],
num_classes=256,
),
}
return (inputs, outputs)
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 process_batch function is introduced to handle batched processing for JAX backend. It correctly extracts inputs and one-hot encodes the sub_bytes_in for the first byte. This is a good addition for multi-backend support.

Comment on lines +277 to +302
match keras.backend.backend():
case "tensorflow":
train_ds = dataset.as_tfdataset(
split="train",
process_record=process_record,
batch_size=batch_size,
)
validation_ds = dataset.as_tfdataset(
split="test",
process_record=process_record,
batch_size=batch_size,
)
case "jax":
train_ds = dataset.as_numpy_iterator_rust_batched(
split="train",
process_batch=process_batch,
batch_size=batch_size,
)
validation_ds = dataset.as_numpy_iterator_rust_batched(
split="test",
process_batch=process_batch,
batch_size=batch_size,
)
case _:
print(f"TODO support {keras.backend.backend() = }")
return
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 match statement for keras.backend.backend() is a clean way to handle different backends. The implementation for 'tensorflow' and 'jax' correctly uses as_tfdataset and as_numpy_iterator_rust_batched respectively. The TODO for unsupported backends is also appropriate.

@kralka kralka requested a review from jmichelp February 20, 2026 16:02
@kralka kralka enabled auto-merge February 20, 2026 16:03
@kralka kralka added this pull request to the merge queue Feb 20, 2026
Merged via the queue into google:main with commit 3a5bca4 Feb 20, 2026
51 checks passed
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