sim-lib-compute-auto 0.2.0

Automatic tensor compute site selection for SIM.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Measured compute profiles, bounded persistence, and conservative routing.

use std::collections::BTreeSet;
use std::sync::{Arc, Mutex};

use sim_kernel::Symbol;
use sim_lib_compute_model::ModeledComputeProfile;
use sim_lib_numbers_tensor::TensorRequest;

const DEFAULT_STALE_AFTER_TICKS: u64 = 10_000;
const CELL_BYTES: u64 = 8;

/// Adapter, driver, and backend identity for measured compute evidence.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ComputeDeviceIdentity {
    /// Stable adapter label.
    pub adapter: String,
    /// Driver label or version string.
    pub driver: String,
    /// Backend label, such as `wgpu`, `cuda`, `rocm`, or `modeled`.
    pub backend: String,
}

impl ComputeDeviceIdentity {
    /// Builds a device identity.
    pub fn new(
        adapter: impl Into<String>,
        driver: impl Into<String>,
        backend: impl Into<String>,
    ) -> Self {
        Self {
            adapter: adapter.into(),
            driver: driver.into(),
            backend: backend.into(),
        }
    }

    fn compatible_with(&self, other: &Self) -> bool {
        self == other
    }
}

/// Hardware and scheduling limits captured with a measured profile.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ComputeProfileLimits {
    /// Maximum resident bytes accepted by the provider.
    pub max_resident_bytes: u64,
    /// Maximum storage binding bytes accepted by the provider.
    pub max_storage_binding_bytes: u64,
    /// Maximum queued submissions.
    pub max_queue_depth: usize,
    /// Maximum queued bytes.
    pub max_queue_bytes: u64,
    /// Maximum submission deadline in logical ticks.
    pub submission_deadline_ticks: u64,
}

impl From<&ModeledComputeProfile> for ComputeProfileLimits {
    fn from(profile: &ModeledComputeProfile) -> Self {
        Self {
            max_resident_bytes: profile.max_resident_bytes,
            max_storage_binding_bytes: profile.max_storage_binding_bytes,
            max_queue_depth: profile.max_queue_depth,
            max_queue_bytes: profile.max_queue_bytes,
            submission_deadline_ticks: profile.submission_deadline_ticks,
        }
    }
}

/// Transfer, launch, arithmetic, reduction, and matmul sample distributions.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ComputeProfileSamples {
    /// Upload bandwidth samples, in bytes per logical tick.
    pub upload_bytes_per_tick: Vec<u64>,
    /// Download bandwidth samples, in bytes per logical tick.
    pub download_bytes_per_tick: Vec<u64>,
    /// Launch latency samples, in logical ticks.
    pub launch_ticks: Vec<u64>,
    /// Element-wise throughput samples, in elements per logical tick.
    pub element_elements_per_tick: Vec<u64>,
    /// Reduction throughput samples, in elements per logical tick.
    pub reduction_elements_per_tick: Vec<u64>,
    /// Matmul throughput samples, in multiply-adds per logical tick.
    pub matmul_ops_per_tick: Vec<u64>,
}

impl ComputeProfileSamples {
    fn conclusive(&self) -> bool {
        [
            &self.upload_bytes_per_tick,
            &self.download_bytes_per_tick,
            &self.launch_ticks,
            &self.element_elements_per_tick,
            &self.reduction_elements_per_tick,
            &self.matmul_ops_per_tick,
        ]
        .into_iter()
        .all(|samples| samples.iter().any(|sample| *sample > 0))
    }
}

/// Thermal and power context captured beside benchmark samples.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ComputeThermalPowerContext {
    /// Thermal context label.
    pub thermal: String,
    /// Power context label.
    pub power: String,
}

/// Provenance for a measured profile.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ComputeProfileProvenance {
    /// Tool or library that produced the profile.
    pub producer: String,
    /// Logical measurement tick.
    pub measured_at_tick: u64,
    /// Profile validity horizon in logical ticks.
    pub stale_after_ticks: u64,
}

/// Checked measured profile used by automatic routing.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MeasuredComputeProfile {
    /// Device identity.
    pub identity: ComputeDeviceIdentity,
    /// Provider limits.
    pub limits: ComputeProfileLimits,
    /// Transfer and operation sample distributions.
    pub samples: ComputeProfileSamples,
    /// Thermal and power measurement context.
    pub context: ComputeThermalPowerContext,
    /// Selected tile byte size.
    pub tile_bytes: u64,
    /// Allocation probe byte sizes that succeeded.
    pub allocation_bytes: Vec<u64>,
    /// Provenance for this record.
    pub provenance: ComputeProfileProvenance,
    /// Modeled profile to use when the evidence is accepted.
    pub modeled: ModeledComputeProfile,
}

impl sim_citizen::Citizen for MeasuredComputeProfile {
    fn citizen_symbol() -> Symbol {
        measured_compute_profile_citizen_symbol()
    }

    fn citizen_version() -> u32 {
        0
    }

    fn citizen_arity() -> usize {
        9
    }

    fn citizen_fields() -> &'static [&'static str] {
        &[
            "identity",
            "limits",
            "samples",
            "context",
            "tile_bytes",
            "allocation_bytes",
            "provenance",
            "modeled_provider",
            "shape",
        ]
    }
}

impl MeasuredComputeProfile {
    /// Returns true when the profile has usable, bounded measurement evidence.
    pub fn is_conclusive(&self) -> bool {
        self.tile_bytes > 0
            && self.allocation_bytes.iter().any(|bytes| *bytes > 0)
            && self.samples.conclusive()
            && self.modeled.fault.is_none()
    }

    /// Returns true when this profile still applies to `identity` at `now_tick`.
    pub fn is_compatible(&self, identity: &ComputeDeviceIdentity, now_tick: u64) -> bool {
        self.identity.compatible_with(identity)
            && now_tick.saturating_sub(self.provenance.measured_at_tick)
                <= self.provenance.stale_after_ticks
            && self.limits.max_resident_bytes > 0
            && self.limits.max_storage_binding_bytes > 0
    }

    /// Returns the modeled executor profile selected by this measured evidence.
    pub fn modeled_profile(&self) -> ModeledComputeProfile {
        self.modeled.clone()
    }

    pub(crate) fn estimated_profile_bytes(&self) -> usize {
        self.identity.adapter.len()
            + self.identity.driver.len()
            + self.identity.backend.len()
            + self.context.thermal.len()
            + self.context.power.len()
            + self.provenance.producer.len()
            + (self.allocation_bytes.len()
                + self.samples.upload_bytes_per_tick.len()
                + self.samples.download_bytes_per_tick.len()
                + self.samples.launch_ticks.len()
                + self.samples.element_elements_per_tick.len()
                + self.samples.reduction_elements_per_tick.len()
                + self.samples.matmul_ops_per_tick.len())
                * std::mem::size_of::<u64>()
            + 256
    }
}

/// Citizen class symbol for measured compute profile read-construct records.
pub fn measured_compute_profile_citizen_symbol() -> Symbol {
    Symbol::qualified("compute-profile", "MeasuredProfile")
}

/// Shape symbol for checked measured compute profile records.
pub fn measured_compute_profile_shape_symbol() -> Symbol {
    Symbol::qualified("compute-profile", "MeasuredProfileShape")
}

/// Bounded synthetic benchmark inputs.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BenchmarkBounds {
    /// Transfer byte sizes to sample.
    pub transfer_bytes: Vec<u64>,
    /// Element counts to sample.
    pub element_counts: Vec<u64>,
    /// Square matrix edges to sample.
    pub matrix_edges: Vec<u64>,
    /// Maximum accepted byte size in any sample.
    pub max_sample_bytes: u64,
}

impl Default for BenchmarkBounds {
    fn default() -> Self {
        Self {
            transfer_bytes: vec![4096, 16 * 1024, 64 * 1024],
            element_counts: vec![256, 1024, 4096],
            matrix_edges: vec![8, 16, 32],
            max_sample_bytes: 256 * 1024,
        }
    }
}

/// Runs a deterministic bounded profile harness from supplied provider facts.
pub fn measure_bounded_profile(
    identity: ComputeDeviceIdentity,
    modeled: ModeledComputeProfile,
    context: ComputeThermalPowerContext,
    producer: impl Into<String>,
    now_tick: u64,
    bounds: BenchmarkBounds,
) -> MeasuredComputeProfile {
    let bounded_transfers = bounded_nonzero(bounds.transfer_bytes, bounds.max_sample_bytes);
    let bounded_elements = bounded_nonzero(
        bounds.element_counts,
        bounds.max_sample_bytes.saturating_div(CELL_BYTES).max(1),
    );
    let bounded_edges = bounded_nonzero(bounds.matrix_edges, 512);
    let tile_bytes = modeled
        .segment_tile_bytes
        .min(modeled.max_storage_binding_bytes)
        .max(CELL_BYTES);
    let allocation_bytes = bounded_transfers
        .iter()
        .copied()
        .filter(|bytes| *bytes <= modeled.max_resident_bytes)
        .collect::<Vec<_>>();
    MeasuredComputeProfile {
        identity,
        limits: ComputeProfileLimits::from(&modeled),
        samples: ComputeProfileSamples {
            upload_bytes_per_tick: bounded_transfers.clone(),
            download_bytes_per_tick: bounded_transfers
                .iter()
                .map(|bytes| (*bytes).saturating_mul(9) / 10)
                .collect(),
            launch_ticks: bounded_transfers
                .iter()
                .enumerate()
                .map(|(index, _)| (index as u64) + 1)
                .collect(),
            element_elements_per_tick: bounded_elements.clone(),
            reduction_elements_per_tick: bounded_elements.iter().map(|count| count / 2).collect(),
            matmul_ops_per_tick: bounded_edges.iter().map(|edge| edge.pow(3)).collect(),
        },
        context,
        tile_bytes,
        allocation_bytes,
        provenance: ComputeProfileProvenance {
            producer: producer.into(),
            measured_at_tick: now_tick,
            stale_after_ticks: DEFAULT_STALE_AFTER_TICKS,
        },
        modeled,
    }
}

/// Reason an automatic route selected CPU or device placement.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AutoRouteDecision {
    /// No measured profile was supplied or loaded.
    Absent,
    /// The measured profile is stale for the current logical tick.
    Stale,
    /// Adapter, driver, or backend identity does not match.
    Incompatible,
    /// The profile lacks required bounded samples or limits.
    Inconclusive,
    /// Device evidence was accepted.
    Device,
}

/// Auto router that accepts device placement only with fresh compatible evidence.
#[derive(Clone, Debug)]
pub struct AutoComputeRouter {
    expected: ComputeDeviceIdentity,
    now_tick: u64,
}

impl AutoComputeRouter {
    /// Builds a router for `expected` device identity at `now_tick`.
    pub fn new(expected: ComputeDeviceIdentity, now_tick: u64) -> Self {
        Self { expected, now_tick }
    }

    /// Returns the decision and modeled profile when device placement is proven.
    pub fn choose(
        &self,
        profile: Option<&MeasuredComputeProfile>,
    ) -> (AutoRouteDecision, Option<ModeledComputeProfile>) {
        let Some(profile) = profile else {
            return (AutoRouteDecision::Absent, None);
        };
        if !profile.identity.compatible_with(&self.expected) {
            return (AutoRouteDecision::Incompatible, None);
        }
        if self
            .now_tick
            .saturating_sub(profile.provenance.measured_at_tick)
            > profile.provenance.stale_after_ticks
        {
            return (AutoRouteDecision::Stale, None);
        }
        if !profile.is_conclusive() {
            return (AutoRouteDecision::Inconclusive, None);
        }
        (AutoRouteDecision::Device, Some(profile.modeled_profile()))
    }
}

/// One provider-selection ledger row.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AutoRoutingEvent {
    /// Provider label chosen for the request or flush.
    pub provider: String,
    /// Routing decision.
    pub decision: AutoRouteDecision,
    /// Estimated materialization bytes.
    pub materialization_bytes: u64,
    /// Synchronization count represented by the event.
    pub synchronizations: usize,
}

/// In-memory routing ledger for explainable placement decisions.
#[derive(Clone, Debug, Default)]
pub struct AutoRoutingLedger {
    events: Arc<Mutex<Vec<AutoRoutingEvent>>>,
}

impl AutoRoutingLedger {
    /// Records one routing event.
    pub fn record(&self, event: AutoRoutingEvent) {
        self.events
            .lock()
            .expect("auto routing ledger poisoned")
            .push(event);
    }

    /// Returns recorded routing events.
    pub fn events(&self) -> Vec<AutoRoutingEvent> {
        self.events
            .lock()
            .expect("auto routing ledger poisoned")
            .clone()
    }
}

pub(crate) fn request_materialization_bytes(request: &TensorRequest) -> u64 {
    request
        .inputs
        .iter()
        .map(|tensor| tensor.shape().iter().copied().product::<usize>() as u64 * CELL_BYTES)
        .chain(std::iter::once(
            request.output.shape().iter().copied().product::<usize>() as u64 * CELL_BYTES,
        ))
        .sum()
}

fn bounded_nonzero(values: Vec<u64>, max: u64) -> Vec<u64> {
    let mut seen = BTreeSet::new();
    values
        .into_iter()
        .filter(|value| *value > 0 && *value <= max)
        .filter(|value| seen.insert(*value))
        .collect()
}