cubecl_runtime/tune/
operation.rs1use alloc::boxed::Box;
2use alloc::string::String;
3use alloc::sync::Arc;
4use alloc::vec::Vec;
5use core::fmt::{Debug, Display};
6use core::hash::Hash;
7use cubecl_common::hash::StableHasher;
8
9use alloc::format;
10
11use crate::tune::{Bounds, BoundsGenerator, Eviction, Evictor};
12
13use super::{
14 AutotuneError, input_generator::InputGenerator, key_generator::KeyGenerator,
15 tune_inputs::TuneInputs,
16};
17use super::{Tunable, TunePlan};
18
19type TuneDelegate<I, Out> =
24 dyn for<'inp> Fn(<I as TuneInputs>::At<'inp>) -> Result<Out, AutotuneError> + Send + Sync;
25
26#[derive(new)]
29pub struct TuneFn<I: TuneInputs, Out> {
30 pub(crate) name: String,
31 func: Box<TuneDelegate<I, Out>>,
32}
33
34impl<I: TuneInputs, Out: 'static> TuneFn<I, Out> {
35 pub fn execute<'a>(&self, inputs: <I as TuneInputs>::At<'a>) -> Result<Out, AutotuneError> {
37 (self.func)(inputs)
38 }
39}
40
41pub struct TunableSet<K: AutotuneKey, F: TuneInputs, Output: 'static> {
44 tunables: Vec<Tunable<K, F, Output>>,
45 key_gen: Arc<dyn KeyGenerator<K, F> + Send + Sync>,
46 input_gen: Arc<dyn InputGenerator<K, F> + Send + Sync>,
47 bounds_gen: Option<Arc<dyn BoundsGenerator<K, F> + Send + Sync>>,
48 eviction: Option<Arc<dyn Eviction<K, F> + Send + Sync>>,
49 short_circuit: bool,
50}
51
52impl<K: AutotuneKey, F: TuneInputs, Output: 'static> TunableSet<K, F, Output> {
53 pub fn len(&self) -> usize {
55 self.tunables.len()
56 }
57
58 pub fn is_empty(&self) -> bool {
60 self.tunables.is_empty()
61 }
62
63 pub fn new(key_gen: impl KeyGenerator<K, F>, input_gen: impl InputGenerator<K, F>) -> Self {
65 Self {
66 tunables: Default::default(),
67 input_gen: Arc::new(input_gen),
68 key_gen: Arc::new(key_gen),
69 bounds_gen: None,
70 eviction: None,
71 short_circuit: true,
72 }
73 }
74
75 pub fn new_cloning_inputs(key_gen: impl KeyGenerator<K, F>) -> Self {
78 Self::new(key_gen, super::CloneInputGenerator)
79 }
80
81 pub fn with(mut self, tunable: Tunable<K, F, Output>) -> Self {
83 self.tunables.push(tunable);
84 self
85 }
86
87 pub fn with_bounds(mut self, bounds: Arc<dyn BoundsGenerator<K, F> + Send + Sync>) -> Self {
89 self.bounds_gen = Some(bounds);
90 self
91 }
92
93 pub fn with_eviction(mut self, eviction: Arc<dyn Eviction<K, F> + Send + Sync>) -> Self {
96 self.eviction = Some(eviction);
97 self
98 }
99
100 pub fn with_short_circuit(mut self, enabled: bool) -> Self {
102 self.short_circuit = enabled;
103 self
104 }
105
106 pub fn is_short_circuit_enabled(&self) -> bool {
108 self.short_circuit
109 }
110
111 pub fn autotunables(&self) -> impl Iterator<Item = &TuneFn<F, Output>> {
113 self.tunables.iter().map(|tunable| &tunable.function)
114 }
115
116 pub(crate) fn plan(&self, key: &K) -> TunePlan {
118 TunePlan::new(key, &self.tunables)
119 }
120
121 pub fn fastest(&self, fastest_index: usize) -> &TuneFn<F, Output> {
124 &self.tunables[fastest_index].function
125 }
126
127 pub fn compute_checksum(&self) -> String {
130 let mut checksum = String::new();
131 for tune in &self.tunables {
132 checksum += &tune.function.name;
133 }
134 format!("{:x}", StableHasher::hash_one(&checksum))
135 }
136
137 pub fn generate_key<'a>(&self, inputs: &F::At<'a>) -> K {
139 self.key_gen.generate(inputs)
140 }
141
142 pub fn generate_inputs<'a>(&self, key: &K, inputs: &F::At<'a>) -> F::At<'a> {
144 self.input_gen.generate(key, inputs)
145 }
146
147 pub(crate) fn evictor<'i>(&self, key: &K, inputs: &F::At<'i>) -> Option<Box<Evictor<'i>>> {
150 let eviction = self.eviction.clone()?;
151 let key = key.clone();
152 let inputs = inputs.clone();
153 Some(Box::new(move || eviction.evict(&key, &inputs)))
154 }
155
156 pub fn bounds(&self, key: &K, inputs: &F::At<'_>) -> Option<Bounds> {
158 self.bounds_gen.as_ref().map(|f| f.generate(key, inputs))
159 }
160}
161
162#[cfg(serializable)]
163pub trait AutotuneKey:
165 Clone
166 + Debug
167 + PartialEq
168 + Eq
169 + Hash
170 + Display
171 + serde::Serialize
172 + serde::de::DeserializeOwned
173 + Send
174 + Sync
175 + 'static
176{
177}
178#[cfg(not(serializable))]
179pub trait AutotuneKey:
181 Clone + Debug + PartialEq + Eq + Hash + Display + Send + Sync + 'static
182{
183}
184
185impl AutotuneKey for String {}