# Binary Crystal Tensor Format (.xtensor) ## Executive Summary **Crystalized tensors** are fully serialized, immutable, byte-aligned binary memory blocks stored directly to disk. They require zero parsing, zero deserialization, zero data movement transformation, and zero heap allocation. When mapped via `mmap()`, the binary payload **instantly matches** the physical memory layout required by x86_64 SIMD registers and NVIDIA H100 Tensor Memory Accelerators (TMA). --- ## 1. Binary Layout ### Physical File Structure ``` Offset Size Content ─────────────────────────────────────────────────── 0 64B Hardware Header (xtensor_header_t) 64 64B Alignment Padding (128-byte boundary) 128 N Raw Tensor Data (FP8/FP16/BF16/FP32) 128+N 64B Alignment Padding 128+N+64 32B SHA-256 Cryptographic Seal ``` ### 64-Byte Header ```c typedef struct __attribute__((packed)) { uint32_t magic; /* 0x58544E53 ('X','T','N','S') */ uint16_t version; /* Format Version (0x0001) */ uint16_t dtype; /* Numerical Precision */ uint32_t rank; /* Tensor Rank (1..8) */ uint32_t flags; /* Bit 0: Little-Endian, Bit 1: Sealed */ uint64_t shape[4]; /* Dimension Sizes [D0, D1, D2, D3] */ uint64_t payload_bytes; /* Raw tensor data byte count */ uint64_t alignment; /* Hardware boundary (always 128) */ uint32_t reserved[2]; /* Pad to 64 bytes */ } xtensor_header_t; ``` ### Data Type Encodings | Code | Name | Format | Bits | Target | |------|------|--------|------|--------| | 0x01 | DTYPE_FP32 | IEEE 754 Float32 | 32 | x86_64 AVX2/AVX-512 | | 0x02 | DTYPE_FP16 | IEEE 754 Float16 | 16 | H100 Tensor Core | | 0x03 | DTYPE_BF16 | Brain Float 16 | 16 | H100 WGMMA | | 0x04 | DTYPE_FP8_E4M3 | FP8 (4-exp, 3-mant) | 8 | H100 SM 90 WGMMA | | 0x05 | DTYPE_FP8_E5M2 | FP8 (5-exp, 2-mant) | 8 | H100 Gradients | --- ## 2. Zero-Copy Ingestion ```c /* Zero-Copy Disk-to-Memory Mapping */ crystal_tensor_t load_crystal_tensor(const char* filepath) { int fd = open(filepath, O_RDONLY); struct stat st; fstat(fd, &st); void* map_base = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); close(fd); crystal_tensor_t t; t.header = (const xtensor_header_t*)map_base; t.data = (const void*)((uintptr_t)map_base + 128); t.total_size = st.st_size; return t; } ``` **Direct Hardware Execution**: x86_64 Vector Load: ```asm vmovaps ymm0, [rdi] ; Load 256 bits (8 × FP32) vfmadd231ps ymm2, ymm0, ymm1 ; Accumulate ``` H100 TMA Transfer: ```c cp.async.bulk.tensor.2d.shared::cluster.global.mbarrier::complete_tx::bytes [smem_tile], [%tma_desc, {%r0, %r1}], [%mbar]; ``` --- ## 3. Cryptographic Immutability Every .xtensor ends with a **32-byte SHA-256 seal**. ```c int verify_crystal_integrity(const crystal_tensor_t* t, const uint8_t expected_hash[32]) { uint8_t computed_hash[32]; size_t hash_len = 128 + t->header->payload_bytes; sha256_compute((const uint8_t*)t->header, hash_len, computed_hash); int match = 1; for (int i = 0; i < 32; i++) { if (computed_hash[i] != expected_hash[i]) match = 0; } return match; } /* Abort if seal is invalid */ if (!verify_crystal_integrity(&tensor, seal)) { fprintf(stderr, "ABORT: Tensor corruption detected\n"); exit(1); } ``` **Execution Prevention on Failure**: If a single bit is corrupted, `verify_crystal_integrity()` returns 0, execution halts, **no partial computation** executed. --- ## 4. Creation Pipeline (PyTorch → .xtensor) ```python import struct, hashlib, numpy as np def create_crystal_tensor(numpy_array, filepath): magic = 0x58544E53 version = 0x0001 rank = len(numpy_array.shape) flags = 0x3 # Little-endian + sealed shape = list(numpy_array.shape) + [0] * (4 - rank) payload_bytes = numpy_array.nbytes header = struct.pack('