From f555b724a8822b57bef2cf542fac862d34b7c5da Mon Sep 17 00:00:00 2001 From: sunjqa1 Date: Wed, 21 Oct 2026 09:06:38 +0000 Subject: [PATCH 1/2] feat(operator): add optional detailed dispatch trace --- src/operator.h | 118 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/src/operator.h b/src/operator.h index 3ec0c6733..0c5826d37 100644 --- a/src/operator.h +++ b/src/operator.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -238,6 +239,121 @@ void TraceOperatorCall(const CacheKey& key, const Config& config) { config.implementation_index()); } +inline bool TraceOperatorCallsDetailedEnabled() { + static const bool enabled = [] { + const char* value = std::getenv("INFINI_OPS_TRACE_CALLS_DETAILED"); + return value != nullptr && value[0] != '\0' && value[0] != '0'; + }(); + return enabled; +} + +// Avoid aborting on unknown dtype/device values while tracing. +inline std::string_view TraceDataTypeDesc(infini::rt::DataType dt) { + using D = infini::rt::DataType; + switch (dt) { + case D::kInt8: + return "int8"; + case D::kInt16: + return "int16"; + case D::kInt32: + return "int32"; + case D::kInt64: + return "int64"; + case D::kUInt8: + return "uint8"; + case D::kUInt16: + return "uint16"; + case D::kUInt32: + return "uint32"; + case D::kUInt64: + return "uint64"; + case D::kFloat16: + return "float16"; + case D::kBFloat16: + return "bfloat16"; + case D::kFloat32: + return "float32"; + case D::kFloat64: + return "float64"; + case D::kBool: + return "bool"; + } + return "unknown"; +} + +inline std::string_view TraceDeviceDesc(infini::rt::Device::Type dt) { + using T = infini::rt::Device::Type; + switch (dt) { + case T::kCpu: + return "cpu"; + case T::kNvidia: + return "nvidia"; + case T::kCambricon: + return "cambricon"; + case T::kAscend: + return "ascend"; + case T::kMetax: + return "metax"; + case T::kMoore: + return "moore"; + case T::kIluvatar: + return "iluvatar"; + case T::kHygon: + return "hygon"; + case T::kThead: + return "thead"; + case T::kMars: + return "mars"; + } + return "unknown"; +} + +// Emits a best-effort detailed trace event at operator call entry. +template +void TraceOperatorCallDetailed(const CacheKey& key, const Config& config) { + if (!TraceOperatorCallsDetailedEnabled()) return; + try { + constexpr auto operator_name = OperatorName(); + const std::size_t n = key.tensors.size(); + + std::string line; + line += "[INFINI_OPS_TRACE_CALLS_DETAILED] {\"operator_name\": \""; + line.append(operator_name.data(), operator_name.size()); + line += "\", \"implementation\": "; + line += std::to_string(config.implementation_index()); + line += ", \"tensors\": ["; + + for (std::size_t i = 0; i < n; ++i) { + const auto& t = key.tensors[i]; + if (i > 0) line += ", "; + line += "{\"index\": "; + line += std::to_string(i); + line += ", \"device\": \""; + const auto dev = TraceDeviceDesc(t.device().type()); + line.append(dev.data(), dev.size()); + line += "\", \"dtype\": \""; + const auto dt = TraceDataTypeDesc(t.dtype()); + line.append(dt.data(), dt.size()); + line += "\", \"shape\": ["; + const std::size_t nd = t.ndim(); + for (std::size_t d = 0; d < nd; ++d) { + if (d > 0) line += ","; + line += std::to_string(static_cast(t.size(d))); + } + line += "], \"strides\": ["; + for (std::size_t d = 0; d < nd; ++d) { + if (d > 0) line += ","; + line += std::to_string(static_cast(t.stride(d))); + } + line += "]}"; + } + line += "]}\n"; + std::fprintf(stderr, "%s", line.c_str()); + } catch (...) { + // Ignore serialization failures so tracing does not alter operator behavior. + } +} + } // namespace infini::ops::detail template <> @@ -450,6 +566,7 @@ class Operator : public OperatorBase { return CacheKeyBuilder{}(*effective_config, args...); }(); detail::TraceOperatorCall(key, *effective_config); + detail::TraceOperatorCallDetailed(key, *effective_config); auto it = [&]() { HostRangeScope host_range_cache_lookup{HostRangeLayer::kCacheLookup}; @@ -465,6 +582,7 @@ class Operator : public OperatorBase { #else auto key = CacheKeyBuilder{}(*effective_config, args...); detail::TraceOperatorCall(key, *effective_config); + detail::TraceOperatorCallDetailed(key, *effective_config); auto it{cache.find(key)}; From 996e3fa4005ce53628e0a704907c3b82bd48f125 Mon Sep 17 00:00:00 2001 From: sunjqa1 Date: Thu, 22 Oct 2026 10:25:21 +0000 Subject: [PATCH 2/2] refactor(operator): reuse InfiniRT metadata descriptions --- src/operator.h | 67 +++----------------------------------------------- 1 file changed, 3 insertions(+), 64 deletions(-) diff --git a/src/operator.h b/src/operator.h index 0c5826d37..f6865fdec 100644 --- a/src/operator.h +++ b/src/operator.h @@ -247,67 +247,6 @@ inline bool TraceOperatorCallsDetailedEnabled() { return enabled; } -// Avoid aborting on unknown dtype/device values while tracing. -inline std::string_view TraceDataTypeDesc(infini::rt::DataType dt) { - using D = infini::rt::DataType; - switch (dt) { - case D::kInt8: - return "int8"; - case D::kInt16: - return "int16"; - case D::kInt32: - return "int32"; - case D::kInt64: - return "int64"; - case D::kUInt8: - return "uint8"; - case D::kUInt16: - return "uint16"; - case D::kUInt32: - return "uint32"; - case D::kUInt64: - return "uint64"; - case D::kFloat16: - return "float16"; - case D::kBFloat16: - return "bfloat16"; - case D::kFloat32: - return "float32"; - case D::kFloat64: - return "float64"; - case D::kBool: - return "bool"; - } - return "unknown"; -} - -inline std::string_view TraceDeviceDesc(infini::rt::Device::Type dt) { - using T = infini::rt::Device::Type; - switch (dt) { - case T::kCpu: - return "cpu"; - case T::kNvidia: - return "nvidia"; - case T::kCambricon: - return "cambricon"; - case T::kAscend: - return "ascend"; - case T::kMetax: - return "metax"; - case T::kMoore: - return "moore"; - case T::kIluvatar: - return "iluvatar"; - case T::kHygon: - return "hygon"; - case T::kThead: - return "thead"; - case T::kMars: - return "mars"; - } - return "unknown"; -} - // Emits a best-effort detailed trace event at operator call entry. template void TraceOperatorCallDetailed(const CacheKey& key, const Config& config) { @@ -329,10 +268,10 @@ void TraceOperatorCallDetailed(const CacheKey& key, const Config& config) { line += "{\"index\": "; line += std::to_string(i); line += ", \"device\": \""; - const auto dev = TraceDeviceDesc(t.device().type()); + const auto dev = Device::StringFromType(t.device().type()); line.append(dev.data(), dev.size()); line += "\", \"dtype\": \""; - const auto dt = TraceDataTypeDesc(t.dtype()); + const auto dt = infini::rt::kDataTypeToDesc.at(t.dtype()); line.append(dt.data(), dt.size()); line += "\", \"shape\": ["; const std::size_t nd = t.ndim(); @@ -350,7 +289,7 @@ void TraceOperatorCallDetailed(const CacheKey& key, const Config& config) { line += "]}\n"; std::fprintf(stderr, "%s", line.c_str()); } catch (...) { - // Ignore serialization failures so tracing does not alter operator behavior. + // Ignore allocation and serialization exceptions from tracing. } }