Skip to content

Commit 17de832

Browse files
Make compatible with thumbv6m-none-eabi + add raspberry pi pico example (#2096)
* Made compatible with thumbv6m-none-eabi * Added example of no_std on rp2040 * Added documentation on usage in no_std * Rename rp2040 example and add README.md
1 parent 48a64d3 commit 17de832

File tree

27 files changed

+6173
-155
lines changed

27 files changed

+6173
-155
lines changed

Cargo.lock

+132-135
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+12-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ members = [
1616

1717
exclude = [
1818
"examples/notebook",
19+
"examples/raspberry-pi-pico", # will cause dependency building issues otherwise
20+
# "crates/burn-cuda", # comment this line to work on burn-cuda
1921
]
2022

2123
[workspace.package]
@@ -72,7 +74,11 @@ serde_bytes = { version = "0.11.15", default-features = false, features = [
7274
] } # alloc for no_std
7375
serde_rusqlite = "0.35.0"
7476
serial_test = "3.1.1"
75-
spin = { version = "0.9.8", features = ["mutex", "spin_mutex"] }
77+
spin = { version = "0.9.8", features = [
78+
"mutex",
79+
"spin_mutex",
80+
"portable-atomic",
81+
] }
7682
strum = "0.26.3"
7783
strum_macros = "0.26.4"
7884
syn = { version = "2.0.74", features = ["full", "extra-traits"] }
@@ -118,7 +124,7 @@ half = { version = "2.4.1", features = [
118124
"num-traits",
119125
"serde",
120126
], default-features = false }
121-
ndarray = { version = "0.15.6", default-features = false }
127+
ndarray = { version = "0.16.0", default-features = false }
122128
matrixmultiply = { version = "0.3.9", default-features = false }
123129
openblas-src = "0.10.9"
124130
blas-src = { version = "0.10.0", default-features = false }
@@ -142,9 +148,11 @@ nvml-wrapper = "0.10.0"
142148
sysinfo = "0.30.13"
143149
systemstat = "0.2.3"
144150

151+
portable-atomic-util = { version = "0.2.2", features = ["alloc"] }
152+
145153
### For the main burn branch. ###
146-
cubecl = { git = "https://github.com/tracel-ai/cubecl", default-features = false, rev = "bee7886b5c3016c425d244136f77442655097f3e" }
147-
cubecl-common = { git = "https://github.com/tracel-ai/cubecl", default-features = false, rev = "bee7886b5c3016c425d244136f77442655097f3e" }
154+
cubecl = { git = "https://github.com/tracel-ai/cubecl", default-features = false, rev = "034f667da6e92a81b7da9f303e8507db944cc2a4" }
155+
cubecl-common = { git = "https://github.com/tracel-ai/cubecl", default-features = false, rev = "034f667da6e92a81b7da9f303e8507db944cc2a4" }
148156
### For local development. ###
149157
# cubecl = { path = "../cubecl/crates/cubecl" }
150158
# cubecl-common = { path = "../cubecl/crates/cubecl-common" }

NOTICES.md

+237
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,240 @@ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
336336
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
337337
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
338338
DEALINGS IN THE SOFTWARE.
339+
340+
## RP
341+
342+
**Source**:
343+
- https://github.com/embassy-rs/embassy/blob/main/examples/rp/Cargo.toml
344+
- https://github.com/embassy-rs/embassy/blob/main/examples/rp/build.rs
345+
- https://github.com/embassy-rs/embassy/blob/main/examples/rp/memory.x
346+
347+
Apache License
348+
Version 2.0, January 2004
349+
http://www.apache.org/licenses/
350+
351+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
352+
353+
1. Definitions.
354+
355+
"License" shall mean the terms and conditions for use, reproduction,
356+
and distribution as defined by Sections 1 through 9 of this document.
357+
358+
"Licensor" shall mean the copyright owner or entity authorized by
359+
the copyright owner that is granting the License.
360+
361+
"Legal Entity" shall mean the union of the acting entity and all
362+
other entities that control, are controlled by, or are under common
363+
control with that entity. For the purposes of this definition,
364+
"control" means (i) the power, direct or indirect, to cause the
365+
direction or management of such entity, whether by contract or
366+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
367+
outstanding shares, or (iii) beneficial ownership of such entity.
368+
369+
"You" (or "Your") shall mean an individual or Legal Entity
370+
exercising permissions granted by this License.
371+
372+
"Source" form shall mean the preferred form for making modifications,
373+
including but not limited to software source code, documentation
374+
source, and configuration files.
375+
376+
"Object" form shall mean any form resulting from mechanical
377+
transformation or translation of a Source form, including but
378+
not limited to compiled object code, generated documentation,
379+
and conversions to other media types.
380+
381+
"Work" shall mean the work of authorship, whether in Source or
382+
Object form, made available under the License, as indicated by a
383+
copyright notice that is included in or attached to the work
384+
(an example is provided in the Appendix below).
385+
386+
"Derivative Works" shall mean any work, whether in Source or Object
387+
form, that is based on (or derived from) the Work and for which the
388+
editorial revisions, annotations, elaborations, or other modifications
389+
represent, as a whole, an original work of authorship. For the purposes
390+
of this License, Derivative Works shall not include works that remain
391+
separable from, or merely link (or bind by name) to the interfaces of,
392+
the Work and Derivative Works thereof.
393+
394+
"Contribution" shall mean any work of authorship, including
395+
the original version of the Work and any modifications or additions
396+
to that Work or Derivative Works thereof, that is intentionally
397+
submitted to Licensor for inclusion in the Work by the copyright owner
398+
or by an individual or Legal Entity authorized to submit on behalf of
399+
the copyright owner. For the purposes of this definition, "submitted"
400+
means any form of electronic, verbal, or written communication sent
401+
to the Licensor or its representatives, including but not limited to
402+
communication on electronic mailing lists, source code control systems,
403+
and issue tracking systems that are managed by, or on behalf of, the
404+
Licensor for the purpose of discussing and improving the Work, but
405+
excluding communication that is conspicuously marked or otherwise
406+
designated in writing by the copyright owner as "Not a Contribution."
407+
408+
"Contributor" shall mean Licensor and any individual or Legal Entity
409+
on behalf of whom a Contribution has been received by Licensor and
410+
subsequently incorporated within the Work.
411+
412+
2. Grant of Copyright License. Subject to the terms and conditions of
413+
this License, each Contributor hereby grants to You a perpetual,
414+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
415+
copyright license to reproduce, prepare Derivative Works of,
416+
publicly display, publicly perform, sublicense, and distribute the
417+
Work and such Derivative Works in Source or Object form.
418+
419+
3. Grant of Patent License. Subject to the terms and conditions of
420+
this License, each Contributor hereby grants to You a perpetual,
421+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
422+
(except as stated in this section) patent license to make, have made,
423+
use, offer to sell, sell, import, and otherwise transfer the Work,
424+
where such license applies only to those patent claims licensable
425+
by such Contributor that are necessarily infringed by their
426+
Contribution(s) alone or by combination of their Contribution(s)
427+
with the Work to which such Contribution(s) was submitted. If You
428+
institute patent litigation against any entity (including a
429+
cross-claim or counterclaim in a lawsuit) alleging that the Work
430+
or a Contribution incorporated within the Work constitutes direct
431+
or contributory patent infringement, then any patent licenses
432+
granted to You under this License for that Work shall terminate
433+
as of the date such litigation is filed.
434+
435+
4. Redistribution. You may reproduce and distribute copies of the
436+
Work or Derivative Works thereof in any medium, with or without
437+
modifications, and in Source or Object form, provided that You
438+
meet the following conditions:
439+
440+
(a) You must give any other recipients of the Work or
441+
Derivative Works a copy of this License; and
442+
443+
(b) You must cause any modified files to carry prominent notices
444+
stating that You changed the files; and
445+
446+
(c) You must retain, in the Source form of any Derivative Works
447+
that You distribute, all copyright, patent, trademark, and
448+
attribution notices from the Source form of the Work,
449+
excluding those notices that do not pertain to any part of
450+
the Derivative Works; and
451+
452+
(d) If the Work includes a "NOTICE" text file as part of its
453+
distribution, then any Derivative Works that You distribute must
454+
include a readable copy of the attribution notices contained
455+
within such NOTICE file, excluding those notices that do not
456+
pertain to any part of the Derivative Works, in at least one
457+
of the following places: within a NOTICE text file distributed
458+
as part of the Derivative Works; within the Source form or
459+
documentation, if provided along with the Derivative Works; or,
460+
within a display generated by the Derivative Works, if and
461+
wherever such third-party notices normally appear. The contents
462+
of the NOTICE file are for informational purposes only and
463+
do not modify the License. You may add Your own attribution
464+
notices within Derivative Works that You distribute, alongside
465+
or as an addendum to the NOTICE text from the Work, provided
466+
that such additional attribution notices cannot be construed
467+
as modifying the License.
468+
469+
You may add Your own copyright statement to Your modifications and
470+
may provide additional or different license terms and conditions
471+
for use, reproduction, or distribution of Your modifications, or
472+
for any such Derivative Works as a whole, provided Your use,
473+
reproduction, and distribution of the Work otherwise complies with
474+
the conditions stated in this License.
475+
476+
5. Submission of Contributions. Unless You explicitly state otherwise,
477+
any Contribution intentionally submitted for inclusion in the Work
478+
by You to the Licensor shall be under the terms and conditions of
479+
this License, without any additional terms or conditions.
480+
Notwithstanding the above, nothing herein shall supersede or modify
481+
the terms of any separate license agreement you may have executed
482+
with Licensor regarding such Contributions.
483+
484+
6. Trademarks. This License does not grant permission to use the trade
485+
names, trademarks, service marks, or product names of the Licensor,
486+
except as required for reasonable and customary use in describing the
487+
origin of the Work and reproducing the content of the NOTICE file.
488+
489+
7. Disclaimer of Warranty. Unless required by applicable law or
490+
agreed to in writing, Licensor provides the Work (and each
491+
Contributor provides its Contributions) on an "AS IS" BASIS,
492+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
493+
implied, including, without limitation, any warranties or conditions
494+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
495+
PARTICULAR PURPOSE. You are solely responsible for determining the
496+
appropriateness of using or redistributing the Work and assume any
497+
risks associated with Your exercise of permissions under this License.
498+
499+
8. Limitation of Liability. In no event and under no legal theory,
500+
whether in tort (including negligence), contract, or otherwise,
501+
unless required by applicable law (such as deliberate and grossly
502+
negligent acts) or agreed to in writing, shall any Contributor be
503+
liable to You for damages, including any direct, indirect, special,
504+
incidental, or consequential damages of any character arising as a
505+
result of this License or out of the use or inability to use the
506+
Work (including but not limited to damages for loss of goodwill,
507+
work stoppage, computer failure or malfunction, or any and all
508+
other commercial damages or losses), even if such Contributor
509+
has been advised of the possibility of such damages.
510+
511+
9. Accepting Warranty or Additional Liability. While redistributing
512+
the Work or Derivative Works thereof, You may choose to offer,
513+
and charge a fee for, acceptance of support, warranty, indemnity,
514+
or other liability obligations and/or rights consistent with this
515+
License. However, in accepting such obligations, You may act only
516+
on Your own behalf and on Your sole responsibility, not on behalf
517+
of any other Contributor, and only if You agree to indemnify,
518+
defend, and hold each Contributor harmless for any liability
519+
incurred by, or claims asserted against, such Contributor by reason
520+
of your accepting any such warranty or additional liability.
521+
522+
END OF TERMS AND CONDITIONS
523+
524+
APPENDIX: How to apply the Apache License to your work.
525+
526+
To apply the Apache License to your work, attach the following
527+
boilerplate notice, with the fields enclosed by brackets "[]"
528+
replaced with your own identifying information. (Don't include
529+
the brackets!) The text should be enclosed in the appropriate
530+
comment syntax for the file format. We also recommend that a
531+
file or class name and description of purpose be included on the
532+
same "printed page" as the copyright notice for easier
533+
identification within third-party archives.
534+
535+
Copyright (c) Embassy project contributors
536+
537+
Licensed under the Apache License, Version 2.0 (the "License");
538+
you may not use this file except in compliance with the License.
539+
You may obtain a copy of the License at
540+
541+
http://www.apache.org/licenses/LICENSE-2.0
542+
543+
Unless required by applicable law or agreed to in writing, software
544+
distributed under the License is distributed on an "AS IS" BASIS,
545+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
546+
See the License for the specific language governing permissions and
547+
limitations under the License.
548+
549+
MIT license
550+
551+
Copyright (c) Embassy project contributors
552+
553+
Permission is hereby granted, free of charge, to any
554+
person obtaining a copy of this software and associated
555+
documentation files (the "Software"), to deal in the
556+
Software without restriction, including without
557+
limitation the rights to use, copy, modify, merge,
558+
publish, distribute, sublicense, and/or sell copies of
559+
the Software, and to permit persons to whom the Software
560+
is furnished to do so, subject to the following
561+
conditions:
562+
563+
The above copyright notice and this permission notice
564+
shall be included in all copies or substantial portions
565+
of the Software.
566+
567+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
568+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
569+
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
570+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
571+
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
572+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
573+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
574+
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
575+
DEALINGS IN THE SOFTWARE.

burn-book/book.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ authors = [
66
"Dilshod Tadjibaev",
77
"Guillaume Lagrange",
88
"Sylvain Benner",
9+
"Bjorn Beishline"
910
]
1011
language = "en"
1112
multilingual = false

burn-book/src/SUMMARY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
- [Custom WGPU Kernel](./advanced/backend-extension/custom-wgpu-kernel.md)
3232
- [Custom Optimizer]()
3333
- [WebAssembly]()
34-
- [No-Std]()
34+
- [No-Std](./advanced/no-std.md)

burn-book/src/advanced/no-std.md

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# No Standard Library
2+
3+
In this section, you will learn how to run an onnx inference model on an embedded system, with no standard library support on a Raspberry Pi Pico. This should be universally applicable to other platforms. All the code can be found under the
4+
[examples directory](https://github.com/tracel-ai/burn/tree/main/examples/raspberry-pi-pico).
5+
6+
## Step-by-Step Guide
7+
8+
Let's walk through the process of running an embedded ONNX model:
9+
10+
### Setup
11+
Follow the [embassy guide](https://embassy.dev/book/#_getting_started) for your specific environment. Once setup, you should have something similar to the following.
12+
```
13+
./inference
14+
├── Cargo.lock
15+
├── Cargo.toml
16+
├── build.rs
17+
├── memory.x
18+
└── src
19+
└── main.rs
20+
```
21+
22+
Some other dependencies have to be added
23+
```toml
24+
[dependencies]
25+
embedded-alloc = "0.5.1" # Only if there is no default allocator for your chip
26+
burn = { version = "0.14", default-features = false, features = ["ndarray"] } # Backend must be ndarray
27+
28+
[build-dependencies]
29+
burn-import = { version = "0.14" } # Used to auto generate the rust code to import the model
30+
```
31+
32+
### Import the Model
33+
Follow the directions to [import models](./import/README.md).
34+
35+
Use the following ModelGen config
36+
```rs
37+
ModelGen::new()
38+
.input(my_model)
39+
.out_dir("model/")
40+
.record_type(RecordType::Bincode)
41+
.embed_states(true)
42+
.run_from_script();
43+
```
44+
45+
### Global Allocator
46+
First define a global allocator (if you are on a no_std system without alloc).
47+
48+
```rs
49+
use embedded_alloc::Heap;
50+
51+
#[global_allocator]
52+
static HEAP: Heap = Heap::empty();
53+
54+
#[embassy_executor::main]
55+
async fn main(_spawner: Spawner) {
56+
{
57+
use core::mem::MaybeUninit;
58+
const HEAP_SIZE: usize = 100 * 1024; // This is dependent on the model size in memory.
59+
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
60+
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
61+
}
62+
}
63+
```
64+
65+
### Define Backend
66+
We are using ndarray, so we just need to define the NdArray backend as usual
67+
```rs
68+
use burn::{backend::NdArray, tensor::Tensor};
69+
70+
type Backend = NdArray<f32>;
71+
type BackendDeice = <Backend as burn::tensor::backend::Backend>::Device;
72+
```
73+
74+
Then inside the `main` function add
75+
```rs
76+
use your_model::Model;
77+
78+
// Get a default device for the backend
79+
let device = BackendDeice::default();
80+
81+
// Create a new model and load the state
82+
let model: Model<Backend> = Model::default();
83+
```
84+
85+
### Running the Model
86+
To run the model, just call it as you would normally
87+
```rs
88+
// Define the tensor
89+
let input = Tensor::<Backend, 2>::from_floats([[input]], &device);
90+
91+
// Run the model on the input
92+
let output = model.forward(input);
93+
```
94+
95+
## Conclusion
96+
Running a model in a no_std environment is pretty much identical to a normal environment. All that is needed is a global allocator.

0 commit comments

Comments
 (0)