feat: edit validated per-model engine start profiles (#1070)
This commit is contained in:
@ -18,6 +18,10 @@ mod mock;
|
||||
mod ollama;
|
||||
mod process;
|
||||
mod types;
|
||||
// Dieselbe reine Parameterdefinition wie nexus-core, ohne neue Crate-Abhängigkeit.
|
||||
#[path = "../../startparams.rs"]
|
||||
#[doc(hidden)]
|
||||
pub mod startparams;
|
||||
|
||||
pub use engine::Engine;
|
||||
pub use llama::{LlamaCppConfig, LlamaCppEngine};
|
||||
|
||||
@ -118,6 +118,43 @@ impl LlamaCppEngine {
|
||||
}
|
||||
args.extend(self.config.extra_args.iter().cloned());
|
||||
args.extend(profile.extra_args.iter().cloned());
|
||||
// Jede überschriebene Option kommt genau einmal vor, unabhängig vom
|
||||
// Verhalten der Engine bei mehrfach angegebenen Optionen.
|
||||
for spec in crate::startparams::start_parameters("llama.cpp")
|
||||
.iter()
|
||||
.filter(|p| {
|
||||
profile.start_parameters.contains_key(p.name)
|
||||
|| (p.name == "cpu_moe"
|
||||
&& profile.start_parameters.contains_key("cpu_moe_layers"))
|
||||
|| (p.name == "cpu_moe_layers"
|
||||
&& profile
|
||||
.start_parameters
|
||||
.get("cpu_moe")
|
||||
.is_some_and(|v| v == "true"))
|
||||
})
|
||||
{
|
||||
let mut i = 0;
|
||||
while i < args.len() {
|
||||
let inline = args[i].starts_with(&format!("{}=", spec.flag));
|
||||
if args[i] == spec.flag || inline {
|
||||
args.remove(i);
|
||||
if !inline
|
||||
&& !matches!(spec.kind, crate::startparams::ParameterKind::Switch)
|
||||
&& i < args.len()
|
||||
{
|
||||
args.remove(i);
|
||||
}
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Geprüfte Profilwerte stehen zuletzt und überschreiben Vorgaben.
|
||||
args.extend(crate::startparams::parameter_arguments(
|
||||
"llama.cpp",
|
||||
&profile.start_parameters,
|
||||
false,
|
||||
));
|
||||
|
||||
let mut env: Vec<(String, String)> = self
|
||||
.config
|
||||
@ -131,6 +168,12 @@ impl LlamaCppEngine {
|
||||
}
|
||||
|
||||
fn validate(profile: &ProfileSpec) -> Result<()> {
|
||||
crate::startparams::validate_start_parameters(
|
||||
"llama.cpp",
|
||||
&profile.start_parameters,
|
||||
profile.gpus.len(),
|
||||
)
|
||||
.map_err(|e| AdapterError::InvalidProfile(e.into()))?;
|
||||
if profile.gpus.is_empty() {
|
||||
return Err(AdapterError::InvalidProfile(
|
||||
"keine GPU angegeben".to_string(),
|
||||
@ -546,6 +589,7 @@ mod tests {
|
||||
|
||||
fn profile() -> ProfileSpec {
|
||||
ProfileSpec {
|
||||
start_parameters: Default::default(),
|
||||
model_alias: "bonsai".to_string(),
|
||||
model_path: PathBuf::from("/srv/models/gguf/Bonsai-1.7B-Q1_0.gguf"),
|
||||
gpus: vec![0, 1],
|
||||
@ -554,6 +598,32 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn startprofil_baut_nur_freigegebene_argumente_und_behaelt_gateway_adresse() {
|
||||
let engine = LlamaCppEngine::new(LlamaCppConfig::default());
|
||||
let mut p = profile();
|
||||
p.extra_args.clear();
|
||||
let default = engine.build_command(&p, 18000);
|
||||
assert!(!default.args.contains(&"-np".to_string()));
|
||||
p.start_parameters = crate::startparams::StartParameters::from([
|
||||
("context".into(), "8192".into()),
|
||||
("slots".into(), "4".into()),
|
||||
("cpu_moe_layers".into(), "1".into()),
|
||||
]);
|
||||
let line = engine.build_command(&p, 18000);
|
||||
assert!(line.args.windows(2).any(|w| w == ["-np", "4"]));
|
||||
assert!(line.args.windows(2).any(|w| w == ["-ncmoe", "1"]));
|
||||
assert!(line.args.windows(2).any(|w| w == ["--host", "127.0.0.1"]));
|
||||
assert!(line.args.windows(2).any(|w| w == ["--port", "18000"]));
|
||||
assert_eq!(
|
||||
line.args
|
||||
.iter()
|
||||
.rposition(|x| x == "-c")
|
||||
.map(|i| line.args[i + 1].as_str()),
|
||||
Some("8192")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kommandozeile_bindet_nur_an_localhost_und_waehlt_gpus() {
|
||||
let engine = LlamaCppEngine::new(LlamaCppConfig::default());
|
||||
|
||||
@ -202,7 +202,7 @@ impl MayaEngine {
|
||||
})?;
|
||||
let engine_log = log_dir.join(format!("instance-{name}-{id}-engine.log"));
|
||||
let keep_cwd = self.flavor == Flavor::Strata;
|
||||
let copy = instance_config_for(
|
||||
let mut copy = instance_config_for(
|
||||
template,
|
||||
port,
|
||||
&engine_log,
|
||||
@ -210,6 +210,9 @@ impl MayaEngine {
|
||||
profile.context,
|
||||
keep_cwd,
|
||||
)?;
|
||||
if self.flavor == Flavor::Strata {
|
||||
apply_strata_parameters(&mut copy, profile)?;
|
||||
}
|
||||
// Strata braucht seinen Arbeitsordner (Terminal), Maya läuft im eigenen.
|
||||
let process_dir = copy
|
||||
.get("cwd")
|
||||
@ -255,8 +258,16 @@ impl MayaEngine {
|
||||
.map_err(io_err)?;
|
||||
let log_err = log.try_clone().map_err(io_err)?;
|
||||
|
||||
let mut server_args = self.build_args(&config_path, port);
|
||||
if self.flavor == Flavor::Strata {
|
||||
server_args.extend(crate::startparams::parameter_arguments(
|
||||
"strata",
|
||||
&profile.start_parameters,
|
||||
true,
|
||||
));
|
||||
}
|
||||
let mut child = Command::new(&self.config.python)
|
||||
.args(self.build_args(&config_path, port))
|
||||
.args(server_args)
|
||||
.current_dir(&workdir)
|
||||
.envs(&self.config.env)
|
||||
// Eigene Prozessgruppe: Server, Engine und Hilfsprozesse lassen sich
|
||||
@ -448,6 +459,58 @@ pub(crate) fn instance_config_for(
|
||||
Ok(Value::Object(map))
|
||||
}
|
||||
|
||||
/// Ändert nur die instanzeigene Kopie, niemals die vorhandene Laufkonfiguration.
|
||||
fn apply_strata_parameters(copy: &mut Value, profile: &ProfileSpec) -> Result<()> {
|
||||
use crate::startparams::{
|
||||
ParameterKind, parameter_arguments, start_parameters, validate_start_parameters,
|
||||
};
|
||||
if profile.start_parameters.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
validate_start_parameters("strata", &profile.start_parameters, profile.gpus.len())
|
||||
.map_err(|e| AdapterError::InvalidProfile(e.into()))?;
|
||||
if let Some(value) = profile.start_parameters.get("gpu")
|
||||
&& crate::startparams::gpu_selection(value).as_deref() != Some(profile.gpus.as_slice())
|
||||
{
|
||||
return Err(AdapterError::InvalidProfile(
|
||||
"GPU-Auswahl stimmt nicht mit der Reservierung überein.".into(),
|
||||
));
|
||||
}
|
||||
let map = copy
|
||||
.as_object_mut()
|
||||
.ok_or_else(|| AdapterError::InvalidProfile("Konfiguration ist kein Objekt.".into()))?;
|
||||
let args = map
|
||||
.entry("args")
|
||||
.or_insert_with(|| Value::Array(Vec::new()))
|
||||
.as_array_mut()
|
||||
.ok_or_else(|| AdapterError::InvalidProfile("Engine-Argumente sind keine Liste.".into()))?;
|
||||
for spec in start_parameters("strata")
|
||||
.iter()
|
||||
.filter(|p| !p.server && profile.start_parameters.contains_key(p.name))
|
||||
{
|
||||
let mut i = 0;
|
||||
while i < args.len() {
|
||||
let inline = args[i]
|
||||
.as_str()
|
||||
.is_some_and(|v| v.starts_with(&format!("{}=", spec.flag)));
|
||||
if args[i] == spec.flag || inline {
|
||||
args.remove(i);
|
||||
if !inline && !matches!(spec.kind, ParameterKind::Switch) && i < args.len() {
|
||||
args.remove(i);
|
||||
}
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
args.extend(
|
||||
parameter_arguments("strata", &profile.start_parameters, false)
|
||||
.into_iter()
|
||||
.map(Value::String),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Alle Prozesse der Prozessgruppe `pgid` (aus `/proc`).
|
||||
fn group_pids(pgid: u32) -> Vec<u32> {
|
||||
let Ok(dir) = std::fs::read_dir("/proc") else {
|
||||
@ -550,6 +613,12 @@ impl Engine for MayaEngine {
|
||||
|
||||
async fn start(&self, profile: &ProfileSpec) -> Result<InstanceHandle> {
|
||||
Self::validate(profile)?;
|
||||
crate::startparams::validate_start_parameters(
|
||||
self.name(),
|
||||
&profile.start_parameters,
|
||||
profile.gpus.len(),
|
||||
)
|
||||
.map_err(|e| AdapterError::InvalidProfile(e.into()))?;
|
||||
let port = self.ports.acquire()?;
|
||||
let id = self.next_id.fetch_add(1, Ordering::SeqCst);
|
||||
match self.spawn_and_wait(id, port, profile).await {
|
||||
@ -730,6 +799,42 @@ mod strata_tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn strata_startprofil_ueberschreibt_nur_enginewerte_und_standard_bleibt_identisch() {
|
||||
let mut copy = serde_json::json!({"exe":"engine", "args":["--max-context","4096","--vision","--kv","fp16"],"host":"127.0.0.1","port":18000,"gpu":[0,1]});
|
||||
let original = copy.clone();
|
||||
let mut profile = ProfileSpec {
|
||||
start_parameters: Default::default(),
|
||||
model_alias: "m".into(),
|
||||
model_path: "x.json".into(),
|
||||
gpus: vec![0, 1],
|
||||
context: None,
|
||||
extra_args: vec![],
|
||||
};
|
||||
apply_strata_parameters(&mut copy, &profile).unwrap();
|
||||
assert_eq!(copy, original);
|
||||
profile.start_parameters = crate::startparams::StartParameters::from([
|
||||
("context".into(), "8192".into()),
|
||||
("kv".into(), "int8".into()),
|
||||
("vision".into(), "false".into()),
|
||||
("slots".into(), "2".into()),
|
||||
]);
|
||||
apply_strata_parameters(&mut copy, &profile).unwrap();
|
||||
let args = copy["args"].as_array().unwrap();
|
||||
assert!(!args.iter().any(|x| x == "--vision" || x == "4096"));
|
||||
assert!(
|
||||
args.windows(2)
|
||||
.any(|w| w[0] == "--max-context" && w[1] == "8192")
|
||||
);
|
||||
assert!(args.windows(2).any(|w| w[0] == "--kv" && w[1] == "int8"));
|
||||
assert_eq!(copy["port"], 18000);
|
||||
assert_eq!(copy["gpu"], serde_json::json!([0, 1]));
|
||||
assert_eq!(
|
||||
crate::startparams::parameter_arguments("strata", &profile.start_parameters, true),
|
||||
vec!["--parallel", "2"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strata_startet_den_server_mit_engine_strata_und_behaelt_den_arbeitsordner() {
|
||||
let engine = MayaEngine::strata(MayaConfig::default());
|
||||
|
||||
@ -152,6 +152,7 @@ mod tests {
|
||||
|
||||
fn profile() -> ProfileSpec {
|
||||
ProfileSpec {
|
||||
start_parameters: Default::default(),
|
||||
model_alias: "m".to_string(),
|
||||
model_path: PathBuf::from("/x.gguf"),
|
||||
gpus: vec![0],
|
||||
|
||||
@ -734,6 +734,7 @@ mod tests {
|
||||
|
||||
fn profile(gpus: Vec<u32>) -> ProfileSpec {
|
||||
ProfileSpec {
|
||||
start_parameters: Default::default(),
|
||||
model_alias: "klein".to_string(),
|
||||
model_path: PathBuf::from("qwen3:0.6b"),
|
||||
gpus,
|
||||
|
||||
@ -72,6 +72,7 @@ pub struct DiscoveredArtifact {
|
||||
/// Modellname dieser Engine (zum Beispiel `qwen3:0.6b`).
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ProfileSpec {
|
||||
pub start_parameters: crate::startparams::StartParameters,
|
||||
/// Name, unter dem die Engine das Modell anbietet.
|
||||
pub model_alias: String,
|
||||
pub model_path: PathBuf,
|
||||
|
||||
Reference in New Issue
Block a user