{"id":21224636,"url":"https://github.com/kurtschelfthout/minidiff","last_synced_at":"2025-07-10T14:31:54.436Z","repository":{"id":60042874,"uuid":"527053718","full_name":"kurtschelfthout/minidiff","owner":"kurtschelfthout","description":"A minimal reference implementation of automatic differentiation in Rust","archived":false,"fork":false,"pushed_at":"2022-09-23T20:46:06.000Z","size":18,"stargazers_count":16,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2023-05-19T08:34:36.848Z","etag":null,"topics":["automatic-differentiation","differentiable-programming","reference-implementation","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kurtschelfthout.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-08-20T22:31:45.000Z","updated_at":"2023-03-10T17:07:52.000Z","dependencies_parsed_at":"2022-09-25T23:31:01.115Z","dependency_job_id":null,"html_url":"https://github.com/kurtschelfthout/minidiff","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kurtschelfthout%2Fminidiff","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kurtschelfthout%2Fminidiff/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kurtschelfthout%2Fminidiff/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kurtschelfthout%2Fminidiff/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kurtschelfthout","download_url":"https://codeload.github.com/kurtschelfthout/minidiff/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225642427,"owners_count":17501312,"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":["automatic-differentiation","differentiable-programming","reference-implementation","rust"],"created_at":"2024-11-20T22:59:25.041Z","updated_at":"2024-11-20T22:59:25.508Z","avatar_url":"https://github.com/kurtschelfthout.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MiniDiff: A minimal reference implementation of automatic differentiation in Rust\n\nCompanion repo for [Automatic Differentiation: From Forward to Reverse in Small Steps](https://getcode.substack.com/p/automatic-differentiation-from-forward?r=1dboko\u0026s=w\u0026utm_campaign=post\u0026utm_medium=web)\n\nMiniDiff implements both forward and reverse mode automatic differentiation, and so enables differentiable programming in Rust.\n\nIt is meant to explain how automatic differentiation works, not as a crate to be used - though someone sufficiently motivated could develop it.\n\nThe development is largely based on \u003chttps://simon.peytonjones.org/provably-correct/\u003e\n\n## Short guide to the repo\n\nRead the article linked above for full details, the tl;dr is:\n\n- [playground.rs](https://github.com/kurtschelfthout/minidiff/blob/main/src/playground.rs): introduces the concept of AD. Standalone, also at this Rust playground: \u003chttps://play.rust-lang.org/?version=stable\u0026mode=debug\u0026edition=2021\u0026gist=3575c2c0ce68498e6f1528855c364e7c\u003e\n- [ad.rs](https://github.com/kurtschelfthout/minidiff/blob/main/src/ad.rs): the implementations, and the intermediate steps from forward to reverse mode AD. If you're only interested in the \"finished products\" (i.e. not the intermediate steps, certainly don't mean they're production ready whatsoever):\n  - [The code marked as Step 1](https://github.com/kurtschelfthout/minidiff/blob/main/src/ad.rs#L8-L151) is forward mode AD\n  - [The code marked as Step 3 and Step 4](https://github.com/kurtschelfthout/minidiff/blob/main/src/ad.rs#L178-L377) *together* is reverse mode AD.\n- [example1.rs](https://github.com/kurtschelfthout/minidiff/blob/main/src/example1.rs) and [example2.rs](https://github.com/kurtschelfthout/minidiff/blob/main/src/example2.rs): Examples of usage of the various steps. Again usage of the \"finished products\":\n  - [Usage of forward mode AD](https://github.com/kurtschelfthout/minidiff/blob/main/src/example1.rs#L5-L37)\n\n    ```rust\n    fn f\u003cT: VectorSpace\u003e(t: \u0026Dual\u003cT\u003e) -\u003e Dual\u003cT\u003e {\n        t.powi(2) + t + Dual::constant(1.0)\n    }\n\n    let res = f(\u0026Dual::new(10.0, 1.0));\n    println!(\"{res:?}\");\n\n    // prints Dual { primal: 111.0, tangent: 21.0 }\n    ```\n\n  - [Usage of reverse mode AD](https://github.com/kurtschelfthout/minidiff/blob/main/src/example1.rs#L97-L104) and [here in example2.rs](https://github.com/kurtschelfthout/minidiff/blob/main/src/example2.rs#L45-L56)\n\n    ```rust\n    fn f_sharing_fixed\u003c'a\u003e(x: \u0026DualTrace\u003c'a\u003e, y: \u0026DualTrace\u003c'a\u003e) -\u003e DualTrace\u003c'a\u003e {\n        let ref s = x * y;\n        s + s\n    }\n\n    fn df_sharing_fixed(x: f64, y: f64) -\u003e Vec\u003cf64\u003e {\n        let trace = Trace::new();\n        let x = \u0026trace.var(x);\n        let y = \u0026trace.var(y);\n        let dual_trace = f_sharing_fixed(x, y);\n        eval(2, \u0026dual_trace)\n    }\n\n    let res = df_sharing_fixed(3.0, 2.0);\n    println!(\"{res:?}\");\n\n    // prints [4.0, 6.0]\n    ```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkurtschelfthout%2Fminidiff","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkurtschelfthout%2Fminidiff","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkurtschelfthout%2Fminidiff/lists"}