TorchLean API

NN.Data.IO.Npy

NPY Loader #

This module implements the small, explicit .npy subset that TorchLean's native training examples use:

The loader stays narrow. It is a runtime bridge for trusted experiment artifacts, not a general NumPy parser and not part of the formal tensor semantics. Tensor construction happens in NN.API.Data.Sources, which keeps this file-format boundary independent of model training.

Reference:

In-memory representation of a loaded .npy file in TorchLean's supported subset.

values is always flattened in C-order. If the source file declares fortran_order = True, we reorder the payload during parsing and store fortran := false in the returned value so downstream tensor loaders never have to reason about storage order.

  • dtype : String

    Dtype string as stored in the header, for example "<f4" or "<f8".

  • shape : Array Nat

    Logical array shape as stored in the header.

  • fortran : Bool

    Whether the returned flat payload is still Fortran-ordered. This loader returns false.

  • values : Array Float

    Flattened numeric payload, converted to Lean Float values.

Instances For

    Validated NPY metadata, without reading or allocating the numeric payload.

    Dimensions and the element count fit an addressable Array Float. Payload availability is checked separately by full or prefix readers; a valid header alone does not certify a complete file.

    • dtype : String

      Supported dtype descriptor, either "<f4" or "<f8".

    • shape : Array Nat

      Logical array shape from the header.

    • fortran : Bool

      Whether the on-disk payload is Fortran-ordered.

    • dataStart : Nat

      Byte offset where the numeric payload begins.

    • elementBytes : Nat

      Number of bytes occupied by one on-disk element.

    • elementCount : Nat

      Product of the validated dimensions; scalar arrays contain one element.

    Instances For

      Prefix products of a shape array.

      For a shape [d₀, d₁, d₂], this returns [1, d₀, d₀*d₁], which are exactly the Fortran-order strides. We use these strides to convert Fortran storage into TorchLean's ordinary C-order flattening convention.

      Instances For

        Convert a linear C-order index to the corresponding linear Fortran-order index.

        Both indices describe the same multi-dimensional coordinate. The difference is only how the coordinate is flattened into a one-dimensional payload.

        Instances For

          Reorder a Fortran-ordered flat array into C-order, rejecting an inconsistent payload.

          Instances For

            Safe ByteArray indexing.

            Instances For

              Read a little-endian UInt16 at byte offset i, returning none on out-of-bounds input.

              Instances For

                Read a little-endian UInt32 at byte offset i, returning none on out-of-bounds input.

                Instances For

                  Read a little-endian UInt64 at byte offset i, returning none on out-of-bounds input.

                  Instances For

                    Parse a shape tuple like (3, 4) or (3,) from a NumPy header fragment.

                    Instances For

                      Parse the NumPy header dictionary.

                      We only need three standard fields: descr, fortran_order, and shape. The header format is a Python-literal dictionary padded to an alignment boundary, so this parser stays field-oriented rather than trying to become a full Python parser.

                      Instances For

                        Bound header allocation before interpreting an untrusted version-2 header length.

                        Instances For

                          Validate the magic and version, returning the header offset and its bounded byte length.

                          Instances For

                            Byte width for the dtypes supported by TorchLean's NPY loader.

                            Instances For

                              Bound each dimension and the shape product before allocating a decoded Array Float.

                              Instances For

                                Parse and validate the metadata shared by in-memory and file-backed NPY readers.

                                Instances For

                                  Read one supported numeric element from an NPY payload.

                                  Instances For

                                    Validate a C-order leading prefix and retain only its requested shape and element count.

                                    Instances For

                                      Decode a validated full or prefix payload, checking its byte range before reserving values.

                                      Instances For

                                        Read exactly the requested bytes using bounded allocations, rejecting an early end of file.

                                        The buffer grows only with bytes actually read. An untrusted element count never becomes the capacity of a read buffer before the corresponding file contents have been observed.

                                        Instances For

                                          Read only the bounded NPY preamble and header, leaving the handle at the numeric payload.

                                          Instances For

                                            Read and decode the validated payload at the handle's current position.

                                            Instances For

                                              Parse the bytes of a .npy file into NpyData.

                                              The parser rejects unsupported dtypes, missing or malformed required header fields, and truncated payloads. Header parsing recognizes the three required fields; it is not a general Python parser. That makes loader failures explicit at the trust boundary instead of silently producing tensors with the wrong shape or partial data.

                                              Instances For

                                                Parse only the requested leading rows of a C-order .npy array.

                                                This supports large exported tensors kept on disk while a run uses only the first n rows. The rank and trailing dimensions must match exactly; only the leading axis may be larger than requested.

                                                The implementation shares header and dtype parsing with parseNpy, then decodes only the requested prefix. This avoids building a full Array Float when a command asks for a small leading slice of a real image or sequence dataset. Only the requested range must be present; unused trailing rows are not checked for truncation.

                                                Why C-order only? In row-major NPY files, the first n rows are physically contiguous, so the prefix is exactly the first n * trailingSize elements. In Fortran-order files the same logical prefix is interleaved across the payload, so prefix decoding would be unsound. Rather than silently returning bad rows, we reject Fortran-order prefix loading and ask callers to convert the array to C-order first.

                                                Instances For

                                                  Read validated metadata without reading the payload.

                                                  Supports version 1 and 2 headers up to one MiB. Dtype, storage-order syntax, dimensions, and shape products are validated; payload completeness belongs to readNpy or readNpyLeadingAxisPrefix.

                                                  Instances For

                                                    Read the full declared NPY payload, rejecting truncation and converting Fortran to C order.

                                                    Instances For

                                                      Read only the header and requested leading rows of a C-order .npy file.

                                                      Unrequested rows are neither read nor decoded. The requested range must be complete, and rank and trailing dimensions must match exactly. Reads use bounded chunks so a malformed claimed size cannot trigger a correspondingly large allocation before any bytes have been read.

                                                      Instances For