Efficient ML

NN tolerate approximation -> efficiency opportunities. Bit-level (quantisation), tensor-level (pruning), operator-level (efficient operators), architecture-level (Neural Architecture Search).

1. Quantization

We can represent weights / activations with lower-precision data types (e.g. int8, fp16, fp4) instead of fp32. Weights are simply stored in lower precision, while activations are computed in lower precision and then converted back to higher precision for the next layer.

This reduces both memory footprint and energy consumption while increasing throughput and compute density.

1.1 Floating Point

FP32 consists of sign bit, exponent bits, and mantissa bits.

Where . We can represent infinity as and . NaN is given by and .

A subnormal (denormalized) number is and .

We may wanna repurpose some underused bits here (e.g. sign bit) in a kind of quantisation.

FormatPrecision
FP641152102315-16 digits
FP328231276-7 digits
FP16510153-4 digits
FP84372-3 digits
FP42111 digit

1.2 Integer

Unsigned integers are simply where is the number of bits.

Signed integers are usually represented in two's complement with range and are computed as

1.3 Emerging Data Types

Block Floating Point: keep the exponent shared across a block of numbers, while each number has its own mantissa and sign bit. This allows for a more efficient representation of numbers with similar magnitudes. This saves memory and potentially improves performance as fixed-point arithmetic can be used within a block.

Microscaling (MX): use a block of scalar values, each with its own exponent and mantissa, but the exponent is scaled by a factor of . This allows for a more efficient representation of numbers with similar magnitudes, while still allowing for some flexibility in the range of values that can be represented. .

1.4 Quantisation Paradigms

Fake Quantization: simulates quantization during QAT by injecting quantization noise (while still learning at high precision). Useful when no hardware support for low-precision computation.

1.5 Linear Quantization

An affine mapping of integers to real numbers is given by (dequantization) where:

So, for tensor , . The rounding function also affects quantization error. Matrix multiplication can be formulated as:

Multiple layers can be chained together. Quantize initial input tensor, then for each layer:

  1. Perform matmul in integer.
  2. Accumulate results in high bit-width (e.g. INT32).
  3. Apply scaling rounding and zero-point to get quantized output tensor.
  4. Map results back to low-bit integer for next layer.

1.6 Symmetric vs Asymmetric

1.7 Non-linear Quantization

Linear quantisation is suboptimal for heavy-tailed or highly-skewed distributions.

An example of non-linear quantisation is logarithmic quantisation where:

, scaled by if not in this range. Multiplications become bit-shifts, which are more efficient!

K-means quantization clusters weight values into a limited number of centroids:

  1. Apply K-means to group weights into -clusters.
  2. Store cluster centroids in a lookup table (codebook).
  3. Use an index, replacing each weight by its centroid index.
  4. During inference, weights are reconstructed from the lookup table.

1.8 Block / Group Size

There are granularity trade-offs:

Addionally, we can have:

1.9 Dynamic vs Static

2. Pruning

Reduce model size by removing redundant weights.

  1. Learn connectivity via normal training.
  2. Prune low-weight connections (e.g. below a threshold).
  3. Retain network with sparse connections.

Iterative pruning: repeats step 2 and 3, until satisfactory performance is achieved. Compression rate is .

This reduces computation and memory cost. Pruning can be:

Structured pruning enforces regular sparsity patterns for hardware efficiency. This depends on the memory access patterns and accelerator support. Modern AI accelerators integrate sparisty-aware computation.

NVIDIA supports -sparse matrix multiplication, where is the number of non-zero elements per row and is the number of non-zero elements per column. This allows for efficient computation on sparse matrices.

3. Efficient Operators

We want to convert dense operations to sparse operations, and reduce the number of operations required for a given task.

In multi-head attention, seperate , and matrices are computed for each head. This is inefficient, as the same input is used to compute all heads. Instead, we can share and matrices across multiple s, reducing memory and computation for multi , pairs. However, this causes accuracy loss.

Instead, grouped query attention (GQA) makes the number of shared , pairs a hyperparameter, balancing tradeoff between accuracy and memory/compute complexity.

Neural architecture is manually designed, constrained by hardware and requires time consuming hyperparameter tuning.

Using Reinforcement Learning for NAS: adopts an RNN to predict neural architecture configuration, train the architecture, and use the validation accuracy as a reward signal to update the RNN. This is repeated until convergence. However, this is very computationally expensive. Search cost depends on:

4.1 DARTS

Differentiable Architecture Search (DARTS) relaxes the discrete search space to a continuous one, allowing for gradient-based optimization. It represents the NAS problem as a DAG, where each node represents a tensor/activaation and each edge represents some operations.

For an operation pool :

This reduces NAS search cost with comparable results, but has high memory consumption and no hardware awareness. To fix this, we can add a expected latency loss to the loss function, which penalizes architectures with high latency. This encourages the search to find architectures that are both accurate and efficient.

Back to Home