Play: the large Maia (79M), the slider in real strength
- The slider shows Maia's measured strength on the Lichess blitz scale (1100–2000, step 50); per-model tables turn it into her rating input. - The large model plays every strength once downloaded (316 MB); with only the small one, up to 1800, above it «Download and play» brings the large. - The network runs on half the cores; Maia's clock takes the computing when it is longer than her pause. - A rematch or a game from a position whose model is missing reopens the dialog with the same choices; the main button keeps its width; no game while a download runs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
7fdc22345e
commit
d4cccb3f68
8 changed files with 529 additions and 100 deletions
|
|
@ -1399,10 +1399,11 @@ impl App {
|
|||
}
|
||||
}
|
||||
|
||||
/// A new game against Maia (the weights are there: the dialog saw to it).
|
||||
/// A new game against Maia. Her model missing (a rematch, a game from a position):
|
||||
/// the dialog with the same choices, to download it.
|
||||
fn start_play(&mut self, ctx: &egui::Context, start: crate::play::Start) {
|
||||
let Some(weights) = crate::maia_download::installed() else {
|
||||
self.play_dialog = Some(crate::play::Dialog::new(&self.settings));
|
||||
let Ok(maia) = crate::play::model_for(start.strength) else {
|
||||
self.play_dialog = Some(crate::play::Dialog::for_start(&self.settings, &start));
|
||||
return;
|
||||
};
|
||||
// The engine judging Maia's resignation: ChessPenguin's Stockfish (its node count
|
||||
|
|
@ -1419,7 +1420,7 @@ impl App {
|
|||
None => self.new_game(),
|
||||
}
|
||||
if let Some(g) = self.game.as_mut() {
|
||||
g.start_play(start, weights, judge, ctx);
|
||||
g.start_play(start, maia, judge, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2080,7 +2080,7 @@ impl GameView {
|
|||
pub fn start_play(
|
||||
&mut self,
|
||||
start: crate::play::Start,
|
||||
weights: std::path::PathBuf,
|
||||
(model, weights): (crate::maia_download::Model, std::path::PathBuf),
|
||||
judge: Option<std::path::PathBuf>,
|
||||
ctx: &egui::Context,
|
||||
) {
|
||||
|
|
@ -2176,6 +2176,7 @@ impl GameView {
|
|||
self.play = Some(Session {
|
||||
user: start.user,
|
||||
strength: start.strength,
|
||||
model,
|
||||
user_elo: start.user_elo,
|
||||
clock: start.clock.map(Clock::new),
|
||||
opponent: Opponent::start(weights, ctx),
|
||||
|
|
@ -2328,7 +2329,7 @@ impl GameView {
|
|||
let elo = s.user_elo.unwrap_or(s.strength);
|
||||
s.opponent.ask(
|
||||
positions,
|
||||
elo,
|
||||
crate::play::rating_input(s.model, elo),
|
||||
s.strength,
|
||||
std::time::Duration::from_millis(30),
|
||||
);
|
||||
|
|
@ -2346,8 +2347,10 @@ impl GameView {
|
|||
} else {
|
||||
crate::play::pause(clock, ply, only)
|
||||
};
|
||||
// Her rating input is what makes her play at her strength; the user's is as is.
|
||||
let oppo = s.user_elo.unwrap_or(s.strength);
|
||||
s.opponent.ask(positions, s.strength, oppo, pause);
|
||||
let own = crate::play::rating_input(s.model, s.strength);
|
||||
s.opponent.ask(positions, own, oppo, pause);
|
||||
// Is her position lost? (Unless the user wants to play to the end.)
|
||||
if !s.play_on
|
||||
&& let (Some(j), Some(ep)) = (s.judge.as_mut(), engine_pos)
|
||||
|
|
@ -2455,7 +2458,7 @@ impl GameView {
|
|||
}
|
||||
}
|
||||
if let Some(err) = s.opponent.failed() {
|
||||
crate::maia_download::forget_broken();
|
||||
crate::maia_download::forget_broken(s.model);
|
||||
actions.push(GameAction::Toast(trf(
|
||||
"Maia did not start: {e}",
|
||||
&[("e", &err)],
|
||||
|
|
|
|||
|
|
@ -10870,15 +10870,26 @@ const ROWS: &[[&str; 9]] = &[
|
|||
"Шектеусіз",
|
||||
],
|
||||
[
|
||||
"The Lichess scale: Maia 1500 is weaker than a club player rated 1500.",
|
||||
"Шкала Lichess: Maia 1500 слабее клубного игрока с рейтингом 1500.",
|
||||
"Échelle Lichess : Maia 1500 est plus faible qu’un joueur de club classé 1500.",
|
||||
"Lichess-Skala: Maia 1500 ist schwächer als ein Vereinsspieler mit 1500.",
|
||||
"Escala de Lichess: Maia 1500 es más débil que un jugador de club con 1500.",
|
||||
"Scala Lichess: Maia 1500 è più debole di un giocatore di circolo con 1500.",
|
||||
"Escala do Lichess: Maia 1500 é mais fraca que um jogador de clube com 1500.",
|
||||
"Lichess shkalasi: Maia 1500 reytingi 1500 bo‘lgan klub o‘yinchisidan kuchsizroq.",
|
||||
"Lichess шкаласы: Maia 1500 рейтингі 1500 клуб ойыншысынан әлсіздеу.",
|
||||
"Her measured strength, on the Lichess blitz scale.",
|
||||
"Её измеренная сила, по шкале блица Lichess.",
|
||||
"Sa force mesurée, sur l’échelle du blitz Lichess.",
|
||||
"Ihre gemessene Spielstärke, auf der Lichess-Blitz-Skala.",
|
||||
"Su fuerza medida, en la escala de blitz de Lichess.",
|
||||
"La sua forza misurata, sulla scala del blitz di Lichess.",
|
||||
"A força medida dela, na escala de blitz do Lichess.",
|
||||
"Uning o‘lchangan kuchi, Lichess blits shkalasi bo‘yicha.",
|
||||
"Оның өлшенген күші, Lichess блиц шкаласы бойынша.",
|
||||
],
|
||||
[
|
||||
"Stronger than {elo} plays the large Maia: {mb} MB, downloaded once.",
|
||||
"Сильнее {elo} играет большая Maia: {mb} МБ, скачивается один раз.",
|
||||
"Au-delà de {elo}, c’est la grande Maia qui joue : {mb} Mo, téléchargée une seule fois.",
|
||||
"Stärker als {elo} spielt die große Maia: {mb} MB, einmalig heruntergeladen.",
|
||||
"Por encima de {elo} juega la Maia grande: {mb} MB, se descarga una sola vez.",
|
||||
"Oltre {elo} gioca la Maia grande: {mb} MB, si scarica una sola volta.",
|
||||
"Acima de {elo} joga a Maia grande: {mb} MB, baixada uma única vez.",
|
||||
"{elo} dan kuchliroq katta Maia o‘ynaydi: {mb} MB, bir marta yuklab olinadi.",
|
||||
"{elo}-ден күштірек үлкен Maia ойнайды: {mb} МБ, бір рет жүктеледі.",
|
||||
],
|
||||
[
|
||||
"Maia resigned",
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
//! Downloading the Maia-3 weights on request: the published checkpoint `maia3-5m.pt`
|
||||
//! of a pinned revision from huggingface.co/UofTCSSLab/Maia3-5M, checked against its
|
||||
//! SHA-256 and loaded once before use. Nothing is downloaded without the user asking;
|
||||
//! the file is the authors' own, not redistributed by us.
|
||||
//! Downloading the Maia-3 weights on request: a published checkpoint of a pinned
|
||||
//! revision from huggingface.co/UofTCSSLab (the large `maia3-79m.pt`; the small
|
||||
//! `maia3-5m.pt` of earlier versions is still used when it is there), checked against
|
||||
//! its SHA-256 and loaded once before use. Nothing is downloaded without the user
|
||||
//! asking; the file is the authors' own, not redistributed by us.
|
||||
|
||||
use std::io::{Read, Write};
|
||||
use std::path::PathBuf;
|
||||
|
|
@ -10,32 +11,76 @@ use std::sync::{Arc, Mutex};
|
|||
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
const REVISION: &str = "b6559de2398d7140b985f28fd2c19fb5e47ddabe";
|
||||
const FILE: &str = "maia3-5m.pt";
|
||||
const SHA256: &str = "ba14208b2992d85502f5fb501934abf6aaaeb355e9f3fdf90e326911f562524f";
|
||||
pub const SIZE: u64 = 20_968_049;
|
||||
/// The published models.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Model {
|
||||
/// 5M parameters: plays up to about 1800.
|
||||
Small,
|
||||
/// 79M parameters: stronger and closer to people, several times slower.
|
||||
Large,
|
||||
}
|
||||
|
||||
/// `<local data dir>/maia/maia3-5m.pt`.
|
||||
impl Model {
|
||||
fn repo(self) -> &'static str {
|
||||
match self {
|
||||
Model::Small => "Maia3-5M",
|
||||
Model::Large => "Maia3-79M",
|
||||
}
|
||||
}
|
||||
|
||||
fn revision(self) -> &'static str {
|
||||
match self {
|
||||
Model::Small => "b6559de2398d7140b985f28fd2c19fb5e47ddabe",
|
||||
Model::Large => "a107d6ceb7b298cb04ae1da4edffe2939858b894",
|
||||
}
|
||||
}
|
||||
|
||||
fn file(self) -> &'static str {
|
||||
match self {
|
||||
Model::Small => "maia3-5m.pt",
|
||||
Model::Large => "maia3-79m.pt",
|
||||
}
|
||||
}
|
||||
|
||||
fn sha256(self) -> &'static str {
|
||||
match self {
|
||||
Model::Small => "ba14208b2992d85502f5fb501934abf6aaaeb355e9f3fdf90e326911f562524f",
|
||||
Model::Large => "3fc6181d5db789b45a15305732148757ae74efa3e0028e81ba335b462dac45c2",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn size(self) -> u64 {
|
||||
match self {
|
||||
Model::Small => 20_968_049,
|
||||
Model::Large => 315_651_851,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `<local data dir>/maia/<file>`.
|
||||
fn dir() -> Option<PathBuf> {
|
||||
directories::ProjectDirs::from("io.github", "vrubelroman", "ChessPenguin")
|
||||
.map(|d| d.data_local_dir().join("maia"))
|
||||
}
|
||||
|
||||
/// The weights, when they are there (`CP_MAIA` points elsewhere: development).
|
||||
pub fn installed() -> Option<PathBuf> {
|
||||
/// The weights of `model`, when they are there. `CP_MAIA` (development) points to a
|
||||
/// file elsewhere: the large model when its name says 79m, else the small one.
|
||||
pub fn installed(model: Model) -> Option<PathBuf> {
|
||||
if let Some(p) = std::env::var_os("CP_MAIA") {
|
||||
return Some(PathBuf::from(p));
|
||||
let large = p.to_string_lossy().contains("79m");
|
||||
let p = PathBuf::from(p);
|
||||
return (large == (model == Model::Large) && p.is_file()).then_some(p);
|
||||
}
|
||||
let p = dir()?.join(FILE);
|
||||
let p = dir()?.join(model.file());
|
||||
p.is_file().then_some(p)
|
||||
}
|
||||
|
||||
/// The downloaded weights did not load: removed, so that the dialog offers them again.
|
||||
pub fn forget_broken() {
|
||||
pub fn forget_broken(model: Model) {
|
||||
if std::env::var_os("CP_MAIA").is_none()
|
||||
&& let Some(d) = dir()
|
||||
{
|
||||
let _ = std::fs::remove_file(d.join(FILE));
|
||||
let _ = std::fs::remove_file(d.join(model.file()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -49,12 +94,13 @@ pub enum Stage {
|
|||
|
||||
/// A download in a background thread.
|
||||
pub struct Download {
|
||||
pub model: Model,
|
||||
stage: Arc<Mutex<Stage>>,
|
||||
cancel: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl Download {
|
||||
pub fn start(ctx: &egui::Context) -> Download {
|
||||
pub fn start(ctx: &egui::Context, model: Model) -> Download {
|
||||
let stage = Arc::new(Mutex::new(Stage::Downloading { done: 0 }));
|
||||
let cancel = Arc::new(AtomicBool::new(false));
|
||||
let (s, c, ctx) = (stage.clone(), cancel.clone(), ctx.clone());
|
||||
|
|
@ -67,13 +113,17 @@ impl Download {
|
|||
}
|
||||
ctx.request_repaint();
|
||||
};
|
||||
let end = match run(&report, &c) {
|
||||
let end = match run(model, &report, &c) {
|
||||
Ok(p) => Stage::Done(p),
|
||||
Err(e) => Stage::Failed(e),
|
||||
};
|
||||
report(end);
|
||||
});
|
||||
Download { stage, cancel }
|
||||
Download {
|
||||
model,
|
||||
stage,
|
||||
cancel,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stage(&self) -> Stage {
|
||||
|
|
@ -90,14 +140,23 @@ impl Drop for Download {
|
|||
}
|
||||
}
|
||||
|
||||
fn run(report: &dyn Fn(Stage), cancel: &AtomicBool) -> Result<PathBuf, String> {
|
||||
fn run(model: Model, report: &dyn Fn(Stage), cancel: &AtomicBool) -> Result<PathBuf, String> {
|
||||
let dir = dir().ok_or("no data directory")?;
|
||||
std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
|
||||
let part = dir.join(format!("{FILE}.part"));
|
||||
let url = format!("https://huggingface.co/UofTCSSLab/Maia3-5M/resolve/{REVISION}/{FILE}");
|
||||
let name = model.file();
|
||||
let part = dir.join(format!("{name}.part"));
|
||||
let url = format!(
|
||||
"https://huggingface.co/UofTCSSLab/{}/resolve/{}/{name}",
|
||||
model.repo(),
|
||||
model.revision()
|
||||
);
|
||||
let result = (|| {
|
||||
let mut response = ureq::get(&url).call().map_err(|e| e.to_string())?;
|
||||
let mut body = response.body_mut().with_config().limit(SIZE + 1).reader();
|
||||
let mut body = response
|
||||
.body_mut()
|
||||
.with_config()
|
||||
.limit(model.size() + 1)
|
||||
.reader();
|
||||
let mut file = std::fs::File::create(&part).map_err(|e| e.to_string())?;
|
||||
let mut hasher = Sha256::new();
|
||||
let mut buf = vec![0u8; 256 * 1024];
|
||||
|
|
@ -121,12 +180,12 @@ fn run(report: &dyn Fn(Stage), cancel: &AtomicBool) -> Result<PathBuf, String> {
|
|||
.iter()
|
||||
.map(|b| format!("{b:02x}"))
|
||||
.collect();
|
||||
if done != SIZE || digest != SHA256 {
|
||||
if done != model.size() || digest != model.sha256() {
|
||||
return Err(crate::i18n::tr("The file is not the published one").to_owned());
|
||||
}
|
||||
report(Stage::Checking);
|
||||
maia::Maia::load(&part).map_err(|e| e.to_string())?;
|
||||
let dest = dir.join(FILE);
|
||||
let dest = dir.join(name);
|
||||
std::fs::rename(&part, &dest).map_err(|e| e.to_string())?;
|
||||
Ok(dest)
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ use chess_core::shakmaty::{Chess, Color, EnPassantMode, Move, Position};
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::i18n::tr;
|
||||
use crate::maia_download::Model;
|
||||
|
||||
// ----- time controls --------------------------------------------------------------------
|
||||
|
||||
|
|
@ -132,7 +133,9 @@ pub struct PlayPrefs {
|
|||
pub color: ColorChoice,
|
||||
/// `None`: no clock.
|
||||
pub clock: Option<TimeControl>,
|
||||
/// Maia's rating, 600–2600.
|
||||
/// Maia's strength: a rating on the Lichess blitz scale (`level`: before 1.5 the
|
||||
/// field `strength` held her rating input, a different scale, and is dropped).
|
||||
#[serde(rename = "level")]
|
||||
pub strength: u32,
|
||||
}
|
||||
|
||||
|
|
@ -149,8 +152,63 @@ impl Default for PlayPrefs {
|
|||
}
|
||||
}
|
||||
|
||||
pub const STRENGTH_MIN: u32 = 600;
|
||||
pub const STRENGTH_MAX: u32 = 2600;
|
||||
pub const STRENGTH_MIN: u32 = 1100;
|
||||
pub const STRENGTH_MAX: u32 = 2000;
|
||||
|
||||
// ----- Maia's strength ----------------------------------------------------------------
|
||||
|
||||
/// The strongest the small model plays (with the large one missing).
|
||||
pub const SMALL_MAX: u32 = 1800;
|
||||
|
||||
/// Measured (docs/research/maia-strength.md): Maia's rating input → her strength on
|
||||
/// the Lichess blitz scale, as she plays here (temperature 1, rare moves dropped).
|
||||
const SMALL: &[(u32, u32)] = &[
|
||||
(1100, 1314),
|
||||
(1300, 1426),
|
||||
(1500, 1485),
|
||||
(1700, 1563),
|
||||
(1900, 1632),
|
||||
(2100, 1683),
|
||||
(2300, 1737),
|
||||
(2600, 1800),
|
||||
];
|
||||
const LARGE: &[(u32, u32)] = &[(1500, 1615), (1900, 1794), (2300, 1964), (2600, 2000)];
|
||||
|
||||
/// The rating to give `model` for it to play at `strength` (between the measured
|
||||
/// points, in proportion; outside them, the nearest).
|
||||
pub fn rating_input(model: Model, strength: u32) -> u32 {
|
||||
let table = match model {
|
||||
Model::Small => SMALL,
|
||||
Model::Large => LARGE,
|
||||
};
|
||||
let (first, last) = (table[0], table[table.len() - 1]);
|
||||
if strength <= first.1 {
|
||||
return first.0;
|
||||
}
|
||||
if strength >= last.1 {
|
||||
return last.0;
|
||||
}
|
||||
let i = table
|
||||
.iter()
|
||||
.position(|p| p.1 >= strength)
|
||||
.unwrap_or(table.len() - 1);
|
||||
let ((a_in, a_out), (b_in, b_out)) = (table[i - 1], table[i]);
|
||||
let t = f64::from(strength - a_out) / f64::from(b_out - a_out);
|
||||
(f64::from(a_in) + t * f64::from(b_in - a_in)).round() as u32
|
||||
}
|
||||
|
||||
/// The model to play at `strength`, with its file: the large one when it is there,
|
||||
/// else the small one up to its ceiling. `Err`: the model to download.
|
||||
pub fn model_for(strength: u32) -> Result<(Model, std::path::PathBuf), Model> {
|
||||
use crate::maia_download::installed;
|
||||
if let Some(p) = installed(Model::Large) {
|
||||
return Ok((Model::Large, p));
|
||||
}
|
||||
match installed(Model::Small) {
|
||||
Some(p) if strength <= SMALL_MAX => Ok((Model::Small, p)),
|
||||
_ => Err(Model::Large),
|
||||
}
|
||||
}
|
||||
|
||||
// ----- the clock -----------------------------------------------------------------------
|
||||
|
||||
|
|
@ -406,7 +464,10 @@ impl Opponent {
|
|||
let m = maia::pick(&p, 1.0, random_unit());
|
||||
let rest = r.pause.saturating_sub(started.elapsed());
|
||||
std::thread::sleep(rest);
|
||||
if ans_tx.send((r.generation, m, r.pause)).is_err() {
|
||||
// Her clock: the pause, or the computing when it took longer (a slow
|
||||
// computer, the large model).
|
||||
let spent = r.pause.max(started.elapsed());
|
||||
if ans_tx.send((r.generation, m, spent)).is_err() {
|
||||
return;
|
||||
}
|
||||
ctx.request_repaint();
|
||||
|
|
@ -440,7 +501,8 @@ impl Opponent {
|
|||
self.thinking = false;
|
||||
}
|
||||
|
||||
/// The move asked for last, when it has come, with the pause it was given.
|
||||
/// The move asked for last, when it has come, with the time it took (its pause, or
|
||||
/// the computing when longer).
|
||||
pub fn answer(&mut self) -> Option<(Move, Duration)> {
|
||||
let mut out = None;
|
||||
while let Ok((g, m, pause)) = self.rx.try_recv() {
|
||||
|
|
@ -497,6 +559,28 @@ pub fn pause(clock: Option<(Duration, u32)>, ply: usize, only_move: bool) -> Dur
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn strength_to_rating_input() {
|
||||
use crate::maia_download::Model;
|
||||
// The measured points themselves, in proportion between them, the ends held.
|
||||
assert_eq!(rating_input(Model::Large, 1615), 1500);
|
||||
assert_eq!(rating_input(Model::Large, 2000), 2600);
|
||||
assert_eq!(rating_input(Model::Large, STRENGTH_MIN), 1500);
|
||||
assert_eq!(rating_input(Model::Small, 1485), 1500);
|
||||
assert_eq!(rating_input(Model::Small, SMALL_MAX), 2600);
|
||||
let mid = rating_input(Model::Large, (1615 + 1794) / 2);
|
||||
assert!((1690..=1710).contains(&mid), "{mid}");
|
||||
// Stronger always means a higher input.
|
||||
for model in [Model::Small, Model::Large] {
|
||||
let mut last = 0;
|
||||
for level in (STRENGTH_MIN..=STRENGTH_MAX).step_by(50) {
|
||||
let v = rating_input(model, level);
|
||||
assert!(v >= last, "{model:?} {level}");
|
||||
last = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maia_resigns_only_when_hopeless() {
|
||||
use uci::Score;
|
||||
|
|
@ -651,7 +735,9 @@ mod tests {
|
|||
/// A game against Maia in progress (lives in the game screen while it is played).
|
||||
pub struct Session {
|
||||
pub user: Color,
|
||||
/// Maia's strength (Lichess blitz scale) and the model playing it.
|
||||
pub strength: u32,
|
||||
pub model: Model,
|
||||
pub user_elo: Option<u32>,
|
||||
pub clock: Option<Clock>,
|
||||
pub opponent: Opponent,
|
||||
|
|
@ -841,6 +927,9 @@ pub struct Dialog {
|
|||
play_after: bool,
|
||||
/// The game goes on from a position of another game.
|
||||
from: Option<StartFrom>,
|
||||
/// The colour chosen is this game's, not a habit to remember (from a position, a
|
||||
/// rematch).
|
||||
keep_color: bool,
|
||||
focus: bool,
|
||||
}
|
||||
|
||||
|
|
@ -866,6 +955,7 @@ impl Dialog {
|
|||
play_after: false,
|
||||
focus: true,
|
||||
from: None,
|
||||
keep_color: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -877,6 +967,25 @@ impl Dialog {
|
|||
Color::Black => ColorChoice::Black,
|
||||
};
|
||||
d.from = Some(from);
|
||||
d.keep_color = true;
|
||||
d
|
||||
}
|
||||
|
||||
/// The dialog again for a game that could not start (its Maia missing): the same
|
||||
/// position, strength, colour and time.
|
||||
pub fn for_start(settings: &crate::settings::Settings, start: &Start) -> Dialog {
|
||||
let mut d = match &start.from {
|
||||
Some(from) => Dialog::new_from(settings, (**from).clone()),
|
||||
None => Dialog::new(settings),
|
||||
};
|
||||
d.prefs.strength = start.strength;
|
||||
d.prefs.clock = start.clock;
|
||||
d.custom = start.clock.is_some_and(|c| !PRESETS.contains(&c));
|
||||
d.prefs.color = match start.user {
|
||||
Color::White => ColorChoice::White,
|
||||
Color::Black => ColorChoice::Black,
|
||||
};
|
||||
d.keep_color = true;
|
||||
d
|
||||
}
|
||||
|
||||
|
|
@ -896,7 +1005,7 @@ impl Dialog {
|
|||
) -> Option<DialogOutcome> {
|
||||
use egui::RichText;
|
||||
let mut out = None;
|
||||
let installed = crate::maia_download::installed();
|
||||
let model = model_for(self.prefs.strength);
|
||||
let body_h = (ctx.content_rect().height() - 140.0).max(160.0);
|
||||
egui::Modal::new(egui::Id::new("play_dialog")).show(ctx, |ui| {
|
||||
ui.set_width(460.0);
|
||||
|
|
@ -984,13 +1093,13 @@ impl Dialog {
|
|||
ui.spacing_mut().slider_width = W - 2.0 * 34.0 - 2.0 * GAP;
|
||||
ui.add(
|
||||
egui::Slider::new(&mut self.prefs.strength, STRENGTH_MIN..=STRENGTH_MAX)
|
||||
.step_by(100.0)
|
||||
.step_by(50.0)
|
||||
.show_value(false),
|
||||
);
|
||||
end(ui, STRENGTH_MAX);
|
||||
});
|
||||
ui.label(
|
||||
RichText::new(tr("The Lichess scale: Maia 1500 is weaker than a club player rated 1500."))
|
||||
RichText::new(tr("Her measured strength, on the Lichess blitz scale."))
|
||||
.weak()
|
||||
.size(11.5),
|
||||
);
|
||||
|
|
@ -1086,22 +1195,25 @@ impl Dialog {
|
|||
|
||||
// Maia itself, downloaded once: under the body, by the buttons, always seen.
|
||||
let stage = self.download.as_ref().map(|d| d.stage());
|
||||
let mut ready = installed.is_some();
|
||||
let busy = !ready
|
||||
&& matches!(
|
||||
let mut ready = model.is_ok();
|
||||
// A download going on: no game meanwhile (closing the dialog would cancel it).
|
||||
let busy = matches!(
|
||||
stage,
|
||||
Some(crate::maia_download::Stage::Downloading { .. } | crate::maia_download::Stage::Checking)
|
||||
);
|
||||
if !ready {
|
||||
// While the large model is missing the line stays (also when the small one
|
||||
// plays the strength chosen), so that the dialog does not jump with the slider.
|
||||
let large = crate::maia_download::installed(Model::Large).is_some();
|
||||
if !ready || !large {
|
||||
ui.add_space(6.0);
|
||||
match stage {
|
||||
None => {
|
||||
ui.label(RichText::new(trf_size()).weak());
|
||||
ui.label(RichText::new(download_note(model.as_ref().err().copied())).weak());
|
||||
}
|
||||
Some(crate::maia_download::Stage::Downloading { done }) => {
|
||||
let size = self.download.as_ref().map_or(1, |d| d.model.size());
|
||||
ui.add(
|
||||
egui::ProgressBar::new(done as f32 / crate::maia_download::SIZE as f32)
|
||||
.show_percentage(),
|
||||
egui::ProgressBar::new(done as f32 / size as f32).show_percentage(),
|
||||
);
|
||||
}
|
||||
Some(crate::maia_download::Stage::Checking) => {
|
||||
|
|
@ -1111,7 +1223,7 @@ impl Dialog {
|
|||
});
|
||||
}
|
||||
Some(crate::maia_download::Stage::Done(_)) => ready = true,
|
||||
Some(crate::maia_download::Stage::Failed(e)) => {
|
||||
Some(crate::maia_download::Stage::Failed(ref e)) => {
|
||||
ui.label(RichText::new(e).color(ui.visuals().error_fg_color));
|
||||
}
|
||||
}
|
||||
|
|
@ -1119,16 +1231,26 @@ impl Dialog {
|
|||
ui.add_space(6.0);
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
let label = if ready { tr("Play") } else { tr("Download and play") };
|
||||
// As wide as the longer label: «Cancel» stays in its place.
|
||||
let width = [tr("Play"), tr("Download and play")]
|
||||
.iter()
|
||||
.map(|t| crate::game_view::text_width(ui, t, egui::TextStyle::Button))
|
||||
.fold(0.0, f32::max)
|
||||
+ 2.0 * ui.spacing().button_padding.x
|
||||
+ 4.0;
|
||||
let main = ui
|
||||
.add_enabled_ui(!busy, |ui| crate::widgets::primary(ui, label))
|
||||
.add_enabled_ui(!busy, |ui| crate::widgets::primary_wide(ui, label, width))
|
||||
.inner;
|
||||
let pressed = !busy
|
||||
&& (main.clicked() || ui.input(|i| i.key_pressed(egui::Key::Enter)));
|
||||
if pressed && !ready {
|
||||
self.download = Some(crate::maia_download::Download::start(ctx));
|
||||
if pressed && !ready && let Err(m) = model {
|
||||
self.download = Some(crate::maia_download::Download::start(ctx, m));
|
||||
self.play_after = true;
|
||||
}
|
||||
if ready && (pressed || self.play_after) {
|
||||
// After a download: the game starts when it is done (not when the slider
|
||||
// moves down to what another model plays).
|
||||
let downloaded = matches!(stage, Some(crate::maia_download::Stage::Done(_)));
|
||||
if ready && (pressed || (self.play_after && downloaded)) {
|
||||
self.play_after = false;
|
||||
let user = match self.prefs.color {
|
||||
ColorChoice::White => Color::White,
|
||||
|
|
@ -1146,7 +1268,7 @@ impl Dialog {
|
|||
// From a position the colour is the side to move's, not a habit.
|
||||
let color = settings.play.color;
|
||||
settings.play = self.prefs.clone();
|
||||
if self.from.is_some() {
|
||||
if self.keep_color {
|
||||
settings.play.color = color;
|
||||
}
|
||||
out = Some(DialogOutcome::Play(Start {
|
||||
|
|
@ -1267,11 +1389,22 @@ fn position_block(
|
|||
});
|
||||
}
|
||||
|
||||
fn trf_size() -> String {
|
||||
crate::i18n::trf(
|
||||
"Maia is downloaded once ({mb} MB, from huggingface.co, the authors' own file).",
|
||||
&[("mb", &crate::maia_download::SIZE.div_ceil(1_000_000))],
|
||||
)
|
||||
/// What the download button brings: Maia herself, or (the small one being there) the
|
||||
/// large one for the strength chosen.
|
||||
fn download_note(model: Option<Model>) -> String {
|
||||
let model = model.unwrap_or(Model::Large);
|
||||
let mb = model.size().div_ceil(1_000_000);
|
||||
if crate::maia_download::installed(Model::Small).is_some() {
|
||||
crate::i18n::trf(
|
||||
"Stronger than {elo} plays the large Maia: {mb} MB, downloaded once.",
|
||||
&[("elo", &SMALL_MAX), ("mb", &mb)],
|
||||
)
|
||||
} else {
|
||||
crate::i18n::trf(
|
||||
"Maia is downloaded once ({mb} MB, from huggingface.co, the authors' own file).",
|
||||
&[("mb", &mb)],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// A game just played: what the result strip says.
|
||||
|
|
|
|||
|
|
@ -72,10 +72,15 @@ pub fn update_button(ui: &mut egui::Ui, label: &str) -> egui::Response {
|
|||
|
||||
/// The main button of a window: filled, a little taller.
|
||||
pub fn primary(ui: &mut egui::Ui, label: &str) -> egui::Response {
|
||||
primary_wide(ui, label, 0.0)
|
||||
}
|
||||
|
||||
/// `primary()` at least `width` wide (a button whose label changes keeps its place).
|
||||
pub fn primary_wide(ui: &mut egui::Ui, label: &str, width: f32) -> egui::Response {
|
||||
ui.add(
|
||||
egui::Button::new(egui::RichText::new(label).strong())
|
||||
.fill(ui.visuals().selection.bg_fill)
|
||||
.min_size(egui::vec2(0.0, 30.0)),
|
||||
.min_size(egui::vec2(width, 30.0)),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue