From 13a329156f5ae51a0ffffcb0083d807da9e5b19f Mon Sep 17 00:00:00 2001 From: Jerry Ge Date: Mon, 3 Jul 2023 16:36:41 +0000 Subject: Support reading anydtype into a 32-bit buffer Signed-off-by: Jerry Ge Change-Id: Ic6b43539fcb2d75c5614d3addccd24a06e9f2a31 --- include/numpy_utils.h | 12 +--- src/numpy_utils.cpp | 180 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 158 insertions(+), 34 deletions(-) diff --git a/include/numpy_utils.h b/include/numpy_utils.h index 29d7e11..e9c4bb4 100644 --- a/include/numpy_utils.h +++ b/include/numpy_utils.h @@ -1,5 +1,5 @@ -// Copyright (c) 2020-2021, ARM Limited. +// Copyright (c) 2020-2023, ARM Limited. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -37,6 +37,7 @@ public: FILE_TYPE_MISMATCH, HEADER_PARSE_ERROR, BUFFER_SIZE_MISMATCH, + DATA_TYPE_NOT_SUPPORTED, }; static NPError readFromNpyFile(const char* filename, const uint32_t elems, float* databuf); @@ -45,14 +46,6 @@ public: static NPError readFromNpyFile(const char* filename, const uint32_t elems, half_float::half* databuf); - static NPError readFromNpyFile(const char* filename, const uint32_t elems, uint8_t* databuf); - - static NPError readFromNpyFile(const char* filename, const uint32_t elems, int8_t* databuf); - - static NPError readFromNpyFile(const char* filename, const uint32_t elems, uint16_t* databuf); - - static NPError readFromNpyFile(const char* filename, const uint32_t elems, int16_t* databuf); - static NPError readFromNpyFile(const char* filename, const uint32_t elems, int32_t* databuf); static NPError readFromNpyFile(const char* filename, const uint32_t elems, int64_t* databuf); @@ -112,6 +105,7 @@ private: void* databuf, bool bool_translate); static NPError checkNpyHeader(FILE* infile, const uint32_t elems, const char* dtype_str); + static NPError getHeader(FILE* infile, bool& is_signed, int& bit_length, char& byte_order); static NPError writeNpyHeader(FILE* outfile, const std::vector& shape, const char* dtype_str); }; diff --git a/src/numpy_utils.cpp b/src/numpy_utils.cpp index 65d76e3..d31ec1c 100644 --- a/src/numpy_utils.cpp +++ b/src/numpy_utils.cpp @@ -1,5 +1,5 @@ -// Copyright (c) 2020-2021, ARM Limited. +// Copyright (c) 2020-2023, ARM Limited. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,12 +15,14 @@ #include "numpy_utils.h" #include "half.hpp" - +#include // Magic NUMPY header static const char NUMPY_HEADER_STR[] = "\x93NUMPY\x1\x0\x76\x0{"; static const int NUMPY_HEADER_SZ = 128; // Maximum shape dimensions supported static const int NUMPY_MAX_DIMS_SUPPORTED = 10; +// Offset for NUMPY header desc dictionary string +static const int NUMPY_HEADER_DESC_OFFSET = 8; NumpyUtilities::NPError NumpyUtilities::readFromNpyFile(const char* filename, const uint32_t elems, bool* databuf) { @@ -28,34 +30,118 @@ NumpyUtilities::NPError NumpyUtilities::readFromNpyFile(const char* filename, co return readFromNpyFileCommon(filename, dtype_str, 1, elems, databuf, true); } -NumpyUtilities::NPError NumpyUtilities::readFromNpyFile(const char* filename, const uint32_t elems, uint8_t* databuf) +NumpyUtilities::NPError NumpyUtilities::readFromNpyFile(const char* filename, const uint32_t elems, int32_t* databuf) { - const char dtype_str[] = "'|u1'"; - return readFromNpyFileCommon(filename, dtype_str, sizeof(uint8_t), elems, databuf, false); -} + const char dtype_str_uint8[] = "'|u1'"; + const char dtype_str_int8[] = "'|i1'"; + const char dtype_str_uint16[] = "'