Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using no_std on thumbv6m-none-eabi #718

Open
Tracked by #743
spadarian opened this issue Apr 20, 2023 · 16 comments
Open
Tracked by #743

Using no_std on thumbv6m-none-eabi #718

spadarian opened this issue Apr 20, 2023 · 16 comments

Comments

@spadarian
Copy link

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 😄)

@coreylowman
Copy link
Owner

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

@spadarian
Copy link
Author

spadarian commented Apr 20, 2023

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.

@coreylowman
Copy link
Owner

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

@spadarian
Copy link
Author

Maybe...

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

@coreylowman
Copy link
Owner

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
Copy link
Owner

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

@spadarian
Copy link
Author

I'm using:

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

@coreylowman
Copy link
Owner

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 mvdnes/spin-rs#151.

@spadarian
Copy link
Author

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.

@coreylowman
Copy link
Owner

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

@spadarian
Copy link
Author

Yes... that was the error.

@coreylowman
Copy link
Owner

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

@coreylowman
Copy link
Owner

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
Copy link
Owner

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 coreylowman mentioned this issue Apr 27, 2023
8 tasks
@coreylowman coreylowman changed the title Using no_std on embedded Using no_std on thumbv6m-none-eabi Apr 28, 2023
@alisomay
Copy link

alisomay commented Aug 8, 2023

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

@coreylowman
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants