pub mod rpc;
mod client;
pub use client::WeirwoodClient;
pub use rpc::inference_service_server::{InferenceService, InferenceServiceServer};
pub use rpc::inference_service_client::InferenceServiceClient;
pub use rpc::{
InitSessionRequest, InitSessionResponse, ModelInfo, PredictRequest, PredictResponse,
};
use std::io::Cursor;
use tfhe::named::Named;
use tfhe::safe_serialization::{safe_deserialize, safe_serialize};
use tfhe::{Unversionize, Versionize};
use crate::Error;
use crate::eval::fhe::{EncryptedScore, ServerContext};
use crate::model::{Objective, WeirwoodTree};
impl ModelInfo {
pub fn from_model(model: &WeirwoodTree) -> Self {
let (objective_name, num_class) = match &model.objective {
Objective::BinaryLogistic => ("binary:logistic".to_string(), 0),
Objective::RegSquaredError => ("reg:squarederror".to_string(), 0),
Objective::MultiSoftmax { num_class } => {
("multi:softmax".to_string(), *num_class as u32)
}
Objective::Other(name) => (name.clone(), 0),
};
Self {
num_features: model.num_features as u32,
objective_name,
num_class,
}
}
}
impl From<&WeirwoodTree> for ModelInfo {
fn from(model: &WeirwoodTree) -> Self {
Self::from_model(model)
}
}
const SIZE_LIMIT_BYTES: u64 = 512 * 1024 * 1024;
pub const MAX_GRPC_MESSAGE_BYTES: usize = 512 * 1024 * 1024;
fn safe_to_bytes<T>(value: &T, what: &str) -> Result<Vec<u8>, Error>
where
T: serde::Serialize + Versionize + Named,
{
let mut buf = Vec::new();
safe_serialize(value, &mut buf, SIZE_LIMIT_BYTES)
.map_err(|e| Error::Fhe(format!("failed to serialize {what}: {e}")))?;
Ok(buf)
}
fn safe_from_bytes<T>(bytes: &[u8], what: &str) -> Result<T, Error>
where
T: serde::de::DeserializeOwned + Unversionize + Named,
{
safe_deserialize(&mut Cursor::new(bytes), SIZE_LIMIT_BYTES)
.map_err(|e| Error::Fhe(format!("failed to deserialize {what}: {e}")))
}
pub fn serialize_server_context(ctx: &ServerContext) -> Result<Vec<u8>, Error> {
safe_to_bytes(&ctx.server_key, "server key")
}
pub fn deserialize_server_context(bytes: &[u8]) -> Result<ServerContext, Error> {
let server_key: tfhe::ServerKey = safe_from_bytes(bytes, "server key")?;
Ok(ServerContext::from_key(server_key))
}
pub fn serialize_feature(feature: &tfhe::FheInt32) -> Result<Vec<u8>, Error> {
safe_to_bytes(feature, "feature")
}
pub fn deserialize_feature(bytes: &[u8]) -> Result<tfhe::FheInt32, Error> {
safe_from_bytes(bytes, "feature")
}
pub fn serialize_score(score: &EncryptedScore) -> Result<Vec<u8>, Error> {
safe_to_bytes(score, "encrypted score")
}
pub fn deserialize_score(bytes: &[u8]) -> Result<EncryptedScore, Error> {
safe_from_bytes(bytes, "encrypted score")
}