std :: ml::tensor
Struct std::TensorArena
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
chunk list (internal)
tensors to release on reset (internal)
Methods
this thread's new tensors go to this arena
drop every tensor in it, keep the memory
= drop()
Bytes handed out since the last reset.
chunkBytes 0 = 16 MiB chunks (larger requests get their own chunk).