// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "contrib_ops/cpu/utils/dump_tensor.h" #include #include #include #include #include "core/framework/print_tensor_utils.h" #include "contrib_ops/cpu/utils/debug_macros.h" #include "core/platform/env_var_utils.h" namespace onnxruntime { namespace contrib { #if DUMP_CPU_TENSOR_LEVEL > 0 // Environment variable to enable/disable dumping constexpr const char* kEnableCpuTensorDumper = "ORT_ENABLE_CPU_DUMP"; // Environment variable to enable/disable dumping thread id constexpr const char* kDumpThreadId = "ORT_DUMP_THREAD_ID"; // To avoid dumping at the same time from multiple threads static std::mutex s_mutex; static bool s_output_thread_id = false; template void DumpCpuTensor(const char* name, const T* tensor, int dim0, int dim1) { std::unique_lock lock(s_mutex); if (s_output_thread_id) std::cout << "Thread ID:" << std::this_thread::get_id() << std::endl; if (nullptr != name) { std::cout << std::string(name) << std::endl; } if (onnxruntime::utils::kDefaultSnippetThreshold < static_cast(dim0 * dim1)) { onnxruntime::utils::PrintCpuTensorSnippet(tensor, dim0, dim1, onnxruntime::utils::kDefaultSnippetEdgeItems); } else { onnxruntime::utils::PrintCpuTensorFull(tensor, dim0, dim1); } } template void DumpCpuTensor(const char* name, const T* tensor, int dim0, int dim1, int dim2) { std::unique_lock lock(s_mutex); if (s_output_thread_id) std::cout << "Thread ID:" << std::this_thread::get_id() << std::endl; if (nullptr != name) { std::cout << std::string(name) << std::endl; } if (onnxruntime::utils::kDefaultSnippetThreshold < static_cast(dim0 * dim1 * dim2)) { onnxruntime::utils::PrintCpuTensorSnippet(tensor, dim0, dim1, dim2, onnxruntime::utils::kDefaultSnippetEdgeItems); } else { onnxruntime::utils::PrintCpuTensorFull(tensor, dim0, dim1, dim2); } } template void DumpCpuTensor(const char* name, const T* tensor, int dim0, int dim1, int dim2, int dim3) { std::unique_lock lock(s_mutex); if (s_output_thread_id) std::cout << "Thread ID:" << std::this_thread::get_id() << std::endl; if (nullptr != name) { std::cout << std::string(name) << std::endl; } if (onnxruntime::utils::kDefaultSnippetThreshold < static_cast(dim0 * dim1 * dim2 * dim3)) { for (int i = 0; i < dim0; i++) { std::cout << "[" << i << "]:" << std::endl; onnxruntime::utils::PrintCpuTensorSnippet(tensor + i * dim1 * dim2 * dim3, dim1, dim2, dim3, onnxruntime::utils::kDefaultSnippetEdgeItems); } } else { for (int i = 0; i < dim0; i++) { std::cout << "[" << i << "]:" << std::endl; onnxruntime::utils::PrintCpuTensorFull(tensor + i * dim1 * dim2 * dim3, dim1, dim2, dim3); } } } void DumpCpuTensor(const char* name, const Tensor& tensor, int dim0, int dim1, int dim2, int dim3) { MLDataType dataType = tensor.DataType(); if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1, dim2, dim3); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1, dim2, dim3); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1, dim2, dim3); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1, dim2, dim3); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1, dim2, dim3); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1, dim2, dim3); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1, dim2, dim3); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1, dim2, dim3); } else { assert(0); } } void DumpCpuTensor(const char* name, const Tensor& tensor, int dim0, int dim1, int dim2) { MLDataType dataType = tensor.DataType(); if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1, dim2); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1, dim2); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1, dim2); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1, dim2); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1, dim2); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1, dim2); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1, dim2); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1, dim2); } else { assert(0); } } void DumpCpuTensor(const char* name, const Tensor& tensor, int dim0, int dim1) { MLDataType dataType = tensor.DataType(); if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1); } else if (dataType == DataTypeImpl::GetType()) { DumpCpuTensor(name, tensor.Data(), dim0, dim1); } else { assert(0); } } void DumpCpuTensor(const char* name, const Tensor& tensor, int dim0) { DumpCpuTensor(name, tensor, 1, dim0); } void DumpCpuTensor(const char* name, const Tensor& tensor) { const auto& shape = tensor.Shape(); if (nullptr != name) { std::cout << std::string(name) << std::endl; } std::cout << "Shape:" << shape << std::endl; size_t num_dims = shape.NumDimensions(); if (num_dims >= 4) { int dim0 = static_cast(shape.SizeToDimension(num_dims - 4)); int dim1 = static_cast(shape[num_dims - 3]); int dim2 = static_cast(shape[num_dims - 2]); int dim3 = static_cast(shape[num_dims - 1]); DumpCpuTensor(nullptr, tensor, dim0, dim1, dim2, dim3); return; } if (num_dims == 3) { int dim0 = static_cast(shape[0]); int dim1 = static_cast(shape[1]); int dim2 = static_cast(shape[2]); DumpCpuTensor(nullptr, tensor, dim0, dim1, dim2); return; } if (num_dims == 2) { int dim0 = static_cast(shape[0]); int dim1 = static_cast(shape[1]); DumpCpuTensor(nullptr, tensor, dim0, dim1); return; } if (num_dims == 1) { DumpCpuTensor(nullptr, tensor, static_cast(shape[0])); } } CpuTensorConsoleDumper::CpuTensorConsoleDumper() { is_enabled_ = ParseEnvironmentVariableWithDefault(kEnableCpuTensorDumper, 1) != 0; s_output_thread_id = ParseEnvironmentVariableWithDefault(kDumpThreadId, 0) != 0; } void CpuTensorConsoleDumper::Print(const std::string& value) const { if (!is_enabled_) return; std::unique_lock lock(s_mutex); if (s_output_thread_id) std::cout << "Thread ID:" << std::this_thread::get_id() << std::endl; std::cout << value << std::endl; } void CpuTensorConsoleDumper::Print(const char* name, const Tensor& tensor) const { if (!is_enabled_) return; DumpCpuTensor(name, tensor); } void CpuTensorConsoleDumper::Print(const char* name, const OrtValue& value) const { const Tensor& tensor = value.Get(); Print(name, tensor); } #define TENSOR_DUMPER_PRINT_TYPE(dtype) \ void CpuTensorConsoleDumper::Print(const char* name, const dtype* tensor, int dim0, int dim1) const { \ if (is_enabled_) \ DumpCpuTensor(name, tensor, dim0, dim1); \ } \ void CpuTensorConsoleDumper::Print(const char* name, const dtype* tensor, int dim0, int dim1, int dim2) const { \ if (is_enabled_) \ DumpCpuTensor(name, tensor, dim0, dim1, dim2); \ } \ void CpuTensorConsoleDumper::Print(const char* name, const dtype* tensor, int dim0, int dim1, int dim2, int dim3) const { \ if (is_enabled_) \ DumpCpuTensor(name, tensor, dim0, dim1, dim2, dim3); \ } \ void CpuTensorConsoleDumper::Print(const char* name, const dtype* tensor, gsl::span& dims) const { \ PrintTensorByDims(this, name, tensor, dims); \ } TENSOR_DUMPER_PRINT_TYPE(int8_t) TENSOR_DUMPER_PRINT_TYPE(uint8_t) TENSOR_DUMPER_PRINT_TYPE(int32_t) TENSOR_DUMPER_PRINT_TYPE(int64_t) TENSOR_DUMPER_PRINT_TYPE(float) TENSOR_DUMPER_PRINT_TYPE(MLFloat16) TENSOR_DUMPER_PRINT_TYPE(BFloat16) TENSOR_DUMPER_PRINT_TYPE(UInt4x2) TENSOR_DUMPER_PRINT_TYPE(Int4x2) #undef TENSOR_DUMPER_PRINT_TYPE #else CpuTensorConsoleDumper::CpuTensorConsoleDumper() { } void CpuTensorConsoleDumper::Print(const std::string&) const { } void CpuTensorConsoleDumper::Print(const char*, const Tensor&) const { } void CpuTensorConsoleDumper::Print(const char*, const OrtValue&) const { } #define TENSOR_DUMPER_PRINT_TYPE(dtype) \ void CpuTensorConsoleDumper::Print(const char*, const dtype*, int, int) const { \ } \ void CpuTensorConsoleDumper::Print(const char*, const dtype*, int, int, int) const { \ } \ void CpuTensorConsoleDumper::Print(const char*, const dtype*, int, int, int, int) const { \ } \ void CpuTensorConsoleDumper::Print(const char*, const dtype*, gsl::span&) const { \ } TENSOR_DUMPER_PRINT_TYPE(int8_t) TENSOR_DUMPER_PRINT_TYPE(uint8_t) TENSOR_DUMPER_PRINT_TYPE(int32_t) TENSOR_DUMPER_PRINT_TYPE(int64_t) TENSOR_DUMPER_PRINT_TYPE(float) TENSOR_DUMPER_PRINT_TYPE(MLFloat16) TENSOR_DUMPER_PRINT_TYPE(BFloat16) TENSOR_DUMPER_PRINT_TYPE(UInt4x2) TENSOR_DUMPER_PRINT_TYPE(Int4x2) #undef TENSOR_DUMPER_PRINT_TYPE #endif } // namespace contrib } // namespace onnxruntime