1use std::fmt;
2use std::mem::{size_of, size_of_val};
3use std::sync::Arc;
4
5use tenferro_runtime::program::{CoreSemanticOp, SemanticOpRef, SemanticOperationView};
6use tenferro_runtime::{
7 assemble_executable_engine_registration, CoreCapabilityBundle, CoreCapabilityKind,
8 CorePrepareContext, DotGeneralPreparation, DotGeneralPrepareRequest, ElementwisePrepareRequest,
9 ElementwiseRuntime, EngineId, EngineRegistration, EngineRegistrationMetadata,
10 ExecutableEngineRegistrationConfig, ExecutionContextIdentity, HardwareClassId,
11 IndexingPrepareRequest, IndexingRuntime, InputIngressContract, InputPlacementContract,
12 InputSignature, InputSignatureContract, InputSpecializationProjection,
13 InputSpecializationRequirements, LayoutPrepareRequest, LayoutProjection, LayoutRuntime,
14 LayoutSpecialization, MemoryKind, PrepareCapability, PrepareError, PreparedOperation,
15 PreparedOperationBinding, PreparedOperationPlan, ProviderContractError, ProviderDeviceIdentity,
16 ProviderId, ReductionPrepareRequest, ReductionRuntime, ResidentOutputContract,
17 RuntimeConfigError, RuntimeInputContract, SpecializationError, SpecializationProjection,
18 SpecializationRequirements, StorageClass, TensorRead, UnsupportedReason,
19};
20
21use crate::CpuEngine;
22
23#[derive(Debug, Clone, Copy, Default)]
25struct CpuPreparer;
26
27const CPU_ENGINE_ID: &str = "tenferro-cpu.default.v1";
28const CPU_HARDWARE_CLASS_ID: &str = "tenferro-cpu.host.v1";
29const CPU_STORAGE_CLASS_ID: &str = "tenferro-cpu.host.v1";
30const UNKNOWN_CORE_OPERATION: &str = "unknown-core-operation";
31
32pub fn runtime_engine_id() -> Result<EngineId, RuntimeConfigError> {
39 EngineId::new(CPU_ENGINE_ID).map_err(RuntimeConfigError::from)
40}
41
42pub(crate) fn default_runtime_engine_id() -> EngineId {
44 match EngineId::new(CPU_ENGINE_ID) {
45 Ok(id) => id,
46 Err(_) => unreachable!("the built-in CPU engine id is a valid identifier"),
47 }
48}
49
50pub fn runtime_hardware_class() -> Result<HardwareClassId, RuntimeConfigError> {
57 HardwareClassId::new(CPU_HARDWARE_CLASS_ID).map_err(RuntimeConfigError::from)
58}
59
60pub fn runtime_engine_registration(
70 engine: &CpuEngine,
71) -> Result<EngineRegistration, RuntimeConfigError> {
72 runtime_engine_registration_with_id(engine, runtime_engine_id()?)
73}
74
75pub fn runtime_engine_registration_with_id(
82 engine: &CpuEngine,
83 engine_id: EngineId,
84) -> Result<EngineRegistration, RuntimeConfigError> {
85 let preparer = Arc::new(CpuPreparer);
86 let elementwise: Arc<dyn ElementwiseRuntime> = preparer.clone();
87 let reduction: Arc<dyn ReductionRuntime> = preparer.clone();
88 let indexing: Arc<dyn IndexingRuntime> = preparer.clone();
89 let dot_general: Arc<dyn DotGeneralPreparation> = preparer.clone();
90 let layout: Arc<dyn LayoutRuntime> = preparer;
91
92 let mut capabilities = CoreCapabilityBundle::builder();
93 capabilities
94 .elementwise(elementwise)
95 .reduction(reduction)
96 .indexing(indexing)
97 .dot_general(dot_general)
98 .layout(layout);
99
100 let storage = runtime_storage_class()?;
101 let default_storage = storage.clone();
102 let placement_storage = storage.clone();
103 let signature_storage = storage.clone();
104 let runtime_storage = storage.clone();
105 let resident_storage = storage.clone();
106 let allocation_domain = engine.allocation_domain();
107 let provider_id = crate::cpu_provider_id();
108 let provider_device_identity =
109 ProviderDeviceIdentity::new(ProviderId::new(provider_id)?, "host".to_owned())?;
110 let ingress = InputIngressContract::new(
111 InputPlacementContract::new(move |placement, candidate| {
112 candidate == &placement_storage
113 && (cpu_input_placement(placement)
114 || (placement.memory_kind == MemoryKind::Managed
115 && allocation_domain.is_some()))
116 }),
117 InputSignatureContract::new(move |placement, family, domain, candidate| {
118 candidate == &signature_storage
119 && cpu_input_signature(placement, family, domain, allocation_domain)
120 }),
121 RuntimeInputContract::new(move |input: &TensorRead<'_>, candidate| {
122 candidate == &runtime_storage && cpu_runtime_input(input, allocation_domain)
123 }),
124 ResidentOutputContract::new(move |input: &TensorRead<'_>, candidate| {
125 candidate == &resident_storage && cpu_runtime_input(input, allocation_domain)
126 }),
127 );
128 let metadata = EngineRegistrationMetadata::new(
129 engine_id,
130 provider_device_identity,
131 runtime_hardware_class()?,
132 Arc::from(vec![storage]),
133 default_storage,
134 capabilities.build(),
135 );
136 assemble_executable_engine_registration(ExecutableEngineRegistrationConfig::new(
137 metadata,
138 ExecutionContextIdentity::of::<CpuEngine>(),
139 ingress,
140 ))
141}
142
143fn cpu_input_signature(
144 placement: &tenferro_tensor::Placement,
145 backend_family: Option<&'static str>,
146 input_domain: Option<tenferro_tensor::AllocationDomainId>,
147 allocation_domain: Option<tenferro_tensor::AllocationDomainId>,
148) -> bool {
149 if placement.memory_kind == MemoryKind::Managed {
150 return backend_family.is_some()
151 && allocation_domain.is_some()
152 && input_domain == allocation_domain;
153 }
154 cpu_input_placement(placement)
155 && match backend_family {
156 None => input_domain.is_none(),
157 Some(_) => allocation_domain.is_some() && input_domain == allocation_domain,
158 }
159}
160
161fn cpu_input_placement(placement: &tenferro_tensor::Placement) -> bool {
162 matches!(
163 placement.memory_kind,
164 MemoryKind::PinnedHost | MemoryKind::UnpinnedHost
165 )
166}
167
168fn cpu_runtime_input(
169 input: &TensorRead<'_>,
170 allocation_domain: Option<tenferro_tensor::AllocationDomainId>,
171) -> bool {
172 cpu_input_signature(
173 input.placement(),
174 input.backend_family(),
175 input.allocation_domain(),
176 allocation_domain,
177 )
178}
179
180fn runtime_storage_class() -> Result<StorageClass, RuntimeConfigError> {
181 StorageClass::new(CPU_STORAGE_CLASS_ID).map_err(RuntimeConfigError::from)
182}
183
184#[derive(Clone, Copy, Debug, Eq, PartialEq)]
185enum CpuPreparedKind {
186 Elementwise,
187 Reduction,
188 Indexing,
189 DotGeneral,
190 Layout,
191}
192
193#[derive(Debug)]
194struct CpuPreparedOperation {
195 binding: PreparedOperationBinding,
196 specialization: SpecializationProjection,
197 #[allow(dead_code, reason = "bounded Debug records the selected CPU family")]
198 kind: CpuPreparedKind,
199}
200
201impl PreparedOperation for CpuPreparedOperation {
202 fn binding(&self) -> &PreparedOperationBinding {
203 &self.binding
204 }
205
206 fn specialization(&self) -> &SpecializationProjection {
207 &self.specialization
208 }
209
210 fn retained_bytes(&self) -> usize {
211 checked_specialization_heap_retained_bytes(&self.specialization).unwrap_or(usize::MAX)
212 }
213}
214
215impl ElementwiseRuntime for CpuPreparer {
216 fn prepare(
217 &self,
218 request: ElementwisePrepareRequest<'_>,
219 ) -> Result<PrepareCapability, PrepareError> {
220 prepare_cpu(
221 request.operation(),
222 request.context(),
223 CpuPreparedKind::Elementwise,
224 )
225 }
226
227 fn max_fused_region_inputs(&self) -> Option<usize> {
228 Some(tenferro_cpu_fused::ERASED_FUSION_MAX_INPUTS)
229 }
230}
231
232impl ReductionRuntime for CpuPreparer {
233 fn prepare(
234 &self,
235 request: ReductionPrepareRequest<'_>,
236 ) -> Result<PrepareCapability, PrepareError> {
237 prepare_cpu(
238 request.operation(),
239 request.context(),
240 CpuPreparedKind::Reduction,
241 )
242 }
243}
244
245impl IndexingRuntime for CpuPreparer {
246 fn prepare(
247 &self,
248 request: IndexingPrepareRequest<'_>,
249 ) -> Result<PrepareCapability, PrepareError> {
250 prepare_cpu(
251 request.operation(),
252 request.context(),
253 CpuPreparedKind::Indexing,
254 )
255 }
256}
257
258impl DotGeneralPreparation for CpuPreparer {
259 fn prepare(
260 &self,
261 request: DotGeneralPrepareRequest<'_>,
262 ) -> Result<PrepareCapability, PrepareError> {
263 prepare_cpu(
264 request.operation(),
265 request.context(),
266 CpuPreparedKind::DotGeneral,
267 )
268 }
269}
270
271impl LayoutRuntime for CpuPreparer {
272 fn prepare(
273 &self,
274 request: LayoutPrepareRequest<'_>,
275 ) -> Result<PrepareCapability, PrepareError> {
276 prepare_cpu(
277 request.operation(),
278 request.context(),
279 CpuPreparedKind::Layout,
280 )
281 }
282
283 fn folds_metadata_views(&self) -> bool {
284 true
286 }
287}
288
289impl tenferro_runtime::ExecutionEngine for CpuEngine {
290 fn target(&self) -> tenferro_runtime::EngineTarget {
291 tenferro_runtime::EngineTarget::new(
292 self.runtime_engine_id.clone(),
293 ExecutionContextIdentity::of::<CpuEngine>(),
294 self.allocation_domain(),
295 )
296 }
297
298 fn take_extension_caches(&mut self) -> tenferro_runtime::ExtensionCacheStore {
299 let store = self.extension_caches.take();
300 debug_assert!(
301 store.is_some(),
302 "extension caches taken twice without a restore"
303 );
304 store.unwrap_or_default()
307 }
308
309 fn restore_extension_caches(&mut self, caches: tenferro_runtime::ExtensionCacheStore) {
310 debug_assert!(
311 self.extension_caches.is_none(),
312 "extension caches restored without a take"
313 );
314 self.extension_caches = Some(caches);
315 }
316
317 fn mark_broken(&mut self) {
318 self.broken = true;
319 }
320
321 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
322 self
323 }
324}
325
326fn prepare_cpu(
327 operation: SemanticOperationView<'_>,
328 context: &CorePrepareContext<'_>,
329 expected_kind: CpuPreparedKind,
330) -> Result<PrepareCapability, PrepareError> {
331 validate_cpu_runtime_context(context)?;
332 let SemanticOpRef::Core(op) = operation.op() else {
333 return Err(wrong_family_error(expected_kind, "extension"));
334 };
335 let Some(actual_kind) = cpu_operation_kind(op) else {
336 return Ok(PrepareCapability::Unsupported(
337 UnsupportedReason::Operation {
338 operation: UNKNOWN_CORE_OPERATION,
339 },
340 ));
341 };
342 if actual_kind != expected_kind {
343 return Err(wrong_family_error(expected_kind, core_operation_name(op)));
344 }
345
346 let minimum = minimum_specialization_requirements(actual_kind, context.inputs())?;
347 let merged =
348 merge_specialization_requirements(context.specialization().requirements(), &minimum);
349 if &merged != context.specialization().requirements() {
350 return Ok(PrepareCapability::NeedsSpecialization(merged));
351 }
352
353 Ok(PrepareCapability::Prepared(
354 PreparedOperationPlan::metadata(Arc::new(CpuPreparedOperation {
355 binding: context.binding().clone(),
356 specialization: context.specialization().clone(),
357 kind: actual_kind,
358 })),
359 ))
360}
361
362fn validate_cpu_runtime_context(context: &CorePrepareContext<'_>) -> Result<(), PrepareError> {
363 let expected_context = ExecutionContextIdentity::of::<CpuEngine>();
364 if context.binding().context_identity() != expected_context {
365 return Err(PrepareError::ProviderContract {
366 source: ProviderContractError::WrongOperationFamily {
367 expected: CoreCapabilityKind::Elementwise,
368 operation: "cpu-context-mismatch",
369 },
370 });
371 }
372 if context.binding().hardware_class().as_str() != CPU_HARDWARE_CLASS_ID {
373 return Err(PrepareError::ProviderContract {
374 source: ProviderContractError::WrongOperationFamily {
375 expected: CoreCapabilityKind::Elementwise,
376 operation: "cpu-hardware-mismatch",
377 },
378 });
379 }
380 if context.resolved_placement().storage_class().as_str() != CPU_STORAGE_CLASS_ID {
381 return Err(PrepareError::Unsupported {
382 reason: UnsupportedReason::StorageClass {
383 storage_class: context.resolved_placement().storage_class().clone(),
384 },
385 });
386 }
387 Ok(())
388}
389
390fn cpu_operation_kind(op: &CoreSemanticOp) -> Option<CpuPreparedKind> {
391 Some(match op {
392 CoreSemanticOp::Add
393 | CoreSemanticOp::Sub
394 | CoreSemanticOp::Mul
395 | CoreSemanticOp::Neg
396 | CoreSemanticOp::Conj
397 | CoreSemanticOp::Div
398 | CoreSemanticOp::Rem
399 | CoreSemanticOp::Abs
400 | CoreSemanticOp::Sign
401 | CoreSemanticOp::Maximum
402 | CoreSemanticOp::Minimum
403 | CoreSemanticOp::Compare(_)
404 | CoreSemanticOp::Select
405 | CoreSemanticOp::Clamp
406 | CoreSemanticOp::Exp
407 | CoreSemanticOp::Log
408 | CoreSemanticOp::Sin
409 | CoreSemanticOp::Cos
410 | CoreSemanticOp::Tanh
411 | CoreSemanticOp::Sqrt
412 | CoreSemanticOp::Rsqrt
413 | CoreSemanticOp::Pow
414 | CoreSemanticOp::Expm1
415 | CoreSemanticOp::Log1p
416 | CoreSemanticOp::Erf => CpuPreparedKind::Elementwise,
417 CoreSemanticOp::ReduceSum { .. }
418 | CoreSemanticOp::ReduceSumSquares { .. }
419 | CoreSemanticOp::ReduceProd { .. }
420 | CoreSemanticOp::ReduceMax { .. }
421 | CoreSemanticOp::ReduceMin { .. } => CpuPreparedKind::Reduction,
422 CoreSemanticOp::Gather(_)
423 | CoreSemanticOp::GatherDynamicSliceSizes { .. }
424 | CoreSemanticOp::Scatter(_)
425 | CoreSemanticOp::Slice(_)
426 | CoreSemanticOp::DynamicSlice { .. }
427 | CoreSemanticOp::DynamicUpdateSlice
428 | CoreSemanticOp::Pad(_)
429 | CoreSemanticOp::Concatenate { .. }
430 | CoreSemanticOp::Reverse { .. }
431 | CoreSemanticOp::ShapeOf { .. }
432 | CoreSemanticOp::DynamicTruncate { .. }
433 | CoreSemanticOp::PadToMatch { .. } => CpuPreparedKind::Indexing,
434 CoreSemanticOp::DotGeneral { .. } => CpuPreparedKind::DotGeneral,
435 CoreSemanticOp::Transpose { .. }
436 | CoreSemanticOp::Reshape { .. }
437 | CoreSemanticOp::BroadcastInDim { .. }
438 | CoreSemanticOp::Convert { .. }
439 | CoreSemanticOp::Constant { .. }
440 | CoreSemanticOp::ExtractDiag { .. }
441 | CoreSemanticOp::EmbedDiag { .. }
442 | CoreSemanticOp::Tril { .. }
443 | CoreSemanticOp::Triu { .. } => CpuPreparedKind::Layout,
444 _ => return None,
445 })
446}
447
448fn minimum_specialization_requirements(
449 kind: CpuPreparedKind,
450 inputs: &InputSignature,
451) -> Result<SpecializationRequirements, PrepareError> {
452 let mut requirements = Vec::with_capacity(inputs.entries().len());
453 for (input, entry) in inputs.entries().iter().enumerate() {
454 let mut builder = InputSpecializationRequirements::builder();
455 builder.dtype(true).rank(true);
456 match kind {
457 CpuPreparedKind::Indexing => {
458 builder.concrete_dimensions(concrete_axes_for_rank(input, entry.shape().len())?);
459 }
460 CpuPreparedKind::DotGeneral => {
461 builder
462 .concrete_dimensions(concrete_axes_for_rank(input, entry.shape().len())?)
463 .layout(LayoutSpecialization::Class);
464 }
465 CpuPreparedKind::Elementwise | CpuPreparedKind::Reduction | CpuPreparedKind::Layout => {
466 }
467 }
468 requirements.push(
469 builder
470 .build()
471 .expect("CPU minimum specialization requirements are internally valid"),
472 );
473 }
474 Ok(SpecializationRequirements::new(requirements))
475}
476
477fn concrete_axes_for_rank(input: usize, rank: usize) -> Result<Vec<u32>, PrepareError> {
478 if u32::try_from(rank).is_err() {
479 return Err(PrepareError::Specialization {
480 source: SpecializationError::ProjectionOverflow { input, rank },
481 });
482 }
483 Ok((0..rank)
484 .map(|axis| u32::try_from(axis).expect("rank precheck keeps axes encodable"))
485 .collect())
486}
487
488fn merge_specialization_requirements(
489 current: &SpecializationRequirements,
490 minimum: &SpecializationRequirements,
491) -> SpecializationRequirements {
492 debug_assert_eq!(current.inputs().len(), minimum.inputs().len());
493 let inputs = current
494 .inputs()
495 .iter()
496 .zip(minimum.inputs())
497 .map(|(current, minimum)| merge_input_requirements(current, minimum))
498 .collect::<Vec<_>>();
499 SpecializationRequirements::new(inputs)
500}
501
502fn merge_input_requirements(
503 current: &InputSpecializationRequirements,
504 minimum: &InputSpecializationRequirements,
505) -> InputSpecializationRequirements {
506 let mut axes = current.concrete_dimensions().to_vec();
507 for axis in minimum.concrete_dimensions() {
508 if !axes.contains(axis) {
509 axes.push(*axis);
510 }
511 }
512 let layout = current.layout().max(minimum.layout());
513 let rank = current.specializes_rank()
514 || minimum.specializes_rank()
515 || !axes.is_empty()
516 || layout == LayoutSpecialization::ExactStrides;
517 let alignment = match (current.alignment_log2(), minimum.alignment_log2()) {
518 (Some(left), Some(right)) => Some(left.max(right)),
519 (Some(value), None) | (None, Some(value)) => Some(value),
520 (None, None) => None,
521 };
522 let mut builder = InputSpecializationRequirements::builder();
523 builder
524 .dtype(current.specializes_dtype() || minimum.specializes_dtype())
525 .rank(rank)
526 .concrete_dimensions(axes)
527 .placement(current.placement().max(minimum.placement()))
528 .layout(layout)
529 .alignment_log2(alignment);
530 builder
531 .build()
532 .expect("merged CPU specialization requirements preserve builder invariants")
533}
534
535fn wrong_family_error(expected_kind: CpuPreparedKind, operation: &'static str) -> PrepareError {
536 PrepareError::ProviderContract {
537 source: ProviderContractError::WrongOperationFamily {
538 expected: expected_kind.core_capability(),
539 operation,
540 },
541 }
542}
543
544impl CpuPreparedKind {
545 fn core_capability(self) -> CoreCapabilityKind {
546 match self {
547 Self::Elementwise => CoreCapabilityKind::Elementwise,
548 Self::Reduction => CoreCapabilityKind::Reduction,
549 Self::Indexing => CoreCapabilityKind::Indexing,
550 Self::DotGeneral => CoreCapabilityKind::DotGeneral,
551 Self::Layout => CoreCapabilityKind::Layout,
552 }
553 }
554}
555
556fn core_operation_name(op: &CoreSemanticOp) -> &'static str {
557 match op {
558 CoreSemanticOp::Add => "add",
559 CoreSemanticOp::Sub => "sub",
560 CoreSemanticOp::Mul => "mul",
561 CoreSemanticOp::Neg => "neg",
562 CoreSemanticOp::Conj => "conj",
563 CoreSemanticOp::DotGeneral { .. } => "dot_general",
564 CoreSemanticOp::Transpose { .. } => "transpose",
565 CoreSemanticOp::Reshape { .. } => "reshape",
566 CoreSemanticOp::BroadcastInDim { .. } => "broadcast_in_dim",
567 CoreSemanticOp::Convert { .. } => "convert",
568 CoreSemanticOp::Constant { .. } => "constant",
569 CoreSemanticOp::ReduceSum { .. } => "reduce_sum",
570 CoreSemanticOp::ReduceSumSquares { .. } => "reduce_sum_squares",
571 CoreSemanticOp::Div => "div",
572 CoreSemanticOp::Rem => "rem",
573 CoreSemanticOp::Abs => "abs",
574 CoreSemanticOp::Sign => "sign",
575 CoreSemanticOp::Maximum => "maximum",
576 CoreSemanticOp::Minimum => "minimum",
577 CoreSemanticOp::Compare(_) => "compare",
578 CoreSemanticOp::Select => "select",
579 CoreSemanticOp::Clamp => "clamp",
580 CoreSemanticOp::Exp => "exp",
581 CoreSemanticOp::Log => "log",
582 CoreSemanticOp::Sin => "sin",
583 CoreSemanticOp::Cos => "cos",
584 CoreSemanticOp::Tanh => "tanh",
585 CoreSemanticOp::Sqrt => "sqrt",
586 CoreSemanticOp::Rsqrt => "rsqrt",
587 CoreSemanticOp::Pow => "pow",
588 CoreSemanticOp::Expm1 => "expm1",
589 CoreSemanticOp::Log1p => "log1p",
590 CoreSemanticOp::Erf => "erf",
591 CoreSemanticOp::ExtractDiag { .. } => "extract_diag",
592 CoreSemanticOp::EmbedDiag { .. } => "embed_diag",
593 CoreSemanticOp::Tril { .. } => "tril",
594 CoreSemanticOp::Triu { .. } => "triu",
595 CoreSemanticOp::Gather(_) => "gather",
596 CoreSemanticOp::GatherDynamicSliceSizes { .. } => "gather_dynamic_slice_sizes",
597 CoreSemanticOp::Scatter(_) => "scatter",
598 CoreSemanticOp::Slice(_) => "slice",
599 CoreSemanticOp::DynamicSlice { .. } => "dynamic_slice",
600 CoreSemanticOp::DynamicUpdateSlice => "dynamic_update_slice",
601 CoreSemanticOp::Pad(_) => "pad",
602 CoreSemanticOp::Concatenate { .. } => "concatenate",
603 CoreSemanticOp::Reverse { .. } => "reverse",
604 CoreSemanticOp::ShapeOf { .. } => "shape_of",
605 CoreSemanticOp::DynamicTruncate { .. } => "dynamic_truncate",
606 CoreSemanticOp::PadToMatch { .. } => "pad_to_match",
607 CoreSemanticOp::ReduceProd { .. } => "reduce_prod",
608 CoreSemanticOp::ReduceMax { .. } => "reduce_max",
609 CoreSemanticOp::ReduceMin { .. } => "reduce_min",
610 _ => UNKNOWN_CORE_OPERATION,
611 }
612}
613
614fn checked_specialization_heap_retained_bytes(
615 specialization: &SpecializationProjection,
616) -> Option<usize> {
617 let requirements = specialization.requirements();
618 checked_sum([
619 requirements
620 .inputs()
621 .len()
622 .checked_mul(size_of::<InputSpecializationRequirements>())?,
623 checked_sum(
624 requirements
625 .inputs()
626 .iter()
627 .map(|input| size_of_val(input.concrete_dimensions())),
628 )?,
629 specialization
630 .inputs()
631 .len()
632 .checked_mul(size_of::<InputSpecializationProjection>())?,
633 checked_sum_options(
634 specialization
635 .inputs()
636 .iter()
637 .map(input_projection_retained_bytes),
638 )?,
639 ])
640}
641
642fn input_projection_retained_bytes(projection: &InputSpecializationProjection) -> Option<usize> {
643 size_of_val(projection.concrete_dimensions()).checked_add(match projection.layout() {
644 Some(LayoutProjection::ExactStrides(strides)) if strides.is_heap() => {
645 size_of_val(strides.as_slice())
646 }
647 _ => 0,
648 })
649}
650
651fn checked_sum(values: impl IntoIterator<Item = usize>) -> Option<usize> {
652 values
653 .into_iter()
654 .try_fold(0usize, |sum, value| sum.checked_add(value))
655}
656
657fn checked_sum_options(values: impl IntoIterator<Item = Option<usize>>) -> Option<usize> {
658 values
659 .into_iter()
660 .try_fold(0usize, |sum, value| sum.checked_add(value?))
661}
662
663impl fmt::Display for CpuPreparedKind {
664 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
665 formatter.write_str(match self {
666 Self::Elementwise => "elementwise",
667 Self::Reduction => "reduction",
668 Self::Indexing => "indexing",
669 Self::DotGeneral => "dot_general",
670 Self::Layout => "layout",
671 })
672 }
673}
674
675#[cfg(test)]
676mod tests;