Skip to content

Commit

Permalink
[onert] Support int64 for nnpackage_run (Samsung#2473)
Browse files Browse the repository at this point in the history
Add random input generation and h5 dump/load, type for supporting int64

ONE-DCO-1.0-Signed-off-by: sungho22.lee <[email protected]>
  • Loading branch information
intom authored Jun 24, 2020
1 parent e19e9fa commit 201181c
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions runtime/libs/misc/include/misc/RandomGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class RandomGenerator
template <> uint8_t RandomGenerator::generate<uint8_t>(void);
template <> bool RandomGenerator::generate<bool>(void);
template <> int32_t RandomGenerator::generate<int32_t>(void);
template <> int64_t RandomGenerator::generate<int64_t>(void);

} // namespace misc
} // namespace nnfw
Expand Down
12 changes: 12 additions & 0 deletions runtime/libs/misc/src/RandomGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,17 @@ template <> int32_t RandomGenerator::generate<int32_t>(void)
return dist(_rand);
}

template <> int64_t RandomGenerator::generate<int64_t>(void)
{
// Instead of INT_MAX, 99 is chosen because int64_t input does not mean
// that the model can have any value in int64_t can hold.
// For example, one_hot operation gets indices as int64_t tensor.
// However, we usually expect it would hold a value in [0..depth).
// In our given model, depth was 10137.
const int64_t int64_random_max = 99;
std::uniform_int_distribution<> dist(0, int64_random_max);
return dist(_rand);
}

} // namespace misc
} // namespace nnfw
5 changes: 5 additions & 0 deletions runtime/onert/api/include/nnfw.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ typedef enum {
NNFW_TYPE_TENSOR_QUANT8_ASYMM = 2,
/** A tensor of boolean */
NNFW_TYPE_TENSOR_BOOL = 3,

/** A tensor of 8 bit unsigned integer */
NNFW_TYPE_TENSOR_UINT8 = 4,

/** A tensor of 64 bit signed integer */
NNFW_TYPE_TENSOR_INT64 = 5,

} NNFW_TYPE;

/**
Expand Down
1 change: 1 addition & 0 deletions runtime/onert/api/src/nnfw_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ STATIC_ASSERT_ENUM_CHECK(NNFW_TYPE_TENSOR_INT32, 1);
STATIC_ASSERT_ENUM_CHECK(NNFW_TYPE_TENSOR_QUANT8_ASYMM, 2);
STATIC_ASSERT_ENUM_CHECK(NNFW_TYPE_TENSOR_BOOL, 3);
STATIC_ASSERT_ENUM_CHECK(NNFW_TYPE_TENSOR_UINT8, 4);
STATIC_ASSERT_ENUM_CHECK(NNFW_TYPE_TENSOR_INT64, 5);
STATIC_ASSERT_ENUM_CHECK(NNFW_STATUS_NO_ERROR, 0);
STATIC_ASSERT_ENUM_CHECK(NNFW_STATUS_ERROR, 1);
STATIC_ASSERT_ENUM_CHECK(NNFW_LAYOUT_NONE, 0);
Expand Down
2 changes: 2 additions & 0 deletions runtime/onert/api/src/nnfw_api_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ static NNFW_TYPE datatype_to_nnfw_dtype(onert::ir::DataType dt)
return NNFW_TYPE_TENSOR_BOOL;
case DataType::UINT8:
return NNFW_TYPE_TENSOR_UINT8;
case DataType::INT64:
return NNFW_TYPE_TENSOR_INT64;
case DataType::UINT32:
case DataType::QUANT_INT8_SYMM:
default:
Expand Down
3 changes: 3 additions & 0 deletions runtime/onert/backend/cpu/ops/OperationUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ uint32_t sizeOfData(OperandType type, const std::vector<uint32_t> &dimensions)
case OperandType::QUANT_INT8_SYMM:
size = 1;
break;
case OperandType::INT64:
size = 8;
break;
default:
throw std::runtime_error("Not supported operand type.");
break;
Expand Down
13 changes: 13 additions & 0 deletions tests/tools/nnpackage_run/src/h5formatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ void H5Formatter::loadInputs(const std::string &filename, std::vector<Allocation
else
throw std::runtime_error("model input type is i32. But h5 data type is different.");
break;
case NNFW_TYPE_TENSOR_INT64:
if (type == H5::PredType::STD_I64BE || type == H5::PredType::STD_I64LE)
data_set.read(inputs[i].data(), H5::PredType::NATIVE_INT64);
else
throw std::runtime_error("model input type is i64. But h5 data type is different.");
break;
case NNFW_TYPE_TENSOR_QUANT8_ASYMM:
case NNFW_TYPE_TENSOR_BOOL:
case NNFW_TYPE_TENSOR_UINT8:
Expand Down Expand Up @@ -134,6 +140,13 @@ void H5Formatter::dumpOutputs(const std::string &filename, std::vector<Allocatio
data_set.write(outputs[i].data(), H5::PredType::NATIVE_INT32);
break;
}
case NNFW_TYPE_TENSOR_INT64:
{
H5::DataSet data_set =
value_group.createDataSet(std::to_string(i), H5::PredType::STD_I64LE, data_space);
data_set.write(outputs[i].data(), H5::PredType::NATIVE_INT64);
break;
}
case NNFW_TYPE_TENSOR_QUANT8_ASYMM:
{
H5::DataSet data_set =
Expand Down
2 changes: 2 additions & 0 deletions tests/tools/nnpackage_run/src/nnfw_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ uint64_t bufsize_for(const nnfw_tensorinfo *ti)
sizeof(uint8_t), /* NNFW_TYPE_TENSOR_QUANT8_ASYMM */
sizeof(bool), /* NNFW_TYPE_TENSOR_BOOL = 3 */
sizeof(uint8_t), /* NNFW_TYPE_TENSOR_UINT8 = 4 */
sizeof(int64_t), /* NNFW_TYPE_TENSOR_INT64 = 5 */

};
return elmsize[ti->dtype] * num_elems(ti);
}
Expand Down
9 changes: 7 additions & 2 deletions tests/tools/nnpackage_run/src/nnpackage_run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ int main(const int argc, char **argv)
{
nnfw_tensorinfo ti;
NNPR_ENSURE_STATUS(nnfw_input_tensorinfo(session, i, &ti));
if (ti.dtype < NNFW_TYPE_TENSOR_FLOAT32 || ti.dtype > NNFW_TYPE_TENSOR_UINT8)

if (ti.dtype < NNFW_TYPE_TENSOR_FLOAT32 || ti.dtype > NNFW_TYPE_TENSOR_INT64)
{
std::cerr << "E: not supported input type" << std::endl;
exit(-1);
Expand All @@ -136,7 +137,8 @@ int main(const int argc, char **argv)
{
nnfw_tensorinfo ti;
NNPR_ENSURE_STATUS(nnfw_output_tensorinfo(session, i, &ti));
if (ti.dtype < NNFW_TYPE_TENSOR_FLOAT32 || ti.dtype > NNFW_TYPE_TENSOR_UINT8)

if (ti.dtype < NNFW_TYPE_TENSOR_FLOAT32 || ti.dtype > NNFW_TYPE_TENSOR_INT64)
{
std::cerr << "E: not supported output type" << std::endl;
exit(-1);
Expand Down Expand Up @@ -206,6 +208,9 @@ int main(const int argc, char **argv)
case NNFW_TYPE_TENSOR_INT32:
randomData<int32_t>(randgen, inputs[i].data(), num_elems(&ti));
break;
case NNFW_TYPE_TENSOR_INT64:
randomData<int64_t>(randgen, inputs[i].data(), num_elems(&ti));
break;
default:
std::cerr << "Not supported input type" << std::endl;
std::exit(-1);
Expand Down

0 comments on commit 201181c

Please sign in to comment.