Tensor
Tensor
[struct] Tensor
A struct tensor
is a multi-dimensional, and strided matrix (and nrank=0 to scalar value) containing elements of the single dtype.
Also the tensor has following slots:
- shape[list] A list of number, symbol, or
Tensor
. - buffer[Buffer] A buffer of the tensor. Realized arrays are stored here.
- dtype[keyword] A dtype of the tensor.
- order[order] A memory layout of the tensor, selected from either of :row or :column.
- id[symbol] A unique identifier of the tensor. (usually created by gensym)
- op[Func]
Func
object that represents the operation of the tensor. - views[list] A list of
ViewRange
objects, determining the bound of loops. - requires-grad[boolean] A flag to determine whether the tensor requires a gradient.
- grad[Tensor] A gradient and realized tensor of the tensor.
- grad-id[symbol] A unique identifier of the gradient tensor.
- variables[list] A list of
Tensor
objects that are used in the operation of the tensor. Tensors listed here are involved in the compilation.
[function] make-tensor
(make-tensor shape &key (dtype *default-float*) (order *default-order*) (id (gensym "TID")) (requires-grad nil) (initial-element nil) (views nil) (from nil))
- shape[list] A list of number, symbol, or
Tensor
. - dtype[keyword] A dtype of the tensor.
- order[order] A memory layout of the tensor, selected from either of :row or :column.
- id[symbol] A unique identifier of the tensor. (usually created by gensym)
- requires-grad[boolean] A flag to determine whether the tensor requires a gradient.
- initial-element[null|number|symbol|ScalarTensor] An initial value of the tensor.
- views[list] A list of
ViewRange
objects, determining the bound of loops. - from[null|Buffer|Symbol] A buffer used to initialize the tensor. If symbol is given, the buffer is taken from the variable table.
[function] make-scalar
(make-scalar value &key (dtype *default-float*) (order *default-order*) (id (gensym "SID")) (requires-grad nil))
- value[number|symbol] A value of the scalar tensor.
- dtype[keyword] A dtype of the tensor.
- order[order] A memory layout of the tensor, selected from either of :row or :column.
- id[symbol] A unique identifier of the tensor. (usually created by gensym)
- requires-grad[boolean] A flag to determine whether the tensor requires a gradient.
Tips: You can use fconst
, uconst
, and iconst
to create a scalar tensor with a default dtype. (arguments are the same as make-scalar
.)
[function] grad
Returns a gradient of the tensor[function] shape
Returns a copy of the shape of the tensor[function] ndim
Returns a rank of the tensor[function] dtype-of
Returns a dtype of the tensor[function] order
Returns a memory-layout of the tensor.[function] caten
(caten tensors &key (jit (= 1 (ctx:getenv :JIT))) (name :main) (simplifiers *external-simplifiers*))
Compiles the given tensors, returning an AVM struct.
- tensor[Tensor|List] toplevel tensors.
- jit[boolean] If set to 0, caten only applies the graph-level compilation. If set to 1, caten calls
%jit
to generate the fast kernel supported bycaten/ajit
. This parameter should be specified using the(ctx:with-contextvar)
macro. - name[keyword] the name of compiled avm.
- simplifiers[list] a list of external simplifiers used in the graph-level compilation. (defined by defsimplifier) Pass the function name.
[method] forward
Compute the forward pass of the compiled computational graph (AVM). The params are additional parameters that are used to initialize the AVM variable table, passed in the following format:
(symbol . number)
Loaded as a*default-int*
scalar tensor, used to determine the shape of dynamic shaped tensors.(symbol . buffer)
or(symbol . tensor)
Used to assign the initial elements of the tensor specified by the name inform
in(make-tensor ... :from x)
Here's an example.
Examples
(let ((model (caten (!randn `(a b)))))
(forward model `(a . 10) `(b . 10)))
Result
{Tensor[float32] :shape (A B) :id STC172222
((0.8699693 -0.44608942 -1.8202708 -0.9519131 0.8862769 0.35413826 0.12511837 -0.2703172 0.91452694 0.87142044)
(-0.66261375 0.673902 0.4323759 1.5011042 0.0020170507 1.6046505 0.271811 0.44790822 1.4448129 0.5116637)
(-0.64295256 0.17321691 0.51136935 0.062459193 -1.108816 -0.74708456 -0.109215215 -0.23178817 -1.234643 1.4630153)
(1.5821607 -0.29032183 0.88036096 -0.37020132 -0.38598806 -0.68073285 -0.13286385 0.8254134 -2.2787051 -0.0849715)
(-0.4255275 -0.011317276 0.6451945 -0.19079696 -0.8422523 1.5988636 -1.2472394 -0.50586087 0.5730076 0.52169573)
(-0.54117465 -0.45347175 -1.7619958 0.16331524 0.43195075 0.22981542 -0.1878958 -1.5063673 -0.373785 0.53300387)
(0.48313424 0.28539532 1.2640736 0.7678193 0.89247286 -1.9459732 -0.09509196 -0.17241766 -0.22554936 0.32811493)
(1.0621549 -0.24774244 0.13375968 -0.54587674 0.5180124 -1.2464281 -1.3902633 -0.36368564 -1.570286 0.21062613)
(-0.66637444 0.19291694 0.18343669 -0.2486207 1.1883638 1.700066 -1.1439961 0.6069278 -0.7946846 0.27585047)
(0.1533004 0.5702872 -0.7072851 -0.09736286 0.958433 2.0140476 -0.32732645 -1.4507971 0.4102621 0.5866135))
:op #<PROCEEDNODE {10018455F3}>
:requires-grad NIL
:variables (STC172221)}
[method] backward
Compute the backward pass of the compiled computational graph (AVM). Note that the prev-dout
is ignored. Forward pass must be computed first. Gradients are automatically reset to zero before the forward pass.
[function] proceed
Compiles the given tensors, returning an evaluated tensors.
[macro] with-no-grad
Under the scope of with-no-grad
, the gradient computation is disabled. This parameter should be set during inference, and the compiler will generate a more efficient code.
Examples
Tensor Creation
Result
Realize
Result
{Tensor[float32] :shape (30 30) :id STC172306
((0.0 0.0 0.0 0.0 0.0 ~ 0.0 0.0 0.0 0.0 0.0)
(0.0 0.0 0.0 0.0 0.0 ~ 0.0 0.0 0.0 0.0 0.0)
(0.0 0.0 0.0 0.0 0.0 ~ 0.0 0.0 0.0 0.0 0.0)
(0.0 0.0 0.0 0.0 0.0 ~ 0.0 0.0 0.0 0.0 0.0)
(0.0 0.0 0.0 0.0 0.0 ~ 0.0 0.0 0.0 0.0 0.0)
...
(0.0 0.0 0.0 0.0 0.0 ~ 0.0 0.0 0.0 0.0 0.0)
(0.0 0.0 0.0 0.0 0.0 ~ 0.0 0.0 0.0 0.0 0.0)
(0.0 0.0 0.0 0.0 0.0 ~ 0.0 0.0 0.0 0.0 0.0)
(0.0 0.0 0.0 0.0 0.0 ~ 0.0 0.0 0.0 0.0 0.0)
(0.0 0.0 0.0 0.0 0.0 ~ 0.0 0.0 0.0 0.0 0.0))
:op #<PROCEEDNODE {100268B813}>
:requires-grad NIL
:variables (STC172305)}
Creating a computational graph (Lazy)
Result
Evaluating a computational graph.
Result
Floating Features
[function] inf
Returns a positive infinity of the dtype for the current Common Lisp implementation.This feature is supported by float-features
Result
{Tensor[float32] :shape (3 3) :id STC172599
((#.SB-EXT:SINGLE-FLOAT-POSITIVE-INFINITY #.SB-EXT:SINGLE-FLOAT-POSITIVE-INFINITY #.SB-EXT:SINGLE-FLOAT-POSITIVE-INFINITY)
(#.SB-EXT:SINGLE-FLOAT-POSITIVE-INFINITY #.SB-EXT:SINGLE-FLOAT-POSITIVE-INFINITY #.SB-EXT:SINGLE-FLOAT-POSITIVE-INFINITY)
(#.SB-EXT:SINGLE-FLOAT-POSITIVE-INFINITY #.SB-EXT:SINGLE-FLOAT-POSITIVE-INFINITY #.SB-EXT:SINGLE-FLOAT-POSITIVE-INFINITY))
:op #<PROCEEDNODE {1002EAD473}>
:requires-grad NIL
:variables (STC172598)}
[function] -inf
Returns a negative infinity of the dtype for the current Common Lisp implementation.
This feature is supported by float-features
Result
{Tensor[float32] :shape (3 3) :id STC172665
((#.SB-EXT:SINGLE-FLOAT-NEGATIVE-INFINITY #.SB-EXT:SINGLE-FLOAT-NEGATIVE-INFINITY #.SB-EXT:SINGLE-FLOAT-NEGATIVE-INFINITY)
(#.SB-EXT:SINGLE-FLOAT-NEGATIVE-INFINITY #.SB-EXT:SINGLE-FLOAT-NEGATIVE-INFINITY #.SB-EXT:SINGLE-FLOAT-NEGATIVE-INFINITY)
(#.SB-EXT:SINGLE-FLOAT-NEGATIVE-INFINITY #.SB-EXT:SINGLE-FLOAT-NEGATIVE-INFINITY #.SB-EXT:SINGLE-FLOAT-NEGATIVE-INFINITY))
:op #<PROCEEDNODE {1002ED20D3}>
:requires-grad NIL
:variables (STC172664)}
[function] nan
Returns a NaN of the dtype for the current Common Lisp implementation.
This feature is supported by float-features
Result
{Tensor[float32] :shape (3 3) :id STC172731
((#<SINGLE-FLOAT quiet NaN> #<SINGLE-FLOAT quiet NaN> #<SINGLE-FLOAT quiet NaN>)
(#<SINGLE-FLOAT quiet NaN> #<SINGLE-FLOAT quiet NaN> #<SINGLE-FLOAT quiet NaN>)
(#<SINGLE-FLOAT quiet NaN> #<SINGLE-FLOAT quiet NaN> #<SINGLE-FLOAT quiet NaN>))
:op #<PROCEEDNODE {1002ED6D23}>
:requires-grad NIL
:variables (STC172730)}
[function] float-type-of
Returns :INF
if the number is a negative infinity, :-INF
if the number is a negative infinity, :nan
if the number is NaN, or T otherwise.