-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathscheduler.rs
More file actions
573 lines (553 loc) · 22.5 KB
/
scheduler.rs
File metadata and controls
573 lines (553 loc) · 22.5 KB
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
use crate::{
dfg::{
partition_components, partition_preserving_parallelism, types::*, ComponentEdge, ExecNode,
},
FHE_BATCH_LATENCY_HISTOGRAM, RERAND_LATENCY_BATCH_HISTOGRAM,
};
use anyhow::Result;
use daggy::{
petgraph::{
visit::{EdgeRef, IntoEdgesDirected, IntoNodeIdentifiers},
Direction::{self},
},
Dag, NodeIndex,
};
use fhevm_engine_common::common::FheOperation;
use fhevm_engine_common::telemetry;
use fhevm_engine_common::tfhe_ops::perform_fhe_operation;
use fhevm_engine_common::types::{Handle, SupportedFheCiphertexts};
use fhevm_engine_common::utils::HeartBeat;
use std::collections::HashMap;
use tfhe::ReRandomizationContext;
use tokio::task::JoinSet;
use tracing::{error, info, warn};
use super::{DFComponentGraph, DFGraph, OpNode};
const OPERATION_RERANDOMISATION_DOMAIN_SEPARATOR: [u8; 8] = *b"TFHE_Rrd";
const COMPACT_PUBLIC_ENCRYPTION_DOMAIN_SEPARATOR: [u8; 8] = *b"TFHE_Enc";
pub enum PartitionStrategy {
MaxParallelism,
MaxLocality,
}
enum DeviceSelection {
#[allow(dead_code)]
Index(usize),
RoundRobin,
#[allow(dead_code)]
NA,
}
pub struct Scheduler<'a> {
graph: &'a mut DFComponentGraph,
edges: Dag<(), ComponentEdge>,
#[cfg(not(feature = "gpu"))]
sks: tfhe::ServerKey,
cpk: tfhe::CompactPublicKey,
#[cfg(feature = "gpu")]
csks: Vec<tfhe::CudaServerKey>,
activity_heartbeat: HeartBeat,
}
type PartitionResult = (HashMap<Handle, Result<TaskResult>>, NodeIndex);
impl<'a> Scheduler<'a> {
fn is_ready_task(&self, node: &ExecNode) -> bool {
node.dependence_counter
.load(std::sync::atomic::Ordering::SeqCst)
== 0
}
pub fn new(
graph: &'a mut DFComponentGraph,
#[cfg(not(feature = "gpu"))] sks: tfhe::ServerKey,
cpk: tfhe::CompactPublicKey,
#[cfg(feature = "gpu")] csks: Vec<tfhe::CudaServerKey>,
activity_heartbeat: HeartBeat,
) -> Self {
let edges = graph.graph.map(|_, _| (), |_, edge| *edge);
Self {
graph,
edges,
#[cfg(not(feature = "gpu"))]
sks,
cpk,
#[cfg(feature = "gpu")]
csks,
activity_heartbeat,
}
}
pub async fn schedule(&mut self) -> Result<()> {
let schedule_type = std::env::var("FHEVM_DF_SCHEDULE");
match schedule_type {
Ok(val) => match val.as_str() {
"MAX_PARALLELISM" => {
self.schedule_coarse_grain(PartitionStrategy::MaxParallelism)
.await
}
"MAX_LOCALITY" => {
self.schedule_coarse_grain(PartitionStrategy::MaxLocality)
.await
}
unhandled => {
error!(target: "scheduler", { strategy = ?unhandled },
"Scheduling strategy does not exist");
info!(target: "scheduler", { },
"Reverting to default (generally best performance) strategy MAX_PARALLELISM");
self.schedule_coarse_grain(PartitionStrategy::MaxParallelism)
.await
}
},
// Use overall best strategy as default
#[cfg(not(feature = "gpu"))]
_ => {
self.schedule_coarse_grain(PartitionStrategy::MaxParallelism)
.await
}
#[cfg(feature = "gpu")]
_ => {
self.schedule_coarse_grain(PartitionStrategy::MaxParallelism)
.await
}
}
}
#[cfg(not(feature = "gpu"))]
fn get_keys(
&self,
_target: DeviceSelection,
) -> Result<(tfhe::ServerKey, tfhe::CompactPublicKey)> {
Ok((self.sks.clone(), self.cpk.clone()))
}
#[cfg(feature = "gpu")]
fn get_keys(
&self,
target: DeviceSelection,
) -> Result<(tfhe::CudaServerKey, tfhe::CompactPublicKey)> {
match target {
DeviceSelection::Index(i) => {
if i < self.csks.len() {
Ok((self.csks[i].clone(), self.cpk.clone()))
} else {
error!(target: "scheduler", {index = ?i },
"Wrong device index");
// Instead of giving up, we'll use device 0 (which
// should always be safe to use) and keep making
// progress even if suboptimally
Ok((self.csks[0].clone(), self.cpk.clone()))
}
}
DeviceSelection::RoundRobin => {
static LAST: std::sync::atomic::AtomicUsize =
std::sync::atomic::AtomicUsize::new(0);
// Use fetch_add to increment atomically
let i = LAST.fetch_add(1, std::sync::atomic::Ordering::Relaxed) % self.csks.len();
Ok((self.csks[i].clone(), self.cpk.clone()))
}
DeviceSelection::NA => Ok((self.csks[0].clone(), self.cpk.clone())),
}
}
async fn schedule_coarse_grain(&mut self, strategy: PartitionStrategy) -> Result<()> {
let mut execution_graph: Dag<ExecNode, ()> = Dag::default();
match strategy {
PartitionStrategy::MaxLocality => {
partition_components(&self.graph.graph, &mut execution_graph)?
}
PartitionStrategy::MaxParallelism => {
partition_preserving_parallelism(&self.graph.graph, &mut execution_graph)?
}
};
let task_dependences = execution_graph.map(|_, _| (), |_, edge| *edge);
// Prime the scheduler with all nodes without dependences
let mut set: JoinSet<PartitionResult> = JoinSet::new();
for idx in 0..execution_graph.node_count() {
let index = NodeIndex::new(idx);
let node = execution_graph
.node_weight_mut(index)
.ok_or(SchedulerError::DataflowGraphError)?;
if self.is_ready_task(node) {
let mut args = Vec::with_capacity(node.df_nodes.len());
for nidx in node.df_nodes.iter() {
let tx = self
.graph
.graph
.node_weight_mut(*nidx)
.ok_or(SchedulerError::DataflowGraphError)?;
args.push((
std::mem::take(&mut tx.graph),
std::mem::take(&mut tx.inputs),
tx.transaction_id.clone(),
tx.component_id,
));
}
let (sks, cpk) = self.get_keys(DeviceSelection::RoundRobin)?;
let parent_span = tracing::Span::current();
set.spawn_blocking(move || {
let span_guard = parent_span.enter();
let result = execute_partition(args, index, 0, sks, cpk);
drop(span_guard);
result
});
}
}
while let Some(result) = set.join_next().await {
self.activity_heartbeat.update();
// The result contains all outputs (allowed handles)
// computed within the finished partition. Now check the
// outputs and update the trnsaction inputs of downstream
// transactions
let (sks, _cpk) = self.get_keys(DeviceSelection::RoundRobin)?;
tfhe::set_server_key(sks);
let result = result?;
let task_index = result.1;
for (handle, node_result) in result.0.into_iter() {
// Add computed allowed handles to the graph. These
// can be used as inputs and forwarded to subsequent,
// dependent transactions
self.graph.add_output(&handle, node_result, &self.edges)?;
}
for edge in task_dependences.edges_directed(task_index, Direction::Outgoing) {
let dependent_task_index = edge.target();
let dependent_task = execution_graph
.node_weight_mut(dependent_task_index)
.ok_or(SchedulerError::DataflowGraphError)?;
dependent_task
.dependence_counter
.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
if self.is_ready_task(dependent_task) {
let mut args = Vec::with_capacity(dependent_task.df_nodes.len());
for nidx in dependent_task.df_nodes.iter() {
let tx = self
.graph
.graph
.node_weight_mut(*nidx)
.ok_or(SchedulerError::DataflowGraphError)?;
// Skip transactions that cannot complete
// because of missing dependences.
if tx.is_uncomputable {
continue;
}
args.push((
std::mem::take(&mut tx.graph),
std::mem::take(&mut tx.inputs),
tx.transaction_id.clone(),
tx.component_id,
));
}
let (sks, cpk) = self.get_keys(DeviceSelection::RoundRobin)?;
let parent_span = tracing::Span::current();
set.spawn_blocking(move || {
let span_guard = parent_span.enter();
let result = execute_partition(args, dependent_task_index, 0, sks, cpk);
drop(span_guard);
result
});
}
}
}
Ok(())
}
}
fn re_randomise_operation_inputs(
cts: &mut [SupportedFheCiphertexts],
opcode: i32,
cpk: &tfhe::CompactPublicKey,
) -> Result<()> {
let mut re_rand_context = ReRandomizationContext::new(
OPERATION_RERANDOMISATION_DOMAIN_SEPARATOR,
[opcode.to_be_bytes().as_slice()],
COMPACT_PUBLIC_ENCRYPTION_DOMAIN_SEPARATOR,
);
for ct in cts.iter() {
ct.add_to_re_randomization_context(&mut re_rand_context);
}
let mut seed_gen = re_rand_context.finalize();
for ct in cts.iter_mut() {
if !matches!(ct, SupportedFheCiphertexts::Scalar(_)) {
ct.re_randomise(cpk, seed_gen.next_seed()?)?;
}
}
Ok(())
}
type ComponentSet = Vec<(DFGraph, HashMap<Handle, Option<DFGTxInput>>, Handle, usize)>;
fn execute_partition(
transactions: ComponentSet,
task_id: NodeIndex,
gpu_idx: usize,
#[cfg(not(feature = "gpu"))] sks: tfhe::ServerKey,
#[cfg(feature = "gpu")] sks: tfhe::CudaServerKey,
cpk: tfhe::CompactPublicKey,
) -> PartitionResult {
tfhe::set_server_key(sks);
let mut res: HashMap<Handle, Result<TaskResult>> = HashMap::with_capacity(transactions.len());
// Traverse transactions within the partition. The transactions
// are topologically sorted so the order is executable
'tx: for (ref mut dfg, ref mut tx_inputs, tid, _cid) in transactions {
let txn_id_short = telemetry::short_hex_id(&tid);
// Update the transaction inputs based on allowed handles so
// far. If any input is still missing, and we cannot fill it
// (e.g., error in the producer transaction) we cannot execute
// this transaction and possibly more downstream.
for (h, i) in tx_inputs.iter_mut() {
if i.is_none() {
let Some(Ok(ct)) = res.get(h) else {
warn!(target: "scheduler", {transaction_id = ?hex::encode(tid) },
"Missing input to compute transaction - skipping");
for nidx in dfg.graph.node_identifiers() {
let Some(node) = dfg.graph.node_weight_mut(nidx) else {
error!(target: "scheduler", {index = ?nidx.index() }, "Wrong dataflow graph index");
continue;
};
if node.is_allowed {
res.insert(
node.result_handle.clone(),
Err(SchedulerError::MissingInputs.into()),
);
}
}
continue 'tx;
};
*i = Some(DFGTxInput::Compressed((
ct.compressed_ct.clone(),
ct.is_allowed,
)));
}
}
// Prime the scheduler with ready ops from the transaction's subgraph
let _exec_guard = tracing::info_span!(
"execute_transaction",
txn_id = %txn_id_short,
)
.entered();
let started_at = std::time::Instant::now();
let Ok(ts) = daggy::petgraph::algo::toposort(&dfg.graph, None) else {
error!(target: "scheduler", {transaction_id = ?tid },
"Cyclical dependence error in transaction");
for nidx in dfg.graph.node_identifiers() {
let Some(node) = dfg.graph.node_weight_mut(nidx) else {
error!(target: "scheduler", {index = ?nidx.index() }, "Wrong dataflow graph index");
continue;
};
if node.is_allowed {
res.insert(
node.result_handle.clone(),
Err(SchedulerError::CyclicDependence.into()),
);
}
}
continue 'tx;
};
let edges = dfg.graph.map(|_, _| (), |_, edge| *edge);
for nidx in ts.iter() {
let Some(node) = dfg.graph.node_weight_mut(*nidx) else {
error!(target: "scheduler", {index = ?nidx.index() }, "Wrong dataflow graph index");
continue;
};
let result = try_execute_node(node, nidx.index(), tx_inputs, gpu_idx, &tid, &cpk);
match result {
Ok(result) => {
let nidx = NodeIndex::new(result.0);
if result.1.is_ok() {
for edge in edges.edges_directed(nidx, Direction::Outgoing) {
let child_index = edge.target();
let Some(child_node) = dfg.graph.node_weight_mut(child_index) else {
error!(target: "scheduler", {index = ?child_index.index() }, "Wrong dataflow graph index");
continue;
};
// Update input of consumers
if let Ok(ref res) = result.1 {
child_node.inputs[*edge.weight() as usize] =
DFGTaskInput::Compressed(res.clone());
}
}
}
// Update partition's outputs (allowed handles only)
let Some(node) = dfg.graph.node_weight_mut(nidx) else {
error!(target: "scheduler", {index = ?nidx.index() }, "Wrong dataflow graph index");
continue;
};
res.insert(
node.result_handle.clone(),
result.1.map(|v| TaskResult {
compressed_ct: v,
is_allowed: node.is_allowed,
transaction_id: tid.clone(),
}),
);
}
Err(e) => {
let Some(node) = dfg.graph.node_weight(*nidx) else {
error!(target: "scheduler", {index = ?nidx.index() }, "Wrong dataflow graph index");
continue;
};
if node.is_allowed {
res.insert(node.result_handle.clone(), Err(e));
}
}
}
}
drop(_exec_guard);
let elapsed = started_at.elapsed();
FHE_BATCH_LATENCY_HISTOGRAM.observe(elapsed.as_secs_f64());
}
(res, task_id)
}
fn try_execute_node(
node: &mut OpNode,
node_index: usize,
tx_inputs: &mut HashMap<Handle, Option<DFGTxInput>>,
gpu_idx: usize,
transaction_id: &Handle,
cpk: &tfhe::CompactPublicKey,
) -> Result<(usize, OpResult)> {
if !node.check_ready_inputs(tx_inputs) {
return Err(SchedulerError::SchedulerError.into());
}
let mut cts = Vec::with_capacity(node.inputs.len());
for i in std::mem::take(&mut node.inputs) {
match i {
DFGTaskInput::Value(v) => {
if !matches!(v, SupportedFheCiphertexts::Scalar(_)) {
error!(target: "scheduler", { handle = ?hex::encode(&node.result_handle) },
"Consensus risk: non-scalar uncompressed ciphertext");
}
cts.push(v);
}
DFGTaskInput::Compressed(cct) => {
let decompressed = SupportedFheCiphertexts::decompress(
cct.ct_type,
&cct.ct_bytes,
gpu_idx,
)
.map_err(|e| {
error!(
target: "scheduler",
{ handle = ?hex::encode(&node.result_handle), ct_type = cct.ct_type, error = ?e },
"Error while decompressing op input"
);
telemetry::set_current_span_error(&e);
SchedulerError::DecompressionError
})?;
cts.push(decompressed);
}
DFGTaskInput::Dependence(_) => {
error!(target: "scheduler", { handle = ?hex::encode(&node.result_handle) }, "Computation missing inputs");
return Err(SchedulerError::MissingInputs.into());
}
}
}
// Re-randomize inputs for this operation
{
let _guard = tracing::info_span!("rerandomise_op_inputs").entered();
let started_at = std::time::Instant::now();
if let Err(e) = re_randomise_operation_inputs(&mut cts, node.opcode, cpk) {
error!(target: "scheduler", { handle = ?hex::encode(&node.result_handle), error = ?e },
"Error while re-randomising operation inputs");
telemetry::set_current_span_error(&e);
return Err(SchedulerError::ReRandomisationError.into());
}
let elapsed = started_at.elapsed();
RERAND_LATENCY_BATCH_HISTOGRAM.observe(elapsed.as_secs_f64());
}
let opcode = node.opcode;
let result = std::panic::catch_unwind(|| {
run_computation(opcode, cts, node_index, gpu_idx, transaction_id)
});
match result {
Err(e) => {
let msg = e
.downcast_ref::<&str>()
.map(|s| s.to_string())
.or_else(|| e.downcast_ref::<String>().cloned())
.unwrap_or_else(|| "unknown panic payload".to_string());
eprintln!("Panic while executing operation: {msg}");
error!(target: "scheduler", { handle = ?hex::encode(&node.result_handle), msg },
"Panic while executing operation");
telemetry::set_current_span_error(&msg);
Err(SchedulerError::ExecutionPanic(msg).into())
}
Ok(r) => Ok(r),
}
}
type OpResult = Result<CompressedCiphertext>;
fn run_computation(
operation: i32,
inputs: Vec<SupportedFheCiphertexts>,
graph_node_index: usize,
gpu_idx: usize,
transaction_id: &Handle,
) -> (usize, OpResult) {
let txn_id_short = telemetry::short_hex_id(transaction_id);
let op = FheOperation::try_from(operation);
match op {
Ok(FheOperation::FheGetCiphertext) => {
// Compression span (no FHE here)
let _guard = tracing::info_span!(
"compress_ciphertext",
txn_id = %txn_id_short,
ct_type = inputs[0].type_name(),
operation = "FheGetCiphertext",
compressed_size = tracing::field::Empty,
)
.entered();
let ct_type = inputs[0].type_num();
let compressed = inputs[0].compress();
match compressed {
Ok(ct_bytes) => {
tracing::Span::current().record("compressed_size", ct_bytes.len() as i64);
(
graph_node_index,
Ok(CompressedCiphertext { ct_type, ct_bytes }),
)
}
Err(error) => {
telemetry::set_current_span_error(&error);
(graph_node_index, Err(error.into()))
}
}
}
Ok(fhe_op) => {
let op_name = fhe_op.as_str_name();
// FHE operation span
let _fhe_guard = tracing::info_span!(
"fhe_operation",
txn_id = %txn_id_short,
operation = op_name,
operation_code = operation as i64,
input_type = tracing::field::Empty,
)
.entered();
if !inputs.is_empty() {
tracing::Span::current().record("input_type", inputs[0].type_name());
}
let result = perform_fhe_operation(operation as i16, &inputs, gpu_idx);
match result {
Ok(result) => {
// Compression span
let _guard = tracing::info_span!(
"compress_ciphertext",
txn_id = %txn_id_short,
ct_type = result.type_name(),
operation = op_name,
compressed_size = tracing::field::Empty,
)
.entered();
let ct_type = result.type_num();
let compressed = result.compress();
match compressed {
Ok(ct_bytes) => {
tracing::Span::current()
.record("compressed_size", ct_bytes.len() as i64);
(
graph_node_index,
Ok(CompressedCiphertext { ct_type, ct_bytes }),
)
}
Err(error) => {
telemetry::set_current_span_error(&error);
(graph_node_index, Err(error.into()))
}
}
}
Err(e) => {
telemetry::set_current_span_error(&e);
(graph_node_index, Err(e.into()))
}
}
}
Err(e) => (graph_node_index, Err(e.into())),
}
}