use std::sync::Arc;
use crate::generate::{DecodeError, FinishReason, Usage};
use super::status::DEFAULT_DECODE_LOG_INTERVAL;
pub(super) type DecodeFn = Arc<dyn Fn(&[usize]) -> String + Send + Sync>;
pub(super) type JobResult = Result<(FinishReason, Vec<usize>, String, Usage), DecodeError>;
pub const DEFAULT_PREFILL_CHUNK: usize = 128;
pub const DEFAULT_KV_BLOCK_SIZE: usize = 256;
pub const DEFAULT_MAX_QUEUE: usize = 512;
#[derive(Clone, Copy, Debug)]
pub struct BatcherConfig {
pub max_seqs: usize,
pub prefill_chunk: usize,
pub max_queue: usize,
pub kv_block_size: usize,
pub kv_blocks: Option<usize>,
pub max_context: Option<usize>,
}
impl Default for BatcherConfig {
fn default() -> Self {
BatcherConfig {
max_seqs: usize::MAX,
prefill_chunk: DEFAULT_PREFILL_CHUNK,
max_queue: DEFAULT_MAX_QUEUE,
kv_block_size: DEFAULT_KV_BLOCK_SIZE,
kv_blocks: None,
max_context: None,
}
}
}
impl BatcherConfig {
pub fn from_env() -> Self {
BatcherConfig {
max_seqs: env_positive("FERROX_CB_MAX_SEQS").unwrap_or(usize::MAX),
prefill_chunk: env_positive("FERROX_CB_PREFILL_CHUNK").unwrap_or(DEFAULT_PREFILL_CHUNK),
max_queue: env_positive("FERROX_CB_MAX_QUEUE").unwrap_or(DEFAULT_MAX_QUEUE),
kv_block_size: env_positive("FERROX_CB_KV_BLOCK_SIZE").unwrap_or(DEFAULT_KV_BLOCK_SIZE),
kv_blocks: env_positive("FERROX_CB_KV_BLOCKS"),
max_context: env_positive("FERROX_CB_MAX_CONTEXT"),
}
}
}
pub(super) fn env_positive(name: &str) -> Option<usize> {
let raw = std::env::var(name).ok()?;
let value: usize = raw
.parse()
.unwrap_or_else(|_| panic!("{name} must be a positive integer"));
assert!(value > 0, "{name} must be a positive integer");
Some(value)
}
pub(super) fn decode_log_interval_from_env() -> usize {
std::env::var("FERROX_DECODE_LOG_INTERVAL")
.ok()
.and_then(|raw| raw.parse().ok())
.unwrap_or(DEFAULT_DECODE_LOG_INTERVAL)
}