// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once #include #include "core/framework/print_tensor_utils.h" #include "core/framework/int2.h" namespace onnxruntime { namespace utils { // Currently we only store statistics data for float tensors that printed to stdout. // It can be extended to other types if needed. struct TensorStatisticsData { bool is_float = false; float float_min; float float_max; }; template int my_fpclassify(const T& val) { return std::fpclassify(val); } template <> int my_fpclassify(const MLFloat16& val) { return std::fpclassify(val.ToFloat()); } template <> int my_fpclassify(const BFloat16& val) { return std::fpclassify(val.ToFloat()); } template void PrintFloatStats(const T* data, size_t count) { size_t inf = 0; size_t nan = 0; size_t zero = 0; size_t subnormal = 0; for (size_t i = 0; i < count; i++) { switch (my_fpclassify(data[i])) { case FP_INFINITE: inf++; break; case FP_NAN: nan++; break; case FP_SUBNORMAL: subnormal++; break; case FP_ZERO: zero++; break; default: break; } } if (inf) std::cout << ",Inf=" << inf; if (nan) std::cout << ",NaN=" << nan; if (zero) std::cout << ",Zero=" << zero; if (subnormal) std::cout << ",SubNormal=" << subnormal; } template void PrintCommonStats(const T* data, size_t count, TensorStatisticsData& tensor_statistics) { T min = data[0]; T max = min; for (size_t i = 1; i < count; i++) { auto value = data[i]; if (value > max) { max = value; } if (value < min) { min = value; } } std::cout << "Min="; PrintValue(min); std::cout << ",Max="; PrintValue(max); // Statistics for float and double only for now. if constexpr (std::is_same::value) { tensor_statistics.is_float = true; tensor_statistics.float_min = static_cast(min); tensor_statistics.float_max = static_cast(max); } } #define DEF_PRINT_COMMON_STATS_PACKED(PACKED_TYPE) \ template <> \ inline void PrintCommonStats( \ const PACKED_TYPE* data, size_t count, TensorStatisticsData&) { \ using UnpackedType = typename PACKED_TYPE::UnpackedType; \ UnpackedType min = data[0].GetElem(0); \ UnpackedType max = min; \ for (size_t i = 1; i < count; i++) { \ auto indices = PACKED_TYPE::GetTensorElemIndices(i); \ auto value = data[indices.first].GetElem(indices.second); \ if (value > max) { \ max = value; \ } \ if (value < min) { \ min = value; \ } \ } \ \ std::cout << "Min="; \ PrintValue(min); \ \ std::cout << ",Max="; \ PrintValue(max); \ } DEF_PRINT_COMMON_STATS_PACKED(Int4x2) DEF_PRINT_COMMON_STATS_PACKED(UInt4x2) #if !defined(DISABLE_FLOAT4_TYPES) DEF_PRINT_COMMON_STATS_PACKED(Float4E2M1x2) #endif DEF_PRINT_COMMON_STATS_PACKED(Int2x4) DEF_PRINT_COMMON_STATS_PACKED(UInt2x4) template void PrintHalfStats(const T* data, size_t count) { float min = data[0].ToFloat(); float max = min; for (size_t i = 1; i < count; i++) { float value = data[i].ToFloat(); if (value > max) { max = value; } if (value < min) { min = value; } } std::cout << "Min="; PrintValue(min); std::cout << ",Max="; PrintValue(max); } template void PrintTensorStats(const T* tensor, size_t count, TensorStatisticsData& tensor_statistics) { PrintCommonStats(tensor, count, tensor_statistics); } template <> void PrintTensorStats(const float* tensor, size_t count, TensorStatisticsData& tensor_statistics) { PrintCommonStats(tensor, count, tensor_statistics); PrintFloatStats(tensor, count); } template <> void PrintTensorStats(const double* tensor, size_t count, TensorStatisticsData& tensor_statistics) { PrintCommonStats(tensor, count, tensor_statistics); PrintFloatStats(tensor, count); } template <> void PrintTensorStats(const MLFloat16* tensor, size_t count, TensorStatisticsData&) { PrintHalfStats(tensor, count); PrintFloatStats(tensor, count); } template <> void PrintTensorStats(const BFloat16* tensor, size_t count, TensorStatisticsData&) { PrintHalfStats(tensor, count); PrintFloatStats(tensor, count); } template void PrintCpuTensorStats(const Tensor& tensor, TensorStatisticsData& tensor_statistics) { const auto& shape = tensor.Shape(); auto num_items = shape.Size(); if (num_items == 0) { return; } const T* data = tensor.Data(); PrintTensorStats(data, num_items, tensor_statistics); std::cout << std::endl; } template <> void PrintCpuTensorStats(const Tensor&, TensorStatisticsData&) { } } // namespace utils } // namespace onnxruntime