Skip to content

Commit 627eb5a

Browse files
[feat]: Custom Backend Tracing (#351)
* Add the ability for backends to add entries to the trace --------- Co-authored-by: Iman Tabrizian <[email protected]>
1 parent 1875c07 commit 627eb5a

File tree

6 files changed

+88
-12
lines changed

6 files changed

+88
-12
lines changed

include/triton/core/tritonbackend.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,9 +1722,9 @@ TRITONBACKEND_BackendAttributeSetParallelModelInstanceLoading(
17221722
///
17231723
/// \param batcher User-defined placeholder for backend to store and
17241724
/// retrieve information about the batching strategy for this
1725-
/// model.RITONBACKEND_ISPEC return a TRITONSERVER_Error indicating success or
1726-
/// failure. \param model The backend model for which Triton is forming a batch.
1727-
/// \return a TRITONSERVER_Error indicating success or failure.
1725+
/// model. Returns a TRITONSERVER_Error indicating success
1726+
/// or failure. \param model The backend model for which Triton is forming a
1727+
/// batch. \return a TRITONSERVER_Error indicating success or failure.
17281728
TRITONBACKEND_ISPEC TRITONSERVER_Error* TRITONBACKEND_ModelBatcherInitialize(
17291729
TRITONBACKEND_Batcher** batcher, TRITONBACKEND_Model* model);
17301730

include/triton/core/tritonserver.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ struct TRITONSERVER_MetricFamily;
9191
/// }
9292
///
9393
#define TRITONSERVER_API_VERSION_MAJOR 1
94-
#define TRITONSERVER_API_VERSION_MINOR 32
94+
#define TRITONSERVER_API_VERSION_MINOR 33
9595

9696
/// Get the TRITONBACKEND API version supported by the Triton shared
9797
/// library. This value can be compared against the
@@ -732,7 +732,8 @@ typedef enum tritonserver_traceactivity_enum {
732732
TRITONSERVER_TRACE_REQUEST_END = 6,
733733
TRITONSERVER_TRACE_TENSOR_QUEUE_INPUT = 7,
734734
TRITONSERVER_TRACE_TENSOR_BACKEND_INPUT = 8,
735-
TRITONSERVER_TRACE_TENSOR_BACKEND_OUTPUT = 9
735+
TRITONSERVER_TRACE_TENSOR_BACKEND_OUTPUT = 9,
736+
TRITONSERVER_TRACE_CUSTOM_ACTIVITY = 10
736737
} TRITONSERVER_InferenceTraceActivity;
737738

738739
/// Get the string representation of a trace activity. The returned
@@ -838,6 +839,18 @@ TRITONSERVER_InferenceTraceTensorNew(
838839
TRITONSERVER_InferenceTraceTensorActivityFn_t tensor_activity_fn,
839840
TRITONSERVER_InferenceTraceReleaseFn_t release_fn, void* trace_userp);
840841

842+
/// Report a trace activity. All the traces reported using this API will be
843+
/// using TRITONSERVER_TRACE_CUSTOM_ACTIVITY type.
844+
///
845+
/// \param trace The trace object.
846+
/// \param timestamp The timestamp associated with the trace activity.
847+
/// \param name The trace activity name.
848+
/// \return a TRITONSERVER_Error indicating success or failure.
849+
TRITONSERVER_DECLSPEC TRITONSERVER_Error*
850+
TRITONSERVER_InferenceTraceReportActivity(
851+
TRITONSERVER_InferenceTrace* trace, uint64_t timestamp,
852+
const char* activity_name);
853+
841854
/// Delete a trace object.
842855
///
843856
/// \param trace The trace object.
@@ -921,7 +934,6 @@ TRITONSERVER_DECLSPEC struct TRITONSERVER_Error*
921934
TRITONSERVER_InferenceTraceSetContext(
922935
struct TRITONSERVER_InferenceTrace* trace, const char* trace_context);
923936

924-
925937
/// Get TRITONSERVER_InferenceTrace context.
926938
///
927939
/// \param trace The trace.

src/infer_trace.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626

2727
#include "infer_trace.h"
2828

29+
#define TRITONJSON_STATUSTYPE triton::core::Status
30+
#define TRITONJSON_STATUSRETURN(M) \
31+
return triton::core::Status(triton::core::Status::Code::INTERNAL, (M))
32+
#define TRITONJSON_STATUSSUCCESS triton::core::Status::Success
33+
#include "triton/common/logging.h"
34+
#include "triton/common/triton_json.h"
35+
2936
namespace triton { namespace core {
3037

3138
#ifdef TRITON_ENABLE_TRACING
@@ -48,6 +55,26 @@ InferenceTrace::Release()
4855
release_fn_(reinterpret_cast<TRITONSERVER_InferenceTrace*>(this), userp_);
4956
}
5057

58+
void
59+
InferenceTrace::RecordActivityName(
60+
uint64_t timestamp_ns, std::string activity_name)
61+
{
62+
std::lock_guard<std::mutex> lock(mu_);
63+
triton::common::TritonJson::Value context_json(
64+
triton::common::TritonJson::ValueType::OBJECT);
65+
if (!context_.empty()) {
66+
Status status = context_json.Parse(context_);
67+
if (!status.IsOk()) {
68+
LOG_ERROR << "Error parsing trace context";
69+
}
70+
}
71+
std::string key = std::to_string(timestamp_ns);
72+
context_json.SetStringObject(key.c_str(), activity_name);
73+
triton::common::TritonJson::WriteBuffer buffer;
74+
context_json.Write(&buffer);
75+
context_ = buffer.Contents();
76+
}
77+
5178
std::shared_ptr<InferenceTraceProxy>
5279
InferenceTraceProxy::SpawnChildTrace()
5380
{

src/infer_trace.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <atomic>
2929
#include <chrono>
3030
#include <memory>
31+
#include <mutex>
3132

3233
#include "constants.h"
3334
#include "status.h"
@@ -69,26 +70,33 @@ class InferenceTrace {
6970
void SetModelVersion(int64_t v) { model_version_ = v; }
7071
void SetRequestId(const std::string& request_id) { request_id_ = request_id; }
7172
void SetContext(const std::string& context) { context_ = context; }
73+
void RecordActivityName(uint64_t timestamp_ns, std::string activity_name);
7274

7375
// Report trace activity.
7476
void Report(
75-
const TRITONSERVER_InferenceTraceActivity activity, uint64_t timestamp_ns)
77+
const TRITONSERVER_InferenceTraceActivity activity, uint64_t timestamp_ns,
78+
std::string activity_name = "")
7679
{
7780
if ((level_ & TRITONSERVER_TRACE_LEVEL_TIMESTAMPS) > 0) {
81+
if (activity == TRITONSERVER_TRACE_CUSTOM_ACTIVITY) {
82+
RecordActivityName(timestamp_ns, activity_name);
83+
}
7884
activity_fn_(
7985
reinterpret_cast<TRITONSERVER_InferenceTrace*>(this), activity,
8086
timestamp_ns, userp_);
8187
}
8288
}
8389

8490
// Report trace activity at the current time.
85-
void ReportNow(const TRITONSERVER_InferenceTraceActivity activity)
91+
void ReportNow(
92+
const TRITONSERVER_InferenceTraceActivity activity,
93+
std::string activity_name = "")
8694
{
8795
if ((level_ & TRITONSERVER_TRACE_LEVEL_TIMESTAMPS) > 0) {
88-
Report(
89-
activity, std::chrono::duration_cast<std::chrono::nanoseconds>(
90-
std::chrono::steady_clock::now().time_since_epoch())
91-
.count());
96+
auto now = std::chrono::duration_cast<std::chrono::nanoseconds>(
97+
std::chrono::steady_clock::now().time_since_epoch())
98+
.count();
99+
Report(activity, now, activity_name);
92100
}
93101
}
94102

@@ -128,6 +136,7 @@ class InferenceTrace {
128136
// across traces
129137
static std::atomic<uint64_t> next_id_;
130138
std::string context_;
139+
std::mutex mu_;
131140
};
132141

133142
//
@@ -152,6 +161,10 @@ class InferenceTraceProxy {
152161
void SetRequestId(const std::string& n) { trace_->SetRequestId(n); }
153162
void SetModelVersion(int64_t v) { trace_->SetModelVersion(v); }
154163
void SetContext(const std::string& context) { trace_->SetContext(context); }
164+
void RecordActivityName(uint64_t timestamp_ns, std::string activity_name)
165+
{
166+
trace_->RecordActivityName(timestamp_ns, activity_name);
167+
}
155168

156169
void Report(
157170
const TRITONSERVER_InferenceTraceActivity activity, uint64_t timestamp_ns)

src/tritonserver.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,8 @@ TRITONSERVER_InferenceTraceActivityString(
950950
return "TENSOR_BACKEND_INPUT";
951951
case TRITONSERVER_TRACE_TENSOR_BACKEND_OUTPUT:
952952
return "TENSOR_BACKEND_OUTPUT";
953+
case TRITONSERVER_TRACE_CUSTOM_ACTIVITY:
954+
return "CUSTOM_ACTIVITY";
953955
}
954956

955957
return "<unknown>";
@@ -1115,6 +1117,23 @@ TRITONSERVER_InferenceTraceSpawnChildTrace(
11151117
#endif // TRITON_ENABLE_TRACING
11161118
}
11171119

1120+
TRITONSERVER_DECLSPEC TRITONSERVER_Error*
1121+
TRITONSERVER_InferenceTraceReportActivity(
1122+
TRITONSERVER_InferenceTrace* trace, uint64_t timestamp,
1123+
const char* activity_name)
1124+
{
1125+
#ifdef TRITON_ENABLE_TRACING
1126+
tc::InferenceTrace* ltrace = reinterpret_cast<tc::InferenceTrace*>(trace);
1127+
if (trace != nullptr) {
1128+
ltrace->Report(
1129+
TRITONSERVER_TRACE_CUSTOM_ACTIVITY, timestamp, activity_name);
1130+
}
1131+
return nullptr; // Success
1132+
#else
1133+
return TRITONSERVER_ErrorNew(
1134+
TRITONSERVER_ERROR_UNSUPPORTED, "inference tracing not supported");
1135+
#endif // TRITON_ENABLE_TRACING
1136+
}
11181137

11191138
TRITONAPI_DECLSPEC TRITONSERVER_Error*
11201139
TRITONSERVER_InferenceTraceSetContext(

src/tritonserver_stub.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,11 @@ TRITONBACKEND_BackendAttributeAddPreferredInstanceGroup()
11151115
{
11161116
}
11171117

1118+
TRITONAPI_DECLSPEC void
1119+
TRITONSERVER_InferenceTraceReportActivity()
1120+
{
1121+
}
1122+
11181123
TRITONAPI_DECLSPEC void
11191124
TRITONBACKEND_BackendAttributeSetParallelModelInstanceLoading()
11201125
{

0 commit comments

Comments
 (0)