Skip to content

Commit

Permalink
feat: 添加 rust 成功调用 c 算子库的试验代码
Browse files Browse the repository at this point in the history
  • Loading branch information
kilinchange committed Jun 28, 2024
1 parent 60b0403 commit 608a98a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib-operators/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "lib-operators"
version = "0.1.0"
edition = "2021"

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

[build-dependencies]
bindgen = "0.69"

[dependencies]
digit-layout="0.0"
libc = "0.2"
tensor = { path = "../tensor" }
common = { path = "../common" }
34 changes: 34 additions & 0 deletions lib-operators/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::env;
use std::path::PathBuf;

fn main() {
// Tell cargo to tell rustc to link the shared library.
println!("cargo:rustc-link-search=native=/home/duanchenjie/workspace/operators/build/linux/x86_64/release/");
// 链接动态库,不要包含前缀 lib 和后缀 .so
println!("cargo:rustc-link-lib=dylib=operators"); // 动态库名为 liboperators.so
// Link the OpenMP library
println!("cargo:rustc-link-lib=dylib=gomp");

// The bindgen::Builder is the main entry point to bindgen,
// and lets you build up options for the resulting bindings.
let bindings = bindgen::Builder::default()
.header("wrapper.h")
// Generate rust style enums.
.default_enum_style(bindgen::EnumVariation::Rust {
non_exhaustive: true,
})
// Tell cargo to invalidate the built crate whenever the wrapper changes
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
// Disable layout tests because bitfields might cause issues
.layout_tests(false)
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");

// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
6 changes: 6 additions & 0 deletions lib-operators/wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "/home/duanchenjie/workspace/operators/src/ops/swiglu/swiglu.h"
#include "/home/duanchenjie/workspace/operators/src/ops/rotary_embedding/rotary_embedding.h"
#include "/home/duanchenjie/workspace/operators/src/ops/rms_norm/rms_norm.h"
#include "/home/duanchenjie/workspace/operators/src/ops/reform/reform.h"
#include "/home/duanchenjie/workspace/operators/src/ops/matmul/matmul.h"
#include "/home/duanchenjie/workspace/operators/src/ops/causal_softmax/causal_softmax.h"

0 comments on commit 608a98a

Please sign in to comment.