dfdx icon indicating copy to clipboard operation
dfdx copied to clipboard

Using no_std on thumbv6m-none-eabi

Open spadarian opened this issue 1 year ago • 16 comments

I'm trying to use this awesome library for an embedded project (just inference) but I'm having problems compiling it.

error[E0432]: unresolved import `__alloc::sync`
   --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/no-std-compat-0.4.1/src/generated.rs:173:48
    |
173 |     #[cfg(feature = "alloc")] pub use __alloc::sync::*;
    |                                                ^^^^ could not find `sync` in `__alloc`

For more information about this error, try `rustc --explain E0432`.
error: could not compile `no-std-compat` (lib) due to previous error
warning: build failed, waiting for other jobs to finish...
error[E0463]: can't find crate for `std`
  --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/num-traits-0.2.15/src/lib.rs:21:1
   |
21 | extern crate std;
   | ^^^^^^^^^^^^^^^^^ can't find crate
   |
   = note: the `thumbv6m-none-eabi` target may not support the standard library
   = help: consider building the standard library from source with `cargo build -Zbuild-std`

For more information about this error, try `rustc --explain E0463`.

I tried to run cargo build -Zbuild-std as suggested by I still get the error:

error[E0432]: unresolved import `alloc::sync`
 --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/gimli-0.26.2/src/read/dwarf.rs:2:12
  |
2 | use alloc::sync::Arc;
  |            ^^^^ could not find `sync` in `alloc`

It's probably a misconfiguration from my part but I was wondering if you have any suggestions. Thanks!

@antimora (I saw you in some of the no_std related issues :smile:)

spadarian avatar Apr 20 '23 01:04 spadarian

Can you share your Cargo.toml configuration for dfdx?

For no-std you should have both of these:

  1. Disable default features default-features=false
  2. Enable the no-std feature. features = ["no-std", ...], note that this is different from other no_std packages

If both of those are true this seems like a regression for no-std. We have a CI pipeline that's supposed to check for no-std support but there could be a bug with it

coreylowman avatar Apr 20 '23 15:04 coreylowman

Yes, both are true.

Apparently, it is an specific problem with the thumbv6m-none-eabi target as it doesn't have some atomic ops. See discussion here.

In my particular case, I just want to do inference (loading a model at compile time), so no sync would be needed (single thread). No idea how difficult or useful would be to have an inference feature that could support this case.

spadarian avatar Apr 20 '23 23:04 spadarian

Ahhh I see - it seems like something we could fix by enabling the portable_atomic feature of spin?

coreylowman avatar Apr 21 '23 13:04 coreylowman

Maybe...

Also, not sure what the deal would be with rand_distr which uses the std_math feature.

spadarian avatar Apr 23 '23 03:04 spadarian

Hmm yeah adding portable_atomics addresses one compilation issue, but then I start getting these very weird errors:

error[E0463]: can't find crate for `core`
  --> C:\Users\clowm\.cargo\registry\src\index.crates.io-6f17d22bba15001f\libm-0.2.1\src\math\tgamma.rs:25:1
   |
25 | extern crate core;
   | ^^^^^^^^^^^^^^^^^^ can't find crate
   |
   = note: the `thumbv6m-none-eabi` target may not be installed
   = help: consider downloading the target with `rustup target add thumbv6m-none-eabi`
   = help: consider building the standard library from source with `cargo build -Zbuild-std`

error[E0463]: can't find crate for `core`
  --> C:\Users\clowm\.cargo\registry\src\index.crates.io-6f17d22bba15001f\rand_core-0.6.3\src\lib.rs:41:5
   |
41 | use core::convert::AsMut;
   |     ^^^^ can't find crate
   |
   = note: the `thumbv6m-none-eabi` target may not be installed
   = help: consider downloading the target with `rustup target add thumbv6m-none-eabi`
   = help: consider building the standard library from source with `cargo build -Zbuild-std`

I definitely have thumbv6m-none-eabi installed via rustup, and since we are doing no-std I don't think building the std library from scratch is what we want...

coreylowman avatar Apr 24 '23 15:04 coreylowman

Do you know of no_std crates that compile successfully for this target architecture? We might be able to copy the configuration from there

coreylowman avatar Apr 24 '23 15:04 coreylowman

I'm using:

  • tinyrand
  • num-traits with default features disabled and libm

spadarian avatar Apr 25 '23 01:04 spadarian

Got it thanks! So it looks like with some feature wrangling I was able to reduce the issues I was seeing, and now am just encountering issues with spin. I created an issue on their repo asking for more info here https://github.com/mvdnes/spin-rs/issues/151.

coreylowman avatar Apr 25 '23 13:04 coreylowman

Thanks mate. I tried what they suggested (portable_atomic_unsafe_assume_single_core) but it didn't work either... I don't remember the error but I will post it when I get to work.

spadarian avatar Apr 25 '23 13:04 spadarian

This is the final error I'm getting with the fixes for the target:

error[E0432]: unresolved import `__alloc::sync`
   --> C:\Users\clowm\.cargo\registry\src\github.com-1ecc6299db9ec823\no-std-compat-0.4.1\src\generated.rs:173:48
    |
173 |     #[cfg(feature = "alloc")] pub use __alloc::sync::*;
    |                                                ^^^^ could not find `sync` in `__alloc`

Which is with no-std-compat 🙄

This is with an empty project with Cargo.toml:

[package]
name = "no-std-test"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
dfdx = { path = "../dfdx", default-features = false, features = ["no-std"]}

and .cargo/config:

[target.thumbv6m-none-eabi]
rustflags = ["--cfg", "portable_atomic_unsafe_assume_single_core"]

Command to build is:

cargo build --target thumbv6m-none-eabi

coreylowman avatar Apr 25 '23 13:04 coreylowman

Yes... that was the error.

spadarian avatar Apr 25 '23 13:04 spadarian

Found a related issue on no-std-compat https://gitlab.com/jD91mZM2/no-std-compat/-/issues/6

coreylowman avatar Apr 25 '23 15:04 coreylowman

Okay so I tested out on the main branch of no-std-compat with this dependency:

no-std-compat = { git = "https://gitlab.com/jD91mZM2/no-std-compat.git", branch = "master", default-features = false, features = [ "alloc", "compat_hash" ], optional = true }

This addresses the above error, but it seems like it doesn't include std::sync at all (see errors below). I'm not sure what this means for compiling dfdx on this target. 🤔

error[E0432]: unresolved import `std::sync::Arc`
 --> C:\Users\clowm\Documents\programming\dfdx\src\optim\adam\mod.rs:6:32
  |
6 | use std::{marker::PhantomData, sync::Arc};
  |                                ^^^^^^^^^ no `Arc` in `generated::sync`

error[E0432]: unresolved import `std::sync::Arc`
 --> C:\Users\clowm\Documents\programming\dfdx\src\optim\rmsprop\mod.rs:6:32
  |
6 | use std::{marker::PhantomData, sync::Arc};
  |                                ^^^^^^^^^ no `Arc` in `generated::sync`

error[E0432]: unresolved import `std::sync::Arc`
  --> C:\Users\clowm\Documents\programming\dfdx\src\tensor\cpu\allocate.rs:11:11
   |
11 | use std::{sync::Arc, vec::Vec};
   |           ^^^^^^^^^ no `Arc` in `generated::sync`

coreylowman avatar Apr 25 '23 15:04 coreylowman

I think we may be able to get aroudn this with no-std-compat's "compat_sync" feature, which adds std::sync bindings. However it is currently using a very old version of spin (0.7.0)

coreylowman avatar Apr 25 '23 15:04 coreylowman

Could you make no-std-compat work with thumbv6m-none-eabi in the end? It is also blocking me to use some crates.

alisomay avatar Aug 08 '23 20:08 alisomay

No this is a known issue in the no-std-compat crate: https://gitlab.com/jD91mZM2/no-std-compat/-/issues/6

coreylowman avatar Aug 10 '23 14:08 coreylowman