STRA-338 size-fn: npu-worker, igpu-worker: argument parsing and main split
This commit is contained in:
+113
-64
@@ -33,58 +33,103 @@ fn usage() -> &'static str {
|
||||
"usage: leleka-igpu-worker --serve --model <gpt-oss.gguf> [--threads N] [--ctx N] [--no-warm] [--seed N] [--gpu-min-tokens N] [--gpu-batch N] [--speculate] [--cache-slots N]"
|
||||
}
|
||||
|
||||
fn parse_args() -> Result<Args, String> {
|
||||
let mut args = std::env::args().skip(1);
|
||||
let mut model = None;
|
||||
let mut threads = std::thread::available_parallelism().map_or(1, |n| n.get());
|
||||
let mut context = DEFAULT_CONTEXT;
|
||||
let mut warm = true;
|
||||
let mut seed = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0x5EED, |d| d.as_nanos() as u64);
|
||||
let mut gpu_min_tokens = DEFAULT_GPU_MIN_TOKENS;
|
||||
let mut gpu_batch = leleka_gptoss_igpu::DEFAULT_BATCH;
|
||||
let mut speculate = false;
|
||||
let mut cache_slots = 1usize;
|
||||
let mut serving = false;
|
||||
let number = |name: &str, value: Option<String>| -> Result<u64, String> {
|
||||
value
|
||||
.and_then(|v| v.parse::<u64>().ok())
|
||||
.ok_or_else(|| format!("{name} needs a number"))
|
||||
};
|
||||
while let Some(arg) = args.next() {
|
||||
match arg.as_str() {
|
||||
"--serve" => serving = true,
|
||||
"--model" => model = Some(PathBuf::from(args.next().ok_or("--model needs a path")?)),
|
||||
"--threads" => threads = number("--threads", args.next())?.max(1) as usize,
|
||||
"--ctx" => context = number("--ctx", args.next())?.max(16) as usize,
|
||||
"--no-warm" => warm = false,
|
||||
"--speculate" => speculate = true,
|
||||
"--cache-slots" => {
|
||||
cache_slots = number("--cache-slots", args.next())?.clamp(1, 64) as usize
|
||||
}
|
||||
"--seed" => seed = number("--seed", args.next())?,
|
||||
"--gpu-min-tokens" => {
|
||||
gpu_min_tokens = number("--gpu-min-tokens", args.next())?.max(1) as usize;
|
||||
}
|
||||
"--gpu-batch" => gpu_batch = number("--gpu-batch", args.next())?.max(1) as usize,
|
||||
other => return Err(format!("unknown argument {other}")),
|
||||
/// The options as they are read, before the required ones are checked.
|
||||
struct Parsed {
|
||||
model: Option<PathBuf>,
|
||||
threads: usize,
|
||||
context: usize,
|
||||
warm: bool,
|
||||
seed: u64,
|
||||
gpu_min_tokens: usize,
|
||||
gpu_batch: usize,
|
||||
speculate: bool,
|
||||
cache_slots: usize,
|
||||
serving: bool,
|
||||
}
|
||||
|
||||
fn number(name: &str, value: Option<String>) -> Result<u64, String> {
|
||||
value
|
||||
.and_then(|v| v.parse::<u64>().ok())
|
||||
.ok_or_else(|| format!("{name} needs a number"))
|
||||
}
|
||||
|
||||
impl Parsed {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
model: None,
|
||||
threads: std::thread::available_parallelism().map_or(1, |n| n.get()),
|
||||
context: DEFAULT_CONTEXT,
|
||||
warm: true,
|
||||
seed: std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0x5EED, |d| d.as_nanos() as u64),
|
||||
gpu_min_tokens: DEFAULT_GPU_MIN_TOKENS,
|
||||
gpu_batch: leleka_gptoss_igpu::DEFAULT_BATCH,
|
||||
speculate: false,
|
||||
cache_slots: 1,
|
||||
serving: false,
|
||||
}
|
||||
}
|
||||
if !serving {
|
||||
return Err("only --serve mode exists".into());
|
||||
|
||||
/// Take one argument, and the values that follow it from `rest`.
|
||||
fn apply(&mut self, arg: &str, rest: &mut impl Iterator<Item = String>) -> Result<(), String> {
|
||||
match arg {
|
||||
"--serve" => self.serving = true,
|
||||
"--model" => self.model = Some(PathBuf::from(rest.next().ok_or("--model needs a path")?)),
|
||||
"--no-warm" => self.warm = false,
|
||||
"--speculate" => self.speculate = true,
|
||||
_ => return self.apply_number(arg, rest.next()),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Ok(Args {
|
||||
model: model.ok_or("--model is required")?,
|
||||
threads,
|
||||
context,
|
||||
warm,
|
||||
seed,
|
||||
gpu_min_tokens,
|
||||
gpu_batch,
|
||||
speculate,
|
||||
cache_slots,
|
||||
})
|
||||
|
||||
/// The options that take a number (`value` is the argument after the option).
|
||||
fn apply_number(&mut self, arg: &str, value: Option<String>) -> Result<(), String> {
|
||||
match arg {
|
||||
"--threads" => self.threads = number("--threads", value)?.max(1) as usize,
|
||||
"--ctx" => self.context = number("--ctx", value)?.max(16) as usize,
|
||||
"--cache-slots" => self.cache_slots = number("--cache-slots", value)?.clamp(1, 64) as usize,
|
||||
"--seed" => self.seed = number("--seed", value)?,
|
||||
_ => return self.apply_gpu(arg, value),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The GPU options (`value` is the argument after the option).
|
||||
fn apply_gpu(&mut self, arg: &str, value: Option<String>) -> Result<(), String> {
|
||||
match arg {
|
||||
"--gpu-min-tokens" => self.gpu_min_tokens = number("--gpu-min-tokens", value)?.max(1) as usize,
|
||||
"--gpu-batch" => self.gpu_batch = number("--gpu-batch", value)?.max(1) as usize,
|
||||
other => return Err(format!("unknown argument {other}")),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(self) -> Result<Args, String> {
|
||||
if !self.serving {
|
||||
return Err("only --serve mode exists".into());
|
||||
}
|
||||
Ok(Args {
|
||||
model: self.model.ok_or("--model is required")?,
|
||||
threads: self.threads,
|
||||
context: self.context,
|
||||
warm: self.warm,
|
||||
seed: self.seed,
|
||||
gpu_min_tokens: self.gpu_min_tokens,
|
||||
gpu_batch: self.gpu_batch,
|
||||
speculate: self.speculate,
|
||||
cache_slots: self.cache_slots,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_args() -> Result<Args, String> {
|
||||
let mut args = std::env::args().skip(1);
|
||||
let mut parsed = Parsed::new();
|
||||
while let Some(arg) = args.next() {
|
||||
parsed.apply(&arg, &mut args)?;
|
||||
}
|
||||
parsed.finish()
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
@@ -95,21 +140,23 @@ fn main() -> ExitCode {
|
||||
return ExitCode::from(2);
|
||||
}
|
||||
};
|
||||
let loaded = match engine::load(&LoadOptions {
|
||||
let options = LoadOptions {
|
||||
model: &args.model,
|
||||
threads: args.threads,
|
||||
context: args.context,
|
||||
warm: args.warm,
|
||||
}) {
|
||||
Ok(loaded) => loaded,
|
||||
};
|
||||
match engine::load(&options) {
|
||||
Ok(loaded) => serve_stdin(&args, loaded),
|
||||
Err(message) => {
|
||||
eprintln!("leleka-igpu-worker: {message}");
|
||||
return ExitCode::FAILURE;
|
||||
ExitCode::FAILURE
|
||||
}
|
||||
};
|
||||
let mut engine = loaded.engine;
|
||||
engine.set_speculation(args.speculate);
|
||||
engine.set_cache_slots(args.cache_slots);
|
||||
}
|
||||
}
|
||||
|
||||
/// Open the GPU for long prefills; the worker serves from the CPU only when there is none.
|
||||
fn attach_gpu(engine: &mut engine::GptOssEngine, args: &Args) {
|
||||
match GpuPrefill::new() {
|
||||
Ok(gpu) => {
|
||||
eprintln!(
|
||||
@@ -128,14 +175,16 @@ fn main() -> ExitCode {
|
||||
}
|
||||
Err(e) => eprintln!("leleka-igpu-worker: {e}; serving from the CPU only"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Serve the control plane's requests on standard input and output until it quits.
|
||||
fn serve_stdin(args: &Args, loaded: engine::Loaded) -> ExitCode {
|
||||
let mut engine = loaded.engine;
|
||||
engine.set_speculation(args.speculate);
|
||||
engine.set_cache_slots(args.cache_slots);
|
||||
attach_gpu(&mut engine, args);
|
||||
let stdin = BufReader::new(io::stdin());
|
||||
match serve(
|
||||
&mut engine,
|
||||
stdin,
|
||||
io::stdout(),
|
||||
&loaded.stop_tokens,
|
||||
args.seed,
|
||||
) {
|
||||
match serve(&mut engine, stdin, io::stdout(), &loaded.stop_tokens, args.seed) {
|
||||
Ok(()) => ExitCode::SUCCESS,
|
||||
Err(e) => {
|
||||
eprintln!("leleka-igpu-worker: {e}");
|
||||
|
||||
+154
-106
@@ -38,75 +38,122 @@ fn usage() -> &'static str {
|
||||
"usage: leleka-npu-worker --serve --model <gpt-oss.gguf> --openvino-root <dir> [--npu-service <exe>] [--npu-device NPU] [--npu-min-tokens N] [--npu-batch N] [--threads N] [--ctx N] [--no-warm] [--seed N] [--speculate] [--cache-slots N]"
|
||||
}
|
||||
|
||||
fn parse_args() -> Result<Args, String> {
|
||||
let mut args = std::env::args().skip(1);
|
||||
let mut model = None;
|
||||
let mut threads = std::thread::available_parallelism().map_or(1, |n| n.get());
|
||||
let mut context = DEFAULT_CONTEXT;
|
||||
let mut warm = true;
|
||||
let mut seed = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0x5EED, |d| d.as_nanos() as u64);
|
||||
let mut openvino_root = std::env::var_os("LELEKA_OPENVINO_ROOT").map(PathBuf::from);
|
||||
let mut service = None;
|
||||
let mut device = "NPU".to_owned();
|
||||
let mut min_tokens = DEFAULT_NPU_MIN_TOKENS;
|
||||
let mut batch = DEFAULT_BATCH;
|
||||
let mut speculate = false;
|
||||
let mut cache_slots = 1usize;
|
||||
let mut serving = false;
|
||||
let number = |name: &str, value: Option<String>| -> Result<u64, String> {
|
||||
value
|
||||
.and_then(|v| v.parse::<u64>().ok())
|
||||
.ok_or_else(|| format!("{name} needs a number"))
|
||||
};
|
||||
while let Some(arg) = args.next() {
|
||||
match arg.as_str() {
|
||||
"--serve" => serving = true,
|
||||
"--model" => model = Some(PathBuf::from(args.next().ok_or("--model needs a path")?)),
|
||||
"--threads" => threads = number("--threads", args.next())?.max(1) as usize,
|
||||
"--ctx" => context = number("--ctx", args.next())?.max(16) as usize,
|
||||
"--no-warm" => warm = false,
|
||||
"--speculate" => speculate = true,
|
||||
"--cache-slots" => {
|
||||
cache_slots = number("--cache-slots", args.next())?.clamp(1, 64) as usize
|
||||
}
|
||||
"--seed" => seed = number("--seed", args.next())?,
|
||||
"--openvino-root" => {
|
||||
openvino_root = Some(PathBuf::from(
|
||||
args.next().ok_or("--openvino-root needs a path")?,
|
||||
));
|
||||
}
|
||||
"--npu-service" => {
|
||||
service = Some(PathBuf::from(
|
||||
args.next().ok_or("--npu-service needs a path")?,
|
||||
));
|
||||
}
|
||||
"--npu-device" => device = args.next().ok_or("--npu-device needs a name")?,
|
||||
"--npu-min-tokens" => {
|
||||
min_tokens = number("--npu-min-tokens", args.next())?.max(1) as usize;
|
||||
}
|
||||
"--npu-batch" => batch = number("--npu-batch", args.next())?.max(1) as usize,
|
||||
other => return Err(format!("unknown argument {other}")),
|
||||
/// The options as they are read, before the required ones are checked.
|
||||
struct Parsed {
|
||||
model: Option<PathBuf>,
|
||||
threads: usize,
|
||||
context: usize,
|
||||
warm: bool,
|
||||
seed: u64,
|
||||
openvino_root: Option<PathBuf>,
|
||||
service: Option<PathBuf>,
|
||||
device: String,
|
||||
min_tokens: usize,
|
||||
batch: usize,
|
||||
speculate: bool,
|
||||
cache_slots: usize,
|
||||
serving: bool,
|
||||
}
|
||||
|
||||
fn number(name: &str, value: Option<String>) -> Result<u64, String> {
|
||||
value
|
||||
.and_then(|v| v.parse::<u64>().ok())
|
||||
.ok_or_else(|| format!("{name} needs a number"))
|
||||
}
|
||||
|
||||
/// The path after an option.
|
||||
fn path_after(name: &str, args: &mut impl Iterator<Item = String>) -> Result<PathBuf, String> {
|
||||
args.next()
|
||||
.map(PathBuf::from)
|
||||
.ok_or_else(|| format!("{name} needs a path"))
|
||||
}
|
||||
|
||||
impl Parsed {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
model: None,
|
||||
threads: std::thread::available_parallelism().map_or(1, |n| n.get()),
|
||||
context: DEFAULT_CONTEXT,
|
||||
warm: true,
|
||||
seed: std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0x5EED, |d| d.as_nanos() as u64),
|
||||
openvino_root: std::env::var_os("LELEKA_OPENVINO_ROOT").map(PathBuf::from),
|
||||
service: None,
|
||||
device: "NPU".to_owned(),
|
||||
min_tokens: DEFAULT_NPU_MIN_TOKENS,
|
||||
batch: DEFAULT_BATCH,
|
||||
speculate: false,
|
||||
cache_slots: 1,
|
||||
serving: false,
|
||||
}
|
||||
}
|
||||
if !serving {
|
||||
return Err("only --serve mode exists".into());
|
||||
|
||||
/// Take one argument, and the values that follow it from `rest`.
|
||||
fn apply(&mut self, arg: &str, rest: &mut impl Iterator<Item = String>) -> Result<(), String> {
|
||||
match arg {
|
||||
"--serve" => self.serving = true,
|
||||
"--no-warm" => self.warm = false,
|
||||
"--speculate" => self.speculate = true,
|
||||
"--model" => self.model = Some(path_after("--model", rest)?),
|
||||
"--openvino-root" => self.openvino_root = Some(path_after("--openvino-root", rest)?),
|
||||
"--npu-service" => self.service = Some(path_after("--npu-service", rest)?),
|
||||
_ => return self.apply_number(arg, rest.next()),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Ok(Args {
|
||||
model: model.ok_or("--model is required")?,
|
||||
threads,
|
||||
context,
|
||||
warm,
|
||||
seed,
|
||||
openvino_root,
|
||||
service,
|
||||
device,
|
||||
min_tokens,
|
||||
batch,
|
||||
speculate,
|
||||
cache_slots,
|
||||
})
|
||||
|
||||
/// The options that take a number (`value` is the argument after the option).
|
||||
fn apply_number(&mut self, arg: &str, value: Option<String>) -> Result<(), String> {
|
||||
match arg {
|
||||
"--threads" => self.threads = number("--threads", value)?.max(1) as usize,
|
||||
"--ctx" => self.context = number("--ctx", value)?.max(16) as usize,
|
||||
"--cache-slots" => self.cache_slots = number("--cache-slots", value)?.clamp(1, 64) as usize,
|
||||
"--seed" => self.seed = number("--seed", value)?,
|
||||
_ => return self.apply_npu(arg, value),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The NPU options (`value` is the argument after the option).
|
||||
fn apply_npu(&mut self, arg: &str, value: Option<String>) -> Result<(), String> {
|
||||
match arg {
|
||||
"--npu-device" => self.device = value.ok_or("--npu-device needs a name")?,
|
||||
"--npu-min-tokens" => self.min_tokens = number("--npu-min-tokens", value)?.max(1) as usize,
|
||||
"--npu-batch" => self.batch = number("--npu-batch", value)?.max(1) as usize,
|
||||
other => return Err(format!("unknown argument {other}")),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(self) -> Result<Args, String> {
|
||||
if !self.serving {
|
||||
return Err("only --serve mode exists".into());
|
||||
}
|
||||
Ok(Args {
|
||||
model: self.model.ok_or("--model is required")?,
|
||||
threads: self.threads,
|
||||
context: self.context,
|
||||
warm: self.warm,
|
||||
seed: self.seed,
|
||||
openvino_root: self.openvino_root,
|
||||
service: self.service,
|
||||
device: self.device,
|
||||
min_tokens: self.min_tokens,
|
||||
batch: self.batch,
|
||||
speculate: self.speculate,
|
||||
cache_slots: self.cache_slots,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_args() -> Result<Args, String> {
|
||||
let mut args = std::env::args().skip(1);
|
||||
let mut parsed = Parsed::new();
|
||||
while let Some(arg) = args.next() {
|
||||
parsed.apply(&arg, &mut args)?;
|
||||
}
|
||||
parsed.finish()
|
||||
}
|
||||
|
||||
/// The service executable next to this one, unless the caller named another.
|
||||
@@ -130,58 +177,59 @@ fn main() -> ExitCode {
|
||||
return ExitCode::from(2);
|
||||
}
|
||||
};
|
||||
let loaded = match engine::load(&LoadOptions {
|
||||
let options = LoadOptions {
|
||||
model: &args.model,
|
||||
threads: args.threads,
|
||||
context: args.context,
|
||||
warm: args.warm,
|
||||
}) {
|
||||
Ok(loaded) => loaded,
|
||||
};
|
||||
match engine::load(&options) {
|
||||
Ok(loaded) => serve_stdin(&args, loaded),
|
||||
Err(message) => {
|
||||
eprintln!("leleka-npu-worker: {message}");
|
||||
return ExitCode::FAILURE;
|
||||
ExitCode::FAILURE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Start the NPU service for long prefills; the worker serves from the CPU only when it cannot.
|
||||
fn attach_npu(engine: &mut engine::GptOssEngine, args: &Args) {
|
||||
let Some(openvino_root) = args.openvino_root.clone() else {
|
||||
eprintln!("leleka-npu-worker: no --openvino-root (or LELEKA_OPENVINO_ROOT); serving from the CPU only");
|
||||
return;
|
||||
};
|
||||
let config = ServiceConfig {
|
||||
service: args.service.clone().unwrap_or_else(default_service),
|
||||
openvino_root,
|
||||
device: args.device.clone(),
|
||||
model: args.model.clone(),
|
||||
cache_dir: None,
|
||||
};
|
||||
match NpuPrefill::start(&config, args.batch) {
|
||||
Ok(npu) => {
|
||||
eprintln!(
|
||||
"leleka-npu-worker: NPU prefill on {} for prompts of at least {} tokens (batch {})",
|
||||
npu.device_name(),
|
||||
args.min_tokens,
|
||||
args.batch
|
||||
);
|
||||
engine.set_accelerator(
|
||||
Box::new(npu.with_memory_probe(Box::new(available_memory))),
|
||||
args.min_tokens,
|
||||
);
|
||||
}
|
||||
Err(e) => eprintln!("leleka-npu-worker: {e}; serving from the CPU only"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Serve the control plane's requests on standard input and output until it quits.
|
||||
fn serve_stdin(args: &Args, loaded: engine::Loaded) -> ExitCode {
|
||||
let mut engine = loaded.engine;
|
||||
engine.set_speculation(args.speculate);
|
||||
engine.set_cache_slots(args.cache_slots);
|
||||
match args.openvino_root.clone() {
|
||||
None => eprintln!(
|
||||
"leleka-npu-worker: no --openvino-root (or LELEKA_OPENVINO_ROOT); serving from the CPU only"
|
||||
),
|
||||
Some(openvino_root) => {
|
||||
let config = ServiceConfig {
|
||||
service: args.service.clone().unwrap_or_else(default_service),
|
||||
openvino_root,
|
||||
device: args.device.clone(),
|
||||
model: args.model.clone(),
|
||||
cache_dir: None,
|
||||
};
|
||||
match NpuPrefill::start(&config, args.batch) {
|
||||
Ok(npu) => {
|
||||
eprintln!(
|
||||
"leleka-npu-worker: NPU prefill on {} for prompts of at least {} tokens (batch {})",
|
||||
npu.device_name(),
|
||||
args.min_tokens,
|
||||
args.batch
|
||||
);
|
||||
engine.set_accelerator(
|
||||
Box::new(npu.with_memory_probe(Box::new(available_memory))),
|
||||
args.min_tokens,
|
||||
);
|
||||
}
|
||||
Err(e) => eprintln!("leleka-npu-worker: {e}; serving from the CPU only"),
|
||||
}
|
||||
}
|
||||
}
|
||||
attach_npu(&mut engine, args);
|
||||
let stdin = BufReader::new(io::stdin());
|
||||
match serve(
|
||||
&mut engine,
|
||||
stdin,
|
||||
io::stdout(),
|
||||
&loaded.stop_tokens,
|
||||
args.seed,
|
||||
) {
|
||||
match serve(&mut engine, stdin, io::stdout(), &loaded.stop_tokens, args.seed) {
|
||||
Ok(()) => ExitCode::SUCCESS,
|
||||
Err(e) => {
|
||||
eprintln!("leleka-npu-worker: {e}");
|
||||
|
||||
Reference in New Issue
Block a user