[WIP] Fused Adam Triton Kernels#29
Draft
jeromeku wants to merge 5 commits into
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fused GaLore Adam (WIP)
Various fused implementations of
Adamupdate step per Gradient Low-Rank ProjectionThis is an initial attempt at optimizing the update step of the
GaLore Adamoptimizer.Overview
The
GaLoreAdamoptimizer introduces additional ops to the traditionaladamupdate step.Specifically:
gradis projected to low rank --> additional matmuladamstates are updated withgradelementwise (same asAdamexcept in low-rank)gradis projected to full rank --> additional matmulparamsare updated with the normalized full rank gradImplementation
Various fusions were attempted across 2 kernel implementations:
Fusedadamstate updates are loaded and updated (inplace) during the firstmatmulmatmulHybridtorch matmul(i.e.,cuBlas)FusedPerformance
Below are benchmarks for various kernels:
torch- referencetorchimplementation where each of the steps are implemented verbatim per abovehybrid- see abovefused- see abovecompiled-torchreference implementation compiled usingtorch.compilewithfullgraph=Trueandmode="max-autotune".Configs for each benchmark are the
grad (param)shape,dtypeofgradandadamstates, andallow_tf32, whethertorchandtritonmatmuls are allowed to useTF32tensor cores (seeDiscussion).Grad shape:4096x4096,dtype:torch.float32,allow_tf32:FalseGrad shape:4096x4096,dtype:torch.float32,allow_tf32:TrueGrad shape:4096x11008,dtype:torch.float32,allow_tf32:FalseGrad shape:4096x11008,dtype:torch.float32,allow_tf32:TrueAccuracy
Comparison to reference
torchimplementation:Discussion
Down Projection GEMM Shape
The motivation for the
hybridapproach is the unconventional matrix shapes of the down projection (Step 1):gradmatrix is maintained while other is projected to low rank per theGaLorealgorithmM >= N, the GEMM is of shape (M x N) x (N x rank) = (M x rank), (rank x M) x (M x N) = (rank x N) otherwise{M, N} >> rankby definition, this results in a large reduction dimension relative to one of the output dimensions (output matrix is either fat or skinny)split-k / parallel reductionGEMMparadigm which is more tailored for shapes where both output dims are smaller than the reduction dimension.tritonautotunerfor the down projection step, despite tuning across many compute and io-bound configs (seefused.triton_utils.kernels.matmul.py).triton-tunedmatmulagainst defaulttorch.matmulfor these shapes showed worse performance, fortorch.float32Effect of
TF32tensor coresallow_tf32: this has significant impact on relative performance oftritonvstorchmatmuls:matmulshow that:allow_tf32=Truefor both, triton exhibits~1.30xperformance improvement overtorch.allow_tf32=False, performance oftritondegrades significantly to~.50xoftorch.See this
torch notefor more details on this feature.Note: This might be less of a concern given this incoming triton PR, which implements a fast
TF32trick that improves both performance and accuracy.Repro
tests/test_fused_kernels.pyis aCLIthat has 2 modes, one for testing kernel accuracy, and the other for benchmarking across a number of configs.Examples
Accuracy
Test accuracy of
torchvshybridforM=4096,N=4096,rank=128, andtf32switched on:Benchmark
Benchmark across all kernels without
tf32:Additional options
Note: Passing in the additional flag
--verbosewill showtritonautotuning logs -- I customized thetritonautotuner spit out configs and other details.Test Env
NVIDIA RTX A60008648676MB842.3.0.dev20240310+cu1183.0.0Next Steps
FusedGaLoreOptimizerCutlass- given fixed GEMM shape, experiment withCutlassGEMMs (split-k,stream-k, fasttensorops). Interestingly, profilingtorch.matmulfor down projection shows thatcuBlasdispatches to aCutlasskernel of shape128x128x16.AdamW8bittorch.compileperformance