Skip to content

Commit

Permalink
fix: 'cargo build' with non-wasm targets
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronFriel committed Mar 31, 2024
1 parent 68fa27d commit ce2397a
Show file tree
Hide file tree
Showing 32 changed files with 124 additions and 111 deletions.
2 changes: 1 addition & 1 deletion aicirt/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::HashMap;
use aici_abi::{StorageCmd, TokenId};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::HashMap;

pub type ModuleInstId = u64;

Expand Down
2 changes: 1 addition & 1 deletion aicirt/src/bench.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::HashMap;
use std::{
fmt::{Display, Write},
sync::{Arc, Mutex},
time::{Duration, Instant},
};
use crate::HashMap;

#[derive(PartialEq, Eq)]
enum TimerState {
Expand Down
11 changes: 4 additions & 7 deletions aicirt/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use wasmtime::component::bindgen;

bindgen!("aici" in "../wit");

pub use self::aici::abi::runtime::SeqId;
pub use self::aici::abi::tokenizer::TokenId;
pub use self::exports::aici::abi::controller::*;
pub use self::{
aici::abi::{runtime::SeqId, tokenizer::TokenId},
exports::aici::abi::controller::*,
};

pub trait Json {
fn to_json(&self) -> String;
Expand All @@ -31,9 +32,6 @@ impl From<()> for PreProcessArg {
}
}




#[derive(Serialize, Deserialize)]
#[serde(remote = "Vocabulary")]
struct VocabularyDef {
Expand Down Expand Up @@ -92,7 +90,6 @@ struct PostProcessResultDef {
stop: bool,
}


macro_rules! serde_wrapper {
($name:ident, $wrapper:ident) => {
impl Serialize for $name {
Expand Down
5 changes: 2 additions & 3 deletions aicirt/src/futexshm.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::shm::{Shm, Unlink};
use anyhow::{anyhow, Result};
#[cfg(target_os = "linux")]
use linux_futex::AsFutex;
use serde::{Deserialize, Serialize};
use std::{
ptr,
sync::atomic::{AtomicU32, Ordering},
time::{Duration, Instant},
};

#[cfg(target_os = "linux")]
use linux_futex::AsFutex;
#[cfg(target_os = "linux")]
type Futex = linux_futex::Futex<linux_futex::Shared>;

Expand Down
21 changes: 8 additions & 13 deletions aicirt/src/hostimpl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
bindings::aici::abi::{self, tokenizer::TokenId},
shm::Shm,
worker::{GroupCmd, GroupHandle, GroupResp},
};
use aici_abi::{
Expand Down Expand Up @@ -192,9 +191,9 @@ impl abi::runtime_storage::Host for ModuleData {

match res {
GroupResp::StorageResp { resp } => match resp {
aici_abi::StorageResp::ReadVar { version, value } => Ok(Some(value)),
aici_abi::StorageResp::ReadVar { version: _, value } => Ok(Some(value)),
aici_abi::StorageResp::VariableMissing {} => Ok(None),
aici_abi::StorageResp::WriteVar { version } => Err(anyhow!("unexpected WriteVar")),
aici_abi::StorageResp::WriteVar { .. } => Err(anyhow!("unexpected WriteVar")),
},
}
}
Expand All @@ -210,13 +209,11 @@ impl abi::runtime_storage::Host for ModuleData {
let res = self.group_channel.send_cmd(GroupCmd::StorageCmd { cmd })?;
match res {
GroupResp::StorageResp { resp } => match resp {
aici_abi::StorageResp::ReadVar { version, value } => {
Err(anyhow!("unexpected ReadVar"))
}
aici_abi::StorageResp::VariableMissing {} => {
aici_abi::StorageResp::ReadVar { .. } => Err(anyhow!("unexpected ReadVar")),
aici_abi::StorageResp::VariableMissing { .. } => {
Err(anyhow!("unexpected VariableMissing"))
}
aici_abi::StorageResp::WriteVar { version } => Ok(()),
aici_abi::StorageResp::WriteVar { .. } => Ok(()),
},
}
}
Expand All @@ -232,13 +229,11 @@ impl abi::runtime_storage::Host for ModuleData {
let res = self.group_channel.send_cmd(GroupCmd::StorageCmd { cmd })?;
match res {
GroupResp::StorageResp { resp } => match resp {
aici_abi::StorageResp::ReadVar { version, value } => {
Err(anyhow!("unexpected ReadVar"))
}
aici_abi::StorageResp::VariableMissing {} => {
aici_abi::StorageResp::ReadVar { .. } => Err(anyhow!("unexpected ReadVar")),
aici_abi::StorageResp::VariableMissing { .. } => {
Err(anyhow!("unexpected VariableMissing"))
}
aici_abi::StorageResp::WriteVar { version } => Ok(()),
aici_abi::StorageResp::WriteVar { .. } => Ok(()),
},
}
}
Expand Down
12 changes: 4 additions & 8 deletions aicirt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
pub mod api;
mod bench;
pub mod bindings;
pub mod bintokens;
pub mod futexshm;
pub mod msgchannel;
pub mod semaphore;
pub mod shm;
pub mod bindings;

#[cfg(target_os = "macos")]
mod macos;

use std::fmt::Write;

use anyhow::Result;
pub use bench::*;
use flexi_logger::style;
use flexi_logger::{DeferredNow, Logger, WriteMode};
use flexi_logger::{style, DeferredNow, Logger, WriteMode};
pub use fxhash::{FxHashMap as HashMap, FxHashSet as HashSet};
use log::Record;
use std::fmt::Write;
use thread_priority::{
set_thread_priority_and_policy, thread_native_id, RealtimeThreadSchedulePolicy, ThreadPriority,
ThreadSchedulePolicy,
};

pub use fxhash::FxHashMap as HashMap;
pub use fxhash::FxHashSet as HashSet;

pub enum LogMode {
Normal,
Test,
Expand Down
2 changes: 1 addition & 1 deletion aicirt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::{
};
use aici_abi::{bytes::limit_str, toktree::TokTrie};
use aicirt::{
bintokens::find_tokenizer,
bindings::{MidProcessArg, PostProcessArg, PreProcessArg, SeqId},
bintokens::find_tokenizer,
futexshm::ServerChannel,
*,
};
Expand Down
28 changes: 14 additions & 14 deletions aicirt/src/moduleinstance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ use crate::{
use aici_abi::toktree::TokTrie;
use aicirt::{
api::{AiciMidProcessResultInner, AiciPostProcessResultInner, SequenceResult},
bintokens::ByteTokenizer,
bindings::{self, exports::aici::abi::controller::*, InitPromptResult, PreProcessArg},
bintokens::ByteTokenizer,
user_error,
};
use anyhow::{bail, ensure, Result};
use serde::Deserialize;
use std::{path::Path, sync::Arc, time::Instant};
use wasmtime;

Expand Down Expand Up @@ -124,7 +123,10 @@ impl ModuleInstance {

let component_instance = ctx.component_linker.instantiate(&mut store, &component)?;
let aici = bindings::Aici::new(&mut store, &component_instance)?;
let runner = aici.aici_abi_controller().runner().call_constructor(&mut store, &module_arg)?;
let runner = aici
.aici_abi_controller()
.runner()
.call_constructor(&mut store, &module_arg)?;

Ok(ModuleInstance {
store,
Expand Down Expand Up @@ -187,13 +189,15 @@ impl ModuleInstance {
.call_pre_process(&mut self.store, self.runner)
}

fn do_mid_process(&mut self, op: RtMidProcessArg, shm: &Shm) -> Result<Option<AiciMidProcessResultInner>> {
fn do_mid_process(
&mut self,
op: RtMidProcessArg,
shm: &Shm,
) -> Result<Option<AiciMidProcessResultInner>> {
let vocab_size = self.store.data().globals.tokrx_info.vocab_size as usize;
assert!(op.logit_size == vocab_size * 4);
let logit_ptr = shm.slice_at_byte_offset(op.logit_offset, vocab_size);
logit_ptr
.iter_mut()
.for_each(|x| *x = LOGIT_BIAS_DISALLOW);
logit_ptr.iter_mut().for_each(|x| *x = LOGIT_BIAS_DISALLOW);

match self.aici.aici_abi_controller().runner().call_mid_process(
&mut self.store,
Expand All @@ -209,12 +213,10 @@ impl ModuleInstance {
}
}
Ok(None)
},
}
MidProcessResult::Stop { .. } => {
let eos = self.store.data().globals.tokrx_info.tok_eos;
logit_ptr
.iter_mut()
.for_each(|v| *v = LOGIT_BIAS_DISALLOW);
logit_ptr.iter_mut().for_each(|v| *v = LOGIT_BIAS_DISALLOW);
logit_ptr[eos as usize] = LOGIT_BIAS_ALLOW;
Ok(None)
}
Expand Down Expand Up @@ -242,9 +244,7 @@ impl ModuleInstance {
} else {
// we don't really care about biases, as we'lre going to backtrack this token anyways
// but just in case, allow all
logit_ptr
.iter_mut()
.for_each(|v| *v = LOGIT_BIAS_DISALLOW);
logit_ptr.iter_mut().for_each(|v| *v = LOGIT_BIAS_DISALLOW);
// don't remove anything from ff_tokens - they all need to be appended after backtracking

// backtrack needs to include also the next token to be generated
Expand Down
5 changes: 4 additions & 1 deletion aicirt/src/msgchannel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{semaphore::Semaphore, shm::{Shm, Unlink}};
use crate::{
semaphore::Semaphore,
shm::{Shm, Unlink},
};
use anyhow::Result;
use std::time::Duration;

Expand Down
2 changes: 1 addition & 1 deletion aicirt/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::{
};
use aici_abi::{StorageCmd, StorageOp, StorageResp};
use aicirt::{
bindings::*,
api::{AiciMidProcessResultInner, AiciPostProcessResultInner, SequenceResult},
bindings::*,
futexshm::{TypedClient, TypedClientHandle, TypedServer},
set_max_priority,
shm::Unlink,
Expand Down
6 changes: 2 additions & 4 deletions controllers/aici_abi/src/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::{mem::size_of, slice::from_raw_parts};

use anyhow::{anyhow, Result};

use crate::TokenId;
use anyhow::{anyhow, Result};
use std::{mem::size_of, slice::from_raw_parts};

#[repr(C)]
#[derive(Clone, PartialEq, Eq, Debug)]
Expand Down
4 changes: 2 additions & 2 deletions controllers/aici_abi/src/cfg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::lex::{Lexer, LexerState, StateID, VobIdx, VobSet};
use crate::{
SimpleVob,
lex::{Lexer, LexerState, StateID, VobIdx, VobSet},
toktree::{Recognizer, SpecialToken, TokTrie},
SimpleVob,
};
use anyhow::Result;
use cfgrammar::{
Expand Down
3 changes: 1 addition & 2 deletions controllers/aici_abi/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ pub mod bin_string {
}

pub mod hex_string {
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::bytes::{from_hex_string, to_hex_string};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

pub fn serialize<S: Serializer>(v: &Vec<u8>, s: S) -> Result<S::Ok, S::Error> {
let hexstr = to_hex_string(v);
Expand Down
18 changes: 13 additions & 5 deletions controllers/aici_abi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::{Arc, Mutex};
use serde::{Deserialize, Serialize};
use std::sync::{Arc, Mutex};

#[doc(hidden)]
pub mod bindings {
wit_bindgen::generate!({
world: "aici",
Expand All @@ -9,15 +10,22 @@ pub mod bindings {
pub_export_macro: true
});

pub use self::aici::abi::*;
pub use self::exports::aici::abi::*;
pub use self::{aici::abi::*, exports::aici::abi::*};
}

pub use bindings::controller::*;
pub use bindings::{export, exports, runtime, runtime_storage, tokenizer};
pub use bindings::{controller::*, runtime, runtime_storage, tokenizer};
pub mod svob;
pub use svob::SimpleVob;

#[macro_export]
macro_rules! export {
($ty:ident) => {
#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
$crate::bindings::export!($ty with_types_in $crate::bindings);
};
}

pub mod bytes;
mod host;
pub mod recognizer;
Expand Down
3 changes: 1 addition & 2 deletions controllers/aici_abi/src/substring.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::fmt::Display;

use crate::{
bytes::limit_str,
recognizer::{FunctionalRecognizer, StackRecognizer},
toktree::SpecialToken,
};
use serde_json::json;
use std::fmt::Display;

enum Node {
Inner { children: Vec<(u8, usize)> },
Expand Down
3 changes: 1 addition & 2 deletions controllers/aici_abi/src/svob.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::ops::Index;

use crate::{TokenId, Vocabulary};
use std::ops::Index;

pub type SimpleVob = Vocabulary;
impl Default for SimpleVob {
Expand Down
8 changes: 5 additions & 3 deletions controllers/aici_abi/src/toktree.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// use 8:24 encoding - num_ch:tok_id (ch_byte:ch_off)* - 8 bytes per tree node
// special case num_ch=0xff -> num_ch=0x100

use rustc_hash::FxHashMap;

use crate::{
bytes::{box_from_bytes, clone_as_bytes, clone_vec_as_bytes, to_hex_string, vec_from_bytes, TokRxInfo},
bytes::{
box_from_bytes, clone_as_bytes, clone_vec_as_bytes, to_hex_string, vec_from_bytes,
TokRxInfo,
},
tokenizer, SimpleVob, TokenId,
};
use rustc_hash::FxHashMap;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SpecialToken {
Expand Down
2 changes: 1 addition & 1 deletion controllers/declctrl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use aici_abi::{
bytes::limit_str,
cfg::CfgParser,
export, exports,
export,
rx::{RecRx, RxStackRecognizer},
tokenizer,
toktree::{Recognizer, SpecialToken, TokTrie},
Expand Down
6 changes: 5 additions & 1 deletion controllers/guidance_ctrl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ rustc-hash = "1.1.0"
base64 = "0.22.0"

[lib]
crate-type = ["cdylib"]
crate-type = ["rlib", "cdylib"]

[[bin]]
name = "aici_earley_bench"
path = "src/earley_bench.rs"

[build-dependencies]
glob = "0.3.1"
3 changes: 1 addition & 2 deletions controllers/guidance_ctrl/src/earley/bench.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use aici_abi::toktree;

use super::Parser;
use crate::earley::{from_guidance::earley_grm_from_guidance, parser::ParseResult};
use aici_abi::toktree;

pub fn earley_test(trie: toktree::TokTrie) {
let g_bytes = include_bytes!("../../../aici_abi/grammars/json0.guidance");
Expand Down
Loading

0 comments on commit ce2397a

Please sign in to comment.