Files
rustytorch/docs/archive/legacy/phase1.md
T
2026-03-04 00:08:42 +00:00

202 lines
6.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
RustyTorch++ — Phase 1 Plan (Core Compiler & Runtime)
Objective (34 months): Deliver a functioning core of RustyTorch++ capable of compiling a minimal model graph to GPU code (via rustg) and executing it with a safe runtime: device/stream/event abstractions, pooled allocator, graph capture/replay, and initial fused kernels. Establish deterministic benchmarks and CI gates beyond Phase 0.
Reference paths
RUSTYTORCH_ROOT: /home/osobh/projects/rustytorch
RUSTG_COMPILER: /home/osobh/projects/rust/rustg (GPU codegen backend; RTX 5090 sm_120 primary)
STRATOSWARM_ROOT: /home/osobh/projects/stratoswarm (deployment later)
1. Scope & NonGoals
In scope
Frontend → IR → rustg codegen integration for a minimal operator set (matmul, layernorm, GELU, softmax, elementwise)
Runtime MVP: Device/Stream/Event/Graph/Module/TensorStorage traits + pooled allocator
CUDA Graphs (capture/replay) around forward path; fallback to eager launches
Initial fused kernels (fused MLP, RMSNorm/LayerNorm, rotary embeddings)
AMP (fp16/bf16) with lossscaler and overflow recovery
Determinism suite + seed discipline; fragment/leak checks
Perf smoke suites and baselines (micro + macro) with Nsight/rocprof traces
Out of scope (Phase 2+)
Full autograd; full operator coverage
Distributed training; NCCL/RCCL collectives (design only in Phase 1)
Advanced autotuning and autokernel synthesis (basic param sweeps only)
2. Success Criteria (Exit / PhaseGate)
Compiler path works: Selected ops lower to IR and generate GPU binaries with rustg; runs on RTX 5090 (sm_120).
Runtime stability: Multistream scheduling + pooled allocator pass soak tests (≥ 6h) without leaks; frag% < 15%.
Determinism: Fixedseed runs produce stable outputs within tolerances (fp32 ≤ 1e6; bf16/fp16 ≤ 1e3).
Performance: ≥ 20% steptime improvement vs. naive eager launches on the Phase 1 macrobench.
Observability: Nsight/rocprof traces captured; metrics exported to dashboards; graph capture hitrate ≥ 70%.
Governance: SBOM + signed artifacts; CI gates enforced (perf/determinism/security).
3. Architecture Work
Compiler integration
Wire rtx-compiler passes: Canonicalize → Shape/Type check → Layout Normalize → Early Fusion
Lower supported ops to rustg backends; embed specialization constants; record kernel cache key {arch, layout, tile}
Golden IR snapshots + roundtrip tests
Runtime MVP
Traits: Device, Stream, Event, Graph, Module, TensorStorage
Pooled arena allocator + pagelocked host pool; deterministic allocation order
Stream scheduler with dependency DAG; memcpy/compute overlap; priority streams
Graph capture/replay API (CUDA/HIP), toggleable; eager fallback path
Module loader: fatbin/hsaco; versioned on arch and specialization
Precision & Numerics
AMP (fp16/bf16) lossscaler with overflow counters; automatic recovery policies
Strict determinism mode: stable reductions; reproducible checkpoints (WAL)
4. Operator & Kernel Targets (Phase 1)
Core: matmul, add, mul, gelu, softmax, layer_norm, rms_norm
Embeddings: rotary position embedding (RoPE)
Fusions: MLP (GEMM + bias + activation), LayerNorm epilogue fusions
Utilities: dtype casts, view/reshape, transpose/permute
Bench reference shapes (LLMish):
GEMM: (batch=16..64, seq=1024..4096, hidden=4096..8192)
LN/RMSNorm: (N=hidden); RoPE: (batch, seq, heads, head_dim)
5. Benchmarks & Baselines
Micro
GEMM throughput (TFLOPs), achieved occupancy, DRAM GB/s, L2 hit rate
LN/RMSNorm latency, kernel fusion coverage
Macro
Encoder/decoder block forward step time and peak memory
Graph capture hitrate, memcpy/compute overlap %, allocator fragmentation %
Determinism
Fixed seed, fixed inputs; record outputs, graph hash, kernel cache key; check tolerances
6. CI Gates (Phase 1)
Determinism Gate: bitstable (declared) or tolerancestable outputs; seed & graph hash must match
Perf Gate: macrobench ≥ 20% improvement vs. eager baseline, no >5% regressions weekoverweek
Memory Gate: frag% ≤ 15%, no leaks in 6h soak
Security Gate: pinned deps, cargo-deny/cargo-audit clean, SBOM + signed artifacts
7. Observability & Telemetry
nsys/ncu or rocprof traces attached to CI artifacts; normalized summaries
Prometheus metrics: step_time_ms, tokens_per_s (if applicable), alloc_frag_pct, occ_pct, graph_hit_pct, dram_gbps
Dashboards: perSKU view (RTX 5090 sm_120), trend lines, regression alerts
8. Deliverables
Compiler: IR passes + rustg lowering for core ops; golden tests
Runtime: allocator + stream scheduler + graph capture (CUDA), eager fallback, module loader
Kernels: fused MLP, LN/RMSNorm, RoPE; AMP scaler
Benches: micro + macro; deterministic harness; baseline JSONs
CI: gates for determinism/perf/memory/security; artifact uploads (traces, SBOM)
Docs: docs/phase1.md (how to run benches, interpret traces), ADRs updated
9. RACI — Phase 1 Agent Ownership
Conductor (Orchestrator) — Accountable: critical path, phasegate readiness, dependency unblocking
Rust Engineer — Responsible: runtime traits, allocator, graph capture, module loader
Frontend Compiler Agent — Responsible: IR passes, lowering to rustg, golden IR
IR Rewrite & Fusion Agent — Responsible: early fusions; cost model annotations; epilogue fusion specs
Kernel Synthesizer Agent — Responsible: compile fused kernels; occupancy reports; fallbacks
Performance Engineer — Accountable: bench design, profiler harness, baseline capture, perf gates
Memory Planner Agent — Consulted: allocator policies; fragmentation tests
Security Engineer & Security Auditor — Accountable: dependency policies; scans; SBOM/sign
DevOps Engineer — Responsible: CI matrix; GPU runners; artifact storage; reproducibility
Governance & Provenance Agent — Responsible: signatures, provenance manifests
Agent Organizer — Accountable: tool allowlists; routing; freeze switch if gates fail
10. Risk Register & Mitigations
rustg ABI churn → pin compiler SHA per build; compatibility tests
Graph capture instability → robust fallback path; capture boundary tests
Profiler variance → warmup protocol; driver/toolchain pinning; result smoothing
Memory fragmentation → arena growth policy; periodic defrag windows (doc only in P1)
Numeric drift with AMP → strict reference runs; overflow logging; scaler tuning
11. Timeline (suggested)
Weeks 12: IR integration & golden tests; runtime trait scaffolding; allocator MVP
Weeks 34: Graph capture API + eager fallback; fused kernels v0; AMP scaler
Weeks 56: Micro/macro benches; profiler wiring; baseline capture; CI gates online
Weeks 78: Soak tests; tuning; docs & ADRs; phasegate review
12. Phase2 Handover Seeds
Autograd engine design notes and gradient numerics tests scaffold
NCCL/RCCL design doc: topology discovery, overlap strategy, retry/timeouts
Autotuning roadmap: tile/warp/vector sweeps → bandit search → kernel cache semantics
— End of Phase 1 Plan —