diff --git a/rustuna_core/src/sampler.rs b/rustuna_core/src/sampler.rs index 94237978..87d35f41 100644 --- a/rustuna_core/src/sampler.rs +++ b/rustuna_core/src/sampler.rs @@ -7,19 +7,33 @@ use crate::distribution::Distribution; use crate::storage::Storage; use crate::study::Direction; use crate::trial::TrialStateValues; +use crate::trial_queue::TrialQueue; use crate::Result; -#[derive(Debug, Clone)] +#[derive(Clone)] /// Lightweight study and trial metadata passed to samplers. /// /// Rustuna does not pass full `Study` or `FrozenTrial` objects to samplers. Instead, it provides -/// the identifiers and objective directions required to make sampling decisions, while richer -/// state can be retrieved through [`Storage`] when necessary. +/// the identifiers and objective directions required to make sampling decisions, the shared +/// [`TrialQueue`] for queued trials, and access to richer state through [`Storage`] when necessary. pub struct Context { pub study_id: u32, pub directions: Vec, pub trial_number: u32, pub trial_id: u32, + pub trial_queue: Arc, +} + +impl std::fmt::Debug for Context { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Context") + .field("study_id", &self.study_id) + .field("directions", &self.directions) + .field("trial_number", &self.trial_number) + .field("trial_id", &self.trial_id) + .field("trial_queue", &"") + .finish() + } } /// Interface for parameter suggestion algorithms. @@ -246,6 +260,7 @@ mod tests { use crate::storage::InMemoryStorage; use crate::study::create_study; use crate::trial::Trial; + use crate::trial_queue::InMemoryTrialQueue; pub struct DummyJointSampler { joint_params: HashMap, @@ -294,12 +309,14 @@ mod tests { fn random_sampler_can_be_shared_between_threads() -> Result<()> { let sampler: Arc = Arc::new(RandomSampler::seed_from_u64(0)); let storage: Arc> = Arc::new(RwLock::new(InMemoryStorage::new())); + let trial_queue: Arc = Arc::new(InMemoryTrialQueue::new()); let distribution = Distribution::new_float(-1.0, 1.0, None, false); let mut handles = Vec::new(); for thread_id in 0..4 { let sampler = Arc::clone(&sampler); let storage = Arc::clone(&storage); + let trial_queue = Arc::clone(&trial_queue); let distribution = distribution.clone(); handles.push(thread::spawn(move || -> Result<()> { for trial_number in 0..100 { @@ -308,6 +325,7 @@ mod tests { directions: vec![Direction::Minimize], trial_number, trial_id: thread_id * 100 + trial_number, + trial_queue: Arc::clone(&trial_queue), }; let value = sampler.sample_independent( &ctx, diff --git a/rustuna_core/src/study.rs b/rustuna_core/src/study.rs index 1e6bc661..999ef308 100644 --- a/rustuna_core/src/study.rs +++ b/rustuna_core/src/study.rs @@ -182,6 +182,7 @@ impl Study { trial_number: persisted_trial.number, trial_id: persisted_trial.id, directions: self.directions.clone(), + trial_queue: Arc::clone(&self.queue), }; self.sampler.before_trial(&ctx, self.storage.clone())?; @@ -219,6 +220,7 @@ impl Study { self.directions.clone(), Arc::clone(&self.storage), Arc::clone(&self.sampler), + Arc::clone(&self.queue), joint_params, ); Ok(trial) @@ -253,6 +255,7 @@ impl Study { directions: self.directions.clone(), trial_number, trial_id, + trial_queue: Arc::clone(&self.queue), }; let after_trial_result = self.sampler diff --git a/rustuna_core/src/trial.rs b/rustuna_core/src/trial.rs index b880ef05..40aec7a5 100644 --- a/rustuna_core/src/trial.rs +++ b/rustuna_core/src/trial.rs @@ -9,6 +9,7 @@ use crate::distribution::Distribution; use crate::sampler::{Context as SamplerContext, Sampler}; use crate::storage::Storage; use crate::study::Direction; +use crate::trial_queue::TrialQueue; use crate::{Error, ErrorKind, Result}; const CONSTRAINTS_KEY: &str = "constraints"; @@ -27,6 +28,7 @@ pub struct Trial { directions: Vec, storage: Arc>, sampler: Arc, + trial_queue: Arc, joint_params: HashMap, fixed_params: HashMap, cached_user_attrs: HashMap, @@ -38,6 +40,7 @@ impl Trial { directions: Vec, storage: Arc>, sampler: Arc, + trial_queue: Arc, joint_params: HashMap, ) -> Self { let fixed_params = extract_fixed_params(&trial.attrs); @@ -58,6 +61,7 @@ impl Trial { directions, storage, sampler, + trial_queue, joint_params, fixed_params, cached_user_attrs, @@ -135,6 +139,7 @@ impl Trial { trial_number: self.number, trial_id: self.id, directions: self.directions.clone(), + trial_queue: Arc::clone(&self.trial_queue), }; let param_value = self.sampler diff --git a/rustuna_pyo3/rustuna/_rustuna.pyi b/rustuna_pyo3/rustuna/_rustuna.pyi index 1af6e861..e4e0fa83 100644 --- a/rustuna_pyo3/rustuna/_rustuna.pyi +++ b/rustuna_pyo3/rustuna/_rustuna.pyi @@ -1165,13 +1165,16 @@ class SamplerContext: Attributes: study_id: Study ID. trial_number: Trial number. + trial_id: Trial ID. directions: Optimization directions. + trial_queue: The queue shared with the study. """ study_id: int trial_number: int trial_id: int directions: list[StudyDirection] + trial_queue: TrialQueueProtocol def __init__( self, diff --git a/rustuna_pyo3/src/sampler/context.rs b/rustuna_pyo3/src/sampler/context.rs index d1b27d85..431bc571 100644 --- a/rustuna_pyo3/src/sampler/context.rs +++ b/rustuna_pyo3/src/sampler/context.rs @@ -1,8 +1,12 @@ +use std::sync::Arc; + use pyo3::prelude::*; use rustuna_core::sampler::Context as SamplerContext; +use rustuna_core::trial_queue::InMemoryTrialQueue; use crate::study::PyDirection; +use crate::trial_queue::to_python::ToPythonTrialQueue; #[derive(Clone)] #[pyclass(name = "SamplerContext", skip_from_py_object)] @@ -32,6 +36,7 @@ impl PySamplerContext { trial_number, trial_id, directions: directions.into_iter().map(|d| d.into()).collect(), + trial_queue: Arc::new(InMemoryTrialQueue::new()), }, }) } @@ -55,4 +60,10 @@ impl PySamplerContext { .map(|d| d.clone().into()) .collect() } + + #[getter] + fn trial_queue(&self, py: Python<'_>) -> PyResult> { + let queue = ToPythonTrialQueue::new(Arc::clone(&self.context.trial_queue)); + Ok(Py::new(py, queue)?.into_any()) + } } diff --git a/rustuna_pyo3/src/storage/mod.rs b/rustuna_pyo3/src/storage/mod.rs index 880555a2..c1af63bd 100644 --- a/rustuna_pyo3/src/storage/mod.rs +++ b/rustuna_pyo3/src/storage/mod.rs @@ -3,5 +3,5 @@ pub mod cached; pub mod in_memory; pub mod journal; pub mod sqlite3; -pub mod to_python; +pub(crate) mod to_python; pub mod to_rust; diff --git a/rustuna_pyo3/src/storage/to_python.rs b/rustuna_pyo3/src/storage/to_python.rs index 054cf379..008a616c 100644 --- a/rustuna_pyo3/src/storage/to_python.rs +++ b/rustuna_pyo3/src/storage/to_python.rs @@ -12,18 +12,18 @@ use crate::trial::{PyPersistedTrial, PyTrialState}; #[derive(Clone)] #[pyclass(name = "ToPythonStorage", skip_from_py_object)] #[pyo3(module = "rustuna")] -pub struct ToPythonStorage { +pub(crate) struct ToPythonStorage { pub(crate) binding: StorageBinding, } impl ToPythonStorage { - pub fn new(storage: Arc>) -> Self { + pub(crate) fn new(storage: Arc>) -> Self { Self { binding: StorageBinding { storage }, } } - pub fn storage(&self) -> Arc> { + pub(crate) fn storage(&self) -> Arc> { self.binding.storage.clone() } } diff --git a/rustuna_pyo3/src/trial_queue/mod.rs b/rustuna_pyo3/src/trial_queue/mod.rs index 4fb9c11b..fa7eeecc 100644 --- a/rustuna_pyo3/src/trial_queue/mod.rs +++ b/rustuna_pyo3/src/trial_queue/mod.rs @@ -1,4 +1,5 @@ pub mod directory; pub mod inmemory; pub mod sqlite3; +pub(crate) mod to_python; pub mod to_rust; diff --git a/rustuna_pyo3/src/trial_queue/to_python.rs b/rustuna_pyo3/src/trial_queue/to_python.rs new file mode 100644 index 00000000..a7008d38 --- /dev/null +++ b/rustuna_pyo3/src/trial_queue/to_python.rs @@ -0,0 +1,35 @@ +use std::sync::Arc; + +use pyo3::prelude::*; +use rustuna_core::trial_queue::TrialQueue; +use rustuna_core::ErrorKind; + +use crate::exception::err_to_exceptions; + +/// Python-facing adapter for the trial queue carried by a sampler context. +#[derive(Clone)] +#[pyclass(name = "ToPythonTrialQueue", module = "rustuna", skip_from_py_object)] +pub(crate) struct ToPythonTrialQueue { + queue: Arc, +} + +impl ToPythonTrialQueue { + pub(crate) fn new(queue: Arc) -> Self { + Self { queue } + } +} + +#[pymethods] +impl ToPythonTrialQueue { + fn enqueue(&self, py: Python<'_>, trial_id: u32) -> PyResult<()> { + py.detach(|| self.queue.enqueue(trial_id).map_err(err_to_exceptions)) + } + + fn dequeue(&self, py: Python<'_>) -> PyResult> { + py.detach(|| match self.queue.dequeue() { + Ok(trial_id) => Ok(Some(trial_id)), + Err(error) if matches!(error.kind, ErrorKind::TrialQueueEmpty) => Ok(None), + Err(error) => Err(err_to_exceptions(error)), + }) + } +} diff --git a/rustuna_sampler/src/tpe/sampler.rs b/rustuna_sampler/src/tpe/sampler.rs index 4586d20f..dcddd744 100644 --- a/rustuna_sampler/src/tpe/sampler.rs +++ b/rustuna_sampler/src/tpe/sampler.rs @@ -877,6 +877,10 @@ mod tests { use rustuna_core::study::{create_study, Direction}; use rustuna_core::study::{get_best_trial, get_pareto_front}; + fn test_trial_queue() -> Arc { + Arc::new(rustuna_core::trial_queue::InMemoryTrialQueue::new()) + } + #[test] fn test_optimize() { let storage = InMemoryStorage::new(); @@ -1376,6 +1380,7 @@ mod tests { directions: vec![Direction::Minimize], trial_number: 15, trial_id: 999, + trial_queue: test_trial_queue(), }; let distribution = Distribution::new_float(0.0, 10.0, None, false); let cached = TpeSampler::seed_from_u64(7); @@ -1424,6 +1429,7 @@ mod tests { directions: vec![Direction::Minimize], trial_number: trial_id, trial_id, + trial_queue: test_trial_queue(), }; let key = |trial_id| { ( @@ -1683,6 +1689,7 @@ mod tests { trial_id: 100, trial_number: 1, directions: vec![Direction::Minimize], + trial_queue: test_trial_queue(), }; sampler.before_trial(&ctx, first.clone()).unwrap(); sampler.before_trial(&ctx, second.clone()).unwrap(); @@ -1727,6 +1734,7 @@ mod tests { trial_id: u32::MAX, trial_number: 2, directions: vec![Direction::Minimize], + trial_queue: test_trial_queue(), }; if prime_cache { sampler.snapshot_from_storage(&ctx, &storage).unwrap(); @@ -1762,6 +1770,7 @@ mod tests { trial_id: study_id, trial_number: 0, directions: vec![Direction::Minimize], + trial_queue: test_trial_queue(), }; sampler.before_trial(&ctx, storage.clone()).unwrap(); }