From 9938e7a39f2bb4bf70e6c467d7c9152bdff93936 Mon Sep 17 00:00:00 2001 From: Eric Buehler Date: Fri, 25 Apr 2025 23:09:23 -0400 Subject: [PATCH 1/4] Add bench --- .DS_Store | Bin 0 -> 6148 bytes Cargo.lock | 408 +++++++++++++++++++++++++-- constensor-core/Cargo.toml | 8 + constensor-core/benches/cpu_graph.rs | 19 ++ 4 files changed, 406 insertions(+), 29 deletions(-) create mode 100644 .DS_Store create mode 100644 constensor-core/benches/cpu_graph.rs diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..af9a9ebabd2d00962ea4bce2ed4eaee842808142 GIT binary patch literal 6148 zcmeHK%}T>S5Z-NTn^J@v6nb3nTCkN?D_%mZFJMFuDm5Xc24l9QsX3HF&iX<=iO=KA z?&c6IcoWeX*!^bbXE*af_J=XX-BmbX%w~*P&=5H)TLjHpT{RPo$kiM(mIe885yUds zGSP1|;kP$g#(b8tIjesEd$5S3W!CS1@LIjG(`;ESt7F}Jk23c%KU*zbKfT4#m6UN% z>V9w?&GM z^>}pB6URrR&8BVb9~_=tPM_lEM80YwIdHCI*I)^6pj2vl@zNxg$sFuuP8mx`3=jjv z05S0Y7%*pn)%ky=Q^mvpG4KroxIYMJh>pQRquM&4!|OBp8;B^N<68nz7<3F48o>j? zbt<4v<>raObvoFEiE|7V8g)A3YGs(mtXw``xLO_TLWMK#Xr!JPAO@-o)b-HD^Zx>V znXQlfY6^{r0b<~fF~A!WZ{k8x=4|~^9-g%V+5; + let mut graph = Graph::::empty(); + let a = GraphTensor::::rand(&mut graph); + let b = GraphTensor::::rand(&mut graph); + let _c = a.matmul(b); + graph.optimize(); + let compiled = graph.compile::().unwrap(); + c.bench_function("cpu_graph_matmul_128x128", |bencher| { + bencher.iter(|| compiled.run().unwrap()); + }); +} + +criterion_group!(benches, bench_cpu_graph_matmul_128); +criterion_main!(benches); From b1495b5e85fe084a821a3c282c42add61cd14b59 Mon Sep 17 00:00:00 2001 From: Eric Buehler Date: Fri, 25 Apr 2025 23:15:03 -0400 Subject: [PATCH 2/4] Add some benchmarks --- constensor-core/benches/cpu_graph.rs | 34 +++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/constensor-core/benches/cpu_graph.rs b/constensor-core/benches/cpu_graph.rs index 9b7be02..8e70221 100644 --- a/constensor-core/benches/cpu_graph.rs +++ b/constensor-core/benches/cpu_graph.rs @@ -15,5 +15,37 @@ fn bench_cpu_graph_matmul_128(c: &mut Criterion) { }); } -criterion_group!(benches, bench_cpu_graph_matmul_128); +fn bench_cpu_graph_matmul_64(c: &mut Criterion) { + const N: usize = 64; + type Shape = R3<1, N, N>; + let mut graph = Graph::::empty(); + let a = GraphTensor::::rand(&mut graph); + let b = GraphTensor::::rand(&mut graph); + let _c = a.matmul(b); + graph.optimize(); + let compiled = graph.compile::().unwrap(); + c.bench_function("cpu_graph_matmul_64x64", |bencher| { + bencher.iter(|| compiled.run().unwrap()); + }); +} + +fn bench_cpu_graph_matmul_256(c: &mut Criterion) { + const N: usize = 256; + type Shape = R3<1, N, N>; + let mut graph = Graph::::empty(); + let a = GraphTensor::::rand(&mut graph); + let b = GraphTensor::::rand(&mut graph); + let _c = a.matmul(b); + graph.optimize(); + let compiled = graph.compile::().unwrap(); + c.bench_function("cpu_graph_matmul_256x256", |bencher| { + bencher.iter(|| compiled.run().unwrap()); + }); +} + +criterion_group!(benches, + bench_cpu_graph_matmul_64, + bench_cpu_graph_matmul_128, + bench_cpu_graph_matmul_256 +); criterion_main!(benches); From fcc3ebd459020d0b20a18cc8f4d8fd8813eafe2a Mon Sep 17 00:00:00 2001 From: Eric Buehler Date: Fri, 25 Apr 2025 23:18:49 -0400 Subject: [PATCH 3/4] Bench against candle --- Cargo.lock | 585 +++++++++++++++++++++++++-- constensor-core/Cargo.toml | 1 + constensor-core/benches/cpu_graph.rs | 42 +- 3 files changed, 590 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ea770ef..a77dcf9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,12 +29,27 @@ version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" +[[package]] +name = "arbitrary" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dde20b3d026af13f561bdd0f15edf01fc734f0dafcedbaf42bba506a9517f223" +dependencies = [ + "derive_arbitrary", +] + [[package]] name = "autocfg" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bitflags" version = "2.5.0" @@ -73,6 +88,28 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" +[[package]] +name = "candle-core" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef79e00300acaaa6b0b33168e2ed5050f06125d1add87e7103c40a119083581" +dependencies = [ + "byteorder", + "gemm 0.17.1", + "half", + "memmap2", + "num-traits", + "num_cpus", + "rand 0.8.5", + "rand_distr 0.4.3", + "rayon", + "safetensors", + "thiserror", + "ug", + "yoke", + "zip", +] + [[package]] name = "cast" version = "0.3.0" @@ -141,10 +178,11 @@ checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" name = "constensor-core" version = "0.1.1" dependencies = [ + "candle-core", "criterion", "cudarc", "dirs", - "gemm", + "gemm 0.18.2", "half", "num_cpus", "petgraph", @@ -154,6 +192,15 @@ dependencies = [ "thiserror", ] +[[package]] +name = "crc32fast" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" +dependencies = [ + "cfg-if", +] + [[package]] name = "criterion" version = "0.5.1" @@ -231,6 +278,17 @@ dependencies = [ "libloading", ] +[[package]] +name = "derive_arbitrary" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30542c1ad912e0e3d22a1935c290e12e8a29d704a420177a31faad4a601a0800" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "dirs" version = "5.0.1" @@ -252,6 +310,27 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "displaydoc" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "dyn-stack" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56e53799688f5632f364f8fb387488dd05db9fe45db7011be066fc20e7027f8b" +dependencies = [ + "bytemuck", + "reborrow", +] + [[package]] name = "dyn-stack" version = "0.13.0" @@ -297,23 +376,58 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" +[[package]] +name = "gemm" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ab24cc62135b40090e31a76a9b2766a501979f3070fa27f689c27ec04377d32" +dependencies = [ + "dyn-stack 0.10.0", + "gemm-c32 0.17.1", + "gemm-c64 0.17.1", + "gemm-common 0.17.1", + "gemm-f16 0.17.1", + "gemm-f32 0.17.1", + "gemm-f64 0.17.1", + "num-complex", + "num-traits", + "paste", + "raw-cpuid 10.7.0", + "seq-macro", +] + [[package]] name = "gemm" version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab96b703d31950f1aeddded248bc95543c9efc7ac9c4a21fda8703a83ee35451" dependencies = [ - "dyn-stack", - "gemm-c32", - "gemm-c64", - "gemm-common", - "gemm-f16", - "gemm-f32", - "gemm-f64", + "dyn-stack 0.13.0", + "gemm-c32 0.18.2", + "gemm-c64 0.18.2", + "gemm-common 0.18.2", + "gemm-f16 0.18.2", + "gemm-f32 0.18.2", + "gemm-f64 0.18.2", + "num-complex", + "num-traits", + "paste", + "raw-cpuid 11.5.0", + "seq-macro", +] + +[[package]] +name = "gemm-c32" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9c030d0b983d1e34a546b86e08f600c11696fde16199f971cd46c12e67512c0" +dependencies = [ + "dyn-stack 0.10.0", + "gemm-common 0.17.1", "num-complex", "num-traits", "paste", - "raw-cpuid", + "raw-cpuid 10.7.0", "seq-macro", ] @@ -323,12 +437,27 @@ version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6db9fd9f40421d00eea9dd0770045a5603b8d684654816637732463f4073847" dependencies = [ - "dyn-stack", - "gemm-common", + "dyn-stack 0.13.0", + "gemm-common 0.18.2", "num-complex", "num-traits", "paste", - "raw-cpuid", + "raw-cpuid 11.5.0", + "seq-macro", +] + +[[package]] +name = "gemm-c64" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbb5f2e79fefb9693d18e1066a557b4546cd334b226beadc68b11a8f9431852a" +dependencies = [ + "dyn-stack 0.10.0", + "gemm-common 0.17.1", + "num-complex", + "num-traits", + "paste", + "raw-cpuid 10.7.0", "seq-macro", ] @@ -338,13 +467,33 @@ version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfcad8a3d35a43758330b635d02edad980c1e143dc2f21e6fd25f9e4eada8edf" dependencies = [ - "dyn-stack", - "gemm-common", + "dyn-stack 0.13.0", + "gemm-common 0.18.2", + "num-complex", + "num-traits", + "paste", + "raw-cpuid 11.5.0", + "seq-macro", +] + +[[package]] +name = "gemm-common" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2e7ea062c987abcd8db95db917b4ffb4ecdfd0668471d8dc54734fdff2354e8" +dependencies = [ + "bytemuck", + "dyn-stack 0.10.0", + "half", "num-complex", "num-traits", + "once_cell", "paste", - "raw-cpuid", + "pulp 0.18.22", + "raw-cpuid 10.7.0", + "rayon", "seq-macro", + "sysctl 0.5.5", ] [[package]] @@ -354,18 +503,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a352d4a69cbe938b9e2a9cb7a3a63b7e72f9349174a2752a558a8a563510d0f3" dependencies = [ "bytemuck", - "dyn-stack", + "dyn-stack 0.13.0", "half", "libm", "num-complex", "num-traits", "once_cell", "paste", - "pulp", - "raw-cpuid", + "pulp 0.21.4", + "raw-cpuid 11.5.0", + "rayon", + "seq-macro", + "sysctl 0.6.0", +] + +[[package]] +name = "gemm-f16" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ca4c06b9b11952071d317604acb332e924e817bd891bec8dfb494168c7cedd4" +dependencies = [ + "dyn-stack 0.10.0", + "gemm-common 0.17.1", + "gemm-f32 0.17.1", + "half", + "num-complex", + "num-traits", + "paste", + "raw-cpuid 10.7.0", "rayon", "seq-macro", - "sysctl", ] [[package]] @@ -374,30 +541,60 @@ version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cff95ae3259432f3c3410eaa919033cd03791d81cebd18018393dc147952e109" dependencies = [ - "dyn-stack", - "gemm-common", - "gemm-f32", + "dyn-stack 0.13.0", + "gemm-common 0.18.2", + "gemm-f32 0.18.2", "half", "num-complex", "num-traits", "paste", - "raw-cpuid", + "raw-cpuid 11.5.0", "rayon", "seq-macro", ] +[[package]] +name = "gemm-f32" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9a69f51aaefbd9cf12d18faf273d3e982d9d711f60775645ed5c8047b4ae113" +dependencies = [ + "dyn-stack 0.10.0", + "gemm-common 0.17.1", + "num-complex", + "num-traits", + "paste", + "raw-cpuid 10.7.0", + "seq-macro", +] + [[package]] name = "gemm-f32" version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc8d3d4385393304f407392f754cd2dc4b315d05063f62cf09f47b58de276864" dependencies = [ - "dyn-stack", - "gemm-common", + "dyn-stack 0.13.0", + "gemm-common 0.18.2", + "num-complex", + "num-traits", + "paste", + "raw-cpuid 11.5.0", + "seq-macro", +] + +[[package]] +name = "gemm-f64" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa397a48544fadf0b81ec8741e5c0fba0043008113f71f2034def1935645d2b0" +dependencies = [ + "dyn-stack 0.10.0", + "gemm-common 0.17.1", "num-complex", "num-traits", "paste", - "raw-cpuid", + "raw-cpuid 10.7.0", "seq-macro", ] @@ -407,12 +604,12 @@ version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "35b2a4f76ce4b8b16eadc11ccf2e083252d8237c1b589558a49b0183545015bd" dependencies = [ - "dyn-stack", - "gemm-common", + "dyn-stack 0.13.0", + "gemm-common 0.18.2", "num-complex", "num-traits", "paste", - "raw-cpuid", + "raw-cpuid 11.5.0", "seq-macro", ] @@ -541,7 +738,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34" dependencies = [ "cfg-if", - "windows-targets 0.48.5", + "windows-targets 0.52.6", ] [[package]] @@ -556,7 +753,7 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" dependencies = [ - "bitflags", + "bitflags 2.5.0", "libc", ] @@ -572,6 +769,40 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "memmap2" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd3f7eed9d3848f8b98834af67102b720745c4ec028fcd0aa0239277e7de374f" +dependencies = [ + "libc", + "stable_deref_trait", +] + +[[package]] +name = "num" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + [[package]] name = "num-complex" version = "0.4.6" @@ -582,6 +813,37 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -602,6 +864,27 @@ dependencies = [ "libc", ] +[[package]] +name = "num_enum" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e613fc340b2220f734a8595782c551f1250e969d87d3be1ae0579e8d4065179" +dependencies = [ + "num_enum_derive", +] + +[[package]] +name = "num_enum_derive" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "once_cell" version = "1.21.3" @@ -638,6 +921,12 @@ dependencies = [ "serde", ] +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + [[package]] name = "plotters" version = "0.3.7" @@ -675,6 +964,15 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "proc-macro-crate" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edce586971a4dfaa28950c6f18ed55e0406c1ab88bbce2c6f6293a7aaba73d35" +dependencies = [ + "toml_edit", +] + [[package]] name = "proc-macro2" version = "1.0.85" @@ -684,6 +982,18 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "pulp" +version = "0.18.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0a01a0dc67cf4558d279f0c25b0962bd08fc6dec0137699eae304103e882fe6" +dependencies = [ + "bytemuck", + "libm", + "num-complex", + "reborrow", +] + [[package]] name = "pulp" version = "0.21.4" @@ -719,6 +1029,8 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ + "libc", + "rand_chacha 0.3.1", "rand_core 0.6.4", ] @@ -728,10 +1040,20 @@ version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97" dependencies = [ - "rand_chacha", + "rand_chacha 0.9.0", "rand_core 0.9.3", ] +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + [[package]] name = "rand_chacha" version = "0.9.0" @@ -747,6 +1069,9 @@ name = "rand_core" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.15", +] [[package]] name = "rand_core" @@ -777,13 +1102,22 @@ dependencies = [ "rand 0.9.1", ] +[[package]] +name = "raw-cpuid" +version = "10.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c297679cb867470fa8c9f67dbba74a78d78e3e98d7cf2b08d6d71540f797332" +dependencies = [ + "bitflags 1.3.2", +] + [[package]] name = "raw-cpuid" version = "11.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c6df7ab838ed27997ba19a4664507e6f82b41fe6e20be42929332156e5e85146" dependencies = [ - "bitflags", + "bitflags 2.5.0", ] [[package]] @@ -864,6 +1198,16 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" +[[package]] +name = "safetensors" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44560c11236a6130a46ce36c836a62936dc81ebf8c36a37947423571be0e55b6" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "same-file" version = "1.0.6" @@ -911,6 +1255,12 @@ dependencies = [ "serde", ] +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + [[package]] name = "syn" version = "2.0.66" @@ -922,13 +1272,38 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "synstructure" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "sysctl" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec7dddc5f0fee506baf8b9fdb989e242f17e4b11c61dfbb0635b705217199eea" +dependencies = [ + "bitflags 2.5.0", + "byteorder", + "enum-as-inner", + "libc", + "thiserror", + "walkdir", +] + [[package]] name = "sysctl" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "01198a2debb237c62b6826ec7081082d951f46dbb64b0e8c7649a452230d1dfc" dependencies = [ - "bitflags", + "bitflags 2.5.0", "byteorder", "enum-as-inner", "libc", @@ -966,6 +1341,75 @@ dependencies = [ "serde_json", ] +[[package]] +name = "toml_datetime" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3da5db5a963e24bc68be8b17b6fa82814bb22ee8660f192bb182771d498f09a3" + +[[package]] +name = "toml_edit" +version = "0.22.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10558ed0bd2a1562e630926a2d1f0b98c827da99fabd3fe20920a59642504485" +dependencies = [ + "indexmap", + "toml_datetime", + "winnow", +] + +[[package]] +name = "tracing" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" +dependencies = [ + "once_cell", +] + +[[package]] +name = "ug" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03719c61a91b51541f076dfdba45caacf750b230cefaa4b32d6f5411c3f7f437" +dependencies = [ + "gemm 0.18.2", + "half", + "libloading", + "memmap2", + "num", + "num-traits", + "num_cpus", + "rayon", + "safetensors", + "serde", + "thiserror", + "tracing", + "yoke", +] + [[package]] name = "unicode-ident" version = "1.0.12" @@ -1077,7 +1521,7 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "windows-sys 0.48.0", + "windows-sys 0.59.0", ] [[package]] @@ -1219,13 +1663,46 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "winnow" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cb8234a863ea0e8cd7284fcdd4f145233eb00fee02bbdd9861aec44e6477bc5" +dependencies = [ + "memchr", +] + [[package]] name = "wit-bindgen-rt" version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" dependencies = [ - "bitflags", + "bitflags 2.5.0", +] + +[[package]] +name = "yoke" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "120e6aef9aa629e3d4f52dc8cc43a015c7724194c97dfaf45180d2daf2b77f40" +dependencies = [ + "serde", + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2380878cad4ac9aac1e2435f3eb4020e8374b5f13c296cb75b4620ff8e229154" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", ] [[package]] @@ -1247,3 +1724,39 @@ dependencies = [ "quote", "syn", ] + +[[package]] +name = "zerofrom" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zip" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cc23c04387f4da0374be4533ad1208cbb091d5c11d070dfef13676ad6497164" +dependencies = [ + "arbitrary", + "crc32fast", + "crossbeam-utils", + "displaydoc", + "indexmap", + "num_enum", + "thiserror", +] diff --git a/constensor-core/Cargo.toml b/constensor-core/Cargo.toml index 32aa18e..122fa0d 100644 --- a/constensor-core/Cargo.toml +++ b/constensor-core/Cargo.toml @@ -35,6 +35,7 @@ required-features = [] [dev-dependencies] criterion = "0.5" +candle-core = "0.8" [[bench]] name = "cpu_graph" diff --git a/constensor-core/benches/cpu_graph.rs b/constensor-core/benches/cpu_graph.rs index 8e70221..4e62613 100644 --- a/constensor-core/benches/cpu_graph.rs +++ b/constensor-core/benches/cpu_graph.rs @@ -1,3 +1,4 @@ +use candle_core::{Device, Tensor}; use constensor_core::{Cpu, Graph, GraphTensor, R3}; use criterion::{criterion_group, criterion_main, Criterion}; @@ -43,9 +44,46 @@ fn bench_cpu_graph_matmul_256(c: &mut Criterion) { }); } -criterion_group!(benches, +fn bench_candle_matmul_64(c: &mut Criterion) { + const N: usize = 64; + let a = Tensor::rand(0f32, 1f32, &[1, N, N], &Device::Cpu).unwrap(); + let b = Tensor::rand(0f32, 1f32, &[1, N, N], &Device::Cpu).unwrap(); + c.bench_function("candle_matmul_64x64", |bencher| { + bencher.iter(|| { + let _ = a.matmul(&b).unwrap(); + }); + }); +} + +fn bench_candle_matmul_128(c: &mut Criterion) { + const N: usize = 128; + let a = Tensor::rand(0f32, 1f32, &[1, N, N], &Device::Cpu).unwrap(); + let b = Tensor::rand(0f32, 1f32, &[1, N, N], &Device::Cpu).unwrap(); + c.bench_function("candle_matmul_128x128", |bencher| { + bencher.iter(|| { + let _ = a.matmul(&b).unwrap(); + }); + }); +} + +fn bench_candle_matmul_256(c: &mut Criterion) { + const N: usize = 256; + let a = Tensor::rand(0f32, 1f32, &[1, N, N], &Device::Cpu).unwrap(); + let b = Tensor::rand(0f32, 1f32, &[1, N, N], &Device::Cpu).unwrap(); + c.bench_function("candle_matmul_256x256", |bencher| { + bencher.iter(|| { + let _ = a.matmul(&b).unwrap(); + }); + }); +} + +criterion_group!( + benches, bench_cpu_graph_matmul_64, bench_cpu_graph_matmul_128, - bench_cpu_graph_matmul_256 + bench_cpu_graph_matmul_256, + bench_candle_matmul_64, + bench_candle_matmul_128, + bench_candle_matmul_256 ); criterion_main!(benches); From ba565dc0af43a92b8ae39905b8ccc46f661a746b Mon Sep 17 00:00:00 2001 From: Eric Buehler Date: Fri, 25 Apr 2025 23:21:52 -0400 Subject: [PATCH 4/4] Revamp readme --- README.md | 109 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 82 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index bed8379..ad92166 100644 --- a/README.md +++ b/README.md @@ -1,50 +1,105 @@ -

- constensor -

+# constensor -

-Experimental ML framework featuring a graph-based JIT compiler. -

+[![Crates.io](https://img.shields.io/crates/v/constensor-core.svg)](https://crates.io/crates/constensor-core) [![docs.rs](https://docs.rs/constensor-core/badge.svg)](https://docs.rs/constensor-core) [![CI](https://github.com/EricLBuehler/constensor/actions/workflows/ci.yml/badge.svg)](https://github.com/EricLBuehler/constensor/actions) [![License](https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-blue.svg)](LICENSE) -

Docs +Experimental machine learning framework featuring a graph-based JIT compiler. -

+## Features -Constensor is an ML framework which provides the following key features: - -- **Compile time shape, dtype, and device checking**: Develop quickly and handle common errors -- **Opt-in half precision support**: Run on any GPU -- **Advanced AI compiler features:** +- Compile-time shape, dtype, and device checking +- Opt-in half-precision (`f16`) and bfloat16 (`bf16`) support +- Advanced AI compiler optimizations: - Elementwise JIT kernel fusion - - Automatic inplacing + - Automatic inlining and in-placing - Constant folding - - Dead code removal + - Dead code elimination +- Multi-device support (CPU, optional CUDA) +- Graph visualization (requires Graphviz) +- Zero-cost abstractions with idiomatic Rust API + +## Installation + +Add `constensor-core` to your `Cargo.toml`: + +```toml +[dependencies] +constensor-core = "0.1.1" +``` + +To enable optional features (CUDA, half-precision, bfloat16): + +```toml +[dependencies.constensor-core] +version = "0.1.1" +features = ["cuda", "half", "bfloat"] +``` + +Or using `cargo add`: + +```bash +cargo add constensor-core +``` -You can find out more with the [DeepWiki page](https://deepwiki.com/EricLBuehler/constensor)! +## Quick Start ```rust use constensor_core::{Cpu, Graph, GraphTensor, Tensor, R2}; fn main() { + // Create an empty computation graph let mut graph: Graph = Graph::empty(); - let x: GraphTensor, f32, Cpu> = - GraphTensor::, f32, Cpu>::fill(graph.clone(), 1.0); - let y: GraphTensor, f32, Cpu> = - GraphTensor::, f32, Cpu>::fill(graph.clone(), 2.0); - let z: GraphTensor, f32, Cpu> = y.clone() + y * x; + // Define graph tensors + let x = GraphTensor::, f32, Cpu>::fill(&mut graph, 1.0); + let y = GraphTensor::, f32, Cpu>::fill(&mut graph, 2.0); + + // Build computation + let z = y.clone() + y * x; + + // Optimize the graph graph.optimize(); + // Visualize the optimized graph (requires Graphviz) graph.visualize("graph.png").unwrap(); + // Execute and retrieve the result let tensor: Tensor, f32, Cpu> = z.to_tensor().unwrap(); - - assert_eq!(tensor.data().unwrap().to_vec(), vec![vec![4.0; 4]; 3],); + assert_eq!(tensor.data().unwrap().to_vec(), vec![vec![4.0; 4]; 3]); } +``` + +## Examples +Run the provided examples: + +```bash +cargo run --example hello_world --features half +cargo run --example matmul --features cuda,bfloat ``` -## Opt-in half precision support -Via the following feature flags: -- `half` for f16 -- `bfloat` for bf16 \ No newline at end of file +See more examples in [`constensor-core/examples`](constensor-core/examples). + +## Documentation + +API documentation is available on [docs.rs](https://docs.rs/constensor-core). + +## More Info + +- DeepWiki: https://deepwiki.com/EricLBuehler/constensor + +## Contributing + +Contributions are welcome! Please open issues and submit pull requests. + +- Run tests with all features: + + ```bash + cargo test --workspace --features "cuda half bfloat" + ``` + +- Format code: `cargo fmt --all` +- Lint code: `cargo clippy --all-targets -- -D warnings` + +## License + +Licensed under MIT. See [LICENSE](LICENSE) for details.