fn refuse_constraint_on(config: &InferenceConfig, path: &str, removed_by: &str) -> Result<()> {
if config.constraint.is_some() {
return Err(RealizarError::Constraint(
crate::constrain::ConstraintError::UnsupportedPath {
path: path.to_string(),
removed_by: removed_by.to_string(),
},
));
}
Ok(())
}
const TRACE_REMOVED_BY: &str = "not scheduled: `--trace` runs the instrumented loop, which \
applies no constraint; without `--trace` the engine runs and applies it";
fn dense_cuda_attempted(config: &InferenceConfig, has_legacy_quant: bool) -> bool {
session_cuda_attempted(config) && !has_legacy_quant
}
fn session_cuda_attempted(config: &InferenceConfig) -> bool {
cfg!(feature = "cuda") && !config.no_gpu
}
fn wgpu_attempted(
config: &InferenceConfig,
gen_config: &crate::gguf::QuantizedGenerateConfig,
has_legacy_quant: bool,
) -> bool {
#[cfg(feature = "gpu")]
{
wgpu_fallback_allowed(config.no_gpu, config.accel_forced, has_legacy_quant)
&& crate::sampling::is_greedy(gen_config.temperature, gen_config.top_k)
}
#[cfg(not(feature = "gpu"))]
{
let _ = (config, gen_config, has_legacy_quant);
false
}
}
fn unconstrained_gguf_path(
config: &InferenceConfig,
gen_config: &crate::gguf::QuantizedGenerateConfig,
model: &crate::gguf::OwnedQuantizedModel,
is_qwen35: bool,
) -> Option<(&'static str, &'static str)> {
if crate::gguf::moe_forward_handles(&model.config.architecture) {
return Some((
"qwen3-moe",
"not scheduled: the MoE loop is outside #3568's four PRs",
));
}
if is_qwen35 {
return None;
}
let has_legacy_quant = model_has_legacy_quant(model);
if dense_cuda_attempted(config, has_legacy_quant) {
return gen_config
.trace
.then_some(("gguf-cuda-trace", TRACE_REMOVED_BY));
}
if wgpu_attempted(config, gen_config, has_legacy_quant) {
return Some((
"gguf-wgpu",
"not scheduled: the wgpu loop is outside #3568's four PRs; `--no-gpu` runs the \
CPU, which applies the constraint",
));
}
gen_config.trace.then_some(("gguf-trace", TRACE_REMOVED_BY))
}
fn generate_gguf_constrained(
request: &crate::constrain::ConstraintRequest,
config: &InferenceConfig,
mapped: &crate::gguf::MappedGGUFModel,
owned: Option<crate::gguf::OwnedQuantizedModel>,
qwen35_host: Option<&'static crate::gguf::forward_qwen35::Qwen35Model<'static>>,
input_tokens: &[u32],
gen_config: &crate::gguf::QuantizedGenerateConfig,
) -> Result<(Vec<u32>, bool, bool, crate::session::ConstrainedStop)> {
use crate::constrain::ConstraintError;
let no_model = || RealizarError::InvalidShape {
reason: "no model was loaded".to_string(),
};
let model = owned
.as_ref()
.or(qwen35_host.map(|q| q.base))
.ok_or_else(no_model)?;
if let Some((path, removed_by)) =
unconstrained_gguf_path(config, gen_config, model, qwen35_host.is_some())
{
return Err(RealizarError::Constraint(
ConstraintError::UnsupportedPath {
path: path.to_string(),
removed_by: removed_by.to_string(),
},
));
}
let env = mapped.constraint_env().map_err(RealizarError::Constraint)?;
let mut constraint = request.compile(&env).map_err(RealizarError::Constraint)?;
let context_length = model.config.context_length;
let (turn, stop, gpu_attempted) = if let Some(qwen) = qwen35_host {
crate::gguf::forward_qwen35::qwen35_check_context(input_tokens.len(), context_length)?;
let positions = (input_tokens.len() + gen_config.max_tokens).min(context_length);
let mut session = crate::gguf::qwen35_session::Qwen35Session::load_for_run(
qwen,
mapped,
config.no_gpu,
positions,
)?;
mark_generation_start(); let (turn, stop) =
session.generate_constrained(input_tokens, gen_config, constraint.as_mut())?;
(turn, stop, session_cuda_attempted(config))
} else if let Some(model) = owned {
if input_tokens.len() > context_length {
return Err(RealizarError::ContextLimitExceeded {
provided: input_tokens.len(),
maximum: context_length,
});
}
let gpu_attempted = dense_cuda_attempted(config, model_has_legacy_quant(&model));
let (turn, stop) = dense_constrained_turn(
model,
config,
gpu_attempted,
input_tokens,
gen_config,
constraint.as_mut(),
)?;
(turn, stop, gpu_attempted)
} else {
return Err(no_model());
};
Ok((turn.tokens, turn.used_gpu, gpu_attempted, stop))
}
fn dense_constrained_turn(
model: crate::gguf::OwnedQuantizedModel,
config: &InferenceConfig,
on_device: bool,
input_tokens: &[u32],
gen_config: &crate::gguf::QuantizedGenerateConfig,
constraint: &mut dyn crate::constrain::TokenConstraint,
) -> Result<(crate::session::Turn, crate::session::ConstrainedStop)> {
use crate::gguf::dense_session::{DenseForward, DenseSession};
#[cfg(feature = "cuda")]
let model = if on_device {
match cuda_dense_model(model, input_tokens, gen_config, config.verbose) {
Ok(cuda_model) => {
mark_generation_start(); let mut session = DenseSession::new(DenseForward::cuda(cuda_model));
return session.generate_constrained(input_tokens, gen_config, constraint);
},
Err(model) => *model,
}
} else {
model
};
#[cfg(not(feature = "cuda"))]
let _ = on_device; log_cpu_backend(config.verbose, model_has_legacy_quant(&model));
let mut session = DenseSession::new(DenseForward::cpu(std::sync::Arc::new(model)));
mark_generation_start(); session.generate_constrained(input_tokens, gen_config, constraint)
}
fn gguf_finish_reason(
constrained: Option<crate::session::ConstrainedStop>,
generated: &[u32],
stop_tokens: &[u32],
budget: usize,
) -> run_report::FinishReason {
use crate::session::ConstrainedStop;
match constrained {
Some(ConstrainedStop::Complete) => run_report::FinishReason::ConstraintComplete,
Some(ConstrainedStop::Length) => run_report::FinishReason::Length,
None => run_report::FinishReason::from_decode(generated, stop_tokens, budget),
}
}