Chapter 2

Tensor Data Formats and Memory Layouts

2.1 The Foundation: Tensor Representation

At the heart of every deep learning computation lies the tensor—a multidimensional array of numbers. While conceptually simple, the physical representation of tensors in memory has profound implications for performance, portability, and correctness. WIA-AI-011 Phase 1 establishes comprehensive standards for tensor data formats, ensuring that tensors can be seamlessly shared across different accelerators without conversion overhead or numerical degradation.

A tensor is characterized by several fundamental properties: its shape (dimensions), data type (element format), and layout (memory organization). Consider a simple RGB image tensor representing a batch of 32 images, each 224×224 pixels with 3 color channels. This tensor has shape [32, 3, 224, 224], but how should these 4,816,896 elements be arranged in linear memory?

2.2 Memory Layout Conventions

Different frameworks and hardware vendors have historically adopted different conventions for organizing multidimensional data in memory. The two dominant approaches are row-major (C-style) and column-major (Fortran-style), but for tensors with more than two dimensions, the situation becomes more nuanced.

2.2.1 NCHW Layout (Batch, Channel, Height, Width)

The NCHW layout, also known as channels-first, stores all channels of each spatial location together. For our image example, this means storing all pixels of channel 0, then all pixels of channel 1, then channel 2, for each image in the batch. This layout is favored by NVIDIA's cuDNN library and PyTorch (on GPU) because it enables efficient convolution implementations.

// NCHW memory layout for [2, 3, 2, 2] tensor
Batch 0, Channel 0: [a, b, c, d]
Batch 0, Channel 1: [e, f, g, h]
Batch 0, Channel 2: [i, j, k, l]
Batch 1, Channel 0: [m, n, o, p]
Batch 1, Channel 1: [q, r, s, t]
Batch 1, Channel 2: [u, v, w, x]

Linear memory: [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x]

2.2.2 NHWC Layout (Batch, Height, Width, Channel)

The NHWC layout, or channels-last, stores all channels of each pixel together. For the same tensor, this interleaves the channels: pixel (0,0) gets R,G,B values, then pixel (0,1), and so on. This layout is preferred by TensorFlow, ARM processors, and many mobile NPUs because it exhibits better cache locality for certain operations and matches how images are typically stored.

// NHWC memory layout for [2, 3, 2, 2] tensor
Batch 0, Pixel (0,0): [a, e, i]
Batch 0, Pixel (0,1): [b, f, j]
Batch 0, Pixel (1,0): [c, g, k]
Batch 0, Pixel (1,1): [d, h, l]
Batch 1, Pixel (0,0): [m, q, u]
...

Linear memory: [a,e,i,b,f,j,c,g,k,d,h,l,m,q,u,n,r,v,o,s,w,p,t,x]

2.2.3 WIA-AI-011 Layout Specification

The WIA-AI-011 standard mandates that implementations support both NCHW and NHWC layouts as first-class citizens. Additionally, it defines a stride-based representation that can describe arbitrary layouts, including blocked and padded formats commonly used for hardware optimization.

{
  "shape": [32, 3, 224, 224],
  "layout": "NCHW",
  "strides": [150528, 50176, 224, 1],
  "offset": 0,
  "element_size_bytes": 4
}

// Strides specify the number of elements to skip
// to move to the next position along each dimension
// Element at [n, c, h, w] is at:
// offset + n*strides[0] + c*strides[1] + h*strides[2] + w*strides[3]

2.3 Data Types and Precision

Neural networks can operate at various numerical precisions, trading accuracy for performance and energy efficiency. WIA-AI-011 defines a comprehensive type system covering the full spectrum of precision formats used in modern AI accelerators.

2.3.1 Floating-Point Formats

Standard IEEE 754 floating-point formats provide familiar, well-defined numerical behavior but consume significant memory and bandwidth.

Type Bits Exponent Mantissa Range Use Case
FLOAT64 64 11 52 ±10^308 Scientific computing
FLOAT32 32 8 23 ±10^38 Default training
FLOAT16 16 5 10 ±65504 Mixed precision
BFLOAT16 16 8 7 ±10^38 TPU, modern training

2.3.2 Integer and Fixed-Point Formats

Integer formats, particularly INT8, have become essential for efficient inference. They require careful calibration through quantization but offer 4× memory savings and significant computational speedups.

// Quantization parameters in WIA-AI-011
{
  "dtype": "INT8",
  "quantization": {
    "scheme": "per_tensor_symmetric",
    "scale": 0.0235,
    "zero_point": 0,
    "range": [-128, 127]
  }
}

// Dequantized value = (quantized_value - zero_point) * scale
// For symmetric quantization, zero_point = 0

2.3.3 Specialized Formats

Several specialized formats have emerged for specific use cases:

2.4 Tensor Descriptor Format

The WIA-AI-011 tensor descriptor is a comprehensive metadata structure that fully specifies a tensor's properties. This descriptor serves as the contract between different system components, ensuring unambiguous interpretation.

{
  "version": "WIA-AI-011-v1.0",
  "tensor_id": "uuid-string",
  "name": "input_images",

  "shape": {
    "dimensions": [32, 3, 224, 224],
    "layout": "NCHW",
    "symbolic": false
  },

  "dtype": {
    "base_type": "FLOAT16",
    "quantization": null,
    "byte_order": "little_endian"
  },

  "memory": {
    "strides": [150528, 50176, 224, 1],
    "offset_bytes": 0,
    "total_bytes": 9633792,
    "alignment": 256,
    "device": {
      "type": "NPU",
      "id": 0,
      "memory_space": "device"
    }
  },

  "properties": {
    "requires_grad": false,
    "is_pinned": true,
    "is_contiguous": true,
    "zero_copy_compatible": true
  },

  "metadata": {
    "created_at": "2025-01-15T10:30:00Z",
    "framework": "pytorch",
    "producer": "data_loader_v2"
  }
}

2.5 Memory Alignment and Padding

Modern accelerators impose alignment requirements on memory allocations for optimal performance. Unaligned access can cause significant slowdowns or even hardware exceptions. WIA-AI-011 specifies alignment requirements and padding rules.

2.5.1 Alignment Requirements

Different accelerators have different alignment requirements:

WIA-AI-011 mandates a minimum alignment of 64 bytes for all tensor allocations, with devices free to request stricter alignment through capability queries.

2.5.2 Padding Strategies

Padding adds extra elements to dimensions to meet hardware requirements. For example, many accelerators perform best when channel counts are multiples of 8 or 16.

// Original tensor: [32, 11, 224, 224]
// Padded for optimal performance: [32, 16, 224, 224]
// 5 channels of padding added

{
  "shape": [32, 11, 224, 224],
  "physical_shape": [32, 16, 224, 224],
  "padding": {
    "dims": [false, true, false, false],
    "values": [0, 5, 0, 0],
    "mode": "zero_fill"
  }
}

2.6 Zero-Copy Data Sharing

One of the most significant performance optimizations in WIA-AI-011 is zero-copy data sharing. Traditional approaches require copying data when moving between devices or framework boundaries. Zero-copy mechanisms eliminate these copies by sharing the underlying memory buffer.

2.6.1 Shared Memory Protocol

WIA-AI-011 defines a buffer sharing protocol based on file descriptors (POSIX) or handles (Windows) that allows processes and devices to map the same physical memory:

// Zero-copy buffer sharing
{
  "buffer_handle": {
    "type": "dma_buf_fd",  // Linux DMA-BUF
    "fd": 42,
    "size_bytes": 9633792,
    "permissions": "read_write"
  },
  "mapping": {
    "device_address": "0x7f4a2000",
    "cpu_address": null,
    "access_mode": "device_only"
  }
}

2.6.2 Pinned Memory

Pinned (or page-locked) memory cannot be swapped to disk, ensuring stable physical addresses that hardware DMA engines can use directly. WIA-AI-011 provides APIs for allocating pinned memory:

// Pinned memory allocation
wia_memory_desc_t desc = {
    .size = 9633792,
    .alignment = 256,
    .flags = WIA_MEM_PINNED | WIA_MEM_ZERO_COPY,
    .device_affinity = WIA_DEVICE_NPU_0
};

wia_buffer_t buffer;
wia_status_t status = wia_allocate_memory(&desc, &buffer);

2.7 Handling Dynamic Shapes

Many modern neural networks have dynamic shapes that vary based on input. Transformer models process variable-length sequences, object detectors handle different image sizes, and beam search generates outputs of unknown length. WIA-AI-011 supports dynamic shapes through symbolic dimensions.

{
  "shape": {
    "dimensions": ["batch", "sequence_length", 768],
    "symbolic": true,
    "constraints": {
      "batch": {"min": 1, "max": 256},
      "sequence_length": {"min": 1, "max": 2048, "multiple_of": 8}
    }
  },
  "allocation_strategy": "overallocate",
  "max_physical_shape": [256, 2048, 768]
}

2.8 Endianness and Cross-Platform Compatibility

While most modern systems use little-endian byte ordering, ensuring cross-platform compatibility requires explicit specification. WIA-AI-011 tensors carry endianness metadata:

{
  "dtype": {
    "base_type": "FLOAT32",
    "byte_order": "little_endian",  // or "big_endian", "network_order"
    "bit_order": "lsb_first"
  }
}

When transferring tensors between systems with different endianness, WIA-AI-011 implementations must perform byte swapping automatically or raise an error if in-place conversion is impossible.

2.9 Sparse Tensor Formats

Many neural network tensors are sparse, containing mostly zeros. Storing sparse tensors efficiently requires specialized formats. WIA-AI-011 supports several sparse formats:

2.9.1 Coordinate (COO) Format

{
  "format": "COO",
  "shape": [1000, 1000],
  "nnz": 5000,  // number of non-zero elements
  "indices": {
    "dtype": "INT32",
    "data": [[0,5], [0,12], [1,3], ...]  // [row, col] pairs
  },
  "values": {
    "dtype": "FLOAT32",
    "data": [1.5, 2.3, 0.7, ...]
  }
}

2.9.2 Compressed Sparse Row (CSR) Format

CSR is more efficient for sparse matrix operations, particularly for GPUs:

{
  "format": "CSR",
  "shape": [1000, 1000],
  "row_pointers": [0, 3, 7, 12, ...],  // 1001 elements
  "column_indices": [5, 12, 45, ...],  // nnz elements
  "values": [1.5, 2.3, 0.7, ...]       // nnz elements
}

2.10 Practical Implementation Considerations

Implementing WIA-AI-011 tensor formats requires careful attention to several practical concerns:

Performance Validation

Implementations should include benchmarks verifying that the standardized formats don't introduce overhead compared to vendor-native formats. Layout conversions, when necessary, should be lazy and cached.

Backward Compatibility

Providing adapters for existing framework tensor formats eases adoption. PyTorch's torch.Tensor, TensorFlow's tf.Tensor, and NumPy's ndarray should all have efficient conversion paths to/from WIA-AI-011 descriptors.

Versioning

The tensor descriptor includes a version field, allowing future extensions while maintaining compatibility with existing implementations. Readers must gracefully handle newer versions by ignoring unknown fields.

Summary

Review Questions

  1. Explain the difference between NCHW and NHWC memory layouts. Which is preferred by cuDNN and why?
  2. How do strides enable representation of arbitrary memory layouts? Give an example calculation.
  3. Compare FLOAT16 and BFLOAT16. What are the trade-offs between them?
  4. What is symmetric quantization, and how does it differ from asymmetric quantization?
  5. Why is memory alignment important for accelerator performance? What is WIA-AI-011's minimum alignment requirement?
  6. Describe the zero-copy data sharing mechanism and its performance benefits.
  7. How does WIA-AI-011 handle dynamic tensor shapes? What are symbolic dimensions?
  8. Explain the difference between COO and CSR sparse tensor formats. When would you choose each?
  9. What role does the tensor descriptor play in the WIA-AI-011 ecosystem?
  10. Why must tensor descriptors include endianness information for cross-platform compatibility?
弘益人間 (홍익인간) · Benefit All Humanity

Korea Standardization Infrastructure Mapping

Korea operates a comprehensive standards governance system through inter-ministerial cooperation. National Standards Council (under Prime Minister's Office, per Framework Act on National Standards Article 5) coordinates KATS (Korean Agency for Technology and Standards), MFDS (Ministry of Food and Drug Safety), MOTIE (Ministry of Trade, Industry and Energy), MSIT (Ministry of Science and ICT), MOIS (Ministry of the Interior and Safety), MOE (Ministry of Environment), MOHW (Ministry of Health and Welfare), MND (Ministry of National Defense), MCST (Ministry of Culture, Sports and Tourism), MOFA (Ministry of Foreign Affairs), MOJ (Ministry of Justice), and FSC (Financial Services Commission). Accreditation and Testing: KOLAS (Korea Laboratory Accreditation Scheme) accredits 800+ testing laboratories. KAS (Korea Accreditation System) accredits 50+ certification bodies. KTC (Korea Testing Certification), KTR (Korea Testing & Research Institute), KTL (Korea Testing Laboratory), and KCL (Korea Conformity Laboratories) provide conformance testing. Telecom and Cyber: KCC (Korea Communications Commission), KCA (Korea Communications Agency), TTA (Telecommunications Technology Association), IITP (Institute for Information & Communications Technology Planning & Evaluation), NIPA (National IT Industry Promotion Agency), KISA (Korea Internet & Security Agency), KCMVP (Korea Cryptographic Module Validation Program), NIS (National Intelligence Service), NSR (National Security Research Institute), and NCSC (National Cyber Security Center). National R&D Centers: KIST, ETRI, KAIST, Seoul National University, Yonsei University, Korea University, POSTECH, UNIST, GIST, DGIST, KISTI, KIER, KIMM, KRICT, KFRI, KRIBB. International Standards Cooperation: ISO TC/SC Korean secretariats, IEC TC/SC Korean secretariats, ITU-T Study Group Korean chairs, 3GPP RAN/SA Korean chairs, IEEE 802 Korean chairs, W3C Korea office, OASIS Korea office, IETF Korea cooperation, OECD CSTP, UN ESCAP, APEC SCSC Korean cooperation. Korean Industrial Standards (KS) Catalog: KS X (Information) 25,000+, KS A (Basic) 15,000+, KS B (Machinery) 25,000+, KS C (Electrical) 18,000+, KS D (Metallurgy) 12,000+, KS E (Mining) 5,000+, KS F (Construction) 18,000+, KS H (Food) 8,000+, KS I (Environment) 5,000+, KS J (Biology) 3,000+, KS K (Textile) 15,000+, KS L (Ceramics) 7,000+, KS M (Chemistry) 12,000+, KS P (Medical) 5,000+, KS Q (Quality Mgmt) 4,000+, KS R (Transport) 12,000+, KS S (Service) 3,000+, KS T (Packaging) 4,000+, KS V (Shipbuilding) 5,000+, KS W (Aerospace) 3,000+ — totaling 220,000+ Korean Industrial Standards. Key Acts: Personal Information Protection Act (Act 19234, effective Sept 15, 2024), Electronic Government Act, Electronic Signature Act, Act on Promotion of Information and Communications Network Utilization and Information Protection, Information and Communications Infrastructure Protection Act, Data Industry Act, Public Data Act, AI Framework Act (Act 20212, effective July 2026), Industrial Technology Innovation Promotion Act, Framework Act on Science and Technology — 70+ Korean standardization-related laws.

Korea Digital Transformation Detailed Mapping

Korea operates digital transformation through a comprehensive governance system. Digital Government: Digital Platform Government Committee (established September 2022, under the President)·Ministry of the Interior and Safety Digital Government Bureau·e-Government Support Center·Gov.kr·National Citizen Service·KDIS (Korea Digital Information Society)·NIA (National Information Society Agency)·MOIS (Ministry of the Interior and Safety). K-DNS Infrastructure: Korea Internet & Security Agency (KISA) Korea Internet Center·KISA DNS Root Server·KRNIC (Korea Network Information Center)·BGP Korea·National Cyber Security Center (NCSC)·KCC (Korea Communications Commission)·MSIT (Ministry of Science and ICT)·NIA·NIPA. Korean Cloud Infrastructure: KT Cloud·NAVER Cloud (NCloud)·Samsung SDS Cloud·LG U+ Cloud·NHN Cloud·Kakao Enterprise Cloud·SK Telecom Cloud·KISA Cloud Security Assurance Program (CSAP)·KCMVP-validated cloud·ISMS-P (Information Security & Personal Information Management System). Korean Security Certifications: KISA ISMS-P certification·KCMVP (Korean Cryptographic Module Validation Program)·NIS (National Intelligence Service) "National Cryptographic Technology Operation Standards"·NCSC "National Cyber Security Strategy 2024-2028"·CC (Common Criteria) Korean evaluation bodies·EAL4·EAL5·KS X ISO/IEC 15408·19790·24759 Korean Profile. Korean Data Standards: NIA AI Hub·National Data Standardization Committee·Statistics Korea (KOSTAT)·MyData 4 Designated Combination Specialists (Samsung SDS, KICI, KOSTAT, KFTC)·National Institute of Korean Language·National Law Information Center·National Spatial Information Platform·National Spatial Data Center·Korean Spatial Information Standards. Finance and Fintech Standards: FSC (Financial Services Commission)·FSS (Financial Supervisory Service)·FIU (Financial Intelligence Unit)·BOK (Bank of Korea)·FSEC (Financial Security Institute)·KFTC (Korea Financial Telecommunications)·KSD (Korea Securities Depository)·KRX (Korea Exchange) 8-agency cooperation. 5G/6G Communications Infrastructure: 5G subscribers 35 million (2024)·5G base stations 350,000·6G commercialization target 2028·5G dedicated networks 16 operators·6G Acceleration Council (MSIT, 2024). K-Content: KOCCA (Korea Creative Content Agency)·MCST (Ministry of Culture, Sports and Tourism)·KCA (Korea Communications Agency)·Korea Culture Information Service Agency·Korean Film Archive·Korea Publishing Industry Promotion Agency. Data 3 Acts (Personal Information Protection Act·Credit Information Act·Telecommunications Network Act, 2020 enforcement)·Data Industry Act (2021)·Public Data Act (2013)·AI Framework Act (2026)·Digital Platform Government Framework Act (2024 proposed) — Korea digital transformation core legislation.