/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /*! * \file src/runtime/debug.cc * \brief Helpers for debugging at runtime. */ #include namespace tvm { namespace runtime { template void AppendMembers(std::ostream& os, const NDArray& nd_array, int64_t dim0) { os << "=["; for (int64_t i = 0; i < dim0; ++i) { if (i > 0) { os << ","; } os << reinterpret_cast(nd_array->data)[i]; } os << "]"; } void AppendNDArray(std::ostream& os, const NDArray& nd_array, const DLDevice& host_device, bool show_contents) { os << "NDArray["; os << "("; for (int dim = 0; dim < nd_array->ndim; ++dim) { if (dim > 0) { os << ","; } os << nd_array->shape[dim]; } std::string basic_type = DLDataType2String(nd_array->dtype); os << ")," << basic_type; os << ",(" << nd_array->device.device_type; os << "," << nd_array->device.device_id; os << ")]"; if (show_contents && nd_array->device.device_type == host_device.device_type && nd_array->device.device_id == host_device.device_id) { int64_t dim0; if (nd_array->ndim == 0) { dim0 = 1; } else if (nd_array->ndim == 1) { dim0 = nd_array->shape[0]; if (dim0 > 10) { // Too large. dim0 = 0; } } else { // Not rank-1. dim0 = 0; } if (dim0 > 0) { if (basic_type == "bool") { AppendMembers(os, nd_array, dim0); } else if (basic_type == "int8") { AppendMembers(os, nd_array, dim0); } else if (basic_type == "int16") { AppendMembers(os, nd_array, dim0); } else if (basic_type == "int32") { AppendMembers(os, nd_array, dim0); } else if (basic_type == "int64") { AppendMembers(os, nd_array, dim0); } else if (basic_type == "uint8") { AppendMembers(os, nd_array, dim0); } else if (basic_type == "uint16") { AppendMembers(os, nd_array, dim0); } else if (basic_type == "uint32") { AppendMembers(os, nd_array, dim0); } else if (basic_type == "uint64") { AppendMembers(os, nd_array, dim0); } else if (basic_type == "float32") { AppendMembers(os, nd_array, dim0); } else if (basic_type == "float64") { AppendMembers(os, nd_array, dim0); } } } } void AppendADT(std::ostream& os, const ADT& adt, const DLDevice& host_device, bool show_contents) { os << "ADT(" << adt->tag; for (size_t i = 0; i < adt->size; ++i) { os << ","; AppendRuntimeObject(os, adt[i], host_device, show_contents); } os << ")"; } void AppendRuntimeObject(std::ostream& os, const ObjectRef& object, const DLDevice& host_device, bool show_contents) { if (const auto* adt_obj = object.as()) { AppendADT(os, GetRef(adt_obj), host_device, show_contents); } else if (const auto* nd_array_cont = object.as()) { AppendNDArray(os, GetRef(nd_array_cont), host_device, show_contents); } else { os << "?"; } } std::string RuntimeObject2String(const ObjectRef& object, const DLDevice& host_device, bool show_contents) { std::ostringstream os; AppendRuntimeObject(os, object, host_device, show_contents); return os.str(); } } // namespace runtime } // namespace tvm