use serde::{
Serialize,
de::DeserializeOwned,
};
use serde_json::Value;
use crate::{
data::{
builds::BuildRef,
catalog::presets::{
PresetSelection,
ResolvedPresetSet,
},
},
errors::{
BunsenError,
BunsenResult,
},
support::merge_patch,
};
#[derive(Clone, Debug, PartialEq)]
pub struct ResolvedPresets<C> {
pub config: C,
pub presets: Vec<String>,
pub patch: Option<Value>,
}
impl<C> ResolvedPresets<C> {
pub fn applied(&self) -> PresetSelection {
PresetSelection {
presets: self.presets.clone(),
patch: self.patch.clone(),
}
}
}
pub fn resolve_presets<C>(
base: &C,
set: &ResolvedPresetSet,
selection: &PresetSelection,
) -> BunsenResult<ResolvedPresets<C>>
where
C: Serialize + DeserializeOwned,
{
let selected = set.check_selection(selection)?;
let presets = set.applied(&selected);
let mut value = serde_json::to_value(base)
.map_err(|e| BunsenError::internal("a config that does not serialize").with_cause(e))?;
let open = merge_patch::empty_objects(&value);
apply_layer::<C>(&mut value, &set.common, &open).map_err(|e| e.context("the base patch"))?;
for name in &presets {
let preset = set.get(name).expect("checked: every applied preset exists");
apply_layer::<C>(&mut value, &preset.patch, &open)
.map_err(|e| e.context(format!("the preset `{name}`")))?;
}
if let Some(patch) = &selection.patch {
apply_layer::<C>(&mut value, patch, &open).map_err(|e| e.context("the lookup's patch"))?;
}
let config = serde_json::from_value(value).map_err(|e| {
BunsenError::policy("presets that resolve to an invalid config").with_cause(e)
})?;
Ok(ResolvedPresets {
config,
presets,
patch: selection.patch.clone(),
})
}
pub fn resolve_ref_presets<C>(
base: &C,
model: &BuildRef,
family: &ResolvedPresetSet,
) -> BunsenResult<ResolvedPresets<C>>
where
C: Serialize + DeserializeOwned,
{
let none = PresetSelection::default();
let selection = model.selection().unwrap_or(&none);
let set = match model.build() {
Some((_, build)) if !build.presets.is_empty() => &build.presets,
_ => family,
};
resolve_presets(base, set, selection)
}
fn apply_layer<C>(
value: &mut Value,
patch: &Value,
open: &[merge_patch::PatchPath],
) -> BunsenResult<()>
where
C: Serialize + DeserializeOwned,
{
merge_patch::validate_paths_open(value, patch, open)?;
merge_patch::apply(value, patch);
let typed: C = serde_json::from_value(value.clone()).map_err(|e| {
BunsenError::policy("presets that resolve to an invalid config").with_cause(e)
})?;
*value = serde_json::to_value(typed)
.map_err(|e| BunsenError::internal("a config that does not serialize").with_cause(e))?;
Ok(())
}
#[cfg(test)]
mod tests {
use serde::Deserialize;
use serde_json::json;
use super::*;
use crate::{
data::catalog::{
CatalogName,
document::{
AxisKind,
MergeMode,
PresetEntry,
},
presets::{
AxisInfo,
ResolvedAxisUse,
},
},
errors::{
BunsenErrorKind,
LookupError,
testing::ErrorMatcher,
},
};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct Sampling {
temperature: f64,
top_p: f64,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct Toy {
thinking: bool,
sampling: Sampling,
beam: usize,
yarn: Option<f64>,
}
fn base() -> Toy {
Toy {
thinking: false,
sampling: Sampling {
temperature: 1.0,
top_p: 1.0,
},
beam: 1,
yarn: None,
}
}
fn preset(
name: &str,
patch: Value,
) -> PresetEntry {
PresetEntry {
name: name.to_string(),
merge: MergeMode::Update,
description: format!("the {name} preset"),
patch,
}
}
fn set() -> ResolvedPresetSet {
let thinking_mode = vec!["thinking".to_string(), "no-thinking".to_string()];
ResolvedPresetSet {
presets: vec![
preset(
"thinking",
json!({"thinking": true, "sampling": {"temperature": 0.6, "top_p": 0.95}}),
),
preset(
"no-thinking",
json!({"thinking": false, "sampling": {"temperature": 0.7, "top_p": 0.8}}),
),
preset("yarn-128k", json!({"yarn": 4.0})),
preset("wide", json!({"beam": 5})),
preset("narrow", json!({"beam": 2})),
],
axes: vec![ResolvedAxisUse {
axis: AxisInfo {
name: CatalogName::root("thinking-mode").unwrap(),
kind: AxisKind::Categorical,
members: thinking_mode.clone(),
description: "whether the model thinks before it answers".to_string(),
},
offers: thinking_mode,
default: "thinking".to_string(),
}],
..Default::default()
}
}
fn resolved(selection: PresetSelection) -> BunsenResult<ResolvedPresets<Toy>> {
resolve_presets(&base(), &set(), &selection)
}
#[test]
fn test_no_selection_is_the_default() {
let r = resolved(PresetSelection::default()).unwrap();
assert_eq!(r.presets, vec!["thinking"]);
assert_eq!(
r.config,
Toy {
thinking: true,
sampling: Sampling {
temperature: 0.6,
top_p: 0.95
},
..base()
}
);
assert_eq!(r.patch, None);
}
#[test]
fn test_an_axis_member_displaces_the_default() {
let r = resolved(PresetSelection::of(["no-thinking"])).unwrap();
assert_eq!(r.presets, vec!["no-thinking"]);
assert!(!r.config.thinking);
assert_eq!(r.config.sampling.temperature, 0.7);
let r = resolved(PresetSelection::of(["thinking"])).unwrap();
assert_eq!(r.presets, vec!["thinking"]);
}
#[test]
fn test_presets_off_the_axis_add_to_the_default() {
let r = resolved(PresetSelection::of(["yarn-128k", "wide"])).unwrap();
assert_eq!(r.presets, vec!["thinking", "yarn-128k", "wide"]);
assert_eq!(r.config.yarn, Some(4.0));
assert_eq!(r.config.beam, 5);
assert!(r.config.thinking);
}
#[test]
fn test_selected_presets_commute() {
let a = resolved(PresetSelection::of(["no-thinking", "yarn-128k"])).unwrap();
let b = resolved(PresetSelection::of(["yarn-128k", "no-thinking"])).unwrap();
assert_eq!(a.config, b.config);
}
#[test]
fn test_the_patch_is_last() {
let r = resolved(
PresetSelection::of(["wide"])
.with_patch(json!({"beam": 3, "sampling": {"top_p": 0.5}})),
)
.unwrap();
assert_eq!(r.config.beam, 3);
assert_eq!(r.config.sampling.top_p, 0.5);
assert_eq!(r.config.sampling.temperature, 0.6);
assert_eq!(
r.patch,
Some(json!({"beam": 3, "sampling": {"top_p": 0.5}}))
);
}
#[test]
fn test_an_unknown_preset_lists_the_candidates() {
ErrorMatcher::kind(BunsenErrorKind::Lookup)
.message_contains(
"no preset `thinkin`; there are: thinking, no-thinking, yarn-128k, wide, narrow",
)
.assert_err(&resolved(PresetSelection::of(["thinkin"])));
}
#[test]
fn test_a_preset_selected_twice() {
ErrorMatcher::kind(BunsenErrorKind::Policy)
.message_contains("the preset `wide` is selected twice")
.assert_err(&resolved(PresetSelection::of(["wide", "wide"])));
}
#[test]
fn test_two_members_of_an_axis() {
ErrorMatcher::kind(BunsenErrorKind::Policy)
.message_contains(
"`thinking` and `no-thinking` are both on the axis `thinking-mode`: select one",
)
.assert_err(&resolved(PresetSelection::of(["thinking", "no-thinking"])));
}
#[test]
fn test_selected_presets_that_set_one_path() {
ErrorMatcher::kind(BunsenErrorKind::Policy)
.message_contains("the presets `wide` and `narrow` each set `beam`: select one")
.assert_err(&resolved(PresetSelection::of(["wide", "narrow"])));
}
#[test]
fn test_a_patch_naming_an_unknown_field() {
let r = resolved(PresetSelection::default().with_patch(json!({"sampling": {"top_q": 1}})));
ErrorMatcher::kind(BunsenErrorKind::Lookup)
.message_contains("sampling.top_q")
.has_cause::<LookupError>()
.assert_err(&r);
assert!(
r.unwrap_err().to_string().contains("the lookup's patch"),
"the error names the layer"
);
}
#[test]
fn test_a_result_that_does_not_deserialize() {
ErrorMatcher::kind(BunsenErrorKind::Policy)
.message_contains("invalid config")
.assert_err(&resolved(
PresetSelection::default().with_patch(json!({"beam": "five"})),
));
}
#[test]
fn test_an_empty_set_resolves_to_the_base() {
let r = resolve_presets(
&base(),
&ResolvedPresetSet::default(),
&PresetSelection::default(),
)
.unwrap();
assert_eq!(r.config, base());
assert!(r.presets.is_empty());
}
#[test]
fn test_resolve_ref_presets_falls_back_to_the_family() {
use crate::data::{
catalog::{
CatalogName,
ResolvedBuild,
},
resources::ResourceSet,
};
let named = |presets: ResolvedPresetSet, selection: PresetSelection| {
let mut build =
ResolvedBuild::new(CatalogName::parse("hf/org/repo").unwrap(), "a repo");
build.presets = presets;
BuildRef::named("hf", build, selection)
};
let family = set();
let own = ResolvedPresetSet {
presets: vec![preset("wide", json!({"beam": 9}))],
..Default::default()
};
let r = resolve_ref_presets(&base(), &named(own, PresetSelection::of(["wide"])), &family)
.unwrap();
assert_eq!(r.config.beam, 9);
assert!(!r.config.thinking, "the row's own set, not the family's");
let r = resolve_ref_presets(
&base(),
&named(
ResolvedPresetSet::default(),
PresetSelection::of(["no-thinking"]),
),
&family,
)
.unwrap();
assert_eq!(r.presets, ["no-thinking"]);
let given = BuildRef::Given(ResourceSet::new("files"));
let r = resolve_ref_presets(&base(), &given, &family).unwrap();
assert_eq!(r.presets, ["thinking"]);
let r = resolve_ref_presets(&base(), &given, &ResolvedPresetSet::default()).unwrap();
assert_eq!(r.config, base());
}
#[test]
fn test_the_common_patch_is_under_every_selection() {
let mut set = set();
set.common = json!({"beam": 3, "yarn": 2.0});
let r = resolve_presets(&base(), &set, &PresetSelection::default()).unwrap();
assert_eq!((r.config.beam, r.config.yarn), (3, Some(2.0)));
let r =
resolve_presets(&base(), &set, &PresetSelection::of(["no-thinking", "wide"])).unwrap();
assert_eq!((r.config.beam, r.config.yarn), (5, Some(2.0)));
set.common = json!({"bean": 3});
ErrorMatcher::kind(BunsenErrorKind::Lookup)
.message_contains("bean")
.assert_err(&resolve_presets(&base(), &set, &PresetSelection::default()));
}
}