use alloc::collections::BTreeMap;
use serde::Deserialize;
use super::LayaQuestionKind;
use crate::errors::{
BunsenError,
BunsenResult,
};
#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct LayaAgentConfig {
pub head_layers: usize,
pub max_len: usize,
pub head_max_len: usize,
#[serde(default)]
pub act_costs: BTreeMap<String, f64>,
pub temperature: [f64; 3],
#[serde(default)]
pub temperature_by_options: BTreeMap<String, f64>,
}
impl LayaAgentConfig {
pub fn parse(json: &str) -> BunsenResult<Self> {
serde_json::from_str(json)
.map_err(|e| BunsenError::invalid_resource("an rl_agent_config.json").with_cause(e))
}
pub fn n_act(&self) -> usize {
self.act_costs.len() + 1
}
pub fn temperature_bucket(
kind: LayaQuestionKind,
options: usize,
) -> String {
let size = match options {
0..=2 => "2",
3..=5 => "3-5",
6..=10 => "6-10",
_ => "11+",
};
format!("{}:{size}", kind.name())
}
pub fn temperature(
&self,
kind: LayaQuestionKind,
options: usize,
) -> f64 {
self.temperature_by_options
.get(&Self::temperature_bucket(kind, options))
.copied()
.unwrap_or(self.temperature[kind.index()])
}
pub fn probabilities(
&self,
kind: LayaQuestionKind,
logits: &[f32],
) -> Vec<f64> {
let temperature = self.temperature(kind, logits.len());
let z: Vec<f64> = logits.iter().map(|&l| f64::from(l) / temperature).collect();
let max = z.iter().copied().fold(f64::NEG_INFINITY, f64::max);
let exp: Vec<f64> = z.iter().map(|z| (z - max).exp()).collect();
let sum: f64 = exp.iter().sum();
exp.into_iter().map(|e| e / sum).collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
const JSON: &str = r#"{
"encoder": "answerdotai/ModernBERT-large", "head_layers": 2, "max_len": 512,
"head_max_len": 192, "act_costs": {"escalate": 0.5}, "amp_dtype": "bf16",
"temperature": [1.5, 1.25, 2.0],
"temperature_by_options": {"choice:3-5": 1.75, "noul:2": 1.9},
"training": {"updates": 7313}
}"#;
#[test]
fn test_parse() {
let config = LayaAgentConfig::parse(JSON).unwrap();
assert_eq!(
(config.head_layers, config.max_len, config.head_max_len),
(2, 512, 192)
);
assert_eq!(config.n_act(), 2);
assert!(LayaAgentConfig::parse("{}").is_err());
}
#[test]
fn test_temperature() {
use LayaQuestionKind::*;
let config = LayaAgentConfig::parse(JSON).unwrap();
assert_eq!(LayaAgentConfig::temperature_bucket(Choice, 4), "choice:3-5");
assert_eq!(LayaAgentConfig::temperature_bucket(Score, 11), "score:11+");
assert_eq!(config.temperature(Choice, 4), 1.75);
assert_eq!(config.temperature(Choice, 2), 1.5);
assert_eq!(config.temperature(Noul, 2), 1.9);
assert_eq!(config.temperature(Score, 7), 1.25);
}
#[test]
fn test_probabilities() {
let config = LayaAgentConfig::parse(JSON).unwrap();
let t = config.temperature(LayaQuestionKind::Noul, 2);
let gap = (4.0f64.ln() * t) as f32;
let p = config.probabilities(LayaQuestionKind::Noul, &[gap, 0.0]);
assert!(
(p[0] - 0.8).abs() < 1e-6 && (p[1] - 0.2).abs() < 1e-6,
"{p:?}"
);
}
}