refactor: modularize codebase (Issues #139-#148)#168
Merged
Conversation
Split 2087-line matmul.py into focused modules: - generic.py: matmul, batched_matmul, transpose, linear_bias_gelu - availability.py: all *_available() functions - fp8.py: FP8 GEMM operations - gemv.py: GEMV operations (M=1 optimized) - nvf4.py: NVF4 (4-bit) operations - grouped.py: Grouped GEMM for MoE - w8a16.py: W8A16 GEMM operations - __init__.py: Re-exports for backwards compatibility 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Split 1828-line audio.py into focused modules: - buffer.py: AudioBuffer, AudioRingBuffer, AudioStream, from_pcm - vad.py: VAD, SpeechSegment - preprocessing.py: preemphasis, deemphasis, remove_dc, noise_gate, etc. - spectral.py: STFT, mel-spectrogram, MFCC, delta - phase.py: ISTFT, Griffin-Lim - pitch.py: YIN pitch detection, autocorrelation - features.py: spectral centroid, bandwidth, rolloff, flatness, contrast - cqt.py: Constant-Q Transform, chromagram - hpss.py: Harmonic-Percussive Source Separation - effects.py: time_stretch, pitch_shift - __init__.py: Re-exports for backwards compatibility 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Create llm/models/ directory for model implementations - Move CausalTransformerModel to llm/models/causal.py - Update llm/model.py as re-export module for backwards compatibility - Maintain all existing public API exports (GPT2Model, LlamaModel, etc.) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Create layers/ directory with modular submodules: - linear.py: LinearBF16, LinearFP8 - norm.py: Norm (RMSNorm/LayerNorm) - rope.py: RoPE utilities - attention.py: Attention layer - mlp.py: MLP layer - moe.py: MoELayer - block.py: TransformerBlock - utils.py: repack utilities - Remove monolithic layers.py (1492 lines -> 9 focused modules) - Maintain backwards compatibility via __init__.py re-exports 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…#143) - Extract Dtype, TensorInfo, SafeTensorsFile, ShardedSafeTensorsFile, load_safetensors to safetensors.py - Extract Tokenizer class to tokenizer.py - Reduce __init__.py from ~700 lines to ~197 lines (re-exports only) - Maintain full backwards compatibility via re-exports 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…odules (#144) - Extract FP8QuantConfig, QATQuantConfig, PruningConfig, SparsityConfig, ModelOptimizationInfo, and FP8 utilities to quant.py - Extract repack_model_weights to repack.py - Reduce loader.py from 1244 lines to 614 lines - Maintain full backwards compatibility via re-exports 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Split monolithic nn.py (1000+ lines) into native structure-matching submodules: - activation.py: gelu, silu, sigmoid, tanh - norm.py: layernorm, rmsnorm - attention.py: sdpa_causal, sdpa_causal_fixed_cache, sdpa_causal_fixed_cache_ptr - rope.py: rope_inplace, rope_inplace_f32table - linear.py: bias_add_inplace, split_qkv_batch, slice_rows_range_ptr - recurrent.py: lstm_forward, lstm_bidirectional Backwards-compatible via __init__.py re-exports. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Extract memory management utilities into dedicated module: - get_memory_info: Query GPU memory - copy_to_device/copy_to_device_async: H2D transfers - copy_device_to_device_async/offset: D2D transfers - synchronize: Device synchronization Mirrors native/core/memory.hpp structure. GPUArray class remains in array.py (well-organized as-is). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Extract specialized operations from monolithic matmul.cu: - fused.cu: Fused linear+bias+GELU with CUTLASS epilogue fusion - batched.cu: Batched strided GEMM placeholder matmul.cu now focuses on core GEMM dispatch logic. Build verified: SM 120a CUDA 13.1. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Reorganize examples into logical directories: - benchmarks/: Performance benchmarks (matmul, CUDA Graph) - chat/: Chat CLI applications (standard, MoE, thinking, Triton) - demos/archived/: Version-specific demos (v01-v026) for reference Keep current demos at top level: - demo_gpu.py, demo_cuda_graph.py, demo_llm_e2e.py, etc. Update README.md with new structure and usage instructions. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This was referenced Dec 30, 2025
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.
Summary
This PR refactors the PyGPUkit codebase to improve modularity and maintainability:
Python Modules
matmul.pyinto modular package (matmul/,gemm/,gemv/)audio.pyinto modular package (audio/transforms/,audio/analysis/)CausalTransformerModeltollm/models/packagelayers.pyintolayers/package by layer type (attention, ffn, norm, embedding, recurrent)nn.pyinto modular subpackage matching native structurememory.pymodule for memory utilitiesNative (C++/CUDA)
matmul.cudispatcher - extract fused ops and batched GEMM to separate filesExamples
benchmarks/,chat/,demos/archived/)Changes
Backwards Compatibility
All public APIs are preserved via re-exports in
__init__.pyfiles. Existing code will continue to work.Test plan
🤖 Generated with Claude Code