Skip to main content

flatten_composite

Function flatten_composite 

Source
pub fn flatten_composite<'a>(
    head_name: &'a str,
    head: &'a TensorDescriptor,
    members: &'a [FileCompositeNode<'a>],
) -> Result<Vec<CompositeWrite<'a>>>
Expand description

Validates a composite tree and flattens it into the order its tensors go on disk.

Every tensor — the head and each member, nested or not — gets its own footer-index entry (ADR-027 § Binding), and membership is recovered on read from the head’s member_count plus file-offset adjacency. So the order here is the membership: head first, then each member in turn, depth first.

The whole tree is validated before anything is returned — member counts, partition exact-cover, overlay ordering, at every level — so a torn or invalid composite never reaches a sink. The traversal is an explicit stack rather than recursion (§ 10).

§Errors

Error::Core — a head is not a valid composite head, or a level failed validation.

§Examples

use hurray_core::{
    layout::{CompositeLayout, CompositionRule, LayoutDescriptor},
    BufferHandle, DeviceTag, ElementType, Shape, SyncMode, TensorDescriptor,
    MIN_BUFFER_ALIGNMENT,
};
use hurray_io::file::{flatten_composite, FileCompositeNode};

let head = TensorDescriptor::new(
    1, 0, ElementType::Float32, Shape::new(vec![2u64, 2])?, 0,
    LayoutDescriptor::Composite(CompositeLayout::new(CompositionRule::Group, 1)?),
    vec![], None, None, None, None,
)?;
let handle =
    BufferHandle::new(16, MIN_BUFFER_ALIGNMENT, DeviceTag::Cpu, SyncMode::ProducerSynced)?;
let member = TensorDescriptor::new(
    1, 0, ElementType::Float32, Shape::new(vec![2u64, 2])?, 0,
    LayoutDescriptor::RowMajor, vec![handle], None, None, None, None,
)?;
let data = [0u8; 16];
let buffers: [&[u8]; 1] = [&data];
let members = [FileCompositeNode::Tensor {
    name: "w.0",
    descriptor: &member,
    buffers: &buffers,
}];

let order = flatten_composite("w", &head, &members)?;
assert_eq!(order.len(), 2);
assert_eq!(order[0].name, "w");
assert!(order[0].buffers.is_empty());
assert_eq!(order[1].name, "w.0");