ONE - On-device Neural Engine
Loading...
Searching...
No Matches
luci_interpreter::kernels::BatchMatMul Class Reference

#include <BatchMatMul.h>

Collaboration diagram for luci_interpreter::kernels::BatchMatMul:

Public Member Functions

 BatchMatMul (const Tensor *x, const Tensor *y, Tensor *output, Tensor *x_tmp, Tensor *y_tmp, const BatchMatMulParams &params)
 
const Tensorx () const
 
const Tensory () const
 
Tensoroutput () const
 
void configure () override
 
void execute () const override
 
 BatchMatMul (const Tensor *x, const Tensor *y, Tensor *output, Tensor *x_tmp, Tensor *y_tmp, const BatchMatMulParams &params)
 
const Tensorx () const
 
const Tensory () const
 
Tensoroutput () const
 
void configure () override
 
void execute () const override
 
- Public Member Functions inherited from luci_interpreter::KernelWithParams< BatchMatMulParams >
const BatchMatMulParamsparams () const
 
- Public Member Functions inherited from luci_interpreter::Kernel
virtual ~Kernel ()=default
 
const std::vector< const Tensor * > & getInputTensors () const
 
const std::vector< Tensor * > & getOutputTensors () const
 

Additional Inherited Members

- Protected Member Functions inherited from luci_interpreter::KernelWithParams< BatchMatMulParams >
 KernelWithParams (std::vector< const Tensor * > inputs, std::vector< Tensor * > outputs, const BatchMatMulParams &params)
 
- Protected Member Functions inherited from luci_interpreter::Kernel
 Kernel (std::vector< const Tensor * > inputs, std::vector< Tensor * > outputs)
 
- Protected Attributes inherited from luci_interpreter::KernelWithParams< BatchMatMulParams >
const BatchMatMulParams _params
 
- Protected Attributes inherited from luci_interpreter::Kernel
const std::vector< const Tensor * > _inputs
 
const std::vector< Tensor * > _outputs
 

Detailed Description

Definition at line 28 of file BatchMatMul.h.

Constructor & Destructor Documentation

◆ BatchMatMul() [1/2]

luci_interpreter::kernels::BatchMatMul::BatchMatMul ( const Tensor x,
const Tensor y,
Tensor output,
Tensor x_tmp,
Tensor y_tmp,
const BatchMatMulParams params 
)

Definition at line 46 of file BatchMatMul.cpp.

48 : KernelWithParams({x, y}, {output, x_tmp, y_tmp}, params)
49{
50}
KernelWithParams(std::vector< const Tensor * > inputs, std::vector< Tensor * > outputs, const BatchMatMulParams &params)
Definition Kernel.h:60
const BatchMatMulParams & params() const
Definition Kernel.h:67

References x(), and y().

◆ BatchMatMul() [2/2]

luci_interpreter::kernels::BatchMatMul::BatchMatMul ( const Tensor x,
const Tensor y,
Tensor output,
Tensor x_tmp,
Tensor y_tmp,
const BatchMatMulParams params 
)

Member Function Documentation

◆ configure() [1/2]

void luci_interpreter::kernels::BatchMatMul::configure ( )
overridevirtual

Implements luci_interpreter::Kernel.

Definition at line 52 of file BatchMatMul.cpp.

53{
54 auto lhs = x();
55 auto rhs = y();
56 auto adj_x = params().adj_x;
57 auto adj_y = params().adj_y;
58
59 // TODO Support non-float types
60 if (lhs->element_type() != DataType::FLOAT32 || rhs->element_type() != DataType::FLOAT32)
61 throw std::runtime_error("luci-intp BatchMatMul(1) Unsupported type.");
62
63 LUCI_INTERPRETER_CHECK(lhs->element_type() == rhs->element_type());
64
65 auto lhs_rank = lhs->shape().num_dims();
66 auto rhs_rank = rhs->shape().num_dims();
67 LUCI_INTERPRETER_CHECK(lhs_rank >= 2 && lhs_rank <= 4);
68 LUCI_INTERPRETER_CHECK(rhs_rank >= 2 && rhs_rank <= 4);
69
70 auto lhs_scratchpad = temp_lhs();
71 auto rhs_scratchpad = temp_rhs();
72 luci_interpreter_pal::SetupScratchpadTensor(lhs_scratchpad, rhs_scratchpad, getTensorShape(lhs),
73 getTensorShape(rhs));
74
75 auto output_rank = std::max(lhs_rank, rhs_rank);
76
77 auto extended_lhs_shape = tflite::RuntimeShape::ExtendedShape(output_rank, getTensorShape(lhs));
78 auto extended_rhs_shape = tflite::RuntimeShape::ExtendedShape(output_rank, getTensorShape(rhs));
79
80 // Ensure any batch dimensions obey broacasting rules.
81 for (int i = 0; i < output_rank - 2; ++i)
82 {
83 const int lhs_dim = extended_lhs_shape.Dims(i);
84 const int rhs_dim = extended_rhs_shape.Dims(i);
85 if (lhs_dim != rhs_dim)
86 {
87 if (lhs_dim != 1)
88 {
89 LUCI_INTERPRETER_CHECK(rhs_dim == 1);
90 }
91 }
92 }
93
94 // Ensure other dimensions work for matrix multiplication.
95 int accum_dim_lhs =
96 adj_x ? extended_lhs_shape.Dims(output_rank - 2) : extended_lhs_shape.Dims(output_rank - 1);
97 int accum_dim_rhs =
98 adj_y ? extended_rhs_shape.Dims(output_rank - 1) : extended_rhs_shape.Dims(output_rank - 2);
99 LUCI_INTERPRETER_CHECK(accum_dim_lhs == accum_dim_rhs);
100
101 Shape output_shape(output_rank);
102 // Fill in any broadcast dimensions.
103 for (int i = 0; i < output_rank - 2; ++i)
104 {
105 const int lhs_dim = extended_lhs_shape.Dims(i);
106 const int rhs_dim = extended_rhs_shape.Dims(i);
107 int broadcast_dim = lhs_dim;
108 if ((lhs_dim != rhs_dim) && (lhs_dim == 1))
109 {
110 broadcast_dim = rhs_dim;
111 }
112 output_shape.dim(i) = broadcast_dim;
113 }
114 // Fill in the matmul dimensions.
115 int lhs_rows_index = adj_x ? output_rank - 1 : output_rank - 2;
116 int rhs_cols_index = adj_y ? output_rank - 2 : output_rank - 1;
117
118 output_shape.dim(output_rank - 2) = extended_lhs_shape.Dims(lhs_rows_index);
119 output_shape.dim(output_rank - 1) = extended_rhs_shape.Dims(rhs_cols_index);
120
122}
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
const luci_interpreter::RuntimeShape output_shape
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194
Definition Shape.h:28

References luci_interpreter::BatchMatMulParams::adj_x, luci_interpreter::BatchMatMulParams::adj_y, luci_interpreter::kernels::getTensorShape(), LUCI_INTERPRETER_CHECK, output(), output_shape, luci_interpreter::KernelWithParams< BatchMatMulParams >::params(), luci_interpreter::Tensor::resize(), x(), and y().

◆ configure() [2/2]

void luci_interpreter::kernels::BatchMatMul::configure ( )
overridevirtual

◆ execute() [1/2]

void luci_interpreter::kernels::BatchMatMul::execute ( ) const
overridevirtual

Implements luci_interpreter::Kernel.

Definition at line 151 of file BatchMatMul.cpp.

152{
153 auto lhs = x();
154 auto rhs = y();
155
156 bool adj_x = params().adj_x;
157 bool adj_y = params().adj_y;
158
159 auto orig_lhs_shape = getTensorShape(lhs);
160 auto orig_rhs_shape = getTensorShape(rhs);
161
162 auto rhs_tensor = adj_y ? rhs : temp_rhs();
163 auto lhs_tensor = adj_x ? temp_lhs() : lhs;
164 if (not adj_y)
165 {
166 TransposeRowsColumns(rhs, temp_rhs());
167 }
168 if (adj_x)
169 {
170 TransposeRowsColumns(lhs, temp_lhs());
171 }
172 tflite::RuntimeShape rhs_shape = adj_y ? orig_rhs_shape : SwapRowColumnDims(orig_rhs_shape);
173 tflite::RuntimeShape lhs_shape = adj_x ? orig_lhs_shape : SwapRowColumnDims(orig_lhs_shape);
174
175 switch (x()->element_type())
176 {
177 case DataType::FLOAT32:
178 luci_interpreter_pal::BatchMatMul(rhs_shape, getTensorData<float>(rhs_tensor), lhs_shape,
179 getTensorData<float>(lhs_tensor), getTensorShape(output()),
180 getTensorData<float>(output()));
181 break;
182 default:
183 throw std::runtime_error("luci-intp BatchMatMul(2) Unsupported type.");
184 }
185}
void TransposeRowsColumns(const Tensor *tensor_in, Tensor *tensor_out)
void BatchMatMul(const tflite::RuntimeShape &lhs_shape, const float *lhs_data, const tflite::RuntimeShape &rhs_shape, const float *rhs_data, const tflite::RuntimeShape &output_shape, float *output_data)

References luci_interpreter::BatchMatMulParams::adj_x, luci_interpreter::BatchMatMulParams::adj_y, luci_interpreter_pal::BatchMatMul(), luci_interpreter::kernels::getTensorShape(), output(), luci_interpreter::KernelWithParams< BatchMatMulParams >::params(), luci_interpreter::kernels::TransposeRowsColumns(), x(), and y().

◆ execute() [2/2]

void luci_interpreter::kernels::BatchMatMul::execute ( ) const
overridevirtual

◆ output() [1/2]

Tensor * luci_interpreter::kernels::BatchMatMul::output ( ) const
inline

Definition at line 36 of file BatchMatMul.h.

36{ return _outputs[0]; }
const std::vector< Tensor * > _outputs
Definition Kernel.h:53

References luci_interpreter::Kernel::_outputs.

Referenced by configure(), and execute().

◆ output() [2/2]

Tensor * luci_interpreter::kernels::BatchMatMul::output ( ) const
inline

Definition at line 36 of file BatchMatMul.h.

36{ return _outputs[0]; }

References luci_interpreter::Kernel::_outputs.

◆ x() [1/2]

const Tensor * luci_interpreter::kernels::BatchMatMul::x ( ) const
inline

Definition at line 34 of file BatchMatMul.h.

34{ return _inputs[0]; }
const std::vector< const Tensor * > _inputs
Definition Kernel.h:52

References luci_interpreter::Kernel::_inputs.

Referenced by BatchMatMul(), configure(), and execute().

◆ x() [2/2]

const Tensor * luci_interpreter::kernels::BatchMatMul::x ( ) const
inline

Definition at line 34 of file BatchMatMul.h.

34{ return _inputs[0]; }

References luci_interpreter::Kernel::_inputs.

◆ y() [1/2]

const Tensor * luci_interpreter::kernels::BatchMatMul::y ( ) const
inline

Definition at line 35 of file BatchMatMul.h.

35{ return _inputs[1]; }

References luci_interpreter::Kernel::_inputs.

Referenced by BatchMatMul(), configure(), and execute().

◆ y() [2/2]

const Tensor * luci_interpreter::kernels::BatchMatMul::y ( ) const
inline

Definition at line 35 of file BatchMatMul.h.

35{ return _inputs[1]; }

References luci_interpreter::Kernel::_inputs.


The documentation for this class was generated from the following files: