{"id":13437822,"url":"https://github.com/tpoisonooo/how-to-optimize-gemm","last_synced_at":"2025-04-04T15:10:04.673Z","repository":{"id":41039007,"uuid":"155073640","full_name":"tpoisonooo/how-to-optimize-gemm","owner":"tpoisonooo","description":"row-major matmul optimization","archived":false,"fork":false,"pushed_at":"2023-09-09T07:12:07.000Z","size":13153,"stargazers_count":589,"open_issues_count":5,"forks_count":79,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-10-11T18:10:03.496Z","etag":null,"topics":["arm64","armv7","cuda","cuda-kernel","gemm-optimization","int4","ptx","vulkan"],"latest_commit_sha":null,"homepage":"https://zhuanlan.zhihu.com/p/65436463","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tpoisonooo.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}},"created_at":"2018-10-28T13:23:30.000Z","updated_at":"2024-10-09T15:50:56.000Z","dependencies_parsed_at":"2024-01-13T16:20:35.721Z","dependency_job_id":"53de7f18-2acf-499a-9d71-dad670994064","html_url":"https://github.com/tpoisonooo/how-to-optimize-gemm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpoisonooo%2Fhow-to-optimize-gemm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpoisonooo%2Fhow-to-optimize-gemm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpoisonooo%2Fhow-to-optimize-gemm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpoisonooo%2Fhow-to-optimize-gemm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tpoisonooo","download_url":"https://codeload.github.com/tpoisonooo/how-to-optimize-gemm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247198463,"owners_count":20900080,"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":["arm64","armv7","cuda","cuda-kernel","gemm-optimization","int4","ptx","vulkan"],"created_at":"2024-07-31T03:01:00.487Z","updated_at":"2025-04-04T15:10:04.652Z","avatar_url":"https://github.com/tpoisonooo.png","language":"C++","funding_links":[],"categories":["C++","Learning Resources","Example Implementations 💡"],"sub_categories":["Blogs 🖋️"],"readme":"# how-to-optimize-gemm\n\nEnglish | [简体中文](README_ZH_CN.md)\n\n## News\n\n2023/08 aarch64 add cmake and mperf, try `-DMPERF_ENABLE=ON` !\n\n## Introduction\n\nrow-major matmul optimization tutorial\n\n| backend | armv7 | aarch64 | aarch64-int8 | cuda | cuda-int4 | vulkan | x86 |\n| ----------- | ------- | -- | ---------- | ---------- | ---------- | --------- | --- |\n| support | ✔️ | ✔️ | ✔️ | ✔️ | - | ✔️ | ✅ | \n\nAll backends and corresponding tutorials\n\n| backend | tutorial |\n| ------- | -------- |\n| aarch64 | [GEMM 入门](https://zhuanlan.zhihu.com/p/65436463) |\n| aarch64 | [GEMM caching](https://zhuanlan.zhihu.com/p/69700540) |\n| aarch64-int8 | - |\n| armv7   | [ARMv7 4x4kernel 懒人优化小实践](https://zhuanlan.zhihu.com/p/333799799) |\n| cuda    | [cuda 入门的正确姿势：how-to-optimize-gemm](https://zhuanlan.zhihu.com/p/478846788) |\n| cuda-int4 WIP | [int4 炼丹要术](https://zhuanlan.zhihu.com/p/580752390)\n| vulkan  | [如何火急火燎地上手 Vulkan](https://zhuanlan.zhihu.com/p/487583258) |\n\n\n## Build and run\n\nUsage is similar for all backends:\n\n1. Open the backend directory to be used, and change the `OLD` and `NEW` of `makefile` to the same implementation for the first run, for example\n\n```bash\n$ cd aarch64\n$ cat makefile\nOLD    := MMult_4x4_10\nNEW   := MMult_4x4_10\n..\n```\n\n2. makefile` will compile and run the implementation which `NEW` point at, and copy `output_MMult_4x4_10.m` to `output_new.m`\n```bash\n$ make run\n$ cat output_new.m\n```\n\n3. It may not be intuitive to look at the numbers directly, so draw a line chart\n```bash\n$ python3 -m pip install -r ../requirements.txt\n$ python3 plot.py\n```\n\n## Differences between backends\n\nSpecific to each hardware, there are subtle differences:\n* `NEW` may choose a different name\n* vulkan/int4 needs prerequisitions\n\n## 1. armv7 and aarch64\n\nA. Prepare armv7/aarch64 linux development environment, Raspberry Pi/rk3399/aws arm server are all fine.\n\nB. By default `ARCH := native`, build and run directly\n```\n$ cd armv8 \u0026\u0026 make run\n```\n\n## 2. aarch64 int8\n\n[chgemm](https://github.com/tpoisonooo/chgemm) is an int8 gemm library. \n\n* blue line is chgemm implementation\n* orange line is aarch64 fp32 peak\n![](./images/aarch64-fp32-peak-vs-int8.png)\n\nCompared to the code in this tutorial, the differences are:\n1. Dealing with the boundary problem, unlike the tutorial where only multiples of 4 are considered;\n2. Int8 reaches a maximum of 18.6 gflops (relative to the theoretical limit of fp32 is only 14.3 on RK3399, gemmlowp is about 12-14gflops);\n3. Based on symmetric quantization, input value range must be in \\[-127, +127\\], and -128 cannot appear;\n4. Built-in small example about how to integrate into android studio\n\nchgemm has been merged into [ncnn](https://github.com/tencent/ncnn) INT8 convolution implementation.\n\n\n## 3. x86 original\n[flame](https://github.com/flame/how-to-optimize-gemm/tree/4fcf39bd0963bca62f04bef2aeb49a06ee28508b) referenced by x86 is the original implementation, with some differences from this repo:\n\n1. The original is **column-major** `x86 SSE` version\n2. Both are tutorials, and the `MMult_4x4_17.c` written now can reach 70% of the armv8.1 CPU peak\n3. The boundary problem is not dealt with now, only the case where MNK is a multiple of 4 is considered; `sub_kernel` also only writes the simplest kind of assembly. Practical needs a simple adjustment;\n4. In terms of drawing, `octave` was discarded (it is too troublesome to configure the environment once for embedded devices), and `python` was used instead.\n\n## 4. CUDA\nThis version is **faster than NVIDIA cuBLAS**\n\n* green line is MMult_cuda_12 without tensorcore\n* blue line is cuBLAS without tensorcore\n\n![](images/cublas-vs-MMult_cuda_12.jpg)\n\n1. Need to install cuda driver and nvcc by yourself\n2. CPU OpenBLAS is required to be the baseline\n\n```bash\n$ apt install libopenblas-dev\n```\n\n\n## 5. Vulkan\n\n1. vulkan build depends on kompute API packaging, see [vulkan build documentation](https://github.com/tpoisonooo/how-to-optimize-gemm/tree/master/vulkan) for details\n\n2. More about how to learn compute shader\n\n## 6. CUDA int4\n\nWIP\n\n## Some Tools\n\n* [megpeak](https://github.com/MegEngine/MegPeak): For measuring hardware limit performance, support arm/x86/OCL..\n* [perf](https://perf.wiki.kernel.org): Available in linux system tools, for system-level performance analysis and disassembly\n* [YHs_Sample](https://github.com/Yinghan-Li/YHs_Sample): dalao 's implementation\n\n* [mperf](https://github.com/MegEngine/mperf): optimization tools\n\n## License\n[GPLv3](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftpoisonooo%2Fhow-to-optimize-gemm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftpoisonooo%2Fhow-to-optimize-gemm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftpoisonooo%2Fhow-to-optimize-gemm/lists"}