hf2q 0.1.32

Pure Rust CLI for converting HuggingFace models to hardware-optimized formats and serving them over an OpenAI-compatible API on Apple Silicon
//! Per-step execution choices of a Qwen joint decode step and their
//! switches.
//!
//! The serving worker parses the switches once at start
//! (`qwen35_joint_decode_config`); `forward_gpu_decode_joint_with_options`
//! takes the options explicitly, so one process can run several (the Q1
//! trace).

use super::joint_projection::JointProjectionKernel;

/// Switch for [`JointStepOptions::pooled_outputs`]: `1`/`on`/`true` takes the
/// joint layer chain's multi-row outputs from the decode pool
/// ([`super::decode_pool::StepPooledOutputs`]); unset, blank or
/// `0`/`off`/`false` keeps fresh device allocations. Either mode.
pub(crate) const QWEN_JOINT_POOLED_OUTPUTS_ENV: &str = "HF2Q_QWEN_JOINT_POOLED_OUTPUTS";

/// Production kill switch for the combined joint MTP candidate. Dense Qwen
/// artifacts with an MTP head default to joint MTP verify and, in padded
/// mode, the column-invariant projection. `0`/`off`/`false` restores serial
/// K3 and the canonical projection. MoE and artifacts without MTP stay off by
/// default. The worker reads it once at start with the other joint switches.
pub(crate) const QWEN_JOINT_MTP_ENV: &str = "HF2Q_QWEN_JOINT_MTP";

/// Per-step execution choices of a joint step that its route mode does not
/// fix. The default turns every choice off.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(crate) struct JointStepOptions {
    pub projection: JointProjectionKernel,
    /// Multi-row layer outputs from the decode pool (Class A).
    pub pooled_outputs: bool,
    /// DFlash2 verify steps only: tiled TQ full attention
    /// ([`super::verify_attn_tile`], Class B). Decode steps never take it and
    /// MTP verify rejects it.
    pub verify_attention_tile: bool,
    /// DFlash2 verify steps only: the fused mixer stage
    /// ([`super::joint_mixer_fused`], Class A). Decode steps never take it
    /// and MTP verify rejects it.
    pub verify_mixer_fused: bool,
    /// DFlash2 verify steps only: the cycle-tail choices.
    pub dflash2: DFlash2VerifyOptions,
}

/// Choices of a DFlash2 verify step beyond its kernels' routes. Decode
/// steps and MTP verify ignore them. All off by default.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(crate) struct DFlash2VerifyOptions {
    /// The step's fresh layer outputs stay allocated until its commit
    /// settles instead of being released behind the output head, so the
    /// residency-set commit their release triggers is paid by a command
    /// that is not between the head and the next draft (Class A: only when
    /// the buffers are freed changes).
    pub hold_layer_outputs: bool,
}

impl DFlash2VerifyOptions {
    /// Every choice off.
    pub(crate) const OFF: Self = Self {
        hold_layer_outputs: false,
    };
}

/// Parse an on/off switch named `name`; unset or blank means off.
pub(crate) fn parse_switch(name: &str, raw: Option<&str>) -> Result<bool, String> {
    let Some(raw) = raw.map(str::trim).filter(|raw| !raw.is_empty()) else {
        return Ok(false);
    };
    match raw.to_ascii_lowercase().as_str() {
        "1" | "on" | "true" => Ok(true),
        "0" | "off" | "false" => Ok(false),
        _ => Err(format!(
            "{name} must be 1/on/true or 0/off/false, got {raw:?}"
        )),
    }
}

/// [`parse_switch`] over the process environment.
pub(crate) fn switch_from_process(name: &str) -> Result<bool, String> {
    parse_switch(name, std::env::var(name).ok().as_deref())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn switch_parses_on_off_and_rejects_other_values() {
        let parse = |raw| parse_switch(QWEN_JOINT_POOLED_OUTPUTS_ENV, raw);
        assert_eq!(parse(None), Ok(false));
        assert_eq!(parse(Some("  ")), Ok(false));
        for on in ["1", "on", "TRUE"] {
            assert_eq!(parse(Some(on)), Ok(true));
        }
        for off in ["0", "off", "False"] {
            assert_eq!(parse(Some(off)), Ok(false));
        }
        assert!(parse(Some("pooled")).is_err());
    }
}