Skip to content

armgit / kong

Public Imported from GitHub
0

The world's first agentic reverse engineer.

update arches and add model registry

Amruth4f41b5f901e2
diff --git a/crates/kong-ml/src/lib.rs b/crates/kong-ml/src/lib.rs
index 6a034b9..da1bfaf 100644
--- a/crates/kong-ml/src/lib.rs
+++ b/crates/kong-ml/src/lib.rs
@@ -1,4 +1,5 @@
pub mod tokenizer;
pub mod config;
pub mod model;
-pub mod detector;
\ No newline at end of file
+pub mod detector;
+pub mod registry;
\ No newline at end of file
diff --git a/crates/kong-ml/src/registry.rs b/crates/kong-ml/src/registry.rs
new file mode 100644
index 0000000..7fc9091
--- /dev/null
+++ b/crates/kong-ml/src/registry.rs
@@ -0,0 +1,83 @@
+use std::path::{Path, PathBuf};
+
+use candle_core::{DType, Device};
+use candle_nn::VarBuilder;
+use kong_types::binary::Arch;
+
+use crate::config::XdaConfig;
+use crate::detector::XdaDetector;
+use crate::model::XdaModel;
+
+pub struct ModelRegistry {
+ cache_dir: PathBuf,
+ confidence_threshold: f32,
+}
+
+impl ModelRegistry {
+ pub fn new(confidence_threshold: f32) -> Self {
+ let cache_dir = dirs::data_local_dir()
+ .unwrap_or_else(|| PathBuf::from("."))
+ .join("kong")
+ .join("models");
+
+ Self {
+ cache_dir,
+ confidence_threshold,
+ }
+ }
+
+ pub fn with_cache_dir(cache_dir: PathBuf, confidence_threshold: f32) -> Self {
+ Self {
+ cache_dir,
+ confidence_threshold,
+ }
+ }
+
+ pub fn has_model(&self, arch: Arch) -> bool {
+ self.weights_path(arch).exists() && self.config_path(arch).exists()
+ }
+
+ pub fn available_architectures(&self) -> Vec<Arch> {
+ let all = [
+ Arch::X86_64,
+ Arch::Aarch64,
+ Arch::Arm,
+ Arch::Riscv64,
+ ];
+ all.into_iter().filter(|a| self.has_model(*a)).collect()
+ }
+
+ pub fn load_detector(&self, arch: Arch, device: &Device) -> Result<Option<XdaDetector>, Box<dyn std::error::Error>> {
+ if !self.has_model(arch) {
+ return Ok(None);
+ }
+
+ let config = XdaConfig::from_json(&self.config_path(arch))?;
+ let vb = unsafe {
+ VarBuilder::from_mmaped_safetensors(
+ &[self.weights_path(arch)],
+ DType::F32,
+ device,
+ )?
+ };
+ let model = XdaModel::load(&config, vb)?;
+ let detector = XdaDetector::new(model, device.clone(), self.confidence_threshold);
+
+ Ok(Some(detector))
+ }
+
+ pub fn install(&self, arch: Arch, weights_src: &Path, config_src: &Path) -> Result<(), Box<dyn std::error::Error>> {
+ std::fs::create_dir_all(&self.cache_dir)?;
+ std::fs::copy(weights_src, self.weights_path(arch))?;
+ std::fs::copy(config_src, self.config_path(arch))?;
+ Ok(())
+ }
+
+ fn weights_path(&self, arch: Arch) -> PathBuf {
+ self.cache_dir.join(format!("xda_{}.safetensors", arch.model_name()))
+ }
+
+ fn config_path(&self, arch: Arch) -> PathBuf {
+ self.cache_dir.join(format!("xda_{}_config.json", arch.model_name()))
+ }
+}
\ No newline at end of file
diff --git a/crates/kong-types/src/binary.rs b/crates/kong-types/src/binary.rs
index 7bbf750..b364b81 100644
--- a/crates/kong-types/src/binary.rs
+++ b/crates/kong-types/src/binary.rs
@@ -1,22 +1,25 @@
use serde::{Deserialize, Serialize};
-use strum_macros::AsRefStr;
+use strum_macros::IntoStaticStr;
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, AsRefStr)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, IntoStaticStr)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
pub enum Arch {
X86_64,
- X86,
Aarch64,
Arm,
- Mips,
- Mips64,
Riscv64,
- PowerPc,
+}
+
+impl Arch {
+ // we use strum as_ref() to get the string representation of the enum, NOT a match statement
+ pub fn model_name(&self) -> &'static str {
+ self.into()
+ }
}
impl std::fmt::Display for Arch {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- f.write_str(self.as_ref())
+ f.write_str(self.model_name())
}
}
\ No newline at end of file