{"id":51422175,"url":"https://github.com/mrowan137/mnist-from-scratch-hip","last_synced_at":"2026-07-05T00:30:30.221Z","repository":{"id":350965843,"uuid":"1208929467","full_name":"mrowan137/mnist-from-scratch-hip","owner":"mrowan137","description":"From-scratch implementation of MNIST linear classifier in HIP, tuned for Radeon Pro VII (gfx906)","archived":false,"fork":false,"pushed_at":"2026-06-10T06:51:54.000Z","size":11625,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-07-05T00:30:27.722Z","etag":null,"topics":["amd-gpu","cpp","hip","machine-learning","mnist","rocm"],"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/mrowan137.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,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-04-12T23:28:16.000Z","updated_at":"2026-06-27T05:22:28.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/mrowan137/mnist-from-scratch-hip","commit_stats":null,"previous_names":["mrowan137/mnist-from-scratch-hip"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mrowan137/mnist-from-scratch-hip","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrowan137%2Fmnist-from-scratch-hip","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrowan137%2Fmnist-from-scratch-hip/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrowan137%2Fmnist-from-scratch-hip/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrowan137%2Fmnist-from-scratch-hip/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mrowan137","download_url":"https://codeload.github.com/mrowan137/mnist-from-scratch-hip/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrowan137%2Fmnist-from-scratch-hip/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35140188,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-04T02:00:05.987Z","response_time":113,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["amd-gpu","cpp","hip","machine-learning","mnist","rocm"],"created_at":"2026-07-05T00:30:29.476Z","updated_at":"2026-07-05T00:30:30.189Z","avatar_url":"https://github.com/mrowan137.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MNIST from scratch in HIP\n\nDigit classification with the MNIST dataset is used widely as a 'Hello World' problem in machine learning.\nIn the age of capable ML frameworks and abstractions, it is easier than ever to produce a working MNIST classifier.\nWhile powerful, these can obscure the underlying mechanisms.\n\nThis small project aims to restore some of that view by implementing a simple MNIST linear classification model from scratch in HIP, tuned for Radeon Pro VII (gfx906).\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./imgs/digits.png\" alt=\"MNIST test digits\" width=\"700\"\u003e\n  \u003cbr\u003e\n  \u003csub\u003e\n    MNIST test dataset visualization and predictions (300 training iterations, learning rate 0.01).\n  \u003c/sub\u003e\n  \u003cbr\u003e\n\u003c/p\u003e\n\n\n## Requirements\n- AMD GPU (code is tuned for Radeon Pro VII / gfx906)\n- Compatible ROCm installation (see: https://rocm.docs.amd.com/en/latest/compatibility/compatibility-matrix.html)\n\n    \n## Build and run\n```\nmake\n./mnist ./dataset [iterations] [learning rate]\n```\n\nThe optional arguments default to 100 iterations and learning rate of 0.01.\n\n\n## Model\n\nLet $X^{jk}$ denote images (training or test), where $j$ indexes the image, and $k$ indexes pixels (plus bias).\n    \nLet $W^{ik}$ denote model weights, where $i$ indexes digit class (0–9).\n\nRepeated indices are implicitly summed and unique indices denote free matrix component labels.\nWe use all-up convention, so up/down indices carry no geometric significance.\n\nClass scores are given by:\n\n$$\n\\hat{Y}^{ij} = W^{ik} X^{jk},\n$$\n\nand loss by the average squared residual:\n\n$$\nL := \\frac{1}{N}\\lVert\\hat{Y}^{ij} - Y_{\\mathrm{true}}^{ij}\\rVert_{\\mathrm{F}}^{2},\n$$\n\nwhere $N$ is the number of images. Training proceeds by iterative update of the weights via gradient descent:\n\n$$\n\\begin{aligned}\n  W^{ik} \u0026\\leftarrow W^{ik} - \\Delta \\frac{\\partial L }{\\partial W^{ik}}\\\\\n         \u0026= W^{ik} - \\frac{2 \\Delta}{N} ( \\hat{Y}^{ij} - Y_{\\mathrm{true}}^{ij}) X^{jk},\n\\end{aligned}\n$$\n\nwhere $\\Delta$ is the learning rate.\n\n\n## Training loss and accuracy\n\nOn a Radeon Pro VII test system with ROCm 6.4.3, this implementation achieved 84.8% training / 85.5% test accuracy in 300 iterations using learning rate 0.01, 4.18 ms / iteration.\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./imgs/loss.png\" alt=\"MNIST training loss\" width=\"500\"\u003e\n  \u003cbr\u003e\n  \u003csub\u003e\n    MNIST training loss over 300 iterations (learning rate 0.01).\n  \u003c/sub\u003e\n  \u003cbr\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./imgs/accuracy.png\" alt=\"MNIST training accuracy\" width=\"500\"\u003e\n  \u003cbr\u003e\n  \u003csub\u003e\n    MNIST training accuracy over 300 iterations (learning rate 0.01).\n  \u003c/sub\u003e\n  \u003cbr\u003e\n\u003c/p\u003e\n\n\n## Optimizations\n\nThe majority of time in the training loop is spent in `sgemm_sub_and_scale` and `sgemm`, which roughly account for forward pass and back propagation, respectively.\nThe primary mathematical operation in each of these steps is a matrix multiplication.\nKey performance considerations and optimizations incorporated across the training kernels are:\n- data layout: format matrices to be amenable to coalesced memory accesses (e.g. transpose matrices)\n- ping-pong buffering: overlap load of $k+1^{th}$ tile with compute of $k^{th}$ tile\n- shared memory tiling: compute partial dot products in a way that optimizes data reuse\n- hand-tuned tiling parameters\n- occupancy tuning via launch bounds, thread, and block parameters\n- loop unrolling (not always a win, but can help if it does not induce register spillage)\n- warp shuffle reduction: reduce atomics contention\n- kernel fusion: store $2(\\hat{Y} - Y)$ during forward pass, in preparation for back propagation\n\n\n## References\n\n- MNIST dataset:\n  - https://storage.googleapis.com/cvdf-datasets/mnist/train-images-idx3-ubyte.gz\n  - https://storage.googleapis.com/cvdf-datasets/mnist/train-labels-idx1-ubyte.gz\n  - https://storage.googleapis.com/cvdf-datasets/mnist/t10k-images-idx3-ubyte.gz\n  - https://storage.googleapis.com/cvdf-datasets/mnist/t10k-labels-idx1-ubyte.gz\n- MNIST handwritten database (archived from Yann LeCun's website): https://web.archive.org/web/20200430193701/http://yann.lecun.com/exdb/mnist/\n- Reading MNIST dataset: https://stackoverflow.com/questions/8286668/how-to-read-mnist-data-in-c\n- Character ramp: https://gist.github.com/micycle1/507c3052a9fcf04520430440d0671ecb\n- Random number initializations: https://github.com/joelkp/ranoise/blob/main/splitmix32.c\n- Neural networks from scratch: https://karpathy.ai/zero-to-hero.html\n- Radeon Pro VII specs: https://www.techpowerup.com/gpu-specs/radeon-pro-vii.c3575\n- ROCm compatibility matrix: https://rocm.docs.amd.com/en/latest/compatibility/compatibility-matrix.html\n- GEMM by hand: https://github.com/AyakaGEMM/Hands-on-GEMM/tree/main\n\n\n## License\n\nThis project is licensed under the [MIT License](./LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrowan137%2Fmnist-from-scratch-hip","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrowan137%2Fmnist-from-scratch-hip","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrowan137%2Fmnist-from-scratch-hip/lists"}