From 485a11d8cb67c8062c632f0987cd31cedbe93d6d Mon Sep 17 00:00:00 2001 From: James Ward Date: Fri, 5 Aug 2022 13:48:37 +0100 Subject: FP16 support in serialization * Allow serialization of fp16 data * Add package to support integrated half data-type (half_float::half), independent of native float: http://half.sourceforge.net/ * Allow passing of accumulate data-type in serialization Signed-off-by: James Ward Change-Id: I54357f02e3776d81958228f699ea5044f2014f4b --- include/attribute.def | 25 +++++---- include/attribute.h | 2 + include/numpy_utils.h | 7 +++ include/tosa_generated.h | 101 +++++++++++++++++++++++++++-------- include/tosa_serialization_handler.h | 2 + 5 files changed, 106 insertions(+), 31 deletions(-) (limited to 'include') diff --git a/include/attribute.def b/include/attribute.def index b40a77b..ebbf024 100644 --- a/include/attribute.def +++ b/include/attribute.def @@ -26,26 +26,29 @@ ...: variadic variables for more arguments, depending on NUM_ARGS_IN_ATTRIBUTES */ -DEF_ATTRIBUTE(Pool, 5, +DEF_ATTRIBUTE(Pool, 6, int32_t, V, pad, int32_t, V, kernel, int32_t, V, stride, int32_t, S, input_zp, - int32_t, S, output_zp) + int32_t, S, output_zp, + DType, S, accum_dtype) -DEF_ATTRIBUTE(Conv, 5, +DEF_ATTRIBUTE(Conv, 6, int32_t, V, pad, int32_t, V, stride, int32_t, V, dilation, int32_t, S, input_zp, - int32_t, S, weight_zp) + int32_t, S, weight_zp, + DType, S, accum_dtype) -DEF_ATTRIBUTE(TransposeConv, 5, +DEF_ATTRIBUTE(TransposeConv, 6, int32_t, V, out_pad, int32_t, V, stride, int32_t, V, output_shape, int32_t, S, input_zp, - int32_t, S, weight_zp) + int32_t, S, weight_zp, + DType, S, accum_dtype) DEF_ATTRIBUTE(Pad, 3, int32_t, V, padding, @@ -106,13 +109,15 @@ DEF_ATTRIBUTE(Transpose, 1, DEF_ATTRIBUTE(Table, 1, int16_t, V, table) -DEF_ATTRIBUTE(MatMul, 2, +DEF_ATTRIBUTE(MatMul, 3, int32_t, S, a_zp, - int32_t, S, b_zp) + int32_t, S, b_zp, + DType, S, accum_dtype) -DEF_ATTRIBUTE(FullyConnected, 2, +DEF_ATTRIBUTE(FullyConnected, 3, int32_t, S, input_zp, - int32_t, S, weight_zp) + int32_t, S, weight_zp, + DType, S, accum_dtype) DEF_ATTRIBUTE(Negate, 2, int32_t, S, input1_zp, diff --git a/include/attribute.h b/include/attribute.h index 93f7bc4..1178ee4 100644 --- a/include/attribute.h +++ b/include/attribute.h @@ -47,6 +47,7 @@ public: #define DEF_ARGS_VER0_S_float(V) DEF_ARGS_VER0_S_DEFAULT(V) #define DEF_ARGS_VER0_S_bool(V) DEF_ARGS_VER0_S_DEFAULT(V) #define DEF_ARGS_VER0_S_ResizeMode(V) DEF_ARGS_VER0_S_DEFAULT(V) +#define DEF_ARGS_VER0_S_DType(V) DEF_ARGS_VER0_S_DEFAULT(V) #define DEF_ARGS_VER0_S_string(V) DEF_ARGS_VER0_S_STR(V) #define DEF_ARGS_VER0_S(T, V) DEF_ARGS_VER0_S_##T(V) @@ -153,6 +154,7 @@ public: #undef DEF_ARGS_VER0_S_float #undef DEF_ARGS_VER0_S_bool #undef DEF_ARGS_VER0_S_ResizeMode +#undef DEF_ARGS_VER0_S_DType #undef DEF_ARGS_VER0_S_string #undef DEF_ARGS_VER0_S_STR #undef DEF_ARGS_VER0_S_DEFAULT diff --git a/include/numpy_utils.h b/include/numpy_utils.h index c64bc17..6a20eb3 100644 --- a/include/numpy_utils.h +++ b/include/numpy_utils.h @@ -24,6 +24,8 @@ #include #include +#include "half.hpp" + class NumpyUtilities { public: @@ -39,6 +41,8 @@ public: static NPError readFromNpyFile(const char* filename, const uint32_t elems, float* databuf); + static NPError readFromNpyFile(const char* filename, const uint32_t elems, half_float::half* 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); @@ -49,6 +53,9 @@ public: static NPError writeToNpyFile(const char* filename, const uint32_t elems, const bool* databuf); + static NPError + writeToNpyFile(const char* filename, const std::vector& shape, const half_float::half* databuf); + static NPError writeToNpyFile(const char* filename, const std::vector& shape, const int32_t* databuf); static NPError writeToNpyFile(const char* filename, const uint32_t elems, const int32_t* databuf); diff --git a/include/tosa_generated.h b/include/tosa_generated.h index b54a324..f0d04d0 100644 --- a/include/tosa_generated.h +++ b/include/tosa_generated.h @@ -94,11 +94,12 @@ enum DType : uint32_t { DType_INT48 = 7, DType_FLOAT = 8, DType_UINT16 = 9, + DType_FP16 = 10, DType_MIN = DType_UNKNOWN, - DType_MAX = DType_UINT16 + DType_MAX = DType_FP16 }; -inline const DType (&EnumValuesDType())[10] { +inline const DType (&EnumValuesDType())[11] { static const DType values[] = { DType_UNKNOWN, DType_BOOL, @@ -109,13 +110,14 @@ inline const DType (&EnumValuesDType())[10] { DType_INT32, DType_INT48, DType_FLOAT, - DType_UINT16 + DType_UINT16, + DType_FP16 }; return values; } inline const char * const *EnumNamesDType() { - static const char * const names[11] = { + static const char * const names[12] = { "UNKNOWN", "BOOL", "UINT8", @@ -126,13 +128,14 @@ inline const char * const *EnumNamesDType() { "INT48", "FLOAT", "UINT16", + "FP16", nullptr }; return names; } inline const char *EnumNameDType(DType e) { - if (flatbuffers::IsOutRange(e, DType_UNKNOWN, DType_UINT16)) return ""; + if (flatbuffers::IsOutRange(e, DType_UNKNOWN, DType_FP16)) return ""; const size_t index = static_cast(e); return EnumNamesDType()[index]; } @@ -582,7 +585,8 @@ struct PoolAttribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VT_KERNEL = 6, VT_STRIDE = 8, VT_INPUT_ZP = 10, - VT_OUTPUT_ZP = 12 + VT_OUTPUT_ZP = 12, + VT_ACCUM_DTYPE = 14 }; const flatbuffers::Vector *pad() const { return GetPointer *>(VT_PAD); @@ -599,6 +603,9 @@ struct PoolAttribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { int32_t output_zp() const { return GetField(VT_OUTPUT_ZP, 0); } + tosa::DType accum_dtype() const { + return static_cast(GetField(VT_ACCUM_DTYPE, 0)); + } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyOffset(verifier, VT_PAD) && @@ -609,6 +616,7 @@ struct PoolAttribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { verifier.VerifyVector(stride()) && VerifyField(verifier, VT_INPUT_ZP, 4) && VerifyField(verifier, VT_OUTPUT_ZP, 4) && + VerifyField(verifier, VT_ACCUM_DTYPE, 4) && verifier.EndTable(); } }; @@ -632,6 +640,9 @@ struct PoolAttributeBuilder { void add_output_zp(int32_t output_zp) { fbb_.AddElement(PoolAttribute::VT_OUTPUT_ZP, output_zp, 0); } + void add_accum_dtype(tosa::DType accum_dtype) { + fbb_.AddElement(PoolAttribute::VT_ACCUM_DTYPE, static_cast(accum_dtype), 0); + } explicit PoolAttributeBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); @@ -649,8 +660,10 @@ inline flatbuffers::Offset CreatePoolAttribute( flatbuffers::Offset> kernel = 0, flatbuffers::Offset> stride = 0, int32_t input_zp = 0, - int32_t output_zp = 0) { + int32_t output_zp = 0, + tosa::DType accum_dtype = tosa::DType_UNKNOWN) { PoolAttributeBuilder builder_(_fbb); + builder_.add_accum_dtype(accum_dtype); builder_.add_output_zp(output_zp); builder_.add_input_zp(input_zp); builder_.add_stride(stride); @@ -665,7 +678,8 @@ inline flatbuffers::Offset CreatePoolAttributeDirect( const std::vector *kernel = nullptr, const std::vector *stride = nullptr, int32_t input_zp = 0, - int32_t output_zp = 0) { + int32_t output_zp = 0, + tosa::DType accum_dtype = tosa::DType_UNKNOWN) { auto pad__ = pad ? _fbb.CreateVector(*pad) : 0; auto kernel__ = kernel ? _fbb.CreateVector(*kernel) : 0; auto stride__ = stride ? _fbb.CreateVector(*stride) : 0; @@ -675,7 +689,8 @@ inline flatbuffers::Offset CreatePoolAttributeDirect( kernel__, stride__, input_zp, - output_zp); + output_zp, + accum_dtype); } struct ConvAttribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { @@ -685,7 +700,8 @@ struct ConvAttribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VT_STRIDE = 6, VT_DILATION = 8, VT_INPUT_ZP = 10, - VT_WEIGHT_ZP = 12 + VT_WEIGHT_ZP = 12, + VT_ACCUM_DTYPE = 14 }; const flatbuffers::Vector *pad() const { return GetPointer *>(VT_PAD); @@ -702,6 +718,9 @@ struct ConvAttribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { int32_t weight_zp() const { return GetField(VT_WEIGHT_ZP, 0); } + tosa::DType accum_dtype() const { + return static_cast(GetField(VT_ACCUM_DTYPE, 0)); + } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyOffset(verifier, VT_PAD) && @@ -712,6 +731,7 @@ struct ConvAttribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { verifier.VerifyVector(dilation()) && VerifyField(verifier, VT_INPUT_ZP, 4) && VerifyField(verifier, VT_WEIGHT_ZP, 4) && + VerifyField(verifier, VT_ACCUM_DTYPE, 4) && verifier.EndTable(); } }; @@ -735,6 +755,9 @@ struct ConvAttributeBuilder { void add_weight_zp(int32_t weight_zp) { fbb_.AddElement(ConvAttribute::VT_WEIGHT_ZP, weight_zp, 0); } + void add_accum_dtype(tosa::DType accum_dtype) { + fbb_.AddElement(ConvAttribute::VT_ACCUM_DTYPE, static_cast(accum_dtype), 0); + } explicit ConvAttributeBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); @@ -752,8 +775,10 @@ inline flatbuffers::Offset CreateConvAttribute( flatbuffers::Offset> stride = 0, flatbuffers::Offset> dilation = 0, int32_t input_zp = 0, - int32_t weight_zp = 0) { + int32_t weight_zp = 0, + tosa::DType accum_dtype = tosa::DType_UNKNOWN) { ConvAttributeBuilder builder_(_fbb); + builder_.add_accum_dtype(accum_dtype); builder_.add_weight_zp(weight_zp); builder_.add_input_zp(input_zp); builder_.add_dilation(dilation); @@ -768,7 +793,8 @@ inline flatbuffers::Offset CreateConvAttributeDirect( const std::vector *stride = nullptr, const std::vector *dilation = nullptr, int32_t input_zp = 0, - int32_t weight_zp = 0) { + int32_t weight_zp = 0, + tosa::DType accum_dtype = tosa::DType_UNKNOWN) { auto pad__ = pad ? _fbb.CreateVector(*pad) : 0; auto stride__ = stride ? _fbb.CreateVector(*stride) : 0; auto dilation__ = dilation ? _fbb.CreateVector(*dilation) : 0; @@ -778,7 +804,8 @@ inline flatbuffers::Offset CreateConvAttributeDirect( stride__, dilation__, input_zp, - weight_zp); + weight_zp, + accum_dtype); } struct TransposeConvAttribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { @@ -788,7 +815,8 @@ struct TransposeConvAttribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Tab VT_STRIDE = 6, VT_OUTPUT_SHAPE = 8, VT_INPUT_ZP = 10, - VT_WEIGHT_ZP = 12 + VT_WEIGHT_ZP = 12, + VT_ACCUM_DTYPE = 14 }; const flatbuffers::Vector *out_pad() const { return GetPointer *>(VT_OUT_PAD); @@ -805,6 +833,9 @@ struct TransposeConvAttribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Tab int32_t weight_zp() const { return GetField(VT_WEIGHT_ZP, 0); } + tosa::DType accum_dtype() const { + return static_cast(GetField(VT_ACCUM_DTYPE, 0)); + } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyOffset(verifier, VT_OUT_PAD) && @@ -815,6 +846,7 @@ struct TransposeConvAttribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Tab verifier.VerifyVector(output_shape()) && VerifyField(verifier, VT_INPUT_ZP, 4) && VerifyField(verifier, VT_WEIGHT_ZP, 4) && + VerifyField(verifier, VT_ACCUM_DTYPE, 4) && verifier.EndTable(); } }; @@ -838,6 +870,9 @@ struct TransposeConvAttributeBuilder { void add_weight_zp(int32_t weight_zp) { fbb_.AddElement(TransposeConvAttribute::VT_WEIGHT_ZP, weight_zp, 0); } + void add_accum_dtype(tosa::DType accum_dtype) { + fbb_.AddElement(TransposeConvAttribute::VT_ACCUM_DTYPE, static_cast(accum_dtype), 0); + } explicit TransposeConvAttributeBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); @@ -855,8 +890,10 @@ inline flatbuffers::Offset CreateTransposeConvAttribute( flatbuffers::Offset> stride = 0, flatbuffers::Offset> output_shape = 0, int32_t input_zp = 0, - int32_t weight_zp = 0) { + int32_t weight_zp = 0, + tosa::DType accum_dtype = tosa::DType_UNKNOWN) { TransposeConvAttributeBuilder builder_(_fbb); + builder_.add_accum_dtype(accum_dtype); builder_.add_weight_zp(weight_zp); builder_.add_input_zp(input_zp); builder_.add_output_shape(output_shape); @@ -871,7 +908,8 @@ inline flatbuffers::Offset CreateTransposeConvAttributeD const std::vector *stride = nullptr, const std::vector *output_shape = nullptr, int32_t input_zp = 0, - int32_t weight_zp = 0) { + int32_t weight_zp = 0, + tosa::DType accum_dtype = tosa::DType_UNKNOWN) { auto out_pad__ = out_pad ? _fbb.CreateVector(*out_pad) : 0; auto stride__ = stride ? _fbb.CreateVector(*stride) : 0; auto output_shape__ = output_shape ? _fbb.CreateVector(*output_shape) : 0; @@ -881,7 +919,8 @@ inline flatbuffers::Offset CreateTransposeConvAttributeD stride__, output_shape__, input_zp, - weight_zp); + weight_zp, + accum_dtype); } struct PadAttribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { @@ -1772,7 +1811,8 @@ struct MatMulAttribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef MatMulAttributeBuilder Builder; enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { VT_A_ZP = 4, - VT_B_ZP = 6 + VT_B_ZP = 6, + VT_ACCUM_DTYPE = 8 }; int32_t a_zp() const { return GetField(VT_A_ZP, 0); @@ -1780,10 +1820,14 @@ struct MatMulAttribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { int32_t b_zp() const { return GetField(VT_B_ZP, 0); } + tosa::DType accum_dtype() const { + return static_cast(GetField(VT_ACCUM_DTYPE, 0)); + } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, VT_A_ZP, 4) && VerifyField(verifier, VT_B_ZP, 4) && + VerifyField(verifier, VT_ACCUM_DTYPE, 4) && verifier.EndTable(); } }; @@ -1798,6 +1842,9 @@ struct MatMulAttributeBuilder { void add_b_zp(int32_t b_zp) { fbb_.AddElement(MatMulAttribute::VT_B_ZP, b_zp, 0); } + void add_accum_dtype(tosa::DType accum_dtype) { + fbb_.AddElement(MatMulAttribute::VT_ACCUM_DTYPE, static_cast(accum_dtype), 0); + } explicit MatMulAttributeBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); @@ -1812,8 +1859,10 @@ struct MatMulAttributeBuilder { inline flatbuffers::Offset CreateMatMulAttribute( flatbuffers::FlatBufferBuilder &_fbb, int32_t a_zp = 0, - int32_t b_zp = 0) { + int32_t b_zp = 0, + tosa::DType accum_dtype = tosa::DType_UNKNOWN) { MatMulAttributeBuilder builder_(_fbb); + builder_.add_accum_dtype(accum_dtype); builder_.add_b_zp(b_zp); builder_.add_a_zp(a_zp); return builder_.Finish(); @@ -1823,7 +1872,8 @@ struct FullyConnectedAttribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Ta typedef FullyConnectedAttributeBuilder Builder; enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { VT_INPUT_ZP = 4, - VT_WEIGHT_ZP = 6 + VT_WEIGHT_ZP = 6, + VT_ACCUM_DTYPE = 8 }; int32_t input_zp() const { return GetField(VT_INPUT_ZP, 0); @@ -1831,10 +1881,14 @@ struct FullyConnectedAttribute FLATBUFFERS_FINAL_CLASS : private flatbuffers::Ta int32_t weight_zp() const { return GetField(VT_WEIGHT_ZP, 0); } + tosa::DType accum_dtype() const { + return static_cast(GetField(VT_ACCUM_DTYPE, 0)); + } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, VT_INPUT_ZP, 4) && VerifyField(verifier, VT_WEIGHT_ZP, 4) && + VerifyField(verifier, VT_ACCUM_DTYPE, 4) && verifier.EndTable(); } }; @@ -1849,6 +1903,9 @@ struct FullyConnectedAttributeBuilder { void add_weight_zp(int32_t weight_zp) { fbb_.AddElement(FullyConnectedAttribute::VT_WEIGHT_ZP, weight_zp, 0); } + void add_accum_dtype(tosa::DType accum_dtype) { + fbb_.AddElement(FullyConnectedAttribute::VT_ACCUM_DTYPE, static_cast(accum_dtype), 0); + } explicit FullyConnectedAttributeBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); @@ -1863,8 +1920,10 @@ struct FullyConnectedAttributeBuilder { inline flatbuffers::Offset CreateFullyConnectedAttribute( flatbuffers::FlatBufferBuilder &_fbb, int32_t input_zp = 0, - int32_t weight_zp = 0) { + int32_t weight_zp = 0, + tosa::DType accum_dtype = tosa::DType_UNKNOWN) { FullyConnectedAttributeBuilder builder_(_fbb); + builder_.add_accum_dtype(accum_dtype); builder_.add_weight_zp(weight_zp); builder_.add_input_zp(input_zp); return builder_.Finish(); diff --git a/include/tosa_serialization_handler.h b/include/tosa_serialization_handler.h index 2a992b2..462c7ef 100644 --- a/include/tosa_serialization_handler.h +++ b/include/tosa_serialization_handler.h @@ -294,6 +294,7 @@ public: tosa_err_t LoadFileSchema(const char* schema_filename); // data format conversion. little-endian. + static tosa_err_t ConvertF16toU8(const std::vector& in, std::vector& out); static tosa_err_t ConvertF32toU8(const std::vector& in, std::vector& out); static tosa_err_t ConvertI48toU8(const std::vector& in, std::vector& out); static tosa_err_t ConvertI32toU8(const std::vector& in, std::vector& out); @@ -302,6 +303,7 @@ public: static tosa_err_t ConvertI4toU8(const std::vector& in, std::vector& out); static tosa_err_t ConvertBooltoU8(const std::vector& in, std::vector& out); + static tosa_err_t ConvertU8toF16(const std::vector& in, uint32_t out_size, std::vector& out); static tosa_err_t ConvertU8toF32(const std::vector& in, uint32_t out_size, std::vector& out); static tosa_err_t ConvertU8toI48(const std::vector& in, uint32_t out_size, std::vector& out); static tosa_err_t ConvertU8toI32(const std::vector& in, uint32_t out_size, std::vector& out); -- cgit v1.2.1