Algorithms, Operators & Analysis

AI is the capability of computational systems to perform tasks associated with intelligence. ML is AI approaches using data driven methods. DL is ML employing Deep Neural Networks.

1. Algorithm Basis

A fundamental example:

1.1 Forward Pass

Forward Pass: perform computation from input to output, compute output through linear combination and activation functions.

1.2 Compute Loss

Compute Loss: how far predictions are from the ground truth. Examples include MSE (Mean Squared Error) and CE (Cross-Entropy) and softmax:

1.3 Backward Pass

Calculate gradients via chain rule, getting local derivatives (e.g. , , , ) and then compute global derivatives (e.g. , , ).

1.4 Gradient Update

Adjust parameters in a direction that reduces loss: .

There are different variants of gradient descent for different environments (e.g. Stochastic Gradient Descent, Mini-batch Gradient Descent, Momentum, Adam).

2. MLP / CNNs

Tensors can be scalar or vectors or N-d arrays . We define scalar multiplication , matrix multiplication , and Hadamard element-wise multiplication .

2.1 Fully Connected Layer

Every neuron in this layer connects to all neurons in the next layer. This is very parameter heavy!

Computation is done with matrix-vector/matrix multiplication. With batch size , input size , output size , we have:

We can calculate the number of FLOPs (Floating Point Operations) as , as each output element requires multiplications and additions, and we have outputs.

This has poor spatial locality, large parameter count and expensive computation.

2.2 Convolutional Layer

This applies a small learnable kernel (e.g. ) across local regions of the input maps.

It is computated in a sliding window manner. With input height , width , kernel size , channels and filters, we have:

This exploits spatial locality, and kernels are applied across all spatial positions (translation invariance).

2.3 3D Convolutional Layer

This applies #d kernels across spatial + temporal (volumetric) dimensions of input feature maps. This is an extention of 2D convolution, so FLOPs:

2.4 Depthwise Convolution

In this case, each input channel is convolved with its own kernel. This reduces the number of parameters and FLOPs, but may reduce accuracy.

This is not efficient on a GPU, as it means more limited parallelism and less weight reuse. Better for mobile edge devices.

2.5 Residual Blocks

This mitigates vanishing gradients by adding a skip connection. i.e .

3. Attention Based NNs

MOTIVATION

Solves sequential data challenges. A hidden state propagates information over time steps, enabling the model to remember previous inputs. This is short term memory:

It can't really learn long term patterns. Instead, use LSTM to maintain cell state .

Attention allows model to attend any part of the sequence directly. For batch size , sequence length , key and value with feature dimension , we have the input linear projection:

The attention map computation:

We have the Scaled Attention operator: .

3.1 Attention Mask

The attention mask prevents certain positions from influencing others - applied before the softmax. This controls different causality. A pattern can be:

Multihead attention allows the model to jointly attend to information from different representation subspaces at different positions.

3.2 Encoder & Decoder

Encoder block has a fully visible attention mask, consisting of self-attention and feedforward layers. It captures bidirection relationship and global context for understanding and representation.

Decoder block has a casual atention mask and feedforward layer. Captures autoregressive dependencies.

An encoder-decoder architecture uses cross attention:

An encoder only architecture has each token attend to all other tokens, bidirectionally. Can't do well in autoregressive generation.

A decoder only architecture has each token attend to all previous tokens, unidirectionally. Can do well in autoregressive generation, but can't do well in bidirectional understanding.

Prefix only decoder architecture is a variant that allows strong conditioning without modifying model weights. Commonly used in LLMs.

3.3 Diffusion Models

Diffusion models are a class of generative models that learn to reverse a gradual noise process. They are particularly effective for generating high-quality images and other complex data types.

Diffusion language models extend this concept with a denoising process in discrete token space. Iteratively denoise a sequence of noise tokens. Non-autoregressive.

4. Roofline Model

For all models, core computations are roughly the same: matrix multiplications, element wise ops, batch ops, etc.

In a roofline model:

Operational Intensity (OI) is the ratio of FLOPs to memory traffic (bytes).

Back to Home