{"id":13858896,"url":"https://github.com/bilal2vec/L2","last_synced_at":"2025-07-14T01:32:08.548Z","repository":{"id":62441806,"uuid":"193277387","full_name":"bilal2vec/L2","owner":"bilal2vec","description":"l2 is a fast, Pytorch-style Tensor+Autograd library written in Rust","archived":false,"fork":false,"pushed_at":"2022-12-25T15:16:55.000Z","size":1295,"stargazers_count":190,"open_issues_count":0,"forks_count":18,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-08-09T09:44:10.292Z","etag":null,"topics":["ai","broadcasting","machine-learning","rust","tensor"],"latest_commit_sha":null,"homepage":"","language":"Rust","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/bilal2vec.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-22T20:37:19.000Z","updated_at":"2024-07-05T05:17:14.000Z","dependencies_parsed_at":"2023-01-30T22:15:55.540Z","dependency_job_id":null,"html_url":"https://github.com/bilal2vec/L2","commit_stats":null,"previous_names":["bkkaggle/l2"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bilal2vec%2FL2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bilal2vec%2FL2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bilal2vec%2FL2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bilal2vec%2FL2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bilal2vec","download_url":"https://codeload.github.com/bilal2vec/L2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225938718,"owners_count":17548540,"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":["ai","broadcasting","machine-learning","rust","tensor"],"created_at":"2024-08-05T03:02:25.014Z","updated_at":"2024-11-22T17:30:30.593Z","avatar_url":"https://github.com/bilal2vec.png","language":"Rust","readme":"\u003ch1 align='center'\u003e\n    l2 • 🤖\n\u003c/h1\u003e\n\n\u003ch4 align='center'\u003e\n    A Pytorch-style Tensor+Autograd library written in Rust\n\u003c/h4\u003e\n\n\u003cp align='center'\u003e\n    \u003ca href=\"\"\u003e\n        \u003cimg src=\"https://github.com/bilal2vec/l2/workflows/Rust/badge.svg\" alt=\"Rust: CI\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://opensource.org/licenses/MIT\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/License-MIT-yellow.svg\" alt=\"License: MIT\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://crates.io/crates/l2\"\u003e\n        \u003cimg alt=\"crates.io l2 badge\" src=\"http://meritbadge.herokuapp.com/l2\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\" https://docs.rs/l2\"\u003e\n        \u003cimg alt=\"docs.rs l2 badge\" src=\"https://docs.rs/l2/badge.svg\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align='center'\u003e\n    \u003ca href='#installation'\u003eInstallation\u003c/a\u003e •\n    \u003ca href='#contributing'\u003eContributing\u003c/a\u003e •\n    \u003ca href='#authors'\u003eAuthors\u003c/a\u003e •\n    \u003ca href='#license'\u003eLicense\u003c/a\u003e •\n    \u003ca href='#acknowledgements'\u003eAcknowledgements\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cdiv\u003e\n    \u003cimg src=\"./screenshot.png\" /\u003e\n\u003c/div\u003e\n\n\u003cp align='center'\u003e\u003cstrong\u003eMade by \u003ca href='https://github.com/bilal2vec'\u003eBilal Khan\u003c/a\u003e • https://bilal.software\u003c/strong\u003e\u003c/p\u003e\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n-   [What is l2?](#what-is-l2)\n-   [Quick start](#quick-start)\n-   [Design choices](#design-choices)\n-   [Contributing](#contributing)\n-   [Authors](#authors)\n-   [License](#license)\n-   [Acknowledgements](#acknowledgements)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n# What is l2?\n\n\u003e l2 is named after the l2 or Euclidean distance, a popular distance function in deep learning\n\nl2 is a Pytorch-style Tensor+Autograd library written in Rust. It contains a multidimensional array class, `Tensor`, with support for strided arrays, numpy-style array slicing, broadcasting, and most major math operations (including fast, BLAS-accelerated matrix multiplication!). On top of this, l2 has a built-in efficient graph-based autograd engine that keeps track of all operations performed on a tensor and topologically sorts and traverses the graph to compute the gradients.\n\nI also made a more simplified C++ version of l2 last year, which you can take a look at [here](https://github.com/bilal2vec/L2/tree/c%2B%2B)\n\n# Quick start\n\nAdd `l2 = \"1.0.3\"` to your `Cargo.toml` file and add the following to `main.rs`\n\n\u003e Note: L2 will by default use Apple's `acclerate` BLAS library on macOS\n\u003e You can also change the BLAS library that you want to use yourself. Take a look at the [`blas-src`](https://crates.io/crates/blas-src) crate for more information\n\n```rust\nuse l2::tensor::*;\n\nlet x: Tensor = Tensor::normal(\u0026[2, 4], 0.0, 1.0)?;\nlet y: Tensor = Tensor::normal(\u0026[4, 1], 0.0, 1.0)?;\n\nlet z: Tensor = l2::matmul(\u0026x, \u0026y)?;\n\nz.backward();\n\nprintln!(\"{}\", z);\n\n```\n\n# Design choices\n\nI made l2 to get better at using Rust and to learn more about how libraries like Pytorch and Tensorflow work behind the scenes, so don't expect this library to be production-ready :)\n\nl2 is surprisingly fast especially since I didn't try very hard to optimize all the operators, it's usually only less than one order of magnitude slower than Pytorch in most of the benchmarks that I ran. l2 only supports a cpu backend at the moment since I'm not familiar enough with rust to start working with CUDA and cudnn. So far, l2 doesn't have any Pytorch-style abstractions like the Parameter, Layer, or Module classes. There might still be some bugs in the transpose operators and calling `.backward()` on tensors with more dimensions. I was interested in using Rust's [Const Generics](https://github.com/rust-lang/rfcs/blob/master/text/2000-const-generics.md) to run compile-time shape checks but I decided to leave it until some other time.\n\n# Contributing\n\nThis repository is still a work in progress, so if you find a bug, think there is something missing, or have any suggestions for new features, feel free to open an issue or a pull request. Feel free to use the library or code from it in your own projects, and if you feel that some code used in this project hasn't been properly accredited, please open an issue.\n\n# Authors\n\n-   _Bilal Khan_\n\n# License\n\nThis project is licensed under the MIT License - see the [license](LICENSE) file for details\n\n# Acknowledgements\n\nThe fast.ai deep learning from the foundations course (https://course.fast.ai/part2) teaches a lot about how to make your own deep learning library\n\nSome of the resources that I found useful when working on this library include:\n\n-   http://blog.ezyang.com/2019/05/pytorch-internals/\n-   https://pytorch.org/tutorials/beginner/nn_tutorial.html\n-   https://eisenjulian.github.io/deep-learning-in-100-lines/\n-   https://medium.com/@florian.caesar/how-to-create-a-machine-learning-framework-from-scratch-in-491-steps-93428369a4eb\n-   https://medium.com/@johan.mabille/how-we-wrote-xtensor-1-n-n-dimensional-containers-f79f9f4966a7\n-   https://erikpartridge.com/2019-03/rust-ml-simd-blas-lapack\n-   https://medium.com/@GolDDranks/things-rust-doesnt-let-you-do-draft-f596a3c740a5\n-   https://datascience.stackexchange.com/questions/20139/gradients-for-bias-terms-in-backpropagation\n-   https://cs231n.github.io/optimization-2/\n-   https://cs231n.github.io/neural-networks-case-study/#grad\n-   https://stackoverflow.com/questions/38082835/backpropagation-in-gradient-descent-for-neural-networks-vs-linear-regression\n-   https://medium.com/@karpathy/yes-you-should-understand-backprop-e2f06eab496b\n-   https://stackoverflow.com/questions/38082835/backpropagation-in-gradient-descent-for-neural-networks-vs-linear-regression\n-   https://github.com/karpathy/micrograd\n-   https://rufflewind.com/2016-12-30/reverse-mode-automatic-differentiation\n    -   https://github.com/ibab/rust-ad\n    -   https://github.com/Rufflewind/revad/blob/eb3978b3ccdfa8189f3ff59d1ecee71f51c33fd7/revad.py\n    -   https://github.com/srirambandi/ai\n-   https://discuss.pytorch.org/t/is-pytorch-autograd-tape-based/13992/3\n-   https://www.reddit.com/r/MachineLearning/comments/8ep130/d_how_does_autograd_work/\n-   https://github.com/mattjj/autodidact\n-   https://github.com/karpathy/recurrentjs\n-   https://github.com/karpathy/randomfun\n-   https://medium.com/@ralphmao95/simple-autograd-implementation-understand-automatic-differentiation-hand-by-hand-9e86f6d703ab\n-   https://evcu.github.io/ml/autograd/\n-   https://blog.paperspace.com/pytorch-101-understanding-graphs-and-automatic-differentiation/\n-   https://github.com/maciejkula/wyrm\n-   https://medium.com/@maciejkula/building-an-autodifferentiation-library-9ccf32c7a658\n-   https://github.com/evcu/numpy_autograd/blob/master/my_autograd.py#L147\n-   https://github.com/evcu/numpy_autograd/blob/master/Autograd.ipynb\n-   https://cs231n.github.io/optimization-2/\n-   https://github.com/explosion/thinc\n-   https://github.com/joelgrus/joelnet\n-   https://github.com/QuantStack/xtensor\n-   https://github.com/ThinkingTransistor/Sigma\n-   https://github.com/mratsim/Arraymancer\n-   https://github.com/siekmanj/sieknet\n-   https://github.com/siekmanj/sieknet_2.0\n-   https://github.com/Daniel-Liu-c0deb0t/Java-Machine-Learning\n-   https://github.com/karpathy/micrograd\n\nThis README is based on:\n\n-   https://github.com/bilal2vec/pytorch_zoo\n-   https://github.com/bilal2vec/grover\n-   https://github.com/rish-16/gpt2client\n-   https://github.com/mxbi/mlcrate\n-   https://github.com/athityakumar/colorls\n-   https://github.com/amitmerchant1990/electron-markdownify\n\nI used carbon.now.sh with the \"Shades of Purple\" theme for the screenshot at the beginning of this README\n\nThis project contains ~4300 lines of code\n","funding_links":[],"categories":["Rust"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbilal2vec%2FL2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbilal2vec%2FL2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbilal2vec%2FL2/lists"}