| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- //! Types for the `before_serve` configuration and `exec_aliases` table.
- //!
- //! Lives outside `config.rs` so the latter stays under the 500-line limit
- //! the project enforces (see AGENTS.md).
- use serde::Deserialize;
- /// Behaviour on a non-zero exit code for a [`PreServeCommand`].
- #[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Deserialize)]
- #[serde(rename_all = "lowercase")]
- pub enum OnFailure {
- #[default]
- Abort,
- Warn,
- Continue,
- }
- /// One entry in a `before_serve` block.
- #[derive(Clone, Debug, Deserialize)]
- #[serde(untagged)]
- pub enum PreServeCommand {
- Bare(String),
- Configured {
- command: String,
- #[serde(default)]
- on_failure: OnFailure,
- },
- }
- impl PreServeCommand {
- pub fn command(&self) -> &str {
- match self {
- PreServeCommand::Bare(c) => c,
- PreServeCommand::Configured { command, .. } => command,
- }
- }
- pub fn on_failure(&self) -> OnFailure {
- match self {
- PreServeCommand::Bare(_) => OnFailure::default(),
- PreServeCommand::Configured { on_failure, .. } => *on_failure,
- }
- }
- }
- #[derive(Clone, Debug, Default, Deserialize)]
- pub struct BeforeServeTable {
- #[serde(default)]
- pub commands: Vec<PreServeCommand>,
- }
- #[derive(Clone, Debug, Default, Deserialize)]
- pub struct ModsTable {
- #[serde(default)]
- pub commands: Vec<PreServeCommand>,
- }
- /// Distributed execution backend — `mp` or `ray`. Absent means single-node.
- #[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
- #[serde(rename_all = "lowercase")]
- pub enum DistributedBackend {
- Mp,
- Ray,
- }
- /// Recipe arguments — either an inline TOML string (split by whitespace) or
- /// a `Vec<String>` list. Accepted in TOML as either:
- ///
- /// ```toml
- /// args = """
- /// --gpu-memory-utilization 0.90
- /// --max-model-len 262144
- /// """
- /// ```
- ///
- /// ```toml
- /// args = ["--gpu-memory-utilization", "0.90"]
- /// ```
- #[derive(Clone, Debug, Deserialize)]
- #[serde(untagged)]
- pub enum RecipeArgs {
- List(Vec<String>),
- Inline(String),
- }
- impl Default for RecipeArgs {
- fn default() -> Self {
- RecipeArgs::List(Vec::new())
- }
- }
- impl RecipeArgs {
- pub fn to_vec(&self) -> Vec<String> {
- match self {
- RecipeArgs::Inline(s) => s.split_whitespace().map(String::from).collect(),
- RecipeArgs::List(v) => v.clone(),
- }
- }
- /// Replace all occurrences of `placeholder` with `replacement` in each
- /// arg string. Used to substitute model path references (e.g.
- /// `{{mtp_draft}}`) with resolved HF cache paths per-node.
- pub fn substitute(&self, placeholder: &str, replacement: &str) -> RecipeArgs {
- match self {
- RecipeArgs::Inline(s) => RecipeArgs::Inline(s.replace(placeholder, replacement)),
- RecipeArgs::List(v) => RecipeArgs::List(
- v.iter()
- .map(|a| a.replace(placeholder, replacement))
- .collect(),
- ),
- }
- }
- }
- impl std::fmt::Display for DistributedBackend {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- match self {
- DistributedBackend::Mp => write!(f, "mp"),
- DistributedBackend::Ray => write!(f, "ray"),
- }
- }
- }
|