eredu-runtime 0.2.0

Backend-neutral model execution runtime for Eredu
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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
//! Neutral lowering of logical parameter placement into bounded checkpoint selections.

use std::collections::{BTreeMap, BTreeSet};

use eredu_checkpoint::{
    recipe::RecipeError,
    store::{CheckpointSource, StoreError, TensorSelection},
};

use crate::{
    LocalModelLayout, LocalTensorLayout, ResidencyDeclarationError, TensorPlacement, WeightBinding,
    WeightBindingSelectionError,
};

/// Validated world identity used by logical checkpoint placement.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct PlacementRank {
    world_size: usize,
    global_rank: usize,
}

impl PlacementRank {
    /// Creates one rank in a non-empty world.
    pub fn new(world_size: usize, global_rank: usize) -> Result<Self, PlacementPlanError> {
        if world_size == 0 || global_rank >= world_size {
            return Err(PlacementPlanError::InvalidRank {
                world_size,
                global_rank,
            });
        }
        Ok(Self {
            world_size,
            global_rank,
        })
    }

    /// Returns the process count.
    pub const fn world_size(self) -> usize {
        self.world_size
    }

    /// Returns the selected process rank.
    pub const fn global_rank(self) -> usize {
        self.global_rank
    }
}

/// A validated contiguous slice of a source tensor.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct TensorSlice {
    axis: usize,
    start: usize,
    end: usize,
    index: usize,
    parts: usize,
}

impl TensorSlice {
    /// Validates and calculates an equal contiguous tensor slice.
    pub fn for_shape(
        shape: &[usize],
        axis: usize,
        index: usize,
        parts: usize,
    ) -> Result<Self, PlacementPlanError> {
        let dimension = *shape.get(axis).ok_or(PlacementPlanError::AxisOutOfBounds {
            axis,
            rank: shape.len(),
        })?;
        if parts == 0 || index >= parts || dimension == 0 || !dimension.is_multiple_of(parts) {
            return Err(PlacementPlanError::InvalidShard {
                axis,
                index,
                parts,
                dimension,
            });
        }
        let width = dimension / parts;
        let start = index
            .checked_mul(width)
            .ok_or(PlacementPlanError::ArithmeticOverflow)?;
        Ok(Self {
            axis,
            start,
            end: start + width,
            index,
            parts,
        })
    }

    /// Returns the source axis.
    pub const fn axis(&self) -> usize {
        self.axis
    }
    /// Returns the inclusive source offset.
    pub const fn start(&self) -> usize {
        self.start
    }
    /// Returns the exclusive source offset.
    pub const fn end(&self) -> usize {
        self.end
    }
    /// Returns the shard index.
    pub const fn index(&self) -> usize {
        self.index
    }
    /// Returns the shard count.
    pub const fn parts(&self) -> usize {
        self.parts
    }
    /// Returns the resulting local shape.
    pub fn local_shape(&self, source_shape: &[usize]) -> Vec<usize> {
        let mut shape = source_shape.to_vec();
        shape[self.axis] = self.end - self.start;
        shape
    }
}

#[derive(Debug, Clone)]
struct TensorPlan {
    placement: TensorPlacement,
    expected_source_shape: Option<Vec<usize>>,
}

/// Exact logical checkpoint placement for one rank.
#[derive(Debug, Clone)]
pub struct PlacementPlan {
    rank: PlacementRank,
    tensors: BTreeMap<String, TensorPlan>,
    default: Option<TensorPlacement>,
}

impl PlacementPlan {
    /// Creates a strict plan in which every checkpoint source must be named.
    pub const fn new(rank: PlacementRank) -> Self {
        Self {
            rank,
            tensors: BTreeMap::new(),
            default: None,
        }
    }

    /// Creates a plan that replicates every checkpoint source.
    pub fn replicated(rank: PlacementRank) -> Self {
        Self::new(rank).with_default(TensorPlacement::Replicated)
    }

    /// Returns the selected logical rank.
    pub const fn rank(&self) -> PlacementRank {
        self.rank
    }

    /// Sets the placement for otherwise unnamed checkpoint sources.
    pub fn with_default(mut self, placement: TensorPlacement) -> Self {
        self.default = Some(placement);
        self
    }

    /// Adds or replaces one exact source placement.
    pub fn insert(&mut self, source: impl Into<String>, placement: TensorPlacement) {
        self.tensors.insert(
            source.into(),
            TensorPlan {
                placement,
                expected_source_shape: None,
            },
        );
    }

    /// Adds one source placement with an exact pre-selection shape.
    pub fn insert_expected(
        &mut self,
        source: impl Into<String>,
        expected_source_shape: impl Into<Vec<usize>>,
        placement: TensorPlacement,
    ) -> Result<(), PlacementPlanError> {
        let shape = expected_source_shape.into();
        validate_tensor_placement(&placement, &shape, self.rank)?;
        self.tensors.insert(
            source.into(),
            TensorPlan {
                placement,
                expected_source_shape: Some(shape),
            },
        );
        Ok(())
    }

    /// Adds a packed weight and its scale and optional bias companions together.
    pub fn insert_quantized_companions(
        &mut self,
        prefix: &str,
        placement: TensorPlacement,
        has_biases: bool,
    ) {
        self.insert(format!("{prefix}.weight"), placement.clone());
        self.insert(format!("{prefix}.scales"), placement.clone());
        if has_biases {
            self.insert(format!("{prefix}.biases"), placement);
        }
    }

    /// Returns an explicit placement by exact source name.
    pub fn placement(&self, source: &str) -> Option<&TensorPlacement> {
        self.tensors.get(source).map(|plan| &plan.placement)
    }

    /// Validates all geometry available before checkpoint access.
    pub fn validate(&self) -> Result<(), PlacementPlanError> {
        for tensor in self.tensors.values() {
            validate_tensor_plan(tensor, self.rank)?;
        }
        if let Some(default) = &self.default {
            validate_tensor_plan(
                &TensorPlan {
                    placement: default.clone(),
                    expected_source_shape: None,
                },
                self.rank,
            )?;
        }
        Ok(())
    }

    /// Returns whether a named source can require materialization on this rank.
    pub fn potentially_local(&self, source: &str) -> Result<bool, PlacementPlanError> {
        let plan = self.source_plan(source)?;
        Ok(!matches!(plan.placement, TensorPlacement::Omit)
            && !matches!(plan.placement, TensorPlacement::Rank { rank } if rank != self.rank.global_rank))
    }

    /// Resolves one named source against its admitted logical shape.
    pub fn resolve(
        &self,
        source: &str,
        shape: &[usize],
    ) -> Result<ResolvedTensorPlacement, PlacementPlanError> {
        let plan = self.source_plan(source)?;
        if plan
            .expected_source_shape
            .as_ref()
            .is_some_and(|expected| expected != shape)
        {
            return Err(PlacementPlanError::SourceShapeMismatch {
                checkpoint_source: source.to_owned(),
                expected: plan.expected_source_shape.clone().unwrap(),
                actual: shape.to_vec(),
            });
        }
        validate_tensor_placement(&plan.placement, shape, self.rank)?;
        Ok(match &plan.placement {
            TensorPlacement::Replicated | TensorPlacement::Local => {
                ResolvedTensorPlacement::Materialize
            }
            TensorPlacement::Omit => ResolvedTensorPlacement::Omit,
            TensorPlacement::Rank { rank } if *rank == self.rank.global_rank => {
                ResolvedTensorPlacement::Materialize
            }
            TensorPlacement::Rank { .. } => ResolvedTensorPlacement::Omit,
            TensorPlacement::Shard { axis, index, parts } => {
                let slice = TensorSlice::for_shape(shape, *axis, *index, *parts)?;
                ResolvedTensorPlacement::Selection(TensorSelection::Range {
                    axis: slice.axis,
                    start: slice.start,
                    end: slice.end,
                })
            }
            TensorPlacement::Range { axis, start, end } => {
                ResolvedTensorPlacement::Selection(TensorSelection::Range {
                    axis: *axis,
                    start: *start,
                    end: *end,
                })
            }
            TensorPlacement::Indices { axis, indices } => {
                ResolvedTensorPlacement::Selection(TensorSelection::Indices {
                    axis: *axis,
                    indices: indices.clone(),
                })
            }
        })
    }

    /// Verifies exact locally required and unexpected source coverage.
    pub fn validate_loaded_sources(
        &self,
        loaded: &BTreeSet<String>,
        mut unexpected: Vec<String>,
    ) -> Result<(), PlacementPlanError> {
        let mut missing = self
            .tensors
            .iter()
            .filter_map(|(source, plan)| {
                let required = !matches!(plan.placement, TensorPlacement::Omit)
                    && !matches!(plan.placement, TensorPlacement::Rank { rank } if rank != self.rank.global_rank);
                (required && !loaded.contains(source)).then_some(source.clone())
            })
            .collect::<Vec<_>>();
        missing.sort();
        unexpected.sort();
        unexpected.dedup();
        if missing.is_empty() && unexpected.is_empty() {
            Ok(())
        } else {
            Err(PlacementPlanError::Coverage {
                missing,
                unexpected,
            })
        }
    }

    fn source_plan(&self, source: &str) -> Result<TensorPlan, PlacementPlanError> {
        self.tensors
            .get(source)
            .cloned()
            .or_else(|| {
                self.default.as_ref().map(|placement| TensorPlan {
                    placement: placement.clone(),
                    expected_source_shape: None,
                })
            })
            .ok_or_else(|| PlacementPlanError::UnexpectedSource {
                checkpoint_source: source.to_owned(),
            })
    }
}

/// Rank-local result of resolving one logical placement.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ResolvedTensorPlacement {
    /// Materialize the complete source.
    Materialize,
    /// Do not access the source on this rank.
    Omit,
    /// Materialize one exact bounded source selection.
    Selection(TensorSelection),
}

fn validate_tensor_plan(plan: &TensorPlan, rank: PlacementRank) -> Result<(), PlacementPlanError> {
    match &plan.placement {
        TensorPlacement::Rank { rank: owner } if *owner >= rank.world_size => {
            Err(PlacementPlanError::OwnerOutOfBounds {
                owner: *owner,
                world_size: rank.world_size,
            })
        }
        TensorPlacement::Shard { axis, index, parts } if *parts == 0 || *index >= *parts => {
            Err(PlacementPlanError::InvalidShard {
                axis: *axis,
                index: *index,
                parts: *parts,
                dimension: 0,
            })
        }
        TensorPlacement::Range { axis, start, end } if start >= end => {
            Err(PlacementPlanError::InvalidRange {
                axis: *axis,
                start: *start,
                end: *end,
                dimension: None,
            })
        }
        TensorPlacement::Indices { axis, indices }
            if indices.is_empty()
                || indices.iter().collect::<BTreeSet<_>>().len() != indices.len() =>
        {
            Err(PlacementPlanError::InvalidIndices { axis: *axis })
        }
        placement => {
            if let Some(shape) = &plan.expected_source_shape {
                validate_tensor_placement(placement, shape, rank)?;
            }
            Ok(())
        }
    }
}

fn validate_tensor_placement(
    placement: &TensorPlacement,
    shape: &[usize],
    rank: PlacementRank,
) -> Result<(), PlacementPlanError> {
    match placement {
        TensorPlacement::Rank { rank: owner } if *owner >= rank.world_size => {
            Err(PlacementPlanError::OwnerOutOfBounds {
                owner: *owner,
                world_size: rank.world_size,
            })
        }
        TensorPlacement::Shard { axis, index, parts } => {
            TensorSlice::for_shape(shape, *axis, *index, *parts).map(|_| ())
        }
        TensorPlacement::Range { axis, start, end } => {
            let dimension = *shape
                .get(*axis)
                .ok_or(PlacementPlanError::AxisOutOfBounds {
                    axis: *axis,
                    rank: shape.len(),
                })?;
            if start >= end || *end > dimension {
                Err(PlacementPlanError::InvalidRange {
                    axis: *axis,
                    start: *start,
                    end: *end,
                    dimension: Some(dimension),
                })
            } else {
                Ok(())
            }
        }
        TensorPlacement::Indices { axis, indices } => {
            let dimension = *shape
                .get(*axis)
                .ok_or(PlacementPlanError::AxisOutOfBounds {
                    axis: *axis,
                    rank: shape.len(),
                })?;
            if indices.is_empty()
                || indices.iter().collect::<BTreeSet<_>>().len() != indices.len()
                || indices.iter().any(|index| *index >= dimension)
            {
                Err(PlacementPlanError::InvalidIndices { axis: *axis })
            } else {
                Ok(())
            }
        }
        _ => Ok(()),
    }
}

/// Failure while validating or resolving a complete logical placement plan.
#[derive(Debug, Clone, Eq, PartialEq, thiserror::Error)]
pub enum PlacementPlanError {
    /// The selected world/rank pair is invalid.
    #[error("global rank {global_rank} is outside world size {world_size}")]
    InvalidRank {
        /// Process count.
        world_size: usize,
        /// Selected process rank.
        global_rank: usize,
    },
    /// A placement owner exceeds the selected world.
    #[error("owner rank {owner} is outside world size {world_size}")]
    OwnerOutOfBounds {
        /// Invalid owner.
        owner: usize,
        /// Process count.
        world_size: usize,
    },
    /// A tensor axis exceeds its rank.
    #[error("tensor axis {axis} is outside rank {rank}")]
    AxisOutOfBounds {
        /// Invalid axis.
        axis: usize,
        /// Tensor rank.
        rank: usize,
    },
    /// Equal-shard geometry is invalid.
    #[error("shard {index}/{parts} on axis {axis} is invalid for dimension {dimension}")]
    InvalidShard {
        /// Selected axis.
        axis: usize,
        /// Shard index.
        index: usize,
        /// Shard count.
        parts: usize,
        /// Source dimension.
        dimension: usize,
    },
    /// Explicit range geometry is invalid.
    #[error("range {start}..{end} on axis {axis} is invalid for dimension {dimension:?}")]
    InvalidRange {
        /// Selected axis.
        axis: usize,
        /// Inclusive offset.
        start: usize,
        /// Exclusive offset.
        end: usize,
        /// Known source dimension.
        dimension: Option<usize>,
    },
    /// Explicit indices are empty, duplicated, or out of bounds.
    #[error("index selection on axis {axis} is invalid")]
    InvalidIndices {
        /// Selected axis.
        axis: usize,
    },
    /// Slice arithmetic overflowed.
    #[error("tensor slice arithmetic overflowed")]
    ArithmeticOverflow,
    /// A strict plan did not declare one checkpoint source.
    #[error("checkpoint source {checkpoint_source:?} is unexpected")]
    UnexpectedSource {
        /// Undeclared checkpoint source.
        checkpoint_source: String,
    },
    /// Admitted and actual source shapes differ.
    #[error("checkpoint source {checkpoint_source:?} has shape {actual:?}, expected {expected:?}")]
    SourceShapeMismatch {
        /// Exact checkpoint source.
        checkpoint_source: String,
        /// Admitted shape.
        expected: Vec<usize>,
        /// Actual shape.
        actual: Vec<usize>,
    },
    /// Local materialization did not exactly cover the plan.
    #[error("placement coverage mismatch: missing {missing:?}, unexpected {unexpected:?}")]
    Coverage {
        /// Required sources not loaded.
        missing: Vec<String>,
        /// Undeclared sources encountered.
        unexpected: Vec<String>,
    },
}

/// Applies all architecture-selected logical placements to parameter recipes.
pub fn place_weight_bindings(
    bindings: Vec<WeightBinding>,
    source: &dyn CheckpointSource,
    layout: &LocalModelLayout,
) -> Result<Vec<WeightBinding>, BindingPlacementError> {
    apply_binding_placements(bindings, source, layout, false)
}

/// Applies remaining non-member placements to addressable-bank bindings.
///
/// The bank catalog has already selected semantic axis zero, so the ordinary
/// logical lowering skips only that consumed member axis.
pub fn place_addressable_member_bindings(
    bindings: Vec<WeightBinding>,
    source: &dyn CheckpointSource,
    layout: &LocalModelLayout,
) -> Result<Vec<WeightBinding>, BindingPlacementError> {
    apply_binding_placements(bindings, source, layout, true)
}

fn apply_binding_placements(
    bindings: Vec<WeightBinding>,
    source: &dyn CheckpointSource,
    layout: &LocalModelLayout,
    skip_member_axis: bool,
) -> Result<Vec<WeightBinding>, BindingPlacementError> {
    let source_keys = source.source_keys().into_iter().collect::<BTreeSet<_>>();
    let mut output = Vec::with_capacity(bindings.len());
    for binding in bindings {
        if binding.is_alias() {
            output.push(binding);
            continue;
        }
        let logical_target = binding
            .logical_target()
            .ok_or_else(|| BindingPlacementError::MissingLogicalTarget {
                binding: binding.name().to_owned(),
            })?
            .to_owned();
        let tensor = layout.tensor(&logical_target).ok_or_else(|| {
            BindingPlacementError::UnknownLogicalTarget {
                binding: binding.name().to_owned(),
                target: logical_target.clone(),
            }
        })?;
        let companions = binding.quantization_companions().cloned();

        if !skip_member_axis
            && binding.recipe().is_none()
            && !source_keys.contains(binding.checkpoint_key())
        {
            if !tensor.additional_placements().is_empty() {
                return Err(BindingPlacementError::CompoundPlacementRequiresRecipe {
                    binding: binding.name().to_owned(),
                });
            }
            let selection = placement_selection(tensor, tensor.placement(), tensor.global_shape())?;
            if selection == TensorSelection::Full {
                output.push(binding);
                continue;
            }
            let global = tensor
                .global_shape()
                .iter()
                .try_fold(1usize, |value, item| value.checked_mul(*item));
            let local = tensor
                .local_shape()
                .iter()
                .try_fold(1usize, |value, item| value.checked_mul(*item));
            let expected_bytes = global
                .zip(local)
                .and_then(|(global, local)| {
                    binding
                        .expected_bytes()
                        .checked_mul(local as u64)
                        .and_then(|bytes| bytes.checked_div(global as u64))
                })
                .ok_or_else(|| BindingPlacementError::ByteGeometry {
                    binding: binding.name().to_owned(),
                })?;
            let mut placed = WeightBinding::new(
                binding.name(),
                binding.checkpoint_key(),
                selection,
                expected_bytes,
            )?
            .with_logical_target(logical_target)?;
            if let Some(companions) = companions {
                placed = placed.with_quantization_companions(
                    companions.scale(),
                    companions.affine_bias().map(str::to_owned),
                )?;
            }
            output.push(placed);
            continue;
        }

        let mut recipe = binding.source_recipe();
        let mut selected = false;
        for placement in tensor
            .additional_placements()
            .iter()
            .chain(std::iter::once(tensor.placement()))
        {
            let member_axis = matches!(
                placement,
                TensorPlacement::Shard { axis: 0, .. }
                    | TensorPlacement::Range { axis: 0, .. }
                    | TensorPlacement::Indices { axis: 0, .. }
            );
            if skip_member_axis && member_axis {
                continue;
            }
            let metadata = recipe.infer(source)?;
            let selection = placement_selection(tensor, placement, metadata.shape())?;
            if selection != TensorSelection::Full {
                recipe = recipe.select_bounded(source, selection)?;
                selected = true;
            }
        }
        if !selected {
            output.push(binding);
            continue;
        }
        let expected_bytes = recipe.infer(source)?.byte_len();
        let mut placed = WeightBinding::from_recipe(binding.name(), recipe, expected_bytes)?
            .with_logical_target(logical_target)?;
        if let Some(companions) = companions {
            placed = placed.with_quantization_companions(
                companions.scale(),
                companions.affine_bias().map(str::to_owned),
            )?;
        }
        output.push(placed);
    }
    Ok(output)
}

/// Lowers one validated logical placement against exact stored geometry.
pub fn placement_selection(
    tensor: &LocalTensorLayout,
    placement: &TensorPlacement,
    stored_shape: &[usize],
) -> Result<TensorSelection, BindingPlacementError> {
    let scale_boundary = |axis: usize, boundary: usize| -> Result<usize, BindingPlacementError> {
        let semantic =
            *tensor
                .global_shape()
                .get(axis)
                .ok_or(BindingPlacementError::AxisOutOfBounds {
                    axis,
                    rank: tensor.global_shape().len(),
                })?;
        let stored = *stored_shape
            .get(axis)
            .ok_or(BindingPlacementError::AxisOutOfBounds {
                axis,
                rank: stored_shape.len(),
            })?;
        boundary
            .checked_mul(stored)
            .and_then(|value| value.checked_div(semantic))
            .filter(|scaled| *scaled * semantic == boundary * stored)
            .ok_or(BindingPlacementError::UnalignedBoundary {
                axis,
                boundary,
                semantic,
                stored,
            })
    };

    Ok(match placement {
        TensorPlacement::Replicated | TensorPlacement::Local => TensorSelection::Full,
        TensorPlacement::Shard { axis, index, parts } => {
            let stored =
                *stored_shape
                    .get(*axis)
                    .ok_or(BindingPlacementError::AxisOutOfBounds {
                        axis: *axis,
                        rank: stored_shape.len(),
                    })?;
            if *parts == 0 || *index >= *parts || !stored.is_multiple_of(*parts) {
                return Err(BindingPlacementError::InvalidShard {
                    axis: *axis,
                    index: *index,
                    parts: *parts,
                    stored,
                });
            }
            let width = stored / *parts;
            TensorSelection::Range {
                axis: *axis,
                start: index * width,
                end: (index + 1) * width,
            }
        }
        TensorPlacement::Range { axis, start, end } => TensorSelection::Range {
            axis: *axis,
            start: scale_boundary(*axis, *start)?,
            end: scale_boundary(*axis, *end)?,
        },
        TensorPlacement::Indices { axis, indices } => {
            let stored =
                *stored_shape
                    .get(*axis)
                    .ok_or(BindingPlacementError::AxisOutOfBounds {
                        axis: *axis,
                        rank: stored_shape.len(),
                    })?;
            let semantic = *tensor.global_shape().get(*axis).ok_or(
                BindingPlacementError::AxisOutOfBounds {
                    axis: *axis,
                    rank: tensor.global_shape().len(),
                },
            )?;
            if stored != semantic {
                return Err(BindingPlacementError::IndexedPackedStorage {
                    axis: *axis,
                    semantic,
                    stored,
                });
            }
            TensorSelection::Indices {
                axis: *axis,
                indices: indices.clone(),
            }
        }
        TensorPlacement::Omit | TensorPlacement::Rank { .. } => {
            return Err(BindingPlacementError::NonLocalPlacement {
                placement: placement.clone(),
            });
        }
    })
}

/// Failure while lowering logical placement into bounded source selections.
#[derive(Debug, thiserror::Error)]
pub enum BindingPlacementError {
    /// A binding omitted its architecture-logical target.
    #[error("binding {binding:?} has no logical placement target")]
    MissingLogicalTarget {
        /// Binding without a logical target.
        binding: String,
    },
    /// The logical target is absent from the selected layout.
    #[error("binding {binding:?} targets unknown layout entry {target:?}")]
    UnknownLogicalTarget {
        /// Binding being placed.
        binding: String,
        /// Missing layout target.
        target: String,
    },
    /// A source-less direct declaration cannot express compound placement.
    #[error("compound placement for {binding:?} requires an admitted checkpoint recipe")]
    CompoundPlacementRequiresRecipe {
        /// Binding requiring an admitted recipe.
        binding: String,
    },
    /// Rank-local byte calculation overflowed or was not integral.
    #[error("cannot derive rank-local byte geometry for {binding:?}")]
    ByteGeometry {
        /// Binding with invalid byte geometry.
        binding: String,
    },
    /// A placement axis exceeds a tensor rank.
    #[error("placement axis {axis} is outside tensor rank {rank}")]
    AxisOutOfBounds {
        /// Invalid axis.
        axis: usize,
        /// Available tensor rank.
        rank: usize,
    },
    /// A semantic boundary is not exactly representable in packed storage.
    #[error("semantic boundary {boundary} on axis {axis} ({semantic}) is not aligned to stored width {stored}")]
    UnalignedBoundary {
        /// Selected axis.
        axis: usize,
        /// Semantic boundary.
        boundary: usize,
        /// Complete semantic width.
        semantic: usize,
        /// Complete stored width.
        stored: usize,
    },
    /// Equal-shard geometry is invalid.
    #[error("shard {index}/{parts} on axis {axis} is invalid for stored width {stored}")]
    InvalidShard {
        /// Selected axis.
        axis: usize,
        /// Selected shard index.
        index: usize,
        /// Requested shard count.
        parts: usize,
        /// Complete stored width.
        stored: usize,
    },
    /// Indexed semantic selection cannot address differently packed storage.
    #[error("indexed axis {axis} has semantic width {semantic} but stored width {stored}")]
    IndexedPackedStorage {
        /// Selected axis.
        axis: usize,
        /// Complete semantic width.
        semantic: usize,
        /// Complete stored width.
        stored: usize,
    },
    /// Execution-unit binding selected an omit or remote-rank placement.
    #[error("execution-unit binding has non-local placement {placement:?}")]
    NonLocalPlacement {
        /// Rejected non-local placement.
        placement: TensorPlacement,
    },
    /// Checkpoint access failed.
    #[error(transparent)]
    Store(#[from] StoreError),
    /// Recipe inference failed.
    #[error(transparent)]
    Recipe(#[from] RecipeError),
    /// Lowered residency declaration is invalid.
    #[error(transparent)]
    Declaration(#[from] ResidencyDeclarationError),
    /// Bounded source selection failed.
    #[error(transparent)]
    Selection(#[from] WeightBindingSelectionError),
}