[EPIC] pipekit-train: the Keras 3 backend

Open
#317 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
keras, python
Domain
backend

Research direction

Start with packages/pipekit-train/docs/design/keras_adapter.md and decisions D18–D21 in packages/pipekit-train/docs/design/decisions.md; the issue lists six child tasks, KB-1 through KB-6, to land in order. The central entry point is pipekit_train.backends.keras.run, currently described as raising NotImplementedError. Done means all children and their PRs are merged, CI trains both a KerasModelOp and a JaxModelOp with the Keras backend, and the shipped design names are marked today.

Written by the indexing model from the issue text.

Description

area:code priority:p2 type:epic-theme

Theme

TrainingLoop(..., backend="keras") trains a Keras 3 model, or an Equinox model, through Keras's own compile and fit on the JAX backend, and returns an operator whose weights round-trip through a ModelRegistry. Today pipekit_train.backends.keras.run raises NotImplementedError.

Parent Wave

None. This is the Keras half of #147 (stateful layers and the Keras backend); the stateful-layer half is #16.

Motivation

The Lightning backend was dropped (D15), so Keras is the one route into TrainingLoop for a model not written in Equinox. Models that already exist in Keras, such as keras-fairkl's FairModelWrapper (a fairness penalty added with add_loss), train today in scripts outside pipekit and lose what the loop gives an Equinox model: a dataset content hash, a step budget, EarlyStopping on validation metrics, checkpoints that resume, HyperSweep ranking, a registry entry and an operator that drops into a pipeline. Equinox users also want Keras tooling (Keras Tuner, Keras preprocessing layers) around their modules, and a benchmark wants one Equinox model trained under both backends.

The full design is packages/pipekit-train/docs/design/keras_adapter.md, with decisions D18 to D21 in packages/pipekit-train/docs/design/decisions.md. In short:

  • D18. Wrap compile and fit. One Keras epoch is one evaluation period P = eval_every_n_steps; fit runs E = ⌈K / P⌉ epochs and a bridge callback stops it at step K = max_steps. The bridge drives the pipekit callbacks and the metric writer, and resets Keras's metrics after each step so the logged loss is the step's loss, not the epoch's running mean.
  • D19. JAX backend only. The backend sets KERAS_BACKEND=jax before the first import keras and raises if Keras was imported on another backend. Without this, import keras fails on a machine without TensorFlow, because Keras's default backend is TensorFlow.
  • D20. KerasModelOp in pipekit_train.backends.keras, with a weight blob of a (path, shape, dtype) header and the raw arrays. A .keras file is not byte-stable (its metadata records the save time), so it cannot be what the registry hashes.
  • D21. An operator that is or holds an Equinox module (JaxOperator, JaxModelOp, EquinoxModelOp) trains as an EquinoxLayer, a keras.layers.JaxLayer over copies of its array leaves, and comes back in the wrapper it was given.

Facts measured on Keras 3.15.1, JAX 0.12.1 and Equinox 0.13.8 that the children rely on:

Check Result
fit with P = 100, K = 250 and a stop at 250 250 optimiser steps; the batch generator is read 250 times; validation at 100, 200 and 250
logs["loss"] in on_train_batch_end the running mean since the epoch began; with reset_metrics() after each step, equal to train_on_batch
Validation on 70 samples in batches of 32 Keras's loss is the exact per-sample mean; a metric returning an averaged scalar is the mean of batch means (3.2899 vs 3.2349)
Resume after 200 of 300 steps from weights, optimiser variables and the Grain iterator state weights equal to the uninterrupted run (difference 0.0)
An Equinox MLP in a JaxLayer one SGD step equals hand-written JAX to 3e-8; Keras deletes the arrays it was given unless they are copied
optax against Keras optimisers, 200 steps SGD identical; Adam and AdamW with matched settings within 1.3e-6; Keras's default epsilon=1e-7 differs from optax's 1e-8

Issues

  • KB-1 — one Grain batch pipeline for every backend
  • KB-2 — KerasModelOp with a weight blob (D20)
  • KB-3 — the Keras backend's run (D18, D19)
  • KB-4 — Equinox models under the Keras backend (D21)
  • KB-5 — Keras checkpoints that resume
  • KB-6 — docs for the Keras backend

Execution Notes

The children land in order, one PR each: KB-1, KB-2, KB-3, KB-4, KB-5, KB-6. KB-3 needs KB-1's shared batches and KB-2's operator; KB-4 and KB-5 extend KB-3's run; KB-6 documents what exists. None depends on #16, which touches backends/equinox/_state.py, not _batches.py.

CI must run the Keras tests: the train-extras dependency group in the root pyproject.toml gains keras, and the backend sets KERAS_BACKEND=jax itself, so CI needs no environment variable.

Parallelism

  • Can run in parallel with: #16
  • Blocked by (inside this wave): none
  • Must complete before: none

Definition of Done

  • All child issues closed, their PRs merged
  • TrainingLoop(..., backend="keras") trains a KerasModelOp and a JaxModelOp in CI
  • The design page's proposed markers for shipped names become today

Relationships

  • Parent: #147
  • Related: #16, #231
Dominant language
Python
Stars
0
Forks
0
Avg merge
3h 50m
Merged PRs (30d)
143

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from jejjohnson/pipekit

All issues in jejjohnson/pipekit

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.