6.2.12 · HinglishGPU Architecture

Tensor cores and matrix operations

2,951 words13 min readRead in English

6.2.12 · Hardware › GPU Architecture

Tensor Cores Kya Hain?

Matrix Multiplication Kaise Kaam Karta Hai (Scratch Se)

Figure — Tensor cores and matrix operations

Traditional Approach: Element-by-Element

C = A × B ke liye jahan A, M×K hai, B, K×N hai, C, M×N hai:

CUDA cores par: Har thread ek C[i][j] compute karta hai:

  1. A ki row i load karna (K elements)
  2. B ka column j load karna (K elements)
  3. K multiply-adds serially perform karna
  4. Result C[i][j] mein likhna

Problem: 1024×1024 matrices ke liye, yeh 1024³ ≈ 1 billion FMAs hain (≈ 2 billion FLOPs). 10 TFLOPS (10×10¹² FLOPS) par, yeh pure compute ke liye lagbhag ms hai — lekin memory stalls aur poor reuse real CUDA-core time ko kaafi bura bana dete hain.

Tensor Core Approach: Tiled Matrix Operations

Key insight: Badi matrix multiply ko chhoti tiles mein todna aur har tile ke liye specialized hardware use karna.

Badi Matrix Multiply ke liye Complete Algorithm

Problem: 4×4 Tensor Cores use karke 1024×1024 matrices multiply karo.

Solution: Accumulation ke saath tiled multiply

# Pseudocode for C = A × B using Tensor Cores
M, N, K = 1024, 1024, 1024
TILE = 4
 
for i in range(0, M, TILE):          # Tile rows of A
    for j in range(0, N, TILE):      # Tile columns of B
        D_tile = zeros(TILE, TILE)   # Accumulator
        for k in range(0, K, TILE):  # Tile inner dimension
            A_tile = A[i:i+TILE, k:k+TILE]
            B_tile = B[k:k+TILE, j:j+TILE]
            # SINGLE TENSOR CORE INSTRUCTION
            D_tile = mma_sync(A_tile, B_tile, D_tile)
        
        C[i:i+TILE, j:j+TILE] = D_tile

Yeh kyun kaam karta hai:

  1. Tiling correctness preserve karta hai: Matrix multiply associative hai, isliye hum partial products sum kar sakte hain
  2. Har iteration ek 4×4 partial product compute karta hai: A[i:i+4, k:k+4] × B[k:k+4, j:j+4]
  3. Accumulation D_tile mein final result ke liye saare K/4 partial products sum karta hai
  4. Outer loops output matrix C ke saare tiles cover karte hain

Performance calculation:

  • Scalar FMAs: 1024³ ≈ 1.07 billion FMAs
  • Tensor Core ops ki number: (1024/4)³ = 256³ ≈ 16.8 million
  • Ratio: → scalar ke comparison mein 64× instruction reduction (har Tensor Core op 4×4×4 = 64 FMAs karta hai)

Precision Modes aur Unka Impact

Tensor Cores Ko Program Karna

WMMA API (Warp-level Matrix Multiply Accumulate)

#include <mma.h>
using namespace nvcuda::wmma;
 
__global__ void matmul_tensor_core(
    half *A, half *B, float *C, int M, int N, int K) {
    // Each warp computes a 16×16 tile of C
    int warpM = (blockIdx.x * blockDim.x + threadIdx.x) / 32;
    int warpN = (blockIdx.y * blockDim.y + threadIdx.y);
    
    // Declare fragments (distributed across warp)
    fragment<matrix_a, 16, 16, 16, half, row_major> a_frag;
    fragment<matrix_b, 16, 16, 16, half, col_major> b_frag;
    fragment<accumulator, 16, 16, 16, float> c_frag;
    fill_fragment(c_frag, 0.0f);  // Initialize accumulator
    
    // Tile K dimension
    for (int i = 0; i < K; i += 16) {
        // Load tiles (each thread gets its portion)
        load_matrix_sync(a_frag, A + warpM*16*K + i, K);
        load_matrix_sync(b_frag, B + i*N + warpN*16, N);
        
        // TENSOR CORE OPERATION
        mma_sync(c_frag, a_frag, b_frag, c_frag);
    }
    
    // Store result
    store_matrix_sync(C + warpM*16*N + warpN*16, c_frag, N, mem_row_major);
}

Har step kyun?

  • fragment: Data warp ke 32 threads mein distributed hai (har thread tile ka ek piece hold karta hai)
  • load_matrix_sync: Warp-level load, har thread global memory se apna portion fetch karta hai
  • mma_sync: The Tensor Core instruction — saare 32 threads cooperatively participate karte hain
  • store_matrix_sync: Warp-level store, accumulator fragment → global memory

cuBLAS aur cuDNN Automatic Use

import torch
 
# PyTorch automatically uses Tensor Cores when:
# 1. Input dtype is float16, bfloat16, or TF32
# 2. Dimensions are multiples of 8
# 3. torch.backends.cuda.matmul.allow_tf32 = True (default on Ampere+)
 
A = torch.randn(1024, 1024, dtype=torch.float16, device='cuda')
B = torch.randn(1024, 1024, dtype=torch.float16, device='cuda')
 
# Uses Tensor Cores automatically
C = torch.matmul(A, B)

Dimensions 8 ke multiples kyun hone chahiye?

  • Tensor Core API tiles 16×16 hoti hain (precision ke hisaab se K, 8/16 ke multiples mein)
  • Multiples tak padding full tile coverage ensure karti hai
  • Partial tiles ko fallback handling chahiye (performance cliff)

Common Mistakes aur Fixes

Performance Characteristics

Memory Layout aur Optimization

Tensor Cores vs CUDA Cores Kab Use Karein

Scenario Choice Reason
Matrix multiply, M=N=K=4096, FP16 Tensor Cores Perfect fit: large, aligned, FP16
Matrix multiply, M=N=K=32, FP16 CUDA Cores Bahut chhota, overhead dominate karta hai
Element-wise ops (ReLU, exp) CUDA Cores Exploit karne ke liye koi matrix structure nahi
Convolution (large channels) Tensor Cores Conv2D, im2col ke zariye matrix multiply hai
Attention mechanism (Q, K, V) Tensor Cores Multiple batched matmuls
FP64 scientific computing CUDA Cores / FP64 TC Early Tensor Cores mein FP64 nahi tha
Sparse matrix multiply Special Ampere sparse Tensor Cores use karo (2:4 sparsity)
Recall Ek 12-Saal ke Bachhe ko Explain Karo

Socho tumhare paas homework ka ek bada dhera hai: 1000 multiplication ke sawaal.

CUDA cores aise hain jaise tumhare paas ek calculator ho. Tum ek sawaal solve karte ho, jawaab likhte ho, agla solve karte ho, woh likhte ho... Kaam karta hai, lekin slow hai. Tensor Cores aise hain jaise tumhare paas ek jaadu ka calculator ho jo ek saath 64 sawaal solve karta hai. Tum use numbers ki ek badi grid dikhate ho, aur woh turant tumhe jawaabon ki poori grid de deta hai.

Lekin ek catch hai: jaadu ka calculator tabhi kaam karta hai jab tum use exactly 64 sawaal ek neat grid mein do. Agar tum use 7 sawaal do, toh woh help nahi kar sakta — tumhe regular calculator use karna padega.

Deep learning (computers ko images recognize karna, speech samajhna sikhana) mein multiplication ke yahi bade grids BHARE PADE hain. Isliye Tensor Cores AI computers ko itna fast banate hain!

Connections

  • 6.2.1-GPU-vs-CPU-architecture — GPUs ko specialized units kyun chahiye
  • 6.2.8-Memory-hierarchy-in-GPUs — Shared memory tiling ko kaise enable karta hai
  • 6.2.10-Warp-scheduling-and-execution — Warps Tensor Core fragments se kaise map hote hain
  • 8.3.4-Mixed-precision-training — Deep learning mein Tensor Cores use karna
  • 8.4.2-Quantization-techniques — INT8 aur INT4 Tensor Core modes
  • 9.1.5-Roofline-model — Tensor Core performance limits analyze karna
  • 7.3.6-cuBLAS-and-cuDNN — Libraries jo Tensor Core usage abstract karti hain

#flashcards/hardware

Tensor Core kya hai?
NVIDIA GPUs mein ek specialized hardware unit jo single instruction mein chhoti matrix tiles par (internally 4×4, API level par 16×16 ke roop mein exposed) matrix multiply-accumulate (MMA) operations perform karta hai, matrix workloads ke liye CUDA cores se 8-16× higher throughput achieve karta hai.
Ek Tensor Core ek instruction mein kya operation perform karta hai?
D = A × B + C, jahan A, B, C, D chhoti matrices hain. Internally ek 4×4 sub-multiply per clock tak 64 multiply-adds karta hai.
Tensor Cores mixed precision (FP16 input, FP32 accumulate) kyun use karte hain?
FP16 inputs 2× chhote hote hain (better memory bandwidth) aur multipliers chhote hote hain (higher compute density), jabki FP32 accumulation kai low-precision products sum karne se error buildup rokta hai.
Tensor Cores efficiently use karne ke liye minimum matrix size kya hai?
Lagbhag 512×512. Chhoti matrices mein compute ke relative bahut zyada overhead hota hai, aur hardware saturate nahi ho pata.
Tensor Cores ke liye matrix dimensions 8 ya 16 ke multiples kyun hone chahiye?
Tensor Cores fixed API tile sizes (16×16) par operate karte hain. Non-multiple dimensions mein padding ya fallback handling chahiye, efficiency kam ho jaati hai.
Scalar FMAs se 4×4 Tensor Core ops tak instruction-count reduction kitni hai?
64×, kyunki har Tensor Core instruction 4×4×4 = 64 FMAs perform karta hai.
Tensor Core matrix multiply ke liye correct PTX instruction ka naam kya hai?
mma.sync (WMMA API mein wmma::mma_sync ke roop mein exposed).
Volta par ek 4×4 Tensor Core sub-multiply ek warp mein kaise distribute hoti hai?
32 threads ka ek warp 4 groups of 8 threads mein split hota hai; har 4×4 sub-multiply ek 8-thread group ke dwara cooperatively execute hoti hai, ek element per thread nahi.

A100 Tensor Cores ka CUDA cores par theoretical speedup kya hai? :::

Concept Map

dominated by

slow on

1 FMA per clock

motivates

computes

one instruction

uses

exposed via

issued as

amortizes

evolved across

added precisions

Deep learning

Matrix multiply C = A x B

CUDA cores scalar FMA

Poor throughput

Tensor Core

Matrix multiply-accumulate D = A x B + C

8-16x throughput

Tiled 4x4 sub-partition

WMMA 16x16 API

mma.sync instruction

64 FMAs per instruction

Volta Turing Ampere Hopper

FP16 INT8 TF32 BF16 FP8