mlc

enum class mlc::ErrorType : int64_t

Values:

enumerator Undefined
enumerator None
enumerator ParseExpectedLeftBracket
enumerator ParseExpectedRightBracket
enumerator ParseExpectedArrow
enumerator ParseExpectedComma
enumerator ParseExpectedDimensionList
enumerator ParseNotAllowedToParseAgain
enumerator ParseUndefinedNode
enumerator EinsumInvalidRoot
enumerator EinsumNotEnoughInputTensors
enumerator EinsumTooManyInputTensors
enumerator EinsumNullPtrAsInputTensor
enumerator ExecuteWrongDType
enumerator ExecuteWrongDimension
enumerator ExecuteWrongPrimitive
enumerator ExecuteFirstTouchPrimitive
enumerator ExecuteWrongFirstTouchPrimitive
enumerator ExecuteWrongMainPrimitive
enumerator ExecuteWrongLastTouchPrimitive
enumerator ExecuteTypeNotSupported
enumerator ExecuteInvalidPrimitiveConfiguration
enumerator ExecuteInvalidFirstTouchConfiguration
enumerator ExecuteInvalidMainConfiguration
enumerator ExecuteInvalidLastTouchConfiguration
enumerator ExecuteInvalidExecutionOrder
enumerator ExecuteInvalidStrides
enumerator ExecuteKDimensionMustNotBeShared
enumerator ExecuteSharedRequiredForParallelExecution
enumerator TensorExpected2DTensor
enumerator ExpectedSingleContraction
enum class mlc::UnaryType : int64_t

Values:

enumerator None
enumerator Zero
enumerator ReLU
enumerator Identity
void mlc::fill_random(Tensor &tensor)

Fills the tensor with random float data.

Parameters:

tensor – The tensor to fill.

void mlc::fill_number(Tensor &tensor, float number)

Fills the tensor with the given number.

Parameters:
  • tensor – The tensor to fill.

  • number – The number used to fill the tensor.

void mlc::fill_counting_up(Tensor &tensor, float start, float step)

Fills the tensor with counting upwards numbers.

Parameters:
  • tensor – The tensor to fill.

  • start – The number to start counting from.

  • step – The amount to increase everytime.

void mlc::fill_counting_down(Tensor &tensor, float start, float step)

Fills the tensor with counting downwards numbers.

Parameters:
  • tensor – The tensor to fill.

  • start – The number to start counting from.

  • step – The amount to decrease everytime.

void mlc::fill_lambda(Tensor &tensor, std::function<float(const Tensor&, size_t)> function)

Fills the tensor based on the given function.

Parameters:
  • tensor – The tensor to fill.

  • function – The function that gets the current tensor and the current index of the tensor as input. index = index0 * stride0 + index1 * stride1 + … + indexN * strideN.

Error mlc::einsum(const std::vector<std::reference_wrapper<const Tensor>> &inputs, Tensor &output, const std::string &tree)
Parameters:
  • inputs – The input tensors.

  • output – The output tensor.

  • tree – The (nested) einsum tree to contract in the format [in0],[in1]->[out].

Returns:

Error The error code or ErrorType::None on success.

Error mlc::einsum(const std::vector<Tensor*> &inputs, Tensor &output, const std::string &tree)

Executes contractions based on the given tree.

Parameters:
  • inputs – The input tensors.

  • output – The output tensor.

  • tree – The (nested) einsum tree to contract in the format [in0],[in1]->[out].

Returns:

Error The error code or ErrorType::None on success.

TensorOperation *mlc::einsum_operation(const std::vector<std::vector<uint64_t>> &inputs, const std::vector<uint64_t> &output, const std::string &tree)

Sets up the einsum tree for contraction based on the given tensor dimensions and tree.

Parameters:
  • inputs – The input tensors shapes.

  • output – The output tensor shape.

  • tree – The einsum tree to contract in the format [in0],[in1]->[out].

Error mlc::contraction(const Tensor &input0, const Tensor &input1, Tensor &output, const std::string &contraction)

Perform a binary contraction and adds it to the output.

Parameters:
  • input0 – The first input tensor.

  • input1 – The second input tensor.

  • output – The output to add the result to.

  • contraction – The string to show the dimension to be contracted in the format [in0],[in1]->[out].

Returns:

Error The error code or ErrorType::None on success.

Error mlc::contraction(const Tensor &input0, const Tensor &input1, Tensor &output, const std::string &contraction, const UnaryType firstTouch, const UnaryType lastTouch)

Performs a contraction on two input tensor and one output tensor. Before and after the contraction, a first touch unary and a last touch unary are applied to the output tensor.

Parameters:
  • input0 – The first input tensor.

  • input1 – The second input tensor.

  • output – The output to add the result to.

  • contraction – The string to show the dimension to be contracted in the format [in0],[in1]->[out].

  • firstTouch – The unary that should be execute before the contraction.

  • lastTouch – The unary that should be executed after the contraction.

Returns:

Error The error code or ErrorType::None on success.

Error mlc::gemm(const Tensor &input0, const Tensor &input1, Tensor &output)

Perform a general matrix-matrix multiplication and adds it to the output.

Parameters:
  • input0 – The first input tensor in the form MxK where M is the leading dimension.

  • input1 – The second input tensor in the form KxN where K is the leading dimension.

  • output – The output to add the result to in the form MxN where M is the leading dimension.

Returns:

Error The error code or ErrorType::None on success.

Error mlc::unary_zero(Tensor &input)

Performs a zero unary that sets the output tensor to zero.

Parameters:

input – The input tensor.

Returns:

Error The error code or ErrorType::None on success.

Error mlc::unary_relu(const Tensor &input, Tensor &output)

Performs a relu unary that applies Rectified Linear Unit on the tensor input.

Parameters:
  • input – The input tensor.

  • output – The ouput tensor.

Returns:

Error The error code or ErrorType::None on success.

Error mlc::unary_identity(const Tensor &input, Tensor &output)

Performs a identity unary that copies the input tensor to the output tensor.

Parameters:
  • input – The input tensor.

  • output – The output tensor.

Returns:

Error The error code or ErrorType::None on success.

class EinsumOperation : public mlc::TensorOperation

Public Functions

EinsumOperation(const std::vector<std::reference_wrapper<const Tensor>> &inputs, Tensor &output, const std::string &tree)
virtual Error execute(const std::vector<std::reference_wrapper<const Tensor>> &inputs, Tensor &output) override

Executes the setup einsum expression with input tensor of the same size.

Parameters:
  • inputs – The inputs to be einsum calculation.

  • output – The output of the einsum calculation.

Returns:

Error The error code or ErrorType::None on success.

virtual Error execute(const std::vector<const Tensor*> &inputs, Tensor &output) override

Executes the setup einsum expression with input tensor of the same size.

Parameters:
  • inputs – The inputs to be einsum calculation.

  • output – The output of the einsum calculation.

Returns:

Error The error code or ErrorType::None on success.

virtual Error getSetupError() const override

Gets the error that was produces during the setup of the tree.

Returns:

Error The error code or ErrorType::None on success.

Private Functions

template<typename T>
inline Error execute(const std::vector<T> &inputs, Tensor &output)

Executes the Einsum operation with the given inputs and output tensor.

template<typename T>
inline Error hasSameDimensions(const std::vector<T> &inputs, const Tensor &output)

Private Members

Error error
mini_jit::EinsumTree einsumTree
struct Error

Public Members

ErrorType type
std::string message
struct Tensor

Public Functions

Tensor() = delete
inline Tensor(float *data, const std::vector<uint64_t> &dim_sizes)

Construct a new Tensor with with a pointer to memory and the dimension sizes sorted in by stride in descending order.

Parameters:
  • data – The pointer to the data array.

  • dim_sizes – The dimension sizes sorted by stride in descending order.

inline Tensor(const std::vector<uint64_t> &dim_sizes)

Construct a new Tensor with the dimension sizes sorted by stride in descending order.

Parameters:

dim_sizes – The dimension sizes sorted by stride in descending order.

inline ~Tensor()

Destroys the tensor.

std::string to_string(std::string name = "tensor")

Converts the tensor into its string representation.

Parameters:

name – Name of the tensor that is printed

Returns:

std::string The string representation of the tensor.

uint64_t size()

Returns the number of elements the tensor has.

Returns:

uint64_t The number of elements in the tensor.

Public Members

bool ownsData = false
float *data = nullptr
std::vector<uint64_t> dim_sizes
std::vector<uint64_t> strides
class TensorOperation

Subclassed by mlc::EinsumOperation

Public Functions

inline virtual ~TensorOperation()
virtual Error execute(const std::vector<std::reference_wrapper<const Tensor>> &inputs, Tensor &output) = 0

Executes the setup einsum expression with input tensor of the same size.

Parameters:
  • inputs – The inputs to be einsum calculation.

  • output – The output of the einsum calculation.

Returns:

Error The error code or ErrorType::None on success.

virtual Error execute(const std::vector<const Tensor*> &inputs, Tensor &output) = 0

Executes the setup einsum expression with input tensor of the same size.

Parameters:
  • inputs – The inputs to be einsum calculation.

  • output – The output of the einsum calculation.

Returns:

Error The error code or ErrorType::None on success.

virtual Error getSetupError() const = 0

Gets the error that was produces during the setup of the tree.

Returns:

Error The error code or ErrorType::None on success.