Mesh TensorFlow is a language for distributed deep learning, whose full name is Mesh TensorFlow - Model Parallelism Made Easier. It is open-sourced by TensorFlow officially on GitHub, and the repository address is https://github.com/tensorflow/mesh。根据官方资料,Mesh TensorFlow can specify a wide range of distributed tensor computation categories, and its purpose is to provide a formal description and implementation of the allocation strategy for computational graphs on hardware/processors, for example, "split the batch across the rows of processors, and split the hidden layer units across the columns of processors". It is presented as a layer on top of TensorFlow.
In terms of use, Mesh TensorFlow is mainly aimed at scenarios requiring more complex parallel computation. According to the official explanation, if only data-parallel training is needed (that is, batch splitting), then Mesh TensorFlow is not needed, although it can also do this. Common reasons for more complex parallel computation include: model parameters cannot fit into a single device, for example, a 5-billion-parameter language model; a single sample is too large, causing activations to not fit into a single device, for example, a large 3D image model (experimental/unet.py); and low-latency parallel inference when the batch size is 1.
In terms of main capabilities, Mesh TensorFlow adopts the concept of a "Mesh", that is, an n-dimensional array composed of processors, connected through a network. Each tensor is distributed across all processors in the Mesh (split and/or replicated). Tensor dimensions and Mesh dimensions both have names, and the layout of all tensors is determined by user-defined layout rules, which specify which tensor dimensions are split across which Mesh dimensions, ensuring that corresponding dimensions in different tensors are split in the same way. Layout does not affect the result, only performance. The implementation of operations involves parallel computation across all processors in the Mesh, and sometimes also collective communication.
In terms of usage, to install the latest stable version or the latest development version, the corresponding installation command can be run (the official documentation provides installation commands, but the specific command contents were not captured here). Installing mesh-tensorflow does not automatically install or update TensorFlow; it is recommended to install it via pip install tensorflow or pip install tensorflow-gpu. If using the development version of Mesh TensorFlow, it may be necessary to use TensorFlow's nightly package (tf-nightly). The official documentation provides an MNIST example (examples/mnist.py) to illustrate model definition and layout specification. The code is divided into two parts: the first part describes the mathematical operations, and the second part describes the devices and tensor/computation layout. By setting layout_rules, data parallel can be achieved.row, model parallelism, or a combination of both. For example, layout_rules=[("hidden", "all_processors")] can achieve model parallelism; on a two-dimensional Mesh, batch and hidden layer units can be split simultaneously.
Mesh TensorFlow uses the Einstein summation notation mtf.einsum(inputs, output_shape), using named dimensions as symbols. Matrix multiplication, broadcasting, sum reduction, and transposition can all be expressed as special cases of mtf.einsum. Network communication occurs when needed; for example, an einsum computation performs an allreduce when splitting the reduction dimension. The choice of layout affects performance and memory consumption, and the official auto_mtf subpackage is provided to automatically choose layouts.
Mesh TensorFlow supports CPU, GPU, and TPU. The TPU implementation is very different from the CPU/GPU implementation: multi-CPU/GPU Mesh uses PlacementMeshImpl, which emits separate TensorFlow operations placed on different devices, all within one large TensorFlow graph; TPU Mesh uses SimdMeshImpl, which emits TensorFlow operations and communication collectives from the perspective of a single core, relying on each core actually executing the same operations, using a "SIMD" approach to avoid the TensorFlow and XLA graphs growing with the number of cores.
Experimental features include an input pipeline for large-scale inputs (experimental/input_reader.py), a spatial partitioning example (experimental/unet.py), and a low-level API (experimental/model_executor.py). The official documentation states that the experimental code has not been tested on GPU and only runs on TPU. In addition, the official documentation provides instructions for running on cloud-tpu, including steps for running the Transformer model and a toy model, and cites the paper "Mesh-TensorFlow: Deep learning for supercomputers" (NeurIPS 2018).
In terms of limitations, some layout rules are illegal; for example, two tensor dimensions cannot be split onto the same Mesh dimension, and the size of a tensor dimension must be divisible by the size of the Mesh dimension. reshape may incursignificant overhead because it may lead to new tensor layouts and network communication. For the TPU implementation, all imported/exported tensors are currently required to be fully replicated, which is a workaround. Some code in the documentation has TODO markers, indicating that it remains to be verified.