{"id":28712854,"url":"https://github.com/dellano54/extreme-matmul","last_synced_at":"2026-04-16T19:44:34.903Z","repository":{"id":298007734,"uuid":"998526625","full_name":"dellano54/extreme-matmul","owner":"dellano54","description":"fastest matmul operation than numpy for intel CPU (xeon)","archived":false,"fork":false,"pushed_at":"2025-06-08T22:22:37.000Z","size":0,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-08T22:28:51.339Z","etag":null,"topics":["matmul","matrix","matrix-library","matrix-multiplication","numpy","numpy-arrays","numpy-library","numpy-matrix","pytorch","pytorch-implementation","pytorch-lightning"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dellano54.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-08T19:50:56.000Z","updated_at":"2025-06-08T22:22:40.000Z","dependencies_parsed_at":"2025-06-08T22:38:59.575Z","dependency_job_id":null,"html_url":"https://github.com/dellano54/extreme-matmul","commit_stats":null,"previous_names":["dellano54/extreme-matmul"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dellano54/extreme-matmul","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dellano54%2Fextreme-matmul","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dellano54%2Fextreme-matmul/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dellano54%2Fextreme-matmul/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dellano54%2Fextreme-matmul/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dellano54","download_url":"https://codeload.github.com/dellano54/extreme-matmul/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dellano54%2Fextreme-matmul/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259901296,"owners_count":22929219,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["matmul","matrix","matrix-library","matrix-multiplication","numpy","numpy-arrays","numpy-library","numpy-matrix","pytorch","pytorch-implementation","pytorch-lightning"],"created_at":"2025-06-15T00:00:56.761Z","updated_at":"2026-04-16T19:44:29.852Z","avatar_url":"https://github.com/dellano54.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ⚡ extreme_matmul\nFast matrix multiplication in Python using Intel MKL\n\n*Because sometimes NumPy just isn't fast enough.*\n\n## What's This About?\n\nEver found yourself waiting around for large matrix multiplications to finish? Yeah, me too. That's why I built **Extreme MatMul** - a custom Python extension that leverages Intel's Math Kernel Library (MKL) to make matrix multiplication blazingly fast.\n\nThis project started as a deep dive into understanding how high-performance computing libraries work under the hood. What I discovered was pretty eye-opening: with the right approach, we can achieve some serious performance gains over standard NumPy operations.\n\n## Performance Results\n\nHere's what really matters - the numbers don't lie:\n\n### Large Matrices (8164 × 8164 × 2048)\n```\n===============================================\nextreme_matmul.fast_matmul    : 1.74 seconds ⚡\nNumPy @ operator             : 7.14 seconds 🐌\ntorch optim                  : 1.97 seconds\n===============================================\n```\n\n### Smaller Matrices (128 × 256 × 256)\n```\n===============================================\nextreme_matmul.fast_matmul    : 0.35ms ⚡\nNumPy @ operator             : 19.25ms\ntorch optim                  : 9.96ms\n===============================================\n\n```\n\u003cp align=\"right\"\u003e\u003csmall\u003e\u003ci\u003e(Tests were ran on Intel(R) Xeon(R) CPU @ 2.00GHz)\u003c/i\u003e\u003c/small\u003e\u003c/p\u003e\n\n**That's roughly 4x faster than NumPy for large matrices and 55x faster for smaller ones!**\n\n\n## How It Works\n\nThe magic happens through a few key optimizations:\n\n1. **Intelligent Algorithm Selection**: For tiny matrices (≤32×32), we use a simple triple-loop implementation that's actually faster due to reduced overhead. For everything else, we call Intel MKL's optimized BLAS routines.\n\n2. **Direct MKL Integration**: Instead of going through NumPy's abstraction layers, we talk directly to Intel's Math Kernel Library - the same engine that powers many scientific computing applications.\n\n3. **Memory Layout Optimization**: The code ensures data is contiguous in memory and properly aligned for SIMD operations.\n\n4. **Flexible Broadcasting**: Supports 1D, 2D, and 3D arrays with proper broadcasting rules, just like NumPy.\n\n## Installation\n\n### Prerequisites\nYou'll need Intel MKL installed on your system. Here's how to get everything set up:\n\n```bash\n# Update your system\nsudo apt update\nsudo apt install -y gpg-agent wget\n\n# Add Intel's repository\nwget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB | gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg \u003e /dev/null\necho \"deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main\" | sudo tee /etc/apt/sources.list.d/oneAPI.list\n\n# Install Intel MKL\nsudo apt update\nsudo apt install intel-oneapi-mkl-devel\n\n# Set up the environment and install\nsource /opt/intel/oneapi/setvars.sh\npip install git+https://github.com/dellano54/extreme-matmul.git\n```\n\n### Build from Source\n```bash\ngit clone https://github.com/yourusername/extreme-matmul.git\ncd extreme-matmul\nsource /opt/intel/oneapi/setvars.sh  # Make sure MKL is in your environment\npython setup.py build_ext --inplace\n```\n\n## Usage\n\nIt's designed to be a drop-in replacement for NumPy's matrix multiplication:\n\n```python\nimport numpy as np\nimport extreme_matmul\n\n# Create some test matrices\nA = np.random.rand(1000, 1000).astype(np.float32)\nB = np.random.rand(1000, 1000).astype(np.float32)\n\n# Use extreme_matmul instead of np.matmul\nresult = extreme_matmul.matmul(A, B)\n\n# That's it! Same API, much faster performance\n```\n\n### Supported Operations\n\n- **Vector × Vector**: Dot product\n- **Matrix × Vector**: Matrix-vector multiplication  \n- **Matrix × Matrix**: Standard matrix multiplication\n- **Batch Operations**: 3D arrays with batch dimensions\n- **Mixed Dimensions**: Flexible broadcasting like NumPy\n\n### Important Notes\n\n- **Float32 Only**: Currently optimized for 32-bit floating point operations\n- **Memory Requirements**: Arrays are converted to contiguous format if needed\n- **Array Limits**: Supports 1D to 3D arrays (batch processing for 3D)\n\n## Running the Benchmarks\n\nWant to see the performance difference yourself?\n\n```python\npython benchmark.py\n```\n\nThis will run the same tests I used to generate the performance numbers above. The benchmark tests both large and small matrix scenarios to show how the algorithm selection works.\n\n## Technical Deep Dive\n\n### Algorithm Selection Strategy\nThe code uses a size-based heuristic to choose between algorithms:\n- **Tiny matrices** (≤32×32): Simple triple-loop implementation\n- **Larger matrices**: Intel MKL's `cblas_sgemm` with full optimizations\n\n### Memory Management\n- Automatic conversion to contiguous arrays when needed\n- Proper reference counting to prevent memory leaks\n- Aligned memory access for optimal SIMD performance\n\n### Error Handling\nComprehensive input validation including:\n- Type checking (float32 requirement)\n- Dimension compatibility verification\n- Memory allocation error handling\n\n## Why This Matters\n\nThis project demonstrates several important concepts:\n\n1. **The Power of Specialized Libraries**: MKL is heavily optimized for Intel processors with years of engineering behind it\n2. **Algorithm Selection**: Sometimes simpler algorithms win for small inputs due to reduced overhead\n3. **C Extensions**: How to write efficient Python extensions that rival compiled languages\n4. **Memory Layout**: The importance of cache-friendly data structures\n\n## Limitations \u0026 Future Work\n\n- **Platform Dependency**: Currently requires Intel MKL (Linux/Intel processors)\n- **Data Type Limitation**: Only supports float32 (could be extended)\n- **GPU Support**: No CUDA implementation yet (interesting future direction)\n\n## Contributing\n\nFound a bug or have an idea for improvement? Feel free to open an issue or submit a pull request. This started as a learning project, but I'm always interested in making it better!\n\n## License\n\nMIT License - feel free to use this in your own projects.\n\n---\n\n*Built with curiosity and a need for speed. If you find this useful, consider giving it a star! ⭐*\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdellano54%2Fextreme-matmul","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdellano54%2Fextreme-matmul","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdellano54%2Fextreme-matmul/lists"}