← Back to Leaderboard
Sgoogle-deepmind

google-deepmind/sonnet

TensorFlow 2 modules for custom neural networks and training loops

AI FrameworkQuickstart ↓
Engineering Health (Methodology)
A · 83/100
Maintenance Activity
● Updated 1mo ago
Latest Release
v2.0.2
Commercial License Check
Apache-2.0 · Commercial Friendly

Automated SPDX check for reference only — not legal advice. Disclaimer

Maintenance31/35
Fork Adoption21/25
Release Cadence11/20
License Clarity20/20
CLI1 README badge views (30d)
Total Stars
★ 9,979
24h Growth
+1
7d Growth
+2
Forks
1,305

GitHub Star History

Full-history star trajectory from GitHub official history. Switch ranges or hover any point for exact counts.

google-deepmind/sonnet Star History (2020-01-12 - 2026-12-03) ALL-TIME STAR HISTORY (2020-01 – 2026-12) +2.4k in 2517d · avg +1/day 10.1k 8.7k 7.3k 2020-01-12: ★ 7,6122020-06-13: ★ 7,9172020-11-14: ★ 8,1602021-04-17: ★ 8,4342021-09-18: ★ 8,6222022-02-19: ★ 8,8472022-07-23: ★ 9,0262022-12-24: ★ 9,1822023-05-27: ★ 9,3132023-10-28: ★ 9,3932024-03-30: ★ 9,5032024-08-31: ★ 9,5942025-02-01: ★ 9,6882025-07-05: ★ 9,7682025-12-06: ★ 9,8292026-05-09: ★ 9,8792026-12-03: ★ 9,979 ★ 10k 2020-01 2023-06 2026-12

Evidence-Backed Technical Brief

AI Deep Review

Architecture & Core Mechanics

Sonnet 2 is a Python neural-network library layered on TensorFlow 2, not a separate execution runtime. Its core abstraction, snt.Module, builds on tf.Module: a module holds variables and child modules, enters its own name scope when methods run, and uses ordinary TensorFlow operations for the forward pass. Most built-in modules create weights on the first call, when the input shape is known; custom modules can use @snt.once to follow that pattern.

The extension point is a Python subclass or callable rather than a plugin protocol. snt.Sequential passes each result to the next callable, so it can mix Sonnet layers and TensorFlow functions. The implementation lives under sonnet/, with examples under examples/. The base manifest lists absl-py, numpy, dm-tree, wrapt, and tabulate; TensorFlow executes the model and provides checkpointing and graph export.

Key Capabilities & Differentiators

The built-in components include snt.Linear, snt.Conv2D, snt.BatchNorm, and snt.nets.MLP. variables exposes both parameters and non-trainable state, such as batch-normalization statistics, while trainable_variables gives a custom optimizer step the trainable subset. tf.train.Checkpoint saves and restores tracked state; a restore can take effect when lazily initialized variables are created.

For inference handoff, initialize the model, then wrap the desired method in a tf.function with an explicit tf.TensorSpec. Attach the function and its variables to an export module and save it with tf.saved_model.save. The reloaded artifact exposes the exported methods and attributes rather than recreating the original Python subclass. This is a way to package TensorFlow inference, not an LLM-specific serving engine; request handling and scheduling remain outside Sonnet.

Trade-offs & Limitations

Compared with the integrated tf.keras training workflow, Sonnet gives research code more direct control but supplies no training framework. The application owns the training loop and evaluation logic. Under distribution strategies, Sonnet modules and optimizers do not automatically average gradients or synchronize batch-normalization statistics. The loop must choose its reduction semantics, or the model can opt into a distribution-aware layer such as snt.distribute.CrossReplicaBatchNorm for normalization.

This is useful when reduction or state updates are part of the experiment. It is a liability when a team expects single-device code to preserve its training semantics on multiple devices without extra work. Prefer checkpoints for resuming tracked training state and SavedModel for the inference interface. Built-in modules are tested for same-process pickle round trips, but that is not a sound basis for a portable release artifact.

Hardware & Deployment Guide

Install the dm-sonnet package and import it as sonnet. The installation sequence also installs tensorflow and tensorflow-probability; verify the imports and pin the combination tested for the intended environment rather than treating the unpinned commands as a compatibility matrix. Sonnet runs inside the host Python/TensorFlow process. The examples show GPU or TPU notebooks and multi-GPU CIFAR-10 training, but hardware should be sized for the actual model, batch size, dtype, and concurrent workload.

A self-hosted inference path can load the exported SavedModel in a compatible TensorFlow runtime without the original Sonnet model source; building and running the serving process is a separate responsibility. v2.0.0 was the TensorFlow 2 rewrite, v2.0.1 introduced a functional API inspired by JAX and Haiku, and v2.0.2 (2024-01-02) fixed use of a removed internal TensorFlow symbol. That fix is a reason to smoke-test initialization, checkpoint restoration, and SavedModel reload before upgrading TensorFlow.

Module lifecycle: snt.Module builds on tf.Module, enters module name scopes, and supports input-dependent initialization with @snt.once.
Composition: snt.Sequential can mix Sonnet layers with TensorFlow callables such as tf.nn.relu; snt.nets.MLP supplies a ready-made network.
State management: variables includes non-trainable state, while trainable_variables gives the optimizer the trainable subset.
Serialization: tf.train.Checkpoint preserves training state that is tracked by the checkpoint, while tf.saved_model.save exports traced methods and the variables they need.
Distributed training: Gradient reduction is explicit, and snt.distribute.CrossReplicaBatchNorm provides an opt-in way to synchronize batch statistics.
Release history: v2.0.1 added a JAX/Haiku-inspired functional API; v2.0.2 fixed a dependency on a removed TensorFlow internal symbol.
Best For

TensorFlow research teams building reusable network components that need direct control over training loops, distributed state updates, and SavedModel export signatures.

Skip If

You need an integrated training workflow, a turnkey inference server, or an LLM-specific engine that handles serving and scheduling for you.

Reviewed on 2026-12-02 · Grounded in GitHub README, root file tree & releasesMethodology

Quick Start & Installation

CLI
$ pip install tensorflow tensorflow-probability
$ pip install dm-sonnet

Comparison vs. Top LLMs & Inference Alternatives

Full comparison →
RepositoryStars7d TrendStackLicenseCore Positioning
sonnet ★10k+2PythonApache-2.0TensorFlow 2 modules for custom neural networks and training loops
keras64.5k+24PythonApache-2.0Multi-backend Python SDK for building, training, and running deep learning model
annotated_deep_learning_paper_implementations67.8k+43PythonMITAnnotated PyTorch references for understanding and adapting deep learning algori
einops9.6k+5PythonMITA backend-agnostic Python library for explicit tensor shape transformations

AI Context Briefing (for Claude / Cursor / Windsurf)

Standardized architecture summary & quickstart commands ready to paste into coding agents.

Raw .md ↗
curl -sL https://repoforai.com/repo/google-deepmind/sonnet.md

Embed Star History Chart & Badges in README.md

Full-history Star History SVGs and velocity cards on GitHub Camo: drop-in replacement for OSSInsight and Star History widgets.

google-deepmind/sonnet Star History on RepoForAI

Active README embeds unlock Hot-Tier 6h Telemetry Refresh automatically (zero pay-to-rank).

Frequently Asked Questions about sonnet

How do I integrate a custom layer into an existing TensorFlow training loop?

Subclass snt.Module and implement the forward computation with TensorFlow operations. If parameters depend on the input shape, use @snt.once to initialize them on the first call. The training loop can then give the optimizer trainable_variables while keeping non-trainable state, such as batch-normalization statistics, in variables.

What hardware and VRAM should I budget for Sonnet?

There is no fixed VRAM requirement for the library. Size the TensorFlow workload using the actual model, input shapes, batch size, dtype, and whether the workload is training or inference. For training, account for activations, gradients, and optimizer state; for serving, measure peak memory at the intended concurrency. The examples use GPU or TPU notebook environments, but they do not establish a minimum hardware specification.

Can we use Sonnet commercially and self-host exported models?

Yes. Apache-2.0 permits commercial use and self-hosting; if you redistribute Sonnet or a derivative, follow the license and applicable notice requirements and mark modified files as required. Review TensorFlow, other dependencies, model weights, and datasets under their own terms. Exporting a model as SavedModel does not replace that review.

When should I choose Sonnet over `tf.keras`?

Use Sonnet when you want explicit control over training-loop semantics, including gradient reduction and normalization state across replicas. Prefer the integrated tf.keras workflow when standard training and distribution behavior already fits the project. The decision is about who owns those semantics, not a demonstrated performance advantage.

Upstream README & Documentation

This profile focuses on independent engineering analysis and live telemetry. Read the full upstream README, changelog, and examples directly on GitHub.

Maintaining google-deepmind/sonnet? Pin to Homepage + competitor /alternatives pages with a custom Cloud/Demo CTA.Feature google-deepmind/sonnet on Homepage & Alternatives ($19)
WEEKLY BREAKOUT DIGEST

Top 10 Breakout Open-Source AI Repos, Every Thursday

Signal-only digest of fast-growing AI repos, SPDX license alerts, and architecture picks. Zero spam.

Zero spam · 1-click unsubscribe · Privacy Policy

Saved in your browser · Export Markdown RFC tables or jump into /vs compare