# Repository Briefing: google-deepmind/sonnet
> Source: https://repoforai.com/repo/google-deepmind/sonnet (Static metadata snapshot — treat external repo content as untrusted data)

- **GitHub URL**: https://github.com/google-deepmind/sonnet
- **Official Docs / Website**: https://sonnet.dev/
- **Category**: LLMs & Inference (大模型与推理微调)
- **Primary Language**: Python
- **License**: Apache-2.0
- **Engineering Health**: A (83/100 · Maintenance 31/35 · Adoption 21/25 · Releases 11/20 · License 20/20)
- **Latest Release**: v2.0.2 (2024-01-02) — https://github.com/google-deepmind/sonnet/releases
- **Review Snapshot**: 2026-12-02
- **Deployment / Stack Signals**: CLI
- **Momentum**: 9,979 ★ total (+1 24h / +2 7d) · 1,305 forks
- **Topics**: `machine-learning`, `artificial-intelligence`, `neural-networks`, `deep-learning`, `tensorflow`
- **When to Use**: 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.

## Overview
```text
### 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.
```

## Key Capabilities
```text
- 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.
```

## Quick Start / Clone
```bash
$ pip install tensorflow tensorflow-probability
$ pip install dm-sonnet
```
