package tensor // Memory is a representation of memory of the value. // // The main reason for requiring both Uintptr() and Pointer() methods is because while Go currently does not have a compacting // garbage collector, from the docs of `unsafe`: // Even if a uintptr holds the address of some object, the garbage collector, will not update that uintptr's value if the object moves, // nor will that uintptr keep the object from being reclaimed. type Memory interface { Uintptr() uintptr MemSize() uintptr } // Engine is a representation of an execution engine. // While different execution engines can have different capabilities, all execution engines must be able to allocate and free memory type Engine interface { AllocAccessible() bool // AllocAccessible returns true if the engine return Go-accessible memory pointers? Alloc(size int64) (Memory, error) // Alloc allocates memory Free(mem Memory, size int64) error // Free rees memory Memset(mem Memory, val interface{}) error // Memset - duh Memclr(mem Memory) // Memclr - duh Memcpy(dst, src Memory) error // Memcpy - duh Accessible(mem Memory) (Memory, error) // Accessible returns Go-accesible memory pointers, or errors, if it cannot be done WorksWith(order DataOrder) bool // WorksWith returns true if the data order can be directly worked with } type standardEngine interface { Engine Adder Suber Muler Diver Power Moder FMAer MatMuler MatVecMuler OuterProder Dotter SVDer Lter Lteer Gter Gteer ElEqer MinBetweener MaxBetweener // Anything that returns interface{} cannot be added here because they will likely have additional // optimized versions of the functions for types. // For example: Tracer and InnerProder both have optimized interfaces for Float32 and Float64 which returns those types specifically. } type arrayMaker interface { makeArray(arr *array, t Dtype, size int) } // NonStdEngine are any engines that do not allocate using the default built in allocator type NonStdEngine interface { NonStdAlloc() // noop } /* Data Agnostic Execution Engine Methods */ // Transposer is any engine that can perform an unsafe transpose of a tensor. type Transposer interface { Transpose(t Tensor, expStrides []int) error } // Concater is any enegine that can concatenate multiple Tensors together type Concater interface { Concat(t Tensor, axis int, others ...Tensor) (Tensor, error) } // Stacker is any engine that can stack multiple Tenosrs along an axis type Stacker interface { Stack(t Tensor, axis int, others ...Tensor) (Tensor, error) } // DenseStacker is any engine that can stack DenseTensors along an axis. This is a specialization of Stacker. type DenseStacker interface { StackDense(t DenseTensor, axis int, others ...DenseTensor) (retVal DenseTensor, err error) } // Repeater is any engine that can repeat values along the given axis. type Repeater interface { Repeat(t Tensor, axis int, repeats ...int) (Tensor, error) RepeatReuse(t Tensor, reuse Tensor, axis int, repeeats ...int) (Tensor, error) } // Diager is any engine that can return a tensor that only contains the diagonal values of the input type Diager interface { Diag(a Tensor) (Tensor, error) } /* NUMBER INTERFACES All these are expected to be unsafe on the first tensor */ // Adder is any engine that can perform elementwise addition. type Adder interface { // Add performs a + b Add(a, b Tensor, opts ...FuncOpt) (Tensor, error) // AddScalar adds a scalar to the tensor. leftTensor indicates if the tensor is the left operand. // Whether or not the input tensor is clobbered is left to the implementation AddScalar(a Tensor, b interface{}, leftTensor bool, opts ...FuncOpt) (Tensor, error) } // Suber is any engine that can perform elementwise subtraction. type Suber interface { // Sub performs a - b Sub(a, b Tensor, opts ...FuncOpt) (Tensor, error) // SubScalar subtracts a scalar from/to the tensor. leftTensor indicates if the tensor is the left operand. // Whether or not the input tensor is clobbered is left to the implementation SubScalar(a Tensor, b interface{}, leftTensor bool, opts ...FuncOpt) (Tensor, error) } // Muler is any engine that can perform elementwise multiplication. // For matrix multiplication, an engine should implement MatMul() or MatVecMul() or Inner() type Muler interface { // Mul performs a * b Mul(a, b Tensor, opts ...FuncOpt) (Tensor, error) // MulScalar multiplies a scalar to the tensor. leftTensor indicates if the tensor is the left operand. // Whether or not the input tensor is clobbered is left to the implementation MulScalar(a Tensor, b interface{}, leftTensor bool, opts ...FuncOpt) (Tensor, error) } // Diver is any engine that can perform elementwise division. type Diver interface { // Div performs a / b Div(a, b Tensor, opts ...FuncOpt) (Tensor, error) // DivScalar divides a scalar from/to the tensor. leftTensor indicates if the tensor is the left operand. // Whether or not the input tensor is clobbered is left to the implementation DivScalar(a Tensor, b interface{}, leftTensor bool, opts ...FuncOpt) (Tensor, error) } // Power is any engine that can perform elementwise Pow() type Power interface { // Pow performs a ^ b Pow(a, b Tensor, opts ...FuncOpt) (Tensor, error) // PowScalar exponentiates a scalar from/to the tensor. leftTensor indicates if the tensor is the left operand. // Whether or not the input tensor is clobbered is left to the implementation PowScalar(a Tensor, b interface{}, leftTensor bool, opts ...FuncOpt) (Tensor, error) } // Moder is any engine that can perform elementwise Mod() type Moder interface { // Mod performs a % b Mod(a, b Tensor, opts ...FuncOpt) (Tensor, error) // ModScalar performs a % b where one of the operands is scalar. leftTensor indicates if the tensor is the left operand. // Whether or not hte input tensor is clobbered is left to the implementation ModScalar(a Tensor, b interface{}, leftTensor bool, opts ...FuncOpt) (Tensor, error) } // MinBetweener is any engine that can perform an elementwise min=between. type MinBetweener interface { MinBetween(a, b Tensor, opts ...FuncOpt) (Tensor, error) MinBetweenScalar(a Tensor, b interface{}, leftTensor bool, opts ...FuncOpt) (Tensor, error) } // MaxBetweener is any engine that can perform an elementwise ma