Skip to content

Commit

Permalink
[onert_run] Revise allocation and session access (#13100)
Browse files Browse the repository at this point in the history
This commit moves allocation and session access from each formatters and random generator into main function.
It will reduce duplication and simplify formatter and generator's logic to fill and dump data.

ONE-DCO-1.0-Signed-off-by: Hyeongseok Oh <hseok82.oh@samsung.com>
  • Loading branch information
hseok-oh authored Jun 5, 2024
1 parent f2167f1 commit 30d45e8
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 212 deletions.
18 changes: 16 additions & 2 deletions tests/tools/onert_run/src/allocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
#ifndef __ONERT_RUN_ALLOCATION_H__
#define __ONERT_RUN_ALLOCATION_H__

#include <cassert>
#include <cstdlib>
#include <cstdint>

#include "nnfw.h"

namespace onert_run
{
class Allocation
Expand All @@ -28,10 +31,21 @@ class Allocation
Allocation() : data_(nullptr) {}
~Allocation() { free(data_); }
void *data() const { return data_; }
void *alloc(uint64_t sz) { return data_ = malloc(sz); }
void *alloc(uint64_t size, NNFW_TYPE dtype)
{
size_ = size;
type_ = dtype;

assert(data_ == nullptr);
return data_ = malloc(size);
}
uint64_t size() const { return size_; }
NNFW_TYPE type() const { return type_; }

private:
void *data_;
void *data_ = nullptr;
uint64_t size_ = 0;
NNFW_TYPE type_ = NNFW_TYPE_TENSOR_FLOAT32;
};
} // namespace onert_run

Expand Down
47 changes: 0 additions & 47 deletions tests/tools/onert_run/src/formatter.h

This file was deleted.

47 changes: 19 additions & 28 deletions tests/tools/onert_run/src/h5formatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ onert_run::TensorShape getShape(H5::DataSet &data_set)

onert_run::TensorShape shape;
for (auto dim : h5_shape)
shape.emplace_back(static_cast<int>(dim));
shape.emplace_back(static_cast<int32_t>(dim));

return shape;
}
Expand All @@ -46,10 +46,9 @@ namespace onert_run
{
static const char *h5_value_grpname = "value";

std::vector<TensorShape> H5Formatter::readTensorShapes(const std::string &filename)
std::vector<TensorShape> H5Formatter::readTensorShapes(const std::string &filename,
uint32_t num_inputs)
{
uint32_t num_inputs;
NNPR_ENSURE_STATUS(nnfw_input_size(session_, &num_inputs));
std::vector<TensorShape> tensor_shapes;

try
Expand Down Expand Up @@ -86,8 +85,7 @@ std::vector<TensorShape> H5Formatter::readTensorShapes(const std::string &filena

void H5Formatter::loadInputs(const std::string &filename, std::vector<Allocation> &inputs)
{
uint32_t num_inputs;
NNPR_ENSURE_STATUS(nnfw_input_size(session_, &num_inputs));
uint32_t num_inputs = inputs.size();
try
{
// Turn off the automatic error printing.
Expand All @@ -97,18 +95,11 @@ void H5Formatter::loadInputs(const std::string &filename, std::vector<Allocation
H5::Group value_group = file.openGroup(h5_value_grpname);
for (uint32_t i = 0; i < num_inputs; ++i)
{
nnfw_tensorinfo ti;
NNPR_ENSURE_STATUS(nnfw_input_tensorinfo(session_, i, &ti));

// TODO Add Assert(nnfw shape, h5 file shape size)

// allocate memory for data
auto bufsz = bufsize_for(&ti);
inputs[i].alloc(bufsz);

auto bufsz = inputs[i].size();
H5::DataSet data_set = value_group.openDataSet(std::to_string(i));
H5::DataType type = data_set.getDataType();
switch (ti.dtype)
switch (inputs[i].type())
{
case NNFW_TYPE_TENSOR_FLOAT32:
if (type == H5::PredType::IEEE_F32BE || type == H5::PredType::IEEE_F32LE)
Expand Down Expand Up @@ -148,8 +139,6 @@ void H5Formatter::loadInputs(const std::string &filename, std::vector<Allocation
default:
throw std::runtime_error("onert_run can load f32, i32, qasymm8, bool and uint8.");
}
NNPR_ENSURE_STATUS(nnfw_set_input(session_, i, ti.dtype, inputs[i].data(), bufsz));
NNPR_ENSURE_STATUS(nnfw_set_input_layout(session_, i, NNFW_LAYOUT_CHANNELS_LAST));
}
}
catch (const H5::Exception &e)
Expand All @@ -164,10 +153,13 @@ void H5Formatter::loadInputs(const std::string &filename, std::vector<Allocation
}
};

void H5Formatter::dumpOutputs(const std::string &filename, std::vector<Allocation> &outputs)
void H5Formatter::dumpOutputs(const std::string &filename, const std::vector<Allocation> &outputs,
const std::vector<TensorShape> &shape_map)
{
uint32_t num_outputs;
NNPR_ENSURE_STATUS(nnfw_output_size(session_, &num_outputs));
uint32_t num_outputs = outputs.size();
if (num_outputs != shape_map.size())
throw std::runtime_error("Number of outputs and shape map are not matched");

try
{
// Turn off the automatic error printing.
Expand All @@ -177,21 +169,20 @@ void H5Formatter::dumpOutputs(const std::string &filename, std::vector<Allocatio
H5::Group value_group = file.createGroup(h5_value_grpname);
for (uint32_t i = 0; i < num_outputs; i++)
{
nnfw_tensorinfo ti;
NNPR_ENSURE_STATUS(nnfw_output_tensorinfo(session_, i, &ti));
std::vector<hsize_t> dims(ti.rank);
for (uint32_t j = 0; j < ti.rank; ++j)
auto shape = shape_map[i];
std::vector<hsize_t> dims(shape.size());
for (uint32_t j = 0; j < shape.size(); ++j)
{
if (ti.dims[j] >= 0)
dims[j] = static_cast<hsize_t>(ti.dims[j]);
if (shape[j] >= 0)
dims[j] = static_cast<hsize_t>(shape[j]);
else
{
std::cerr << "Negative dimension in output tensor" << std::endl;
exit(-1);
}
}
H5::DataSpace data_space(ti.rank, dims.data());
switch (ti.dtype)
H5::DataSpace data_space(shape.size(), dims.data());
switch (outputs[i].type())
{
case NNFW_TYPE_TENSOR_FLOAT32:
{
Expand Down
12 changes: 6 additions & 6 deletions tests/tools/onert_run/src/h5formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#define __ONERT_RUN_H5FORMATTER_H__

#include "allocation.h"
#include "formatter.h"
#include "types.h"

#include <string>
Expand All @@ -28,13 +27,14 @@ struct nnfw_session;

namespace onert_run
{
class H5Formatter : public Formatter
class H5Formatter
{
public:
H5Formatter(nnfw_session *sess) : Formatter(sess) {}
std::vector<TensorShape> readTensorShapes(const std::string &filename) override;
void loadInputs(const std::string &filename, std::vector<Allocation> &inputs) override;
void dumpOutputs(const std::string &filename, std::vector<Allocation> &outputs) override;
H5Formatter() = default;
std::vector<TensorShape> readTensorShapes(const std::string &filename, uint32_t num_inputs);
void loadInputs(const std::string &filename, std::vector<Allocation> &inputs);
void dumpOutputs(const std::string &filename, const std::vector<Allocation> &outputs,
const std::vector<TensorShape> &shape_map);
};
} // namespace onert_run

Expand Down
Loading

0 comments on commit 30d45e8

Please sign in to comment.