// TiledMMAHelper: dispatcher for tiled (OptimalLayout) MMA operations. // // Routes forward and backward to backend-specific implementations: // CUDA: TiledMMACuda / MMAHelperNew (WaveMatrix register ops) // Vulkan: TiledMMAVulkan (WaveMatrix + shmem, CoopMat.Load) // // InputSize and OutputSize must be tile-aligned (multiples of 16). // Callers with arbitrary sizes should pad via PadToTile before instantiation. implementing neural; // Round up a compile-time integer to the next multiple of 16 (cooperative matrix tile size). internal struct TileAlignedSize { internal static const int Value = ((Size + 15) / 16) * 16; } [require(cooperative_matrix)] internal struct TiledMMAHelper where T : __BuiltinFloatingPointType where T.Differential == T { typealias BaseMMA = MMAHelper; internal typealias TileInfoT = BaseMMA.TileInfo; internal typealias TileInfo = TileInfoT; typealias CMShape = BaseMMA.CMShape; typealias ShMemInfo = BaseMMA.ShMemInfo; static const int M = BaseMMA.M; static const int N = BaseMMA.N; static const int K = BaseMMA.K; // ========================================================================= // Forward MMA: dispatches to CUDA or Vulkan backend. // // CUDA: TiledMMACuda.forward (MMAHelperNew, register-based, zero shmem) // Vulkan: TiledMMAVulkan.forward (WaveMatrix + shmem, CoopMat.Load) // ========================================================================= [ForceInline] [require(cooperative_matrix, subgroup_basic, cuda_spirv)] internal static WaveTangledVector forward( WaveTangledVector input, Address weightAddress, Optional
biasAddress) where Address : IPointerLikeAddress { __target_switch { case cuda: typealias Cuda = TiledMMACuda; return Cuda.forward( input, weightAddress, biasAddress); case spirv: typealias Vulkan = TiledMMAVulkan; return Vulkan.forward( input, weightAddress, biasAddress); } } // ========================================================================= // Unified backward: dispatches to CUDA or Vulkan backend. // // CUDA: TiledMMACuda (WaveMatrix register ops, shuffles, ChangeMajor) // Vulkan: TiledMMAVulkan (WaveMatrix + shmem, CoopMat.Load, no register transforms) // ========================================================================= [ForceInline] [require(cooperative_matrix, subgroup_basic, cuda_spirv)] internal static WaveTangledVector backward( WaveTangledVector input, OutputVector dOut, Address weightAddress, Address.Differential weightGradAddress, Address.Differential biasGradAddress) where Address : IPointerLikeAddress where Address.Differential : IPointerLikeAddress where OutputVector : IVector { // T.Differential == T, so Address.Differential == Address at the concrete level. typealias OutputVec = WaveTangledVector; let dOutConcrete = bit_cast(dOut); let weightGradConcrete = bit_cast
(weightGradAddress); let biasGradConcrete = bit_cast
(biasGradAddress); __target_switch { case cuda: typealias Cuda = TiledMMACuda; return Cuda.backward( input, dOutConcrete, weightAddress, weightGradConcrete, biasGradConcrete); case spirv: typealias Vulkan = TiledMMAVulkan; return Vulkan.backward( input, dOutConcrete, weightAddress, weightGradConcrete, biasGradConcrete); } } }