Skip to content

storage #

vtl.storage

Storage backends used by VTL tensors. CPU storage is the default general-purpose path; optional CUDA, Vulkan, and VCL implementations are selected through V compile-time defines and are not interchangeable in availability or maturity.

Most applications should use vtl.Tensor[T] and its creation/manipulation functions instead of constructing storage directly. The backend modules expose lower-level allocation and access primitives for library and backend work.

CPU storage constructors from arrays copy their input; clone creates an independent buffer, while offset refers to a range in the existing storage. At tensor level, view, slice, and stride-only transforms can share backing storage when no layout conversion is needed; copy explicitly allocates independent data. Treat mutations through a view as mutations of shared data, and check whether an operation materializes a new layout before relying on zero-copy behavior. This is a CPU tensor contract, not a promise that each accelerator backend implements identical aliasing.

Borrowed CPU buffers belong here too: borrow and contiguous build validated BufferView[T] values, to_tensor copies their logical elements, and as_borrowed_tensor performs a guarded zero-copy wrap. Root-level vtl.from_borrowed and vtl.BufferView expose the same contract without a separate top-level module.

CPU is the default backend. CUDA and VCL expose backend-specific storage behind compile flags; Vulkan has an implemented conditional backend and a no-backend fallback. These accelerator paths have different allocation, synchronization, transfer, and lifetime constraints and remain experimental. See device memory notes before using them.

Constants #

const vector_minimum_capacity = 2

vector_minimum_capacity is a public constant used by this module.

const vector_growth_factor = 2

vector_growth_factor is a public constant used by this module.

const vector_shrink_threshold = 1.0 / 4.0

vector_shrink_threshold is a public constant used by this module.

fn borrow #

fn borrow[T](data []T, shape []int, strides []int, offset int, mutable bool) !BufferView[T]

borrow validates a strided view over data. Strides must be positive and non-overlapping so that every logical element maps to exactly one physical slot; overlapping layouts are rejected rather than silently aliased.

fn contiguous #

fn contiguous[T](data []T, shape []int) !BufferView[T]

contiguous describes a row-major contiguous view over the whole buffer.

fn element_count #

fn element_count(shape []int) !int

element_count returns the number of logical elements in a shape, rejecting shapes whose product is not representable as an int.

fn from_array #

fn from_array[T](arr []T) &CpuStorage[T]

from_array exposes this operation as part of the public API.

fn from_borrowed #

fn from_borrowed[T](arr []T) &CpuStorage[T]

from_borrowed wraps an existing array without copying it. The caller must keep the backing array alive and must not mutate it while the storage is in use; use from_array when an owned copy is required.

fn storage #

fn storage[T](len int, cap int, init T) &CpuStorage[T]

storage exposes this operation as part of the public API.

fn (BufferView[T]) shape #

fn (v &BufferView[T]) shape() []int

shape returns a copy of the view shape.

fn (BufferView[T]) strides #

fn (v &BufferView[T]) strides() []int

strides returns a copy of the view strides.

fn (BufferView[T]) offset #

fn (v &BufferView[T]) offset() int

offset returns the buffer offset of the logical origin.

fn (BufferView[T]) is_mutable #

fn (v &BufferView[T]) is_mutable() bool

is_mutable reports whether the viewed buffer may be mutated.

fn (BufferView[T]) metadata #

fn (v &BufferView[T]) metadata() BufferMetadata

metadata returns the ABI-friendly description of the view.

fn (BufferView[T]) is_zero_copy_eligible #

fn (v &BufferView[T]) is_zero_copy_eligible() bool

is_zero_copy_eligible reports whether the view can be wrapped without a copy: it must cover the entire buffer, start at offset zero, and be row-major contiguous.

fn (BufferView[T]) values #

fn (v &BufferView[T]) values() []T

values gathers logical elements in row-major order. This is the copy fallback used by to_tensor and is always safe for strided views.

fn (BufferView[T]) to_tensor #

fn (v &BufferView[T]) to_tensor() !&vtl.Tensor[T]

to_tensor copies the logical elements into an owned tensor.

fn (BufferView[T]) as_borrowed_tensor #

fn (v &BufferView[T]) as_borrowed_tensor() !&vtl.Tensor[T]

as_borrowed_tensor wraps eligible buffers without copying. The caller must keep the backing array alive and refrain from mutating it for the tensor's lifetime; call to_tensor otherwise. Read-only views are never eligible: wrapping one would hand out a mutable Tensor over a buffer declared read-only.

fn (CpuStorage[T]) get #

@[inline]
fn (s &CpuStorage[T]) get[T](i int) T

get exposes this operation as part of the public API.

fn (CpuStorage[T]) set #

@[inline]
fn (mut s CpuStorage[T]) set[T](i int, val T)

set exposes this operation as part of the public API.

fn (CpuStorage[T]) fill #

fn (mut s CpuStorage[T]) fill[T](val T)

fill fills an entire storage with a given value

fn (CpuStorage[T]) clone #

@[inline]
fn (s &CpuStorage[T]) clone[T]() &CpuStorage[T]

clone exposes this operation as part of the public API.

fn (CpuStorage[T]) like #

@[inline]
fn (s &CpuStorage[T]) like[T]() &CpuStorage[T]

like exposes this operation as part of the public API.

fn (CpuStorage[T]) like_with_len #

@[inline]
fn (s &CpuStorage[T]) like_with_len[T](len int) &CpuStorage[T]

like_with_len exposes this operation as part of the public API.

fn (CpuStorage[T]) offset #

fn (s &CpuStorage[T]) offset[T](start int) &CpuStorage[T]

offset exposes this operation as part of the public API.

fn (CpuStorage[T]) to_array #

@[inline]
fn (s &CpuStorage[T]) to_array[T]() []T

to_array exposes this operation as part of the public API.

fn (CpuStorage[T]) cuda #

fn (cpu &CpuStorage[T]) cuda(params CudaParams) !&CpuStorage[T]

cuda returns an error when CUDA is not enabled at compile time

fn (CpuStorage[T]) vulkan #

fn (cpu &CpuStorage[T]) vulkan(params VulkanParams) !&CpuStorage[T]

vulkan exposes this operation as part of the public API.

struct BufferMetadata #

struct BufferMetadata {
pub:
	shape        []int
	strides      []int
	offset       int
	element_size int
	mutable      bool
}

BufferMetadata is the stable, ABI-friendly description of a buffer view.

struct BufferView #

struct BufferView[T] {
	data    []T
	shape   []int
	strides []int
	offset  int
	mutable bool
}

BufferView describes a (possibly strided) view over a caller-owned CPU buffer. The view never clones the buffer; conversion to a tensor is either an explicit copy (to_tensor) or a guarded zero-copy wrap (as_borrowed_tensor).

The representation is private so the validation performed by borrow cannot be bypassed with a struct literal: every live view has matching shape/strides ranks, non-negative shapes, positive strides, and non-overlapping in-bounds indices. Inspect it through the read-only accessors below or metadata.

struct CpuStorage #

@[heap]
struct CpuStorage[T] {
pub mut:
	data []T
}

CpuStorage defines a public data structure for this module.

struct CudaParams #

@[params]
struct CudaParams {
	device_id int
	stream    voidptr = unsafe { nil }
}

CudaParams defines a public data structure for this module.

struct VulkanParams #

@[params]
struct VulkanParams {}

VulkanParams defines a public data structure for this module.