google-deepmind/sonnet
TensorFlow 2 modules for custom neural networks and training loops
Automated SPDX check for reference only — not legal advice. Disclaimer
GitHub Star History
Full-history star trajectory from GitHub official history. Switch ranges or hover any point for exact counts.
Evidence-Backed Technical Brief
AI Deep ReviewArchitecture & 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.
snt.Module builds on tf.Module, enters module name scopes, and supports input-dependent initialization with @snt.once.snt.Sequential can mix Sonnet layers with TensorFlow callables such as tf.nn.relu; snt.nets.MLP supplies a ready-made network.variables includes non-trainable state, while trainable_variables gives the optimizer the trainable subset.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.snt.distribute.CrossReplicaBatchNorm provides an opt-in way to synchronize batch statistics.TensorFlow research teams building reusable network components that need direct control over training loops, distributed state updates, and SavedModel export signatures.
You need an integrated training workflow, a turnkey inference server, or an LLM-specific engine that handles serving and scheduling for you.
Quick Start & Installation
$ pip install tensorflow tensorflow-probability $ pip install dm-sonnet
Comparison vs. Top LLMs & Inference Alternatives
Full comparison →| Repository | Stars | 7d Trend | Stack | License | Core Positioning |
|---|---|---|---|---|---|
| sonnet ★ | 10k | +2 | Python | Apache-2.0 | TensorFlow 2 modules for custom neural networks and training loops |
| keras | 64.5k | +24 | Python | Apache-2.0 | Multi-backend Python SDK for building, training, and running deep learning model |
| annotated_deep_learning_paper_implementations | 67.8k | +43 | Python | MIT | Annotated PyTorch references for understanding and adapting deep learning algori |
| einops | 9.6k | +5 | Python | MIT | A 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.
curl -sL https://repoforai.com/repo/google-deepmind/sonnet.md
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?
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?
Can we use Sonnet commercially and self-host exported models?
When should I choose Sonnet over `tf.keras`?
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.
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