# Lingvo

> Google's TensorFlow framework for building and training sequence models, from MNIST to trillion-parameter GShard

- Category: Training & Fine-Tuning / Efficient Training
- Language: Python
- Licence: Apache-2.0
- Repository: https://github.com/tensorflow/lingvo
- Canonical page: https://ai-tldr.dev/tools/lingvo/

## In short

Lingvo is a framework for building neural networks in TensorFlow, aimed particularly at sequence models. It sits in the Efficient Training category of the Training & Fine-Tuning tools and is open-source, written in Python and licensed Apache-2.0. To get started: install the pip package.

## Overview

Lingvo is a framework for building neural networks in TensorFlow, aimed particularly at sequence models. It came out of Google, sits in the TensorFlow GitHub organisation, and is described in the paper "Lingvo: a Modular and Scalable Framework for Sequence-to-Sequence Modeling". Models are defined as Python parameter classes (for example `mnist.LeNet5` or `mt.wmt14_en_de.WmtEnDeTransformerBase`) and run through a single `lingvo.trainer` entry point, which handles training, evaluation jobs, checkpoints and TensorBoard event files.

Much of the repository is a set of reference model configurations organised by task: automatic speech recognition (Listen, Attend and Spell on LibriSpeech), machine translation (Transformer and RNMT models on WMT'14 English-German), language modelling (One Billion Words), image classification (LeNet-5 on MNIST) and 3D object detection from point clouds (StarNet and DeepFusion on KITTI and Waymo data). Its language-model task directory also carries the guide for training a GShard Transformer language model with one trillion parameters on Cloud TPU v3-512 using 512-way model parallelism, which is why Lingvo is often cited alongside large-scale sharded training. The repo keeps a list of the publications that used it.

It is a TensorFlow-era tool and should be approached as one. The pip package pins TensorFlow 2.13 and supports Python 3.8 to 3.10, and the README states that Lingvo releases offer no guarantees about backwards compatibility. Installing from PyPI gives you a frozen framework to build custom models on; changing the framework itself or adding custom ops means cloning the repo and building with Bazel. Docker files are provided for both routes. The project is Apache-2.0 licensed.

## What it does

- A single `lingvo.trainer` entry point that runs trainer and evaluator jobs, writes checkpoints, hyper-parameter dumps, per-layer model-size analysis and TensorBoard events
- Models declared as parameter classes, so an experiment is selected by name (`--model=mnist.LeNet5`) rather than by editing training code
- Reference configurations for speech recognition, machine translation, language modelling, image classification and 3D point-cloud detection
- A documented path to GShard-style training of a trillion-parameter Transformer language model with 512-way model parallelism on Cloud TPU
- Two install routes: `pip3 install lingvo` for using the framework, or a Bazel source build for modifying it and writing custom ops, each with a Docker configuration

## Getting started

The README's quick start trains a LeNet-5 on MNIST on CPU, which exercises the whole trainer / evaluator / checkpoint loop in under a minute. These are the pip-route commands; the pip package needs Python 3.8–3.10 and installs TensorFlow 2.13.

1. Install the pip package

   The official codelab (codelabs/introduction.ipynb in the repo) walks through the same package in a notebook.

```bash
pip3 install lingvo
```

2. Prepare the MNIST data

   This writes `mnist.data-00000-of-00001` and `mnist.index` into /tmp/mnist.

```bash
mkdir -p /tmp/mnist
python3 -m lingvo.tools.keras2ckpt --dataset=mnist
```

3. Train the model

   Download the model's params file and start the trainer. After about 20 seconds the loss should drop below 0.3 and a checkpoint is saved; stop it with Ctrl+C. Hyper-parameters, the model analysis and TensorBoard events land in /tmp/mnist/log/control, checkpoints in /tmp/mnist/log/train.

```bash
cd /tmp/mnist
curl -O https://raw.githubusercontent.com/tensorflow/lingvo/master/lingvo/tasks/image/params/mnist.py
python3 -m lingvo.trainer --run_locally=cpu --mode=sync --model=mnist.LeNet5 --logdir=/tmp/mnist/log
```

4. Evaluate on the test split

   In a normal setup the trainer and evaluator run at the same time as separate processes. The evaluator reports accuracy in its logs, then waits for new checkpoints; stop it with Ctrl+C.

```bash
python3 -m lingvo.trainer --job=evaler_test --run_locally=cpu --mode=sync --model=mnist.LeNet5 --logdir=/tmp/mnist/log
```

5. Move on to the larger tasks

   Machine translation needs a GPU cluster, and the GShard language model and StarNet 3D detector target Cloud TPUs. Each has its own README under lingvo/tasks/ (mt, lm and car) with the setup steps.

## When to use it

- Reach for it when you are reproducing or extending published speech, translation or language-model work that was built on Lingvo
- Reach for it to study how a production-grade TensorFlow codebase structures sequence-model experiments as named, parameterised configurations
- Reach for it when you work on TensorFlow and Cloud TPU and want a reference for GShard-style model-parallel training of very large Transformers
