-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add rust runtime #1597
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
Merged
Merged
Add rust runtime #1597
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
90d1f2c
Add rust runtime
nhynes 81bcb88
Move files
nhynes f57d622
Re-add build script
nhynes f018244
Add now un-ignored files to tests
nhynes 72aa38b
Add docs
nhynes 0b515c9
Rename numel to size
nhynes ce067be
Address code review comments
nhynes 706c8de
Allow building SGX modules
nhynes a0d3d41
Updates
nhynes 88aff7c
Updates
nhynes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Cargo.lock | ||
target/ | ||
**/*.rs.bk |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
max_width = 100 | ||
hard_tabs = false | ||
tab_spaces = 2 | ||
newline_style = "Auto" | ||
use_small_heuristics = "Default" | ||
indent_style = "Block" | ||
wrap_comments = false | ||
comment_width = 80 | ||
normalize_comments = false | ||
format_strings = false | ||
format_macro_matchers = false | ||
format_macro_bodies = true | ||
empty_item_single_line = true | ||
struct_lit_single_line = true | ||
fn_single_line = false | ||
where_single_line = false | ||
imports_indent = "Block" | ||
imports_layout = "Mixed" | ||
merge_imports = true | ||
reorder_imports = true | ||
reorder_modules = true | ||
reorder_impl_items = false | ||
type_punctuation_density = "Wide" | ||
space_before_colon = false | ||
space_after_colon = true | ||
spaces_around_ranges = false | ||
binop_separator = "Front" | ||
remove_nested_parens = true | ||
combine_control_expr = true | ||
struct_field_align_threshold = 0 | ||
match_arm_blocks = true | ||
force_multiline_blocks = false | ||
fn_args_density = "Tall" | ||
brace_style = "SameLineWhere" | ||
control_brace_style = "AlwaysSameLine" | ||
trailing_semicolon = true | ||
trailing_comma = "Vertical" | ||
match_block_trailing_comma = false | ||
blank_lines_upper_bound = 1 | ||
blank_lines_lower_bound = 0 | ||
edition = "Edition2015" | ||
merge_derives = true | ||
use_try_shorthand = true | ||
use_field_init_shorthand = false | ||
force_explicit_abi = true | ||
condense_wildcard_suffixes = false | ||
color = "Auto" | ||
required_version = "0.99.4" | ||
unstable_features = false | ||
disable_all_formatting = false | ||
skip_children = false | ||
hide_parse_errors = false | ||
error_on_line_overflow = false | ||
error_on_unformatted = false | ||
report_todo = "Never" | ||
report_fixme = "Never" | ||
ignore = [] | ||
emit_mode = "Files" | ||
make_backup = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
language: rust | ||
rust: | ||
- nightly | ||
matrix: | ||
fast_finish: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[package] | ||
name = "tvm" | ||
version = "0.1.0" | ||
license = "Apache-2.0" | ||
description = "TVM Rust runtime" | ||
repository = "https://github.com/dmlc/tvm" | ||
readme = "README.md" | ||
keywords = ["tvm", "nnvm"] | ||
categories = ["api-bindings", "science"] | ||
authors = ["Nick Hynes <[email protected]>"] | ||
|
||
[features] | ||
default = ["nom/std"] | ||
sgx = ["nom/alloc"] | ||
|
||
[dependencies] | ||
bounded-spsc-queue = "0.4.0" | ||
error-chain = { version = "0.12.0", default-features = false } | ||
itertools = "0.7.8" | ||
lazy_static = "1.1.0" | ||
ndarray = "0.11.2" | ||
nom = {version = "4.0.0", default-features = false } | ||
serde = "1.0.59" | ||
serde_derive = "1.0.79" | ||
serde_json = "1.0.17" | ||
|
||
[target.'cfg(not(target_env = "sgx"))'.dependencies] | ||
num_cpus = "1.8.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#[cfg(target_env = "sgx")] | ||
use alloc::alloc; | ||
#[cfg(not(target_env = "sgx"))] | ||
use std::alloc; | ||
use std::num; | ||
|
||
use ndarray; | ||
use serde_json; | ||
|
||
error_chain! { | ||
errors { | ||
TryFromTVMRetValueError(expected: String, actual: i64) { | ||
description("mismatched types while downcasting TVMRetValue") | ||
display("invalid downcast: expected `{}` but was `{}`", expected, actual) | ||
} | ||
|
||
GraphFormatError(msg: String) { | ||
description("unable to load graph") | ||
display("could not load graph json: {}", msg) | ||
} | ||
|
||
LoadGraphParamsError(msg: String) { | ||
description("unable to load graph params") | ||
display("could not load graph params: {}", msg) | ||
} | ||
} | ||
foreign_links { | ||
Alloc(alloc::AllocErr); | ||
GraphDeserialize(serde_json::Error); | ||
ParseInt(num::ParseIntError); | ||
ShapeError(ndarray::ShapeError); | ||
} | ||
} | ||
|
||
impl From<alloc::LayoutErr> for Error { | ||
fn from(_err: alloc::LayoutErr) -> Error { | ||
Error::from_kind(ErrorKind::Msg("Layout error".to_string())) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//! This crate is an implementation of the TVM runtime for modules compiled with `--system-lib`. | ||
//! It's mainly useful for compiling to WebAssembly and SGX, | ||
//! but also native if you prefer Rust to C++. | ||
//! | ||
//! For TVM graphs, the entrypoint to this crate is `runtime::GraphExecutor`. | ||
//! Single-function modules are used via the `packed_func!` macro after obtaining | ||
//! the function from `runtime::SystemLibModule` | ||
//! | ||
//! The main entrypoints to this crate are `GraphExecutor` | ||
//! For examples of use, please refer to the multi-file tests in the `tests` directory. | ||
|
||
#![feature( | ||
alloc, | ||
allocator_api, | ||
box_syntax, | ||
extern_prelude, | ||
fn_traits, | ||
try_from, | ||
unboxed_closures, | ||
vec_remove_item | ||
)] | ||
|
||
#[cfg(target_env = "sgx")] | ||
extern crate alloc; | ||
extern crate bounded_spsc_queue; | ||
#[cfg(target_env = "sgx")] | ||
extern crate core; | ||
#[macro_use] | ||
extern crate error_chain; | ||
#[macro_use] | ||
extern crate itertools; | ||
#[macro_use] | ||
extern crate lazy_static; | ||
extern crate ndarray; | ||
#[macro_use] | ||
extern crate nom; | ||
#[cfg(not(target_env = "sgx"))] | ||
extern crate num_cpus; | ||
extern crate serde; | ||
#[macro_use] | ||
extern crate serde_derive; | ||
extern crate serde_json; | ||
|
||
pub mod ffi { | ||
#![allow( | ||
non_camel_case_types, | ||
non_snake_case, | ||
non_upper_case_globals, | ||
unused | ||
)] | ||
|
||
pub mod runtime { | ||
use std::os::raw::{c_char, c_int, c_void}; | ||
|
||
include!(concat!( | ||
env!("CARGO_MANIFEST_DIR"), | ||
"/src/runtime/c_runtime_api.rs" | ||
)); | ||
|
||
pub type BackendPackedCFunc = | ||
extern "C" fn(args: *const TVMValue, type_codes: *const c_int, num_args: c_int) -> c_int; | ||
} | ||
} | ||
|
||
pub mod errors; | ||
pub mod runtime; | ||
|
||
pub use errors::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#[cfg(target_env = "sgx")] | ||
use alloc::alloc::{self, Layout}; | ||
#[cfg(not(target_env = "sgx"))] | ||
use std::alloc::{self, Layout}; | ||
|
||
use errors::*; | ||
|
||
const DEFAULT_ALIGN_BYTES: usize = 4; | ||
|
||
#[derive(PartialEq, Eq)] | ||
pub struct Allocation { | ||
layout: Layout, | ||
ptr: *mut u8, | ||
} | ||
|
||
impl Allocation { | ||
/// Allocates a chunk of memory of `size` bytes with optional alignment. | ||
pub fn new(size: usize, align: Option<usize>) -> Result<Self> { | ||
let alignment = align.unwrap_or(DEFAULT_ALIGN_BYTES); | ||
let layout = Layout::from_size_align(size, alignment)?; | ||
let ptr = unsafe { alloc::alloc(layout.clone()) }; | ||
if ptr.is_null() { | ||
alloc::handle_alloc_error(layout); | ||
} | ||
Ok(Self { | ||
ptr: ptr, | ||
layout: layout, | ||
}) | ||
} | ||
|
||
pub fn as_mut_ptr(&self) -> *mut u8 { | ||
self.ptr | ||
} | ||
|
||
/// Returns the size of the Allocation in bytes. | ||
pub fn size(&self) -> usize { | ||
self.layout.size() | ||
} | ||
|
||
/// Returns the byte alignment of the Allocation. | ||
pub fn align(&self) -> usize { | ||
self.layout.align() | ||
} | ||
} | ||
|
||
impl Drop for Allocation { | ||
fn drop(&mut self) { | ||
unsafe { | ||
alloc::dealloc(self.ptr, self.layout.clone()); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.