GPUTensor<T> class
A GPUTensor in this library is used as the fundamental unit to hold structured collections of float values on the GPU. For CPU computation please refer to Tensor.
These values are stored on the GPU (usually inside the VRAM) with a unique identifier id.
Globally a counter called idCounter ensures uniqueness of Tensors up to the integer limit of instances.
Each GPUTensor is boath holding gradient value and its actual data value. Initialisation is to be done via its constructor.
IMPORTANT: The datatype T is not predefined as a standard value. To ensure compatibility with the framework the use of Scalar,Vector,Matrix and Tensor3D is recommended.
GPUTensors are NOT managed automatically. Allocating is done on construction. To free the tensor again use the free method. Accessing GPUTensor values can be done via its local Buffers data and grad after toCpu was called to update the buffers. Forgetting to update will result in outdated or empty buffers.
Constructors
- GPUTensor(T initialValue, {GPUNode? creator, String? id, bool? requiresGrad})
-
Creates a GPUTensor object storing and managing the value
initialValuegiven. Supported types for GPUTensors are: Scalar,Vector,Matrix and Tensor3D. Short versions of those types are Sc,Vec,Mat and T3D. GPUTensors can be created the following ways: -
GPUTensor.empty(List<
int> initialShape, {GPUNode? creator, bool? requiresGrad}) -
Create a GPUTensor with the given shape and fills the GPUTensor with 0.0.
Additionally the
creatorGPUNode can be set. - GPUTensor.matrix(Matrix initialValue, {GPUNode? creator, String id = "EMPTY", bool? requiresGrad})
-
GPUTensor.randomUniform(List<
int> initialShape, double scale, {int? seed, GPUNode? creator, bool? requiresGrad}) -
Create a GPUTensor with the given shape and fills the GPUTensor with random initialised values in range of provided scale parameter.
Additionally the
creatorGPUNode can be set as well as a seed for randomization. - GPUTensor.scalar(Scalar? initialValue, {GPUNode? creator, String id = "EMPTY", bool? requiresGrad})
- GPUTensor.tensor3D(Tensor3D initialValue, {GPUNode? creator, String id = "EMPTY", bool? requiresGrad})
- GPUTensor.vector(Vector initialValue, {GPUNode? creator, String id = "EMPTY", bool? requiresGrad})
Properties
- creator ↔ GPUNode?
-
GPUNode creator stores the GPUNode that is responsible for the creation of this GPUTensor. This allows for backward gradient propagation as well as tracing the origin path of this GPUTensor.
getter/setter pair
-
data
↔ List<
double> -
Unoptimised List of the retrieved last value of the GPUTensor.
getter/setter pair
-
grad
↔ List<
double> -
Unoptimised List of the retrieved last gradient of the GPUTensor.
getter/setter pair
- gradId → String
-
no setter
- gradValue → T
-
Converts grad via shape and returns the value as
T.no setter - hashCode → int
-
The hash code for this object.
no setterinherited
- id ↔ String
-
Global identifier to find and query tensors. id is set to "t_gpu_
_idCounterby default. It is recommended NOT to set this value manually outside of creation. Uniqueness is to be handled manually when setting the name.getter/setter pair - localGrad ↔ bool
-
getter/setter pair
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
shape
↔ List<
int> -
Stores as which shape the GPUTensors value should be interpreted as. 0-3 dimensions are supported currently (2026-09).
getter/setter pair
-
subMap
↔ Map<
String, GPUTensor> -
Map to capture the sub GPUTensors that may have been allocate during creation.
getter/setter pair
- value → T
-
Converts data via shape and returns the value as
T.no setter
Methods
-
backward(
CommandBuffer backwardTape, {bool fillOnes = true}) → void -
Staring point of gradient calculation. The network of GPUNodes and GPUTensors traverses in topological oder over all GPUTensors writing their given commands to calculate the gradients to
backwardsTape. To actually calculate the values this tape is required to be executed via GPUEngine.run(backwardTape). IffillOnesis set to true, it pushes a command to fill the gradient of this tensor with 1.0. -
free(
) → void - Frees the allocated GPU Memory for both value and gradient.
-
getAllTensorsInGraph(
GPUTensor root) → List< GPUTensor> - Traverses the graph and returns a list of all GPUTensors this root GPUTensor is build from.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
printGraph(
) → void -
Allows the exact printing of the current compute graph for this GPUTensor. Topological Depth is indicated via indents.
Colors indicate tensor types. Green is indication of an input Tensor, blue an intermediate Tensor and Yellow the resulting output of the Network.
Tensor name and shape are listed as
t_tensorid [1,4,5,...]Example for a simple neuronal network: -
pushData(
List< double> values) → void - Manually pushes values to overwrite the GPUTensors values in VRAM.
-
toCpu(
) → void - Retrieves the current state of the GPUTensor from VRAM and writes it to the data and grad buffers.
-
toString(
) → String -
A string representation of this object.
inherited
-
zeroGrad(
CommandBuffer tape) → void - Writes the command to set the gradient of this GPUTensor to 0.
-
zeroGraphGrads(
CommandBuffer tape) → void - Finds every GPUTensor the current GPUTensor depends on and sets their gradients to 0.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- globalGrad ↔ bool
-
Global default for saying if allocated GPUTensors should hold gradient by default.
getter/setter pair