File size: 8,243 Bytes
f7ccd08 32b4893 | 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 | // SPDX-License-Identifier: LicenseRef-Sovereign-Leviathan-SL-AGPL3-001
// Licensing: see LICENSE in the repository root.
//! Materialization barrier: transactional conversion to contiguous memory
//!
//! Materializes non-canonical strides into contiguous buffer.
//! **Transactional**: read → alloc → copy → install → commit
//! **Invariant**: Source buffer remains valid and unchanged
use crate::buffer::{Buffer, BufferId};
use crate::error::{HardwareError, Result};
use crate::view::TensorView;
/// Materialization result with new buffer and view
#[derive(Clone)]
pub struct MaterializationResult {
/// New contiguous buffer
pub new_buffer: Buffer,
/// View of new buffer
pub new_view: TensorView,
/// Source buffer ID (for reference)
pub source_buffer_id: BufferId,
}
/// Transactional materialization barrier
#[derive(Debug)]
pub struct MaterializationBarrier {
/// Next buffer ID to allocate
next_buffer_id: u64,
}
impl MaterializationBarrier {
pub fn new() -> Self {
MaterializationBarrier {
next_buffer_id: 2, // 1 is reserved for initial allocation
}
}
/// Materialize non-canonical tensor to new contiguous buffer
///
/// **Transaction Steps**:
/// 1. **Read**: Load source view metadata (safe snapshot)
/// 2. **Alloc**: Create new buffer with same element count
/// 3. **Copy**: Copy strided data to new contiguous buffer (non-destructive)
/// 4. **Install**: Create new view with new buffer ID and generation
/// 5. **Commit**: Return new buffer (source unchanged)
///
/// **Governance Transitions**:
/// - New buffer: B_own=1 (exclusive), B_layout=1 (canonical)
/// - Source: unchanged (still valid)
/// - New generation counter
///
/// **Safety**: Source buffer refcount incremented, then decremented by drop
pub fn materialize(
&mut self,
source_view: &TensorView,
source_buffer: &Buffer,
) -> Result<MaterializationResult> {
// Step 1: Validate preconditions
source_buffer.validate_generation(source_view.buffer_handle.generation)?;
if source_view.ownership.is_canonical {
// Already canonical, no need to materialize
return Err(HardwareError::MaterializationFailed {
reason: "Tensor is already canonical (materialization not needed)",
});
}
// Step 2: Allocate new buffer
let element_count = source_view.element_count();
let new_buffer_id = self.next_buffer_id;
self.next_buffer_id += 1;
let new_buffer = Buffer::allocate(new_buffer_id, element_count).map_err(|_| {
HardwareError::AllocationFailed {
size: element_count * std::mem::size_of::<f32>(),
}
})?;
// Step 3: Copy strided data to new contiguous buffer
// For each index in the source, copy to linear index in new buffer
self.copy_strided_to_linear(source_view, source_buffer, &new_buffer)?;
// Step 4: Create new view with canonical strides
let canonical_strides = {
let mut s = vec![0; source_view.shape.len()];
let mut stride = 1;
for i in (0..source_view.shape.len()).rev() {
s[i] = stride;
stride *= source_view.shape[i];
}
s
};
let new_view = TensorView {
buffer_handle: crate::buffer::BufferHandle::from_buffer(&new_buffer),
shape: source_view.shape.clone(),
strides: canonical_strides,
offset: 0, // New buffer starts at offset 0
ownership: crate::ownership::OwnershipToken::new_exclusive(),
};
// Step 5: Commit (return new buffer)
Ok(MaterializationResult {
new_buffer,
new_view,
source_buffer_id: source_view.buffer_handle.id,
})
}
/// Copy strided data from source to linear destination
///
/// Uses address calculation to read from arbitrary strides,
/// writes linearly to destination.
fn copy_strided_to_linear(
&self,
source_view: &TensorView,
source_buffer: &Buffer,
dest_buffer: &Buffer,
) -> Result<()> {
let mut linear_idx = 0;
// Iterate through all indices in source tensor
self.iterate_indices(&source_view.shape, |indices| {
// Calculate source address
let source_addr = {
let mut addr = source_view.offset;
for (i, &idx) in indices.iter().enumerate() {
addr = addr.saturating_add(idx.saturating_mul(source_view.strides[i]));
}
addr
};
if source_addr < source_buffer.capacity() && linear_idx < dest_buffer.capacity() {
let value = source_buffer.get_unchecked(source_addr);
dest_buffer.set_unchecked(linear_idx, value);
}
linear_idx += 1;
});
Ok(())
}
/// Iterate through all multi-dimensional indices for a shape
fn iterate_indices<F>(&self, shape: &[usize], mut callback: F)
where
F: FnMut(Vec<usize>),
{
if shape.is_empty() {
callback(vec![]);
return;
}
let mut indices = vec![0; shape.len()];
loop {
callback(indices.clone());
// Increment indices
let mut carry = 1;
for i in (0..shape.len()).rev() {
if carry == 0 {
break;
}
indices[i] += 1;
if indices[i] >= shape[i] {
indices[i] = 0;
} else {
carry = 0;
}
}
if carry != 0 {
break; // Overflow, done iterating
}
}
}
/// Get current next buffer ID (for testing)
pub fn next_id(&self) -> u64 {
self.next_buffer_id
}
}
impl Default for MaterializationBarrier {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::memory::MemoryLayout;
#[test]
fn test_materialize_non_canonical() {
let mut barrier = MaterializationBarrier::new();
// Create source buffer with non-canonical strides
let src_buf = Buffer::allocate(1, 100).unwrap();
let shape = vec![10, 10];
let strides = vec![5, 1]; // Non-canonical (canonical would be [10, 1])
let layout = MemoryLayout::new(100, 0, shape, strides).unwrap();
let mut view = TensorView::from_buffer(&src_buf, layout);
// Make view non-canonical
view.ownership = view.ownership.transition_to_non_canonical().unwrap();
let result = barrier.materialize(&view, &src_buf).unwrap();
assert_eq!(result.new_buffer.generation, 1);
assert!(result.new_view.ownership.is_canonical);
assert!(result.new_view.ownership.is_owner);
}
#[test]
fn test_materialize_canonical_fails() {
let mut barrier = MaterializationBarrier::new();
let src_buf = Buffer::allocate(1, 100).unwrap();
let layout = MemoryLayout::new(100, 0, vec![10, 10], vec![10, 1]).unwrap();
let view = TensorView::from_buffer(&src_buf, layout);
let result = barrier.materialize(&view, &src_buf);
assert!(result.is_err());
}
#[test]
fn test_source_remains_valid() {
let mut barrier = MaterializationBarrier::new();
let src_buf = Buffer::allocate(1, 100).unwrap();
let shape = vec![10, 10];
let strides = vec![5, 1];
let layout = MemoryLayout::new(100, 0, shape, strides).unwrap();
let mut view = TensorView::from_buffer(&src_buf, layout);
view.ownership = view.ownership.transition_to_non_canonical().unwrap();
let src_count_before = src_buf.strong_count();
let _ = barrier.materialize(&view, &src_buf);
let src_count_after = src_buf.strong_count();
// Source buffer should have original refcount
assert_eq!(src_count_before, src_count_after);
}
}
|