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:
- Start with input weights
and bias . - Compute
. - Apply activation
- Loss function
is computed and backpropagation is used to update weights and bias.
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.
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
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
We can calculate the number of FLOPs (Floating Point Operations) as
This has poor spatial locality, large parameter count and expensive computation.
2.2 Convolutional Layer
This applies a small learnable kernel (e.g.
It is computated in a sliding window manner. With input height
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
- Query
- Key
- Value
The attention map computation:
- Score
- Attention Map
- Intermediate Output
- Final Output
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:
- Fully visible: every token attends to every other token. Used in layers where bidirectional context is available.
- Causal Pattern: token at pos
can only attend to tokens at pos . Prevents information leak from the future. Used for autoregressive models for left-to-right generation. - Causal with Prefix: initial tokens visible to all positions, later tokens follow causal pattern. Used for instruction following models where shared context is available before generation.
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:
-axis: Algorithmic Intensity (FLOPs/Byte) -axis: Performance (FLOPS) - Sloped line represents memory bound performance, flat line represents compute bound performance. The intersection is the turning point.
Operational Intensity (OI) is the ratio of FLOPs to memory traffic (bytes).