class ClTensor(T)
- ClTensor(T)
- Reference
- Object
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.crcl_tensor/creation.cr
cl_tensor/linalg.cr
Constructors
-
.new(buffer : LibCL::ClMem, shape : Array(Int))
Creates a
ClTensorfrom a provided buffer and shape. - .new(shape : Array(Int), value : T)
- .new(shape : Array(Int))
Class Method Summary
-
.full(shape : Array(Int), value : Number)
Creates a
ClTensorof a provided shape, filled with a value. - .full_like(other : ClTensor | Tensor, value : Number)
-
.ones(shape : Array(Int))
Creates a
ClTensorof a provided shape, filled with 1. - .ones_like(other : ClTensor | Tensor)
-
.zeros(shape : Array(Int))
Creates a
ClTensorof a provided shape, filled with 0. - .zeros_like(other : ClTensor | Tensor)
Instance Method Summary
-
#asum
ASUM sums the absolute values of the elements of a vector
-
#axpy(a : Number, c : ClTensor(T))
AXPY constant times a vector plus a vector.
-
#axpy!(a : Number, c : ClTensor(T))
AXPY constant times a vector plus a vector.
-
#copy
COPY copies a vector, x, to a vector, y.
- #cpu : Tensor(T)
-
#dot(c : ClTensor(T))
DOT forms the dot product of two vectors.
-
#gemm(b : ClTensor(T))
GEMM performs one of the matrix-matrix operations
-
#nrm2
NRM2 returns the euclidean norm of a vector via the function name, so that
-
#scal(a : Number)
SCAL scales a vector by a constant.
-
#scal!(a : Number)
SCAL scales a vector by a constant.
- #shape : Array(Int32)
- #size : Int32
Constructor Detail
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])
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)
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])
Class Method Detail
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)
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)
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])
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)
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])
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)
Instance Method Detail
ASUM sums the absolute values of the elements of a vector
Arguments
Examples
a = ClTensor(Flaot32).new([3, 3, 2])
a.asum
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)
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)
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]
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]
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)
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]]
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
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)
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)