diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 619415f..eecc55e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -35,7 +35,7 @@ jobs: sudo apt update && sudo apt install software-properties-common git clone https://github.com/WasmEdge/WasmEdge /tmp/WasmEdge mkdir /tmp/build && cd /tmp/build - cmake /tmp/WasmEdge -DCMAKE_BUILD_TYPE=Release -DWASMEDGE_USE_LLVM=OFF -DWASMEDGE_PLUGIN_LLM=ON -DWASMEDGE_BUILD_TESTS=OFF + cmake /tmp/WasmEdge -DCMAKE_BUILD_TYPE=Release -DWASMEDGE_USE_LLVM=OFF -DWASMEDGE_PLUGIN_LLMC=ON -DWASMEDGE_BUILD_TESTS=OFF make -j$(nproc) && sudo make install - name: Train GPT2 run: | diff --git a/README.md b/README.md index 8d554f2..942ec76 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A Rust library for using llm.c functions when the Wasi is being executed on Wasm ```bash git clone https://github.com/WasmEdge/WasmEdge.git cd WasmEdge -cmake -GNinja -Bbuild -DCMAKE_BUILD_TYPE=Release -DWASMEDGE_BUILD_TESTS=OFF -DWASMEDGE_PLUGIN_LLM=On +cmake -GNinja -Bbuild -DCMAKE_BUILD_TYPE=Release -DWASMEDGE_BUILD_TESTS=OFF -DWASMEDGE_PLUGIN_LLMC=ON cmake --build build cmake --install build ``` @@ -20,6 +20,7 @@ wget -P /tmp/data/ https://huggingface.co/datasets/karpathy/llmc-starter-pack/re ## Run the example ```bash +cd example cargo build --target wasm32-wasi --release -wasmedge --dir .:. ./target/wasm32-wasi/release/wasmedge_stable_diffusion_example.wasm +wasmedge --dir .:. ./target/wasm32-wasi/release/example.wasm ``` diff --git a/wasmedge-llmc/src/lib.rs b/wasmedge-llmc/src/lib.rs index 0806a29..530511e 100644 --- a/wasmedge-llmc/src/lib.rs +++ b/wasmedge-llmc/src/lib.rs @@ -45,7 +45,7 @@ impl ConfigBuilder { } impl Model { - pub fn from_checkpoints(checkpoint_path: &str) -> Result { + pub fn from_checkpoints(checkpoint_path: &str) -> Result { let mut model_id = MaybeUninit::::uninit(); unsafe { let result = model_create(checkpoint_path, model_id.as_mut_ptr()); @@ -64,7 +64,7 @@ impl Model { val_data_loader: DataLoader, tokenizer: Tokenizer, config: Config, - ) -> Result<(), WasmedgeLLMErrno> { + ) -> Result<(), WasmedgeLLMCErrno> { unsafe { model_train( self.id, @@ -94,7 +94,7 @@ impl DataLoader { process_rank: u32, num_processes: u32, should_shuffle: bool, - ) -> Result { + ) -> Result { let mut dataloader_id = MaybeUninit::::uninit(); unsafe { let result = dataloader_create( @@ -123,7 +123,7 @@ pub struct Tokenizer { } impl Tokenizer { - pub fn from_file(filepath: &str) -> Result { + pub fn from_file(filepath: &str) -> Result { let mut tokenizer_id = MaybeUninit::::uninit(); unsafe { let result = tokenizer_create(filepath, tokenizer_id.as_mut_ptr()); diff --git a/wasmedge-llmc/src/llmc_interface.rs b/wasmedge-llmc/src/llmc_interface.rs index e751535..8f4d1f4 100644 --- a/wasmedge-llmc/src/llmc_interface.rs +++ b/wasmedge-llmc/src/llmc_interface.rs @@ -2,11 +2,11 @@ use core::fmt; use std::error::Error; #[repr(transparent)] #[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)] -pub struct WasmedgeLLMErrno(u32); -pub const WASMEDGE_LLM_ERRNO_SUCCESS: WasmedgeLLMErrno = WasmedgeLLMErrno(0); -pub const WASMEDGE_LLM_ERRNO_INVALID_ARGUMENT: WasmedgeLLMErrno = WasmedgeLLMErrno(1); -pub const WASMEDGE_LLM_ERRNO_MISSING_MEMORY: WasmedgeLLMErrno = WasmedgeLLMErrno(2); -impl WasmedgeLLMErrno { +pub struct WasmedgeLLMCErrno(u32); +pub const WASMEDGE_LLM_ERRNO_SUCCESS: WasmedgeLLMCErrno = WasmedgeLLMCErrno(0); +pub const WASMEDGE_LLM_ERRNO_INVALID_ARGUMENT: WasmedgeLLMCErrno = WasmedgeLLMCErrno(1); +pub const WASMEDGE_LLM_ERRNO_MISSING_MEMORY: WasmedgeLLMCErrno = WasmedgeLLMCErrno(2); +impl WasmedgeLLMCErrno { pub const fn raw(&self) -> u32 { self.0 } @@ -31,38 +31,38 @@ impl WasmedgeLLMErrno { } } } -impl fmt::Debug for WasmedgeLLMErrno { +impl fmt::Debug for WasmedgeLLMCErrno { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("WasmedgeLLMErrno") + f.debug_struct("WasmedgeLLMCErrno") .field("code", &self.0) .field("name", &self.name()) .field("message", &self.message()) .finish() } } -impl fmt::Display for WasmedgeLLMErrno { +impl fmt::Display for WasmedgeLLMCErrno { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{} (error {})", self.name(), self.0) } } -impl Error for WasmedgeLLMErrno {} +impl Error for WasmedgeLLMCErrno {} #[cfg(feature = "std")] extern crate std; #[cfg(feature = "std")] -impl std::error::Error for WasmedgeLLMErrno {} +impl std::error::Error for WasmedgeLLMCErrno {} pub unsafe fn model_create( checkpoint_path: &str, model_id: *mut u32, -) -> Result<(), WasmedgeLLMErrno> { +) -> Result<(), WasmedgeLLMCErrno> { let checkpoint_path_ptr = checkpoint_path.as_ptr() as u32; let checkpoint_path_len = checkpoint_path.len() as u32; let model_id_ptr = model_id as u32; let result = wasmedge_llm::model_create(checkpoint_path_ptr, checkpoint_path_len, model_id_ptr); if result != 0 { - Err(WasmedgeLLMErrno(result as u32)) + Err(WasmedgeLLMCErrno(result as u32)) } else { Ok(()) } @@ -76,7 +76,7 @@ pub unsafe fn dataloader_create( num_processes: u32, should_shuffule: bool, dataloader_id: *mut u32, -) -> Result<(), WasmedgeLLMErrno> { +) -> Result<(), WasmedgeLLMCErrno> { let data_path_str = data_path.as_ptr() as u32; let data_path_len = data_path.len() as u32; let dataloader_id_ptr = dataloader_id as u32; @@ -91,7 +91,7 @@ pub unsafe fn dataloader_create( dataloader_id_ptr, ); if result != 0 { - Err(WasmedgeLLMErrno(result as u32)) + Err(WasmedgeLLMCErrno(result as u32)) } else { Ok(()) } @@ -100,13 +100,13 @@ pub unsafe fn dataloader_create( pub unsafe fn tokenizer_create( filepath: &str, tokenizer_id: *mut u32, -) -> Result<(), WasmedgeLLMErrno> { +) -> Result<(), WasmedgeLLMCErrno> { let filepath_ptr = filepath.as_ptr() as u32; let filepath_len = filepath.len() as u32; let tokenizer_id_ptr = tokenizer_id as u32; let result = wasmedge_llm::tokenizer_create(filepath_ptr, filepath_len, tokenizer_id_ptr); if result != 0 { - Err(WasmedgeLLMErrno(result as u32)) + Err(WasmedgeLLMCErrno(result as u32)) } else { Ok(()) } @@ -121,7 +121,7 @@ pub unsafe fn model_train( sequence_length: u32, lr: f32, epoch: u32, -) -> Result<(), WasmedgeLLMErrno> { +) -> Result<(), WasmedgeLLMCErrno> { let result = wasmedge_llm::model_train( model_id, train_dataloader_id, @@ -134,14 +134,14 @@ pub unsafe fn model_train( ); if result != 0 { - Err(WasmedgeLLMErrno(result as u32)) + Err(WasmedgeLLMCErrno(result as u32)) } else { Ok(()) } } pub mod wasmedge_llm { - #[link(wasm_import_module = "wasi_llm")] + #[link(wasm_import_module = "wasmedge_llmc")] extern "C" { pub fn model_create( checkpoint_path_ptr: u32,