Skip to content

[core] ``Tensor`` arithmetic operators +,-,/,*

Current state

Actually, Tensor class does not support any arithmetic operators.

New Feature

In the context of kernel conception, it would be a great feature to be able to compose simple arithmetic Operators, as long as they are on the same backend.

For example it would allow to create a kernel for Relative Change(v_1, v_2) = \frac{v_1 - v_2}{v_1} fearly easily: (T1 - T2) / T1

Solution

Composition would use C++ operator+, operator-, opertor/, operator*. Such functions would benefit from the registrar system to target the arithmetic operation on the same backend than the current Tensor backend.

Prototype

Tensor operator+(const Tensor& other_) {
    AIDGE_ASSERT(backend() == other_.backend());
    const std::shared_ptr<Add_Op> add_ = std::make_shared<Add_Op>(2);
    add_ -> associateInput(0, std::shared_from_this());
    add_ -> associateInput(1, std::make_shared<Tensor>(other_));
    add_ -> computeOutputDims();
    add_ -> setBackend(backend());
    add_ -> forward();
    return add_ -> *getOutput(0);
}

A better (faster to compute) way would be to manually create a result Tensor and the right Add kernel instead of creating an Operator from scratch.