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

#include <Tensor.h>

Public Member Functions

 Tensor (DataType element_type, Shape shape, AffineQuantization quantization, std::string name)
 
DataType element_type () const
 
const Shapeshape () const
 
float scale () const
 
int32_t zero_point () const
 
const std::vector< float > & scales () const
 
const std::vector< int32_t > & zero_points () const
 
int32_t quantized_dimension () const
 
template<typename T >
const T * data () const
 
template<typename T >
T * data ()
 
const std::string & name () const
 
void readData (void *data_ptr, size_t data_size) const
 
void writeData (const void *data_ptr, size_t data_size)
 
void resize (const Shape &new_shape)
 
void set_data_buffer (uint8_t *buffer)
 
bool is_observable () const
 
void set_observable (bool value)
 
bool is_allocatable () const
 
void set_allocatable (bool value)
 
bool is_data_allocated () const
 
int32_t get_offset () const
 
void set_offset (int32_t offset)
 

Static Public Member Functions

static float scale (const circle::Tensor *circle_tensor)
 
static int32_t zero_point (const circle::Tensor *circle_tensor)
 
static const std::vector< float > scales (const circle::Tensor *circle_tensor)
 
static const std::vector< int32_t > zero_points (const circle::Tensor *circle_tensor)
 
static int32_t quantized_dimension (const circle::Tensor *circle_tensor)
 
static bool is_constant_tensor (const luci_interpreter::CircleReader *reader, const circle::Tensor *circle_tensor)
 
static DataType element_type (const circle::Tensor *circle_tensor)
 
static VectorWrapper< int32_t > tensor_shape (const circle::Tensor *circle_tensor)
 
static int num_dims (const circle::Tensor *circle_tensor)
 
static int32_t dim (const circle::Tensor *circle_tensor, int i)
 
static int32_t num_elements (const circle::Tensor *circle_tensor)
 

Detailed Description

Definition at line 100 of file Tensor.h.

Constructor & Destructor Documentation

◆ Tensor()

Tensor::Tensor ( DataType  element_type,
Shape  shape,
AffineQuantization  quantization,
std::string  name 
)

Definition at line 25 of file Tensor.cpp.

27 : _element_type(element_type), _shape(std::move(shape)), _quantization(std::move(quantization)),
28 _name(std::move(name)), _data_allocated(false)
29{
30}
const std::string & name() const
Definition Tensor.h:141
const Shape & shape() const
Definition Tensor.h:107
DataType element_type() const
Definition Tensor.h:105

Member Function Documentation

◆ data() [1/2]

template<typename T >
T * luci_interpreter::Tensor::data ( )
inline

Definition at line 134 of file Tensor.h.

135 {
136 static_assert(std::is_same<uint8_t, char>::value or
137 std::is_same<uint8_t, unsigned char>::value);
138 return reinterpret_cast<T *>(_data);
139 }

◆ data() [2/2]

template<typename T >
const T * luci_interpreter::Tensor::data ( ) const
inline

◆ dim()

static int32_t luci_interpreter::Tensor::dim ( const circle::Tensor *  circle_tensor,
int  i 
)
inlinestatic

Definition at line 225 of file Tensor.h.

226 {
227 // TODO check removing of wrap
228 assert(i >= 0);
229 auto const const_dims = wrap(circle_tensor->shape());
230 assert(i < const_dims.size());
231
232 return const_dims[i];
233 }
VectorWrapper< T > wrap(const flatbuffers::Vector< T > *vec)

References luci::wrap().

Referenced by num_elements().

◆ element_type() [1/2]

◆ element_type() [2/2]

static DataType luci_interpreter::Tensor::element_type ( const circle::Tensor *  circle_tensor)
inlinestatic

Definition at line 208 of file Tensor.h.

209 {
210 return luci_datatype(circle_tensor->type());
211 }
loco::DataType luci_datatype(circle::TensorType type)

References luci::luci_datatype().

◆ get_offset()

int32_t luci_interpreter::Tensor::get_offset ( ) const
inline

Definition at line 172 of file Tensor.h.

172{ return _offset; }

◆ is_allocatable()

bool luci_interpreter::Tensor::is_allocatable ( ) const
inline

Definition at line 166 of file Tensor.h.

166{ return _is_allocatable; }

◆ is_constant_tensor()

static bool luci_interpreter::Tensor::is_constant_tensor ( const luci_interpreter::CircleReader reader,
const circle::Tensor *  circle_tensor 
)
inlinestatic

Definition at line 202 of file Tensor.h.

204 {
205 return reader->buffers()[circle_tensor->buffer()]->data() != nullptr;
206 }
CircleBuffers buffers() const
const T * data() const

References luci::CircleReader::buffers(), and luci::VectorWrapper< T >::data().

◆ is_data_allocated()

bool luci_interpreter::Tensor::is_data_allocated ( ) const
inline

Definition at line 170 of file Tensor.h.

170{ return _data_allocated; }

◆ is_observable()

bool luci_interpreter::Tensor::is_observable ( ) const
inline

Definition at line 162 of file Tensor.h.

162{ return _is_observable; }

◆ name()

const std::string & luci_interpreter::Tensor::name ( ) const
inline

Definition at line 141 of file Tensor.h.

141{ return _name; }

◆ num_dims()

static int luci_interpreter::Tensor::num_dims ( const circle::Tensor *  circle_tensor)
inlinestatic

Definition at line 218 of file Tensor.h.

219 {
220 // TODO check removing of wrap
221 auto const const_dims = wrap(circle_tensor->shape());
222 return const_dims.size();
223 }

References luci::wrap().

◆ num_elements()

static int32_t luci_interpreter::Tensor::num_elements ( const circle::Tensor *  circle_tensor)
inlinestatic

Definition at line 235 of file Tensor.h.

236 {
237 int32_t result = 1;
238 auto const const_dims = wrap(circle_tensor->shape());
239 for (const int32_t dim : const_dims)
240 {
241 result *= dim;
242 }
243 return result;
244 }
static int32_t dim(const circle::Tensor *circle_tensor, int i)
Definition Tensor.h:225
result
Definition infer.py:103

References dim(), and luci::wrap().

Referenced by readData(), and writeData().

◆ quantized_dimension() [1/2]

int32_t luci_interpreter::Tensor::quantized_dimension ( ) const
inline

Definition at line 125 of file Tensor.h.

125{ return _quantization.quantized_dimension; }

References luci_interpreter::AffineQuantization::quantized_dimension.

◆ quantized_dimension() [2/2]

static int32_t luci_interpreter::Tensor::quantized_dimension ( const circle::Tensor *  circle_tensor)
inlinestatic

Definition at line 190 of file Tensor.h.

191 {
192 const auto *quant_params = circle_tensor->quantization();
193 if (quant_params == nullptr)
194 {
195 assert(false && "There is no quantization params");
196 return 0;
197 }
198 return quant_params->quantized_dimension();
199 }

◆ readData()

void Tensor::readData ( void *  data_ptr,
size_t  data_size 
) const

Definition at line 32 of file Tensor.cpp.

33{
34 const size_t element_size = getDataTypeSize(element_type());
35 const int32_t num_elements = shape().num_elements();
36 if (data_size != num_elements * element_size)
37 {
38 throw std::invalid_argument("Invalid data size.");
39 }
40 assert(data_ptr != nullptr);
41 std::memcpy(data_ptr, data<void>(), data_size);
42}
int32_t num_elements() const
Definition Tensor.h:53
static int32_t num_elements(const circle::Tensor *circle_tensor)
Definition Tensor.h:235
size_t getDataTypeSize(DataType data_type)
Definition DataType.h:33

References element_type(), luci_interpreter::getDataTypeSize(), luci_interpreter::Shape::num_elements(), num_elements(), and shape().

◆ resize()

void Tensor::resize ( const Shape new_shape)

Definition at line 56 of file Tensor.cpp.

56{ _shape = new_shape; }

Referenced by luci_interpreter::kernels::Abs::configure(), luci_interpreter::kernels::Add::configure(), luci_interpreter::kernels::ArgMax::configure(), luci_interpreter::kernels::AveragePool2D::configure(), luci_interpreter::kernels::BatchMatMul::configure(), luci_interpreter::kernels::BatchToSpaceND::configure(), luci_interpreter::kernels::BroadcastTo::configure(), luci_interpreter::kernels::Cast::configure(), luci_interpreter::kernels::Concatenation::configure(), luci_interpreter::kernels::Conv2D::configure(), luci_interpreter::kernels::Cos::configure(), luci_interpreter::kernels::CumSum::configure(), luci_interpreter::kernels::DepthToSpace::configure(), luci_interpreter::kernels::DepthwiseConv2D::configure(), luci_interpreter::kernels::Dequantize::configure(), luci_interpreter::kernels::Div::configure(), luci_interpreter::kernels::Elu::configure(), luci_interpreter::kernels::Equal::configure(), luci_interpreter::kernels::Exp::configure(), luci_interpreter::kernels::ExpandDims::configure(), luci_interpreter::kernels::Floor::configure(), luci_interpreter::kernels::FloorDiv::configure(), luci_interpreter::kernels::FloorMod::configure(), luci_interpreter::kernels::FullyConnected::configure(), luci_interpreter::kernels::Gather::configure(), luci_interpreter::kernels::Gelu::configure(), luci_interpreter::kernels::Greater::configure(), luci_interpreter::kernels::GreaterEqual::configure(), luci_interpreter::kernels::GRU::configure(), luci_interpreter::kernels::HardSwish::configure(), luci_interpreter::kernels::InstanceNorm::configure(), luci_interpreter::kernels::L2Normalize::configure(), luci_interpreter::kernels::L2Pool2D::configure(), luci_interpreter::kernels::LeakyRelu::configure(), luci_interpreter::kernels::Less::configure(), luci_interpreter::kernels::LessEqual::configure(), luci_interpreter::kernels::LocalResponseNormalization::configure(), luci_interpreter::kernels::Log::configure(), luci_interpreter::kernels::LogicalAnd::configure(), luci_interpreter::kernels::LogicalNot::configure(), luci_interpreter::kernels::LogicalOr::configure(), luci_interpreter::kernels::Logistic::configure(), luci_interpreter::kernels::LogSoftmax::configure(), luci_interpreter::kernels::Maximum::configure(), luci_interpreter::kernels::MaxPool2D::configure(), luci_interpreter::kernels::Mean::configure(), luci_interpreter::kernels::Minimum::configure(), luci_interpreter::kernels::MirrorPad::configure(), luci_interpreter::kernels::Mul::configure(), luci_interpreter::kernels::Neg::configure(), luci_interpreter::kernels::NotEqual::configure(), luci_interpreter::kernels::OneHot::configure(), luci_interpreter::kernels::Pack::configure(), luci_interpreter::kernels::Pad::configure(), luci_interpreter::kernels::PadV2::configure(), luci_interpreter::kernels::Pow::configure(), luci_interpreter::kernels::PRelu::configure(), luci_interpreter::kernels::Quantize::configure(), luci_interpreter::kernels::ReduceMax::configure(), luci_interpreter::kernels::ReduceProd::configure(), luci_interpreter::kernels::Relu::configure(), luci_interpreter::kernels::Relu0To1::configure(), luci_interpreter::kernels::Relu6::configure(), luci_interpreter::kernels::Reshape::configure(), luci_interpreter::kernels::ResizeBilinear::configure(), luci_interpreter::kernels::ResizeNearestNeighbor::configure(), luci_interpreter::kernels::ReverseV2::configure(), luci_interpreter::kernels::RmsNorm::configure(), luci_interpreter::kernels::RoPE::configure(), luci_interpreter::kernels::Rsqrt::configure(), luci_interpreter::kernels::Select::configure(), luci_interpreter::kernels::SelectV2::configure(), luci_interpreter::kernels::ShapeKernel::configure(), luci_interpreter::kernels::Sin::configure(), luci_interpreter::kernels::Slice::configure(), luci_interpreter::kernels::Softmax::configure(), luci_interpreter::kernels::SpaceToBatchND::configure(), luci_interpreter::kernels::SpaceToDepth::configure(), luci_interpreter::kernels::Split::configure(), luci_interpreter::kernels::Sqrt::configure(), luci_interpreter::kernels::Square::configure(), luci_interpreter::kernels::SquaredDifference::configure(), luci_interpreter::kernels::Squeeze::configure(), luci_interpreter::kernels::StridedSlice::configure(), luci_interpreter::kernels::Sub::configure(), luci_interpreter::kernels::Sum::configure(), luci_interpreter::kernels::SVDF::configure(), luci_interpreter::kernels::Tanh::configure(), luci_interpreter::kernels::Tile::configure(), luci_interpreter::kernels::Transpose::configure(), luci_interpreter::kernels::TransposeConv::configure(), luci_interpreter::kernels::UnidirectionalSequenceLSTM::configure(), luci_interpreter::kernels::Unpack::configure(), and luci_interpreter::kernels::If::execute().

◆ scale() [1/2]

◆ scale() [2/2]

static float luci_interpreter::Tensor::scale ( const circle::Tensor *  circle_tensor)
inlinestatic

Definition at line 137 of file Tensor.h.

138 {
139 const auto *quant_params = circle_tensor->quantization();
140 if (quant_params == nullptr)
141 {
142 assert(false && "There is no quantization params");
143 return 0;
144 }
145
146 return *quant_params->scale()->cbegin();
147 }

◆ scales() [1/2]

const std::vector< float > & luci_interpreter::Tensor::scales ( ) const
inline

Definition at line 121 of file Tensor.h.

121{ return _quantization.scale; }

References luci_interpreter::AffineQuantization::scale.

Referenced by scales().

◆ scales() [2/2]

static const std::vector< float > luci_interpreter::Tensor::scales ( const circle::Tensor *  circle_tensor)
inlinestatic

Definition at line 161 of file Tensor.h.

162 {
163 const auto *quant_params = circle_tensor->quantization();
164 if (quant_params == nullptr)
165 {
166 assert(false && "There is no quantization params");
167 return {};
168 }
169 assert(quant_params->scale() != nullptr);
170 std::vector<float> scales(quant_params->scale()->cbegin(), quant_params->scale()->cend());
171
172 return scales;
173 }
const std::vector< float > & scales() const
Definition Tensor.h:121

References scales().

◆ set_allocatable()

void luci_interpreter::Tensor::set_allocatable ( bool  value)
inline

Definition at line 168 of file Tensor.h.

168{ _is_allocatable = value; }

◆ set_data_buffer()

void luci_interpreter::Tensor::set_data_buffer ( uint8_t *  buffer)
inline

Definition at line 149 of file Tensor.h.

150 {
151 if (buffer == nullptr)
152 {
153 _data_allocated = false;
154 }
155 else
156 {
157 _data_allocated = true;
158 }
159 _data = buffer;
160 }

◆ set_observable()

◆ set_offset()

void luci_interpreter::Tensor::set_offset ( int32_t  offset)
inline

Definition at line 174 of file Tensor.h.

174{ _offset = offset; }
__global uchar * offset(const Image *img, int x, int y)
Definition helpers.h:540

References offset().

◆ shape()

const Shape & luci_interpreter::Tensor::shape ( ) const
inline

Definition at line 107 of file Tensor.h.

107{ return _shape; }

Referenced by RandomDataGenerator.RandomDataGenerator::_gen_float32(), RandomDataGenerator.RandomDataGenerator::_gen_int16(), RandomDataGenerator.RandomDataGenerator::_gen_uint8(), luci_interpreter::build_kernel_CircleFullyConnected(), luci_interpreter::kernels::ArgMax::configure(), luci_interpreter::kernels::AveragePool2D::configure(), luci_interpreter::kernels::BatchToSpaceND::configure(), luci_interpreter::kernels::BroadcastTo::configure(), luci_interpreter::kernels::Cast::configure(), luci_interpreter::kernels::Concatenation::configure(), luci_interpreter::kernels::Conv2D::configure(), luci_interpreter::kernels::DepthToSpace::configure(), luci_interpreter::kernels::DepthwiseConv2D::configure(), luci_interpreter::kernels::ExpandDims::configure(), luci_interpreter::kernels::Fill::configure(), luci_interpreter::kernels::FullyConnected::configure(), luci_interpreter::kernels::Gather::configure(), luci_interpreter::kernels::L2Pool2D::configure(), luci_interpreter::kernels::MaxPool2D::configure(), luci_interpreter::kernels::Mean::configure(), luci_interpreter::kernels::MirrorPad::configure(), luci_interpreter::kernels::OneHot::configure(), luci_interpreter::kernels::Pack::configure(), luci_interpreter::kernels::Pad::configure(), luci_interpreter::kernels::PadV2::configure(), luci_interpreter::kernels::PRelu::configure(), luci_interpreter::kernels::ReduceMax::configure(), luci_interpreter::kernels::ReduceProd::configure(), luci_interpreter::kernels::ResizeBilinear::configure(), luci_interpreter::kernels::ResizeNearestNeighbor::configure(), luci_interpreter::kernels::RmsNorm::configure(), luci_interpreter::kernels::Select::configure(), luci_interpreter::kernels::SelectV2::configure(), luci_interpreter::kernels::ShapeKernel::configure(), luci_interpreter::kernels::SpaceToBatchND::configure(), luci_interpreter::kernels::SpaceToDepth::configure(), luci_interpreter::kernels::Split::configure(), luci_interpreter::kernels::SplitV::configure(), luci_interpreter::kernels::Squeeze::configure(), luci_interpreter::kernels::StridedSlice::configure(), luci_interpreter::kernels::Sum::configure(), luci_interpreter::kernels::SVDF::configure(), luci_interpreter::kernels::Tile::configure(), luci_interpreter::kernels::Transpose::configure(), luci_interpreter::kernels::TransposeConv::configure(), luci_interpreter::kernels::UnidirectionalSequenceLSTM::configure(), luci_interpreter::kernels::Unpack::configure(), luci_interpreter_pal::eval_integer_8x8_16_lstm(), luci_interpreter::kernels::lstm::EvalFloat(), luci_interpreter::kernels::ExpandDims::execute(), luci_interpreter::kernels::If::execute(), luci_interpreter::kernels::MirrorPad::execute(), luci_interpreter::kernels::Pad::execute(), luci_interpreter::kernels::PadV2::execute(), luci_interpreter::kernels::Reshape::execute(), luci_interpreter::kernels::StridedSlice::execute(), luci_interpreter::kernels::Transpose::execute(), readData(), and writeData().

◆ tensor_shape()

static VectorWrapper< int32_t > luci_interpreter::Tensor::tensor_shape ( const circle::Tensor *  circle_tensor)
inlinestatic

Definition at line 213 of file Tensor.h.

214 {
215 return wrap(circle_tensor->shape());
216 }

References luci::wrap().

◆ writeData()

void Tensor::writeData ( const void *  data_ptr,
size_t  data_size 
)

Definition at line 44 of file Tensor.cpp.

45{
46 const size_t element_size = getDataTypeSize(element_type());
47 const int32_t num_elements = shape().num_elements();
48 if (data_size != num_elements * element_size)
49 {
50 throw std::invalid_argument("Invalid data size.");
51 }
52 assert(data_ptr != nullptr);
53 std::memcpy(data<void>(), data_ptr, data_size);
54}

References element_type(), luci_interpreter::getDataTypeSize(), luci_interpreter::Shape::num_elements(), num_elements(), and shape().

◆ zero_point() [1/2]

int32_t luci_interpreter::Tensor::zero_point ( ) const
inline

◆ zero_point() [2/2]

static int32_t luci_interpreter::Tensor::zero_point ( const circle::Tensor *  circle_tensor)
inlinestatic

Definition at line 149 of file Tensor.h.

150 {
151 const auto *quant_params = circle_tensor->quantization();
152 if (quant_params == nullptr)
153 {
154 assert(false && "There is no quantization params");
155 return 0;
156 }
157
158 return *quant_params->zero_point()->cbegin();
159 }

◆ zero_points() [1/2]

const std::vector< int32_t > & luci_interpreter::Tensor::zero_points ( ) const
inline

◆ zero_points() [2/2]

static const std::vector< int32_t > luci_interpreter::Tensor::zero_points ( const circle::Tensor *  circle_tensor)
inlinestatic

Definition at line 175 of file Tensor.h.

176 {
177 const auto *quant_params = circle_tensor->quantization();
178 if (quant_params == nullptr)
179 {
180 assert(false && "There is no quantization params");
181 return {};
182 }
183 assert(quant_params->zero_point() != nullptr);
184 std::vector<int32_t> zero_points(quant_params->zero_point()->cbegin(),
185 quant_params->zero_point()->cend());
186
187 return zero_points;
188 }
const std::vector< int32_t > & zero_points() const
Definition Tensor.h:123

References zero_points().


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