File size: 9,646 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 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 | // SPDX-License-Identifier: LicenseRef-Sovereign-Leviathan-SL-AGPL3-001
// Licensing: see LICENSE in the repository root.
//! Tensor views and slicing operations (zero-copy)
//!
//! Views share underlying buffer via Arc, never copy data.
//! Slicing creates new semantic shape/strides without modifying source.
use crate::buffer::{Buffer, BufferHandle};
use crate::error::{HardwareError, Result};
use crate::memory::MemoryLayout;
use crate::ownership::OwnershipToken;
/// Tensor view descriptor with semantic shape/strides
#[derive(Clone, Debug)]
pub struct TensorView {
/// Shared buffer handle
pub buffer_handle: BufferHandle,
/// Semantic shape (may be transposed/reshaped)
pub shape: Vec<usize>,
/// Memory strides for each dimension
pub strides: Vec<usize>,
/// Starting offset in buffer
pub offset: usize,
/// Ownership state
pub ownership: OwnershipToken,
}
impl TensorView {
/// Create a view from buffer and layout
pub fn from_buffer(buffer: &Buffer, layout: MemoryLayout) -> Self {
TensorView {
buffer_handle: BufferHandle::from_buffer(buffer),
shape: layout.shape,
strides: layout.strides,
offset: layout.offset,
ownership: OwnershipToken::new_exclusive(),
}
}
/// Create a shared view (Arc::clone)
///
/// **Governance**: B_own: 1 → 0, data NOT copied
pub fn share(&self) -> Result<Self> {
let new_ownership = self.ownership.transition_to_shared()?;
Ok(TensorView {
buffer_handle: self.buffer_handle,
shape: self.shape.clone(),
strides: self.strides.clone(),
offset: self.offset,
ownership: new_ownership,
})
}
/// Create a slice view (subset of dimensions)
///
/// **Input**: ranges = [(start, end, step), ...] per dimension
/// **Result**:
/// - New shape: (end - start) / step per dimension
/// - New strides: stride[i] * step
/// - New offset: offset + Σ(start[i] * stride[i])
/// - B_own: → 0 (shared)
/// - B_layout: recomputed
///
/// **Invariant**: Source buffer remains valid and unchanged
pub fn slice(&self, ranges: &[(usize, usize, usize)]) -> Result<Self> {
if ranges.len() != self.shape.len() {
return Err(HardwareError::RankMismatch {
expected: self.shape.len(),
actual: ranges.len(),
});
}
let mut new_shape = Vec::with_capacity(self.shape.len());
let mut new_strides = Vec::with_capacity(self.strides.len());
let mut new_offset = self.offset;
for (dim, &(start, end, step)) in ranges.iter().enumerate() {
// Bounds check
if start > end || end > self.shape[dim] || step == 0 {
return Err(HardwareError::IndexOutOfBounds {
index: start,
size: self.shape[dim],
});
}
let new_size = if end > start {
(end - start + step - 1) / step
} else {
0
};
new_shape.push(new_size);
// Update offset
new_offset = new_offset.saturating_add(start.saturating_mul(self.strides[dim]));
// Update stride
new_strides.push(self.strides[dim].saturating_mul(step));
}
let new_ownership = self.ownership.transition_to_shared()?;
Ok(TensorView {
buffer_handle: self.buffer_handle,
shape: new_shape,
strides: new_strides,
offset: new_offset,
ownership: new_ownership,
})
}
/// Transpose (permute) dimensions
///
/// **Input**: permutation = new ordering of dimension indices
/// **Example**: [2, 0, 1] for 3D tensor means new_shape[0] = old_shape[2]
pub fn transpose(&self, permutation: &[usize]) -> Result<Self> {
if permutation.len() != self.shape.len() {
return Err(HardwareError::RankMismatch {
expected: self.shape.len(),
actual: permutation.len(),
});
}
// Validate permutation is a valid reordering
let mut seen = vec![false; permutation.len()];
for &p in permutation {
if p >= permutation.len() || seen[p] {
return Err(HardwareError::ViewCreationFailed {
reason: "Invalid permutation (not a valid reordering)",
});
}
seen[p] = true;
}
// Apply permutation to shape and strides
let mut new_shape = Vec::with_capacity(self.shape.len());
let mut new_strides = Vec::with_capacity(self.strides.len());
for &p in permutation {
new_shape.push(self.shape[p]);
new_strides.push(self.strides[p]);
}
// Transposition makes strides non-canonical
let new_ownership = self.ownership.transition_to_non_canonical()?;
Ok(TensorView {
buffer_handle: self.buffer_handle,
shape: new_shape,
strides: new_strides,
offset: self.offset,
ownership: new_ownership,
})
}
/// Reshape to new shape
///
/// **Precondition**: element_count must match
/// **Note**: May require materialization if strides are non-canonical
pub fn reshape(&self, new_shape: Vec<usize>) -> Result<Self> {
if new_shape.is_empty() {
return Err(HardwareError::ZeroElementShape);
}
let new_element_count: usize = new_shape.iter().product();
if new_element_count != self.element_count() {
return Err(HardwareError::ViewCreationFailed {
reason: "Reshape: element count mismatch",
});
}
// Can only reshape directly if canonical strides
if !self.ownership.is_canonical {
return Err(HardwareError::ViewCreationFailed {
reason: "Cannot reshape non-canonical tensor (needs materialization first)",
});
}
// Compute new canonical strides
let mut new_strides = vec![0; new_shape.len()];
let mut stride: usize = 1;
for i in (0..new_shape.len()).rev() {
new_strides[i] = stride;
stride = stride.saturating_mul(new_shape[i]);
}
Ok(TensorView {
buffer_handle: self.buffer_handle,
shape: new_shape,
strides: new_strides,
offset: self.offset,
ownership: self.ownership,
})
}
/// Get total number of elements
#[inline]
pub fn element_count(&self) -> usize {
self.shape.iter().product()
}
/// Calculate address for multi-dimensional index
pub fn address_for(&self, indices: &[usize]) -> Result<usize> {
if indices.len() != self.shape.len() {
return Err(HardwareError::RankMismatch {
expected: self.shape.len(),
actual: indices.len(),
});
}
// Bounds check
for (i, &idx) in indices.iter().enumerate() {
if idx >= self.shape[i] {
return Err(HardwareError::IndexOutOfBounds {
index: idx,
size: self.shape[i],
});
}
}
// Calculate strided address
let mut addr = self.offset;
for (i, &idx) in indices.iter().enumerate() {
addr = addr.saturating_add(idx.saturating_mul(self.strides[i]));
}
Ok(addr)
}
/// Check if this view has canonical strides
pub fn is_contiguous(&self) -> bool {
self.ownership.is_canonical
}
/// Check if this view is exclusive owner
pub fn is_exclusive(&self) -> bool {
self.ownership.is_owner
}
}
#[cfg(test)]
mod tests {
use super::*;
fn create_test_view(shape: Vec<usize>) -> TensorView {
let buf = Buffer::allocate(1, shape.iter().product()).unwrap();
let strides = {
let mut s = vec![0; shape.len()];
let mut stride = 1;
for i in (0..shape.len()).rev() {
s[i] = stride;
stride *= shape[i];
}
s
};
let layout = MemoryLayout::new(buf.capacity(), 0, shape, strides).unwrap();
TensorView::from_buffer(&buf, layout)
}
#[test]
fn test_share_makes_non_exclusive() {
let view = create_test_view(vec![10, 10]);
assert!(view.is_exclusive());
let shared = view.share().unwrap();
assert!(!shared.is_exclusive());
assert_eq!(shared.element_count(), 100);
}
#[test]
fn test_slice_reduces_dimensions() {
let view = create_test_view(vec![10, 10]);
let sliced = view.slice(&[(2, 5, 1), (3, 7, 1)]).unwrap();
assert_eq!(sliced.shape, vec![3, 4]);
}
#[test]
fn test_transpose_permutes_shape() {
let view = create_test_view(vec![2, 3, 4]);
let transposed = view.transpose(&[2, 0, 1]).unwrap();
assert_eq!(transposed.shape, vec![4, 2, 3]);
}
#[test]
fn test_reshape_requires_canonical() {
let view = create_test_view(vec![10, 10]);
let reshaped = view.reshape(vec![100]).unwrap();
assert_eq!(reshaped.element_count(), 100);
}
#[test]
fn test_address_calculation() {
let view = create_test_view(vec![10, 10]);
let addr = view.address_for(&[2, 3]).unwrap();
assert_eq!(addr, 2 * 10 + 3);
}
}
|