diff --git a/.envrc b/.envrc new file mode 100644 index 00000000..3550a30f --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore index 58b46da6..11b88e85 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,7 @@ scripts/sgemm_baseline_*.csv *.tmp .env private/ + +# Nix +**/.direnv +**/result diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..45c60976 --- /dev/null +++ b/flake.lock @@ -0,0 +1,101 @@ +{ + "nodes": { + "crane": { + "locked": { + "lastModified": 1783203018, + "narHash": "sha256-G6R9IT/xwFuu+CYBWDUAok6AdC4ERC4ZfPPFtEpxnZE=", + "owner": "ipetkov", + "repo": "crane", + "rev": "80db5bdc391be8a1794f6d8a2d56e3a84ebcede2", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "flake-parts": { + "inputs": { + "nixpkgs-lib": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1782949081, + "narHash": "sha256-vp6Y/Grm98ESt6ceOkWiHWyZRDV3J1RID4w+6NWK9yA=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "17c9d6cdfc60c64f4ee8d306f9bc0b4ccb51481e", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "import-tree": { + "locked": { + "lastModified": 1778781969, + "narHash": "sha256-Jjuz5CmSkur8KvLDoGa+vylEp+RkQtv4mt/qcMznpH0=", + "owner": "vic", + "repo": "import-tree", + "rev": "d321337efd0f23a9eb14a42adb7b2c29313ab274", + "type": "github" + }, + "original": { + "owner": "vic", + "repo": "import-tree", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1783446445, + "narHash": "sha256-Zc6g07ESYTTW6xnQ/myoPtiW4gZrSokgz9Dig8FEOIE=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "88ee2ba1409ce4a2f8ea8f1af8e07ebd7844b167", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "master", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "crane": "crane", + "flake-parts": "flake-parts", + "import-tree": "import-tree", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1783404876, + "narHash": "sha256-DAh1CfiRVwr8Szkj5PiHmsqh10NHPb8SKPx7w/B+l9E=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "3c161f20193bd91a73913b417e5b06d41860336d", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..daf64853 --- /dev/null +++ b/flake.nix @@ -0,0 +1,32 @@ +{ + description = "Rust-native framework for Edge AI CV inference"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/master"; + flake-parts = { + url = "github:hercules-ci/flake-parts"; + inputs.nixpkgs-lib.follows = "nixpkgs"; + }; + import-tree = { + url = "github:vic/import-tree"; + }; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + crane.url = "github:ipetkov/crane"; + }; + + outputs = + inputs: + inputs.flake-parts.lib.mkFlake { inherit inputs; } { + systems = [ + "x86_64-linux" + "aarch64-linux" + "aarch64-darwin" + ]; + imports = [ + (inputs.import-tree [ ./rust.nix ]) + ]; + }; +} diff --git a/rust.nix b/rust.nix new file mode 100644 index 00000000..5b6a4a63 --- /dev/null +++ b/rust.nix @@ -0,0 +1,105 @@ +{ inputs, ... }: { + perSystem = + { + pkgs, + lib, + self', + system, + ... + }: + let + isLinux = pkgs.stdenv.isLinux; + isDarwin = pkgs.stdenv.isDarwin; + isAarch64 = pkgs.stdenv.hostPlatform.isAarch64; + isX86_64 = pkgs.stdenv.hostPlatform.isx86_64; + + craneLib = (inputs.crane.mkLib pkgs).overrideToolchain ( + p: + p.rust-bin.stable.latest.default.override { + extensions = [ + "rust-analyzer" + "rust-src" + "rustfmt" + "clippy" + ]; + } + ); + + commonArgs = { + inherit (craneLib.crateNameFromCargoToml { cargoToml = ./Cargo.toml; }) version; + + # This should be filtered, but there are a lot of useful extensions for tests + src = ./.; + strictDeps = true; + + nativeBuildInputs = + with pkgs; + [ + pkg-config + protobuf + rustPlatform.bindgenHook + ] + ++ lib.optionals isLinux [ + linuxHeaders + ]; + + buildInputs = lib.optionals isLinux [ + pkgs.openblas + # TODO : mkl, armpl + ]; + + env = { + PROTOC = "${pkgs.protobuf}/bin/protoc"; + OPENBLAS_DIR = lib.optionalString isLinux "${pkgs.openblas}"; + }; + }; + in + { + _module.args.pkgs = import inputs.nixpkgs { + inherit system; + + overlays = [ + inputs.rust-overlay.overlays.default + ]; + }; + + checks = + let + testFeatures = [ + "blas" + "gpu" + "native-camera" + ] + ++ lib.optionals isDarwin [ + "metal-backend" + ] + ++ lib.optionals (isLinux && isAarch64) [ + "rknn" + # "armpl" + ] + ++ lib.optionals (isLinux && isX86_64) [ + # "mkl" + ]; + + mkTest = feature: { + name = "test-${feature}"; + value = craneLib.cargoTest ( + commonArgs + // { + pname = "yscv-${feature}"; + cargoExtraArgs = "--workspace --features ${feature}"; + + cargoArtifacts = null; + } + ); + }; + in + builtins.listToAttrs (map mkTest testFeatures); + + devShells.default = craneLib.devShell { + inherit (commonArgs) env; + + packages = commonArgs.buildInputs ++ commonArgs.nativeBuildInputs; + }; + }; +}