aboutsummaryrefslogtreecommitdiff
path: root/reference_model
diff options
context:
space:
mode:
authorJames Ward <james.ward@arm.com>2023-01-23 17:13:37 +0000
committerJames Ward <james.ward@arm.com>2023-01-31 11:45:21 +0000
commit736fd1a7e4083153ccc4cf360b44dd07b6788494 (patch)
tree42f34388dddb504650be0e17dbda8c9073223313 /reference_model
parent2138a19ae830ec7d9ce5b15f15cbd7a22864bb8f (diff)
downloadreference_model-736fd1a7e4083153ccc4cf360b44dd07b6788494.tar.gz
Create MI tests for Type Conversion: CAST
* Add exclusion regex's to conformance generation Signed-off-by: James Ward <james.ward@arm.com> Change-Id: I15bef7451efd5662065060242d35bd7fa3381487
Diffstat (limited to 'reference_model')
-rw-r--r--reference_model/src/ops/op_factory.cc7
-rw-r--r--reference_model/src/ops/type_conversion.cc79
-rw-r--r--reference_model/src/ops/type_conversion.h104
3 files changed, 184 insertions, 6 deletions
diff --git a/reference_model/src/ops/op_factory.cc b/reference_model/src/ops/op_factory.cc
index 0d56161..76cf666 100644
--- a/reference_model/src/ops/op_factory.cc
+++ b/reference_model/src/ops/op_factory.cc
@@ -469,26 +469,33 @@ GraphNode* OpFactory::newOp(SubgraphTraverser* sgt,
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, INT16);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, INT32);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, FP16);
+ DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, BF16);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, FP32);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, BOOL);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, INT8);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, INT32);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, FP16);
+ DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, BF16);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, FP32);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, BOOL);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, INT8);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, INT16);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, FP16);
+ DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, BF16);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, FP32);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP16, INT8);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP16, INT16);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP16, INT32);
+ DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP16, FP32);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BF16, INT8);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BF16, INT16);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BF16, INT32);
+ DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BF16, FP32);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, INT8);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, INT16);
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, INT32);
+ DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, FP16);
+ DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, BF16);
break;
case Op_RESCALE:
DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT8, INT8);
diff --git a/reference_model/src/ops/type_conversion.cc b/reference_model/src/ops/type_conversion.cc
index f266675..dbedbad 100644
--- a/reference_model/src/ops/type_conversion.cc
+++ b/reference_model/src/ops/type_conversion.cc
@@ -15,6 +15,7 @@
#include "type_conversion.h"
#include "quant_util.h"
+#include "arith_util.h"
#include "template_types.h"
#include <cmath>
#include "half.hpp"
@@ -307,30 +308,88 @@ CastHelper<DType_BOOL, OutDtype>::CastHelper()
template <DType InDtype>
CastHelper<InDtype, DType_FP16>::CastHelper()
{
+ // Integer data converted to fp16 (stored as fp32)
fcn = [](InEigenType in) -> float {
- half_float::half out = half_float::half_cast<half_float::half, InEigenType>(in); // Cast to half_float
- return half_float::half_cast<float, half_float::half>(out); // Cast to float (underlying FP16 EigenType)
+ half_float::half h = half_float::half(in);
+ float out = half_float::half_cast<float, half_float::half>(h);
+ return out;
+ };
+}
+
+CastHelper<DType_FP32, DType_FP16>::CastHelper()
+{
+ // fp32 data converted to fp16 (stored as fp32)
+ fcn = [](float in) -> float {
+ float out = fpTrunc<DType_FP16>(in); // truncate required for conversion from higher precision
+ return out;
+ };
+}
+
+template <DType InDtype>
+CastHelper<InDtype, DType_BF16>::CastHelper()
+{
+ // Integer data converted to bf16 (stored as fp32)
+ fcn = [](InEigenType in) -> float {
+ float out = (float)in; // default cast to float is round_to_nearest_float()
+ return out;
+ };
+}
+
+CastHelper<DType_FP32, DType_BF16>::CastHelper()
+{
+ // fp32 data converted to bf16 (stored as fp32)
+ fcn = [](float in) -> float {
+ return fpTrunc<DType_BF16>(in); // truncate required for conversions from higher precision
};
}
template <DType OutDtype>
CastHelper<DType_FP16, OutDtype>::CastHelper()
{
- // Assuming InEigenType = float.
+ // fp16 data (stored as fp32) converted to integer
fcn = [](float in) -> OutEigenType {
- // Perform initial rounding in half-precision then cast back to float
- half_float::half h = half_float::half_cast<half_float::half, float>(in);
+ // Cast from float representation back to half_float before rounding
+ half_float::half h = half_float::half(in);
h = std::round(h);
- OutEigenType out = half_float::half_cast<float, half_float::half>(h);
+ OutEigenType out = half_float::half_cast<OutEigenType, half_float::half>(h);
out = std::max<OutEigenType>(out, OutMin);
out = std::min<OutEigenType>(out, OutMax);
return out;
};
}
+CastHelper<DType_FP16, DType_FP32>::CastHelper()
+{
+ // No-op since fp16 values treated internally as their fp32 representation
+ fcn = [](float in) -> OutEigenType {
+ return in;
+ };
+}
+
+template <DType OutDtype>
+CastHelper<DType_BF16, OutDtype>::CastHelper()
+{
+ // bf16 data (stored as fp32) converted to integer
+ fcn = [](float in) -> OutEigenType {
+ OutEigenType out = std::round(in);
+ out = std::max<OutEigenType>(out, OutMin);
+ out = std::min<OutEigenType>(out, OutMax);
+ return out;
+ };
+}
+
+CastHelper<DType_BF16, DType_FP32>::CastHelper()
+{
+ // No-op since bf16 values treated as truncated fp32 internally
+ fcn = [](InEigenType in) -> OutEigenType {
+ return in;
+ };
+}
+
template <DType InDtype>
CastHelper<InDtype, DType_FP32>::CastHelper()
{
+ // Integer data converted to fp32
fcn = [](InEigenType in) -> float {
float out = (OutEigenType)in; // default cast to float is round_to_nearest_float()
return out;
@@ -340,6 +399,7 @@ CastHelper<InDtype, DType_FP32>::CastHelper()
template <DType OutDtype>
CastHelper<DType_FP32, OutDtype>::CastHelper()
{
+ // fp32 data converted to integer
fcn = [](float in) -> OutEigenType {
OutEigenType out = std::round(in);
out = std::max<OutEigenType>(out, OutMin);
@@ -356,26 +416,33 @@ DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, BOOL);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, INT16);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, INT32);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, FP16);
+DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, BF16);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, FP32);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, BOOL);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, INT8);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, INT32);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, FP16);
+DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, BF16);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, FP32);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, BOOL);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, INT8);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, INT16);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, FP16);
+DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, BF16);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, FP32);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP16, INT8);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP16, INT16);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP16, INT32);
+DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP16, FP32);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BF16, INT8);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BF16, INT16);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BF16, INT32);
+DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BF16, FP32);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, INT8);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, INT16);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, INT32);
+DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, FP16);
+DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, BF16);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT8, INT8);
DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT8, INT16);
diff --git a/reference_model/src/ops/type_conversion.h b/reference_model/src/ops/type_conversion.h
index b0de30c..e2fc6e2 100644
--- a/reference_model/src/ops/type_conversion.h
+++ b/reference_model/src/ops/type_conversion.h
@@ -136,6 +136,76 @@ private:
FcnType fcn;
};
+template <>
+class CastHelper<DType_FP32, DType_FP16>
+{
+public:
+ using InEigenType = typename GetEigenType<DType_FP32>::type;
+ using OutEigenType = typename GetEigenType<DType_FP16>::type;
+ using FcnType = std::function<OutEigenType(InEigenType)>;
+ CastHelper();
+ const FcnType& get_fcn() const
+ {
+ return fcn;
+ }
+
+private:
+ FcnType fcn;
+};
+
+template <DType InDtype>
+class CastHelper<InDtype, DType_BF16>
+{
+public:
+ using InEigenType = typename GetEigenType<InDtype>::type;
+ using OutEigenType = typename GetEigenType<DType_BF16>::type;
+ using FcnType = std::function<OutEigenType(InEigenType)>;
+ CastHelper();
+ const FcnType& get_fcn() const
+ {
+ return fcn;
+ }
+
+private:
+ FcnType fcn;
+};
+
+template <DType OutDtype>
+class CastHelper<DType_BF16, OutDtype>
+{
+public:
+ using InEigenType = typename GetEigenType<DType_BF16>::type;
+ using OutEigenType = typename GetEigenType<OutDtype>::type;
+ using FcnType = std::function<OutEigenType(InEigenType)>;
+ static constexpr int32_t OutMin = GetQMin<OutDtype>::value;
+ static constexpr int32_t OutMax = GetQMax<OutDtype>::value;
+ CastHelper();
+ const FcnType& get_fcn() const
+ {
+ return fcn;
+ }
+
+private:
+ FcnType fcn;
+};
+
+template <>
+class CastHelper<DType_FP32, DType_BF16>
+{
+public:
+ using InEigenType = typename GetEigenType<DType_FP32>::type;
+ using OutEigenType = typename GetEigenType<DType_BF16>::type;
+ using FcnType = std::function<OutEigenType(InEigenType)>;
+ CastHelper();
+ const FcnType& get_fcn() const
+ {
+ return fcn;
+ }
+
+private:
+ FcnType fcn;
+};
+
template <DType InDtype>
class CastHelper<InDtype, DType_FP32>
{
@@ -153,6 +223,40 @@ private:
FcnType fcn;
};
+template <>
+class CastHelper<DType_FP16, DType_FP32>
+{
+public:
+ using InEigenType = typename GetEigenType<DType_FP16>::type;
+ using OutEigenType = typename GetEigenType<DType_FP32>::type;
+ using FcnType = std::function<OutEigenType(InEigenType)>;
+ CastHelper();
+ const FcnType& get_fcn() const
+ {
+ return fcn;
+ }
+
+private:
+ FcnType fcn;
+};
+
+template <>
+class CastHelper<DType_BF16, DType_FP32>
+{
+public:
+ using InEigenType = typename GetEigenType<DType_BF16>::type;
+ using OutEigenType = typename GetEigenType<DType_FP32>::type;
+ using FcnType = std::function<OutEigenType(InEigenType)>;
+ CastHelper();
+ const FcnType& get_fcn() const
+ {
+ return fcn;
+ }
+
+private:
+ FcnType fcn;
+};
+
template <DType OutDtype>
class CastHelper<DType_FP32, OutDtype>
{