- Wed 20 December 2023
- Neuron
AWS Neuron SDK, an Overview and Glossary
AWS Neuron is the software development kit (SDK) for running deep learning and generative AI workloads on Amazon EC2 instances built on AWS Inferentia and AWS Trainium. It integrates with PyTorch and TensorFlow, so models written in those frameworks can be compiled, trained and deployed on Neuron hardware with few code changes.
Neuron SDK components
Neuron Compiler. The compiler translates a model from the framework’s graph representation into a Neuron Executable File Format (NEFF) file, which contains code optimized for Inferentia and Trainium.
Neuron Runtime. The runtime loads NEFF files onto the NeuronCores, manages device memory, and executes the compiled models.
Framework integrations. Libraries such as
torch-neuronx, transformers-neuronx and
NeuronX Distributed connect the frameworks to the compiler and runtime,
and provide model-parallel building blocks for large models.
Developer tools. Tools such as
neuron-top, neuron-monitor and
neuron-profile report device utilization, and profile and
debug compiled models.
Inference performance techniques
Neuron applies several techniques to improve inference performance:
- Batching processes multiple inputs together to raise throughput, which suits cost-sensitive workloads.
- Pipelining splits a model across multiple NeuronCores so that each core holds part of the model’s weights on chip. This reduces data movement for latency-sensitive workloads.
- Overlapping operations run independent work concurrently, such as loading data while computing, so that the hardware spends less time idle.
- Operator fusion combines several operations into one, which reduces intermediate memory traffic.
- Quantization lowers the precision of weights and activations to reduce memory use and increase speed, usually with a small loss in accuracy.
- Speculative decoding uses a small draft model to propose several tokens, which the large model then verifies in a single pass.
- Custom C++ operators implement operations that the compiler does not handle well, for specific model components.
Glossary
The following terms come up frequently when working on the Neuron stack.
Hardware
FPGA (Field-Programmable Gate Array). An FPGA contains a fabric of programmable logic blocks that can be configured after manufacturing to perform specific tasks. A GPU has a fixed design. This flexibility makes FPGAs common in cryptography, financial modeling and high-frequency trading.
ASIC (Application-Specific Integrated Circuit). An ASIC is a chip designed for one purpose, and its hardware is optimized for that task. Inferentia and Trainium are ASICs designed for deep learning inference and training.
PCIe (Peripheral Component Interconnect Express). PCIe is a high-speed interface standard that connects devices such as GPUs, accelerators, network cards and NVMe storage to the CPU and memory. Each device connects through a point-to-point link made of one or more lanes, which a root complex or PCIe switch routes. Devices can also transfer data directly to each other through peer-to-peer transfers.
Memory-Mapped I/O (MMIO). With MMIO, a device’s registers and memory appear at specific addresses in the CPU’s address space. The CPU communicates with the device using ordinary load and store instructions instead of special I/O instructions.
Performance
Arithmetic intensity. Arithmetic intensity is the ratio of computation, measured in floating-point operations (FLOPs), to data movement, measured in bytes. It indicates whether an operation is compute-bound or memory-bound. For example, applying ReLU to a BF16 tensor reads 2 bytes, performs 1 comparison and writes 2 bytes per element, which gives an arithmetic intensity of 1 FLOP per 4 bytes. At such a low ratio, memory access takes longer than computation, so the operation is memory-bound (reference).
Gradient checkpointing (activation checkpointing). Gradient checkpointing reduces memory use by discarding the activations of selected layers during the forward pass and recomputing them during the backward pass. It trades extra computation for lower memory use. For a checkpointed module, only the module’s inputs and outputs stay in memory after the forward pass (reference).
Compiler optimizations
Strength reduction replaces an expensive operation with an equivalent cheaper one, for example replacing multiplication by a power of two with a bit shift.
Constant folding evaluates constant expressions at compile time and replaces them with their values.
Common subexpression elimination computes a repeated expression once and reuses the result.
Dead-code elimination removes code that does not affect the program’s output, such as computations whose results are never used.
Scalar replacement replaces array references with scalar variables where possible, which reduces memory accesses.
If-conversion turns conditional branches into predicated or conditional instructions, which removes branch penalties and exposes more instruction-level parallelism.
Function inlining replaces a function call with the body of the function, which removes the call overhead and enables further optimization.
Call specialization creates a specialized version of a function for arguments known at compile time.
Peephole optimization examines a small window of consecutive instructions and replaces inefficient sequences with faster ones.
Loop canonicalization rewrites loops to start at index 0 and step by 1, which simplifies analysis and enables further loop optimizations.
Constant generation replaces index calculations with precomputed constants.
Load/store elimination removes redundant pairs of loads and stores to the same memory location.
Compiler frameworks
XLA (Accelerated Linear Algebra). XLA is a domain-specific compiler for linear algebra that optimizes machine learning computations for accelerators. It originated in TensorFlow and also serves JAX and PyTorch through PyTorch/XLA. The Neuron PyTorch integration builds on PyTorch/XLA.
TVM. TVM is an open-source deep learning compiler stack that optimizes models for many hardware targets, including CPUs, GPUs and specialized accelerators.
MLIR (Multi-Level Intermediate Representation). MLIR is a compiler infrastructure for defining intermediate representations at several levels of abstraction. It lets compilers share optimizations and lower programs step by step toward different hardware.
LLVM and GCC. LLVM is a modular compiler infrastructure built from reusable libraries around a common intermediate representation (IR). Many compilers for GPUs and accelerators use LLVM as their back end because its components are easy to reuse. GCC is a compiler collection with a long history in production systems, and it supports a very wide range of CPU architectures. LLVM’s library design makes it easier to embed in other tools.
Virtualization
Hypervisor. A hypervisor creates and runs virtual machines. A Type 1 (bare-metal) hypervisor runs directly on the hardware and provides strong isolation and performance, for example VMware ESXi, Microsoft Hyper-V, Xen and the AWS Nitro Hypervisor. A Type 2 (hosted) hypervisor runs on a host operating system, for example VMware Workstation and Oracle VirtualBox.
SR-IOV (Single Root I/O Virtualization). SR-IOV lets one physical PCIe device, such as a network card, appear as several separate devices. The device exposes a physical function (PF) and multiple virtual functions (VFs). The hypervisor assigns VFs directly to virtual machines, which then access the device with near-native performance and little virtualization overhead.
For more detail, see the AWS Neuron documentation.
If you found this useful, please cite this post using
Senthilkumar Gopal. (Dec 2023). AWS Neuron SDK, an Overview and Glossary. sengopal.me. https://sengopal.me/posts/aws-neuron-sdk-an-overview-and-glossary
or
@article{gopal2023awsneuronsdkoverview,
title = {AWS Neuron SDK, an Overview and Glossary},
author = {Senthilkumar Gopal},
journal = {sengopal.me},
year = {2023},
month = {Dec},
url = {https://sengopal.me/posts/aws-neuron-sdk-an-overview-and-glossary}
}