Files
2nd/10_Wiki/Topic_Programming/AI_and_ML/Matrix-Operations-and-AI.md
T
Antigravity Agent 9148c358d0 docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를
Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류.

- 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로
  자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거,
  동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거.
- 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming,
  Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business,
  Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로,
  나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는
  title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백).
  원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지.
- 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서.
- 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는
  지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지.
- Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경.
- 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
2026-07-05 00:33:48 +09:00

4.2 KiB

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-matrix-operations-and-ai Matrix Operations and AI 10_Wiki/Topics verified self
Matrix Ops
MatMul
GEMM
Tensor Ops
none A 0.9 applied
ai
ml
math
linear-algebra
gpu
performance
2026-05-10 pending
language framework
python pytorch-numpy-jax

Matrix Operations and AI

매 한 줄

"매 모델은 결국 행렬 곱이다". Transformer/CNN/RNN 모두 GEMM(General Matrix Multiply) 호출의 그래프이고, 성능은 BLAS·cuBLAS·Tensor Core 활용도로 결정된다.

매 핵심

매 핵심 연산

  • MatMul (GEMM): C = A @ B. FLOPs = 2·M·N·K. 모든 dense layer의 본질.
  • Element-wise: ReLU, add, multiply. Memory-bound.
  • Reduction: sum/mean/max. Softmax, LayerNorm 핵심.
  • Broadcasting: shape 자동 확장 (NumPy/PyTorch convention).
  • Einsum: einsum('bij,bjk->bik') - batched matmul 표현.

매 응용

  1. Attention: softmax(QK^T / √d) V - 4번의 matmul.
  2. Conv2d: im2col로 matmul로 변환하거나 Winograd/FFT.
  3. Embedding lookup: sparse matmul (one-hot @ W).
  4. LayerNorm/RMSNorm: reduction + element-wise.
  5. Mixture of Experts: grouped matmul (분산 라우팅).

💻 패턴

Pattern 1 — Basic MatMul (PyTorch)

import torch
A = torch.randn(128, 256, device='cuda')
B = torch.randn(256, 512, device='cuda')
C = A @ B  # or torch.matmul(A, B)
# Batched: (B, M, K) @ (B, K, N) -> (B, M, N)

Pattern 2 — Einsum (명시적)

# Attention scores: batch, heads, seq_q, seq_k
scores = torch.einsum('bhqd,bhkd->bhqk', Q, K)
# 명시적이라 transpose 실수 방지

Pattern 3 — Broadcasting 주의

a = torch.randn(32, 1, 128)
b = torch.randn(1, 64, 128)
c = a + b  # (32, 64, 128) — shape mismatch 시 silent bug

Pattern 4 — Mixed Precision (Tensor Core)

with torch.autocast('cuda', dtype=torch.bfloat16):
    out = model(x)  # GEMM은 bf16, accumulate는 fp32
# A100/H100에서 2-8배 throughput

Pattern 5 — Fused Kernel (FlashAttention)

from torch.nn.functional import scaled_dot_product_attention
out = scaled_dot_product_attention(Q, K, V, is_causal=True)
# Q@K^T → softmax → @V를 SRAM에서 한 번에 (HBM 왕복 제거)

Pattern 6 — Memory Layout (contiguous)

x = x.transpose(1, 2).contiguous()  # stride 재배치
# Non-contiguous matmul은 성능 급락

Pattern 7 — torch.compile (kernel fusion)

@torch.compile
def block(x): return F.gelu(x @ W1) @ W2
# Inductor가 element-wise를 GEMM 주변에 fuse

Pattern 8 — JAX/XLA

import jax.numpy as jnp
@jax.jit
def fwd(x, W): return jnp.einsum('bd,dk->bk', x, W)

매 결정 기준

상황 Approach
표준 dense layer nn.Linear (cuBLAS GEMM)
복잡한 contraction einsum (가독성)
Attention scaled_dot_product_attention (FlashAttn)
작은 batch / inference mixed precision + compile
Custom op Triton 또는 CUDA kernel
분산 학습 tensor parallel (megatron-style)

기본값: PyTorch 2.x + bf16 + torch.compile + FlashAttention.

🔗 Graph

🤖 LLM 활용

언제:

  • Einsum 표기 작성/디버깅 (shape mismatch 검증).
  • Custom matmul 변형 → Triton 코드 초안.
  • Memory-bound vs compute-bound 분석 결정.

언제 X:

  • 정확한 FLOPs/메모리 측정 (실측 도구 사용).
  • 최신 cuBLAS/cutlass 튜닝 파라미터.

안티패턴

  • Python loop로 matmul (for i in range: C[i] = ...) — 1000배 느림.
  • Non-contiguous tensor에 matmul 반복.
  • fp32만 고집 (Tensor Core 미사용).
  • Broadcasting 의도하지 않은 곳에서 발생.
  • 작은 matmul 다수 호출 (kernel launch overhead).

🧪 검증 / 중복

  • Verified. PyTorch 2.5/CUDA 12.x 기준. 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup