class ClTensor(T)

Overview

A ClTensor is a multidimensional container of fixed size, containing elements of type T.

The number of dimensions is specified by a ClTensor's #shape, which is an Array of integers specifying the size of a Tensor in each dimension.

A ClTensor can be created from a wide variety of creation methods. Including from a scalar value and #shape.

ClTensor's store data using a LibCL::ClMem

ClTensor's cannot be resized, and any operation the changes the total number of elements in a ClTensor will return a new object.

Defined in:

cl_tensor/cl_tensor.cr
cl_tensor/creation.cr
cl_tensor/linalg.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(buffer : LibCL::ClMem, shape : Array(Int)) #

Creates a ClTensor from a provided buffer and shape. This should primarily be used by internal methods that need to create ClTensors or pass to lower level libraries

The generic type of the ClTensor must be specified, as LibCL::ClMem does not store this information

Arguments

buffer : LibCL::ClMem Memory buffer for a ClTensor shape : Array(Int) Size of the ClTensor in each dimension

Examples

t = ClTensor(Float32).new(buffer, [3, 3, 2])

[View source]
def self.new(shape : Array(Int), value : T) #

Creates a ClTensor from a provided shape, initializing the ClTensor's buffer with a value specified by the user

The generic type of the ClTensor is inferred from the provided value

Arguments

shape : Array(Int) Size of ClTensor in each dimension value : T Value to fill the ClTensor

Examples

t = ClTensor(Float32).new([3, 3, 2], 3.5)

[View source]
def self.new(shape : Array(Int)) #

Creates a ClTensor from a provided shape, initializing the ClTensor's buffer without filling it with data

When creating a ClTensor from a shape alone, the generic type must be specified

Arguments

shape : Array(Int) Size of ClTensor in each dimension

Examples

t = ClTensor(Float32).new([3, 3, 2])

[View source]

Class Method Detail

def self.full(shape : Array(Int), value : Number) #

Creates a ClTensor of a provided shape, filled with a value. The generic type must be specified.

Arguments

shape Shape of returned ClTensor

Examples

t = ClTensor(Float32).full([3], 3.5)

[View source]
def self.full_like(other : ClTensor | Tensor, value : Number) #

Creates a ClTensor filled with a value, sharing the shape of another provided Tensor or ClTensor

Arguments

t Item to use for output shape

Examples

t = Tensor.new([3]) &.to_f
u = ClTensor(Float32).full_like(t, 3.5)

[View source]
def self.ones(shape : Array(Int)) #

Creates a ClTensor of a provided shape, filled with 1. The generic type must be specified.

Arguments

shape Shape of returned ClTensor

Examples

t = ClTensor(Float32).ones([3])

[View source]
def self.ones_like(other : ClTensor | Tensor) #

Creates a ClTensor filled with 1, sharing the shape of another provided Tensor or ClTensor

Arguments

t Item to use for output shape

Examples

t = Tensor.new([3]) &.to_f
u = ClTensor(Float32).ones_like(t)

[View source]
def self.zeros(shape : Array(Int)) #

Creates a ClTensor of a provided shape, filled with 0. The generic type must be specified.

Arguments

shape Shape of returned ClTensor

Examples

t = ClTensor(Float32).zeros([3])

[View source]
def self.zeros_like(other : ClTensor | Tensor) #

Creates a ClTensor filled with 0, sharing the shape of another provided Tensor or ClTensor

Arguments

t Item to use for output shape

Examples

t = Tensor.new([3]) &.to_f
u = ClTensor(Float32).zeros_like(t)

[View source]

Instance Method Detail

def asum #

ASUM sums the absolute values of the elements of a vector

Arguments

Examples

a = ClTensor(Flaot32).new([3, 3, 2])
a.asum

[View source]
def axpy(a : Number, c : ClTensor(T)) #

AXPY constant times a vector plus a vector. uses unrolled loops for increments equal to one.

Arguments

a : Number Scalar alpha c : ClTensor Argument to add

Examples

a = ClTensor(Float32).ones([3])
b = ClTensor(Float32).ones([3])
c = 1.45
a.axpy!(c, b)

[View source]
def axpy!(a : Number, c : ClTensor(T)) #

AXPY constant times a vector plus a vector. uses unrolled loops for increments equal to one.

Arguments

a : Number Scalar alpha c : ClTensor Argument to add

Examples

a = ClTensor(Float32).ones([3])
b = ClTensor(Float32).ones([3])
c = 1.45
a.axpy!(c, b)

[View source]
def copy #

COPY copies a vector, x, to a vector, y. uses unrolled loops for increments equal to 1.

Arguments

Examples

a = ClTensor(Float32).ones([3, 3, 2])
b = a.copy
b.shape # => [3, 3, 2]

[View source]
def cpu : Tensor(T) #

Move a ClTensor from an OpenCL buffer to a CPU Tensor backed by a Pointer. This operation is slow, and should not be used frequently. Operations are most efficient when done exclusively on a single architecture

This method always blocks on write

Arguments

Examples

t = ClTensor(Float32).new([5], 2.2)
t.cpu # => [2.2, 2.2, 2.2, 2.2, 2.2]

[View source]
def dot(c : ClTensor(T)) #

DOT forms the dot product of two vectors. uses unrolled loops for increments equal to one.

This method treats all inputs as though they were one-dimensional

Arguments

c : ClTensor Argument to dot against self

Examples

a = ClTensor(Float32).ones([3])
b = ClTensor(Float32).ones([3])
c = a.dot(b)

[View source]
def gemm(b : ClTensor(T)) #

GEMM performs one of the matrix-matrix operations

C := alpha*op( A )op( B ) + betaC,

where op( X ) is one of

op( X ) = X or op( X ) = X**T,

alpha and beta are scalars, and A, B and C are matrices, with op( A ) an m by k matrix, op( B ) a k by n matrix and C an m by n matrix.

Arguments

*b* : ClTensor
  Second operand for the matrix multiplication

Examples

a = ClTensor(Float32).ones([3, 3])
b = a.copy
puts a.gemm(b).cpu

# [[3, 3, 3],
#  [3, 3, 3],
#  [3, 3, 3]]

[View source]
def nrm2 #

NRM2 returns the euclidean norm of a vector via the function name, so that

DNRM2 := sqrt( x'*x )

Treats ClTensor as though they were one-dimensional

Arguments

Examples

a = ClTensor(Float32).new([3])
a.nrm2

[View source]
def scal(a : Number) #

SCAL scales a vector by a constant. Uses unrolled loops for increment equal to 1.

Arguments

a : Number Scalar alpha

Examples

a = ClTensor(Float32).ones([3])
a.scal!(3)

[View source]
def scal!(a : Number) #

SCAL scales a vector by a constant. Uses unrolled loops for increment equal to 1.

Arguments

a : Number Scalar alpha

Examples

a = ClTensor(Float32).ones([3])
a.scal!(3)

[View source]
def shape : Array(Int32) #

[View source]
def size : Int32 #

[View source]