Tensor API
GPU tensor with shape and layout metadata.
Constructor
constructor(context: GPUContext, shape: TensorShape, options?: TensorOptions)Create a new tensor with uninitialized data.
Parameters:
context: GPU contextshape: Tensor dimensionsoptions: Configuration options
interface TensorOptions {
layout?: DataLayout; // Default: 'NCHW'
label?: string;
}2
3
4
Static Methods
zeros
static zeros(context: GPUContext, shape: TensorShape, options?: TensorOptions): TensorCreate a tensor filled with zeros.
Example:
const tensor = Tensor.zeros(context, [1, 3, 224, 224]);ones
static ones(context: GPUContext, shape: TensorShape, options?: TensorOptions): TensorCreate a tensor filled with ones.
filled
static filled(context: GPUContext, shape: TensorShape, value: number, options?: TensorOptions): TensorCreate a tensor filled with a specific value.
fromArray
static fromArray(
context: GPUContext,
data: number[] | Float32Array,
shape: TensorShape,
options?: TensorOptions
): Tensor2
3
4
5
6
Create a tensor from an array of numbers.
Example:
const data = [1, 2, 3, 4, 5, 6];
const tensor = Tensor.fromArray(context, data, [2, 3]);2
Methods
upload
async upload(data: Float32Array): Promise<void>Upload data to the GPU buffer.
Parameters:
data: Data to upload (must match tensor size)
Throws:
BufferSizeError: If data length doesn't match tensor size
download
async download(): Promise<Float32Array>Download data from the GPU buffer.
Returns: Float32Array containing tensor data
Example:
const tensor = Tensor.zeros(context, [2, 3]);
await tensor.upload(new Float32Array([1, 2, 3, 4, 5, 6]));
const data = await tensor.download();
console.log(data); // Float32Array [1, 2, 3, 4, 5, 6]2
3
4
reshape
reshape(newShape: TensorShape): TensorCreate a view with a new shape. Zero-copy operation.
Parameters:
newShape: New dimensions (total elements must match)
Returns: New tensor view sharing the same GPU buffer
Example:
const tensor = Tensor.zeros(context, [1, 3, 224, 224]);
const view = tensor.reshape([1, 150528]); // Zero-copy2
convertLayout
async convertLayout(target: DataLayout): Promise<Tensor>Convert memory layout (NCHW ↔ NHWC).
Parameters:
target: Target layout
Returns: New tensor with converted layout
Example:
const nchw = Tensor.zeros(context, [1, 3, 224, 224], { layout: 'NCHW' });
const nhwc = await nchw.convertLayout('NHWC');
console.log(nhwc.shape); // [1, 224, 224, 3]2
3
destroy
destroy(): voidRelease GPU buffer. No-op for view tensors.
Note: Only the original tensor that owns the buffer frees GPU memory.
usesContext
usesContext(context: GPUContext): booleanCheck if this tensor was created with the given context.
Properties
shape
readonly shape: TensorShapeTensor dimensions.
layout
readonly layout: DataLayoutMemory layout (NCHW or NHWC).
buffer
readonly buffer: GPUBufferUnderlying GPU buffer.
size
readonly size: numberTotal number of elements.
ownsBuffer
readonly ownsBuffer: booleanWhether this tensor owns the GPU buffer (false for views).
Memory Management
View Pattern
// Original tensor owns the buffer
const original = new Tensor(context, [1, 3, 224, 224]);
original.ownsBuffer; // true
// View shares the buffer (zero overhead)
const view = original.reshape([1, 3 * 224 * 224]);
view.ownsBuffer; // false
// Cleanup
view.destroy(); // No-op: doesn't own buffer
original.destroy(); // Actually destroys GPU buffer2
3
4
5
6
7
8
9
10
11
Data Roundtrip
const data = new Float32Array([1, 2, 3, 4, 5, 6]);
const tensor = Tensor.fromArray(context, data, [2, 3]);
const result = await tensor.download();
// result equals data2
3
4