std1.0.0-beta-7 SafeC docs

std :: ml::tensor

Struct std::TensorArena

#include <std/ml/tensor.h> source

struct TensorArena {
    unsigned long first;
    unsigned long current;
    unsigned long chunkBytes;
    unsigned long tracked;
    unsigned long trackedCount;
    unsigned long trackedCap;
}

Every op allocates its output. In a training loop that is a malloc (and, for fresh memory, page faults) per op per step: on a small MLP that cost more than the arithmetic. A TensorArena is a chunked bump allocator for a step's temporaries: between a.begin() and tensor_arena_end() every tensor created on this thread (data, shape, gradient) comes from it, and a.reset() drops them all at once while keeping the memory -- already faulted in -- for the next step.

struct TensorArena a = TensorArena::new(0UL);
while (step < steps) {
    a.begin();
    ... forward, Tensor::backward, optimizer update ...
    tensor_arena_end();
    a.reset();     // this step's tensors are gone
}
a.free();

Tensors created outside the arena (parameters) stay on the heap, and so do their gradients, so they survive resets. Tensor::free() on an arena tensor does nothing. A tensor must not be used after its arena is reset.

Fields

§unsigned long firstsource

chunk list (internal)

§unsigned long currentsource
§unsigned long chunkBytessource
§unsigned long trackedsource

tensors to release on reset (internal)

§unsigned long trackedCountsource
§unsigned long trackedCapsource

Methods

§void begin()source · definition

this thread's new tensors go to this arena

§void reset()source · definition

drop every tensor in it, keep the memory

§void drop()source · definition
§void free() consumesource · definition

= drop()

§unsigned long used() constsource · definition

Bytes handed out since the last reset.

§static struct TensorArena new(unsigned long chunkBytes)source · definition

chunkBytes 0 = 16 MiB chunks (larger requests get their own chunk).