24 #ifndef TVM_RUNTIME_PROFILING_H_
25 #define TVM_RUNTIME_PROFILING_H_
36 #include <unordered_map>
76 static constexpr
const char*
_type_key =
"TimerNode";
154 namespace profiling {
165 static constexpr
const char*
_type_key =
"runtime.profiling.DeviceWrapper";
220 String AsTable(
bool sort =
true,
bool aggregate =
true,
bool compute_col_sums =
true)
const;
255 static constexpr
const char*
_type_key =
"runtime.profiling.Report";
319 static constexpr
const char*
_type_key =
"runtime.profiling.MetricCollector";
379 explicit Profiler(std::vector<Device> devs, std::vector<MetricCollector> metric_collectors,
380 std::unordered_map<String, ObjectRef> configuration = {});
402 std::unordered_map<std::string, ObjectRef> extra_metrics = {});
407 void StopCall(std::unordered_map<std::string, ObjectRef> extra_metrics = {});
420 std::vector<Device> devs_;
421 bool is_running_{
false};
422 std::vector<CallFrame> calls_;
423 std::stack<CallFrame> in_flight_;
424 std::vector<MetricCollector> collectors_;
425 std::unordered_map<String, ObjectRef> configuration_;
439 static constexpr
const char*
_type_key =
"runtime.profiling.Duration";
454 static constexpr
const char*
_type_key =
"runtime.profiling.Percent";
469 static constexpr
const char*
_type_key =
"runtime.profiling.Count";
484 static constexpr
const char*
_type_key =
"runtime.profiling.Ratio";
588 int limit_zero_time_iterations,
int cooldown_interval_ms,
589 int repeats_to_cooldown,
int cache_flush_bytes = 0,
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
Module container of TVM.
Definition: module.h:79
Managed NDArray. The array is backed by reference counted blocks.
Definition: ndarray.h:51
Base class of all object reference.
Definition: object.h:519
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:605
base class of all object containers.
Definition: object.h:171
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:141
Reference to string objects.
Definition: string.h:98
Base class for all implementations.
Definition: profiling.h:50
virtual int64_t SyncAndGetElapsedNanos()=0
Synchronize timer state and return elapsed time between Start and Stop.
static constexpr const char * _type_key
Definition: profiling.h:76
virtual void Stop()=0
Stop the timer.
virtual void Start()=0
Start the timer.
TVM_DECLARE_BASE_OBJECT_INFO(TimerNode, Object)
virtual ~TimerNode()
Definition: profiling.h:74
Timer for a specific device.
Definition: profiling.h:86
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(Timer, ObjectRef, TimerNode)
static Timer Start(Device dev)
Get a device specific timer.
Definition: profiling.h:459
static constexpr const char * _type_key
Definition: profiling.h:469
TVM_DECLARE_FINAL_OBJECT_INFO(CountNode, Object)
CountNode(int64_t a)
Definition: profiling.h:467
int64_t value
Definition: profiling.h:462
Wrapper for Device.
Definition: profiling.h:170
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(DeviceWrapper, ObjectRef, DeviceWrapperNode)
DeviceWrapper(Device dev)
Definition: profiling.h:172
Definition: profiling.h:429
double microseconds
Definition: profiling.h:432
DurationNode(double a)
Definition: profiling.h:437
static constexpr const char * _type_key
Definition: profiling.h:439
TVM_DECLARE_FINAL_OBJECT_INFO(DurationNode, Object)
Interface for user defined profiling metric collection.
Definition: profiling.h:296
static constexpr const char * _type_key
Definition: profiling.h:319
virtual ObjectRef Start(Device dev)=0
Start colling metrics for a function call.
virtual void Init(Array< DeviceWrapper > devs)=0
Initialization call. Called before profiling has started. Any expensive precomputation should happen ...
TVM_DECLARE_BASE_OBJECT_INFO(MetricCollectorNode, Object)
virtual Map< String, ObjectRef > Stop(ObjectRef obj)=0
Stop collecting metrics.
virtual ~MetricCollectorNode()
Definition: profiling.h:317
Wrapper for MetricCollectorNode.
Definition: profiling.h:324
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(MetricCollector, ObjectRef, MetricCollectorNode)
Definition: profiling.h:444
PercentNode(double a)
Definition: profiling.h:452
static constexpr const char * _type_key
Definition: profiling.h:454
double percent
Definition: profiling.h:447
TVM_DECLARE_FINAL_OBJECT_INFO(PercentNode, Object)
Definition: profiling.h:364
bool IsRunning() const
Check if the profiler is currently running.
Definition: profiling.h:417
void StartCall(String name, Device dev, std::unordered_map< std::string, ObjectRef > extra_metrics={})
Start a function call.
void Stop()
Stop the profiler.
Profiler(std::vector< Device > devs, std::vector< MetricCollector > metric_collectors, std::unordered_map< String, ObjectRef > configuration={})
void StopCall(std::unordered_map< std::string, ObjectRef > extra_metrics={})
Stop the last StartCall.
profiling::Report Report()
A report of total runtime between Start and Stop as well as individual statistics for each StartCall-...
void Start()
Start the profiler.
Definition: profiling.h:474
TVM_DECLARE_FINAL_OBJECT_INFO(RatioNode, Object)
RatioNode(double a)
Definition: profiling.h:482
double ratio
Definition: profiling.h:477
static constexpr const char * _type_key
Definition: profiling.h:484
Data collected from a profiling run. Includes per-call metrics and per-device metrics.
Definition: profiling.h:178
Map< String, ObjectRef > configuration
Definition: profiling.h:200
static constexpr const char * _type_key
Definition: profiling.h:255
String AsTable(bool sort=true, bool aggregate=true, bool compute_col_sums=true) const
Create a human readable table of profiling metrics.
String AsJSON() const
Convert this report to JSON.
Array< Map< String, ObjectRef > > calls
A list of function calls and the metrics recorded for that call.
Definition: profiling.h:187
String AsCSV() const
Output calls in CSV format.
TVM_DECLARE_FINAL_OBJECT_INFO(ReportNode, Object)
Map< String, Map< String, ObjectRef > > device_metrics
Metrics collected for the entire run of the model on a per-device basis.
Definition: profiling.h:195
Definition: profiling.h:259
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(Report, ObjectRef, ReportNode)
Report(Array< Map< String, ObjectRef >> calls, Map< String, Map< String, ObjectRef >> device_metrics, Map< String, ObjectRef > configuration)
static Report FromJSON(String json)
Abstract device memory management API.
Runtime Map container types.
String ShapeString(const std::vector< NDArray > &shapes)
String representation of an array of NDArray shapes.
PackedFunc ProfileFunction(Module mod, std::string func_name, int device_type, int device_id, int warmup_iters, Array< MetricCollector > collectors)
Collect performance information of a function execution. Usually used with a compiled PrimFunc (via t...
PackedFunc WrapTimeEvaluator(PackedFunc f, Device dev, int number, int repeat, int min_repeat_ms, int limit_zero_time_iterations, int cooldown_interval_ms, int repeats_to_cooldown, int cache_flush_bytes=0, PackedFunc f_preproc=nullptr)
Wrap a timer function to measure the time cost of a given packed function.
Timer DefaultTimer(Device dev)
Default timer if one does not exist for the device.
constexpr const char * device_id
The allocation device for global malloc in host.
Definition: stmt.h:1420
constexpr const char * device_type
The device type.
Definition: stmt.h:1422
tvm::PrimExpr mod(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:290
Tensor shape(const Tensor &src, DataType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:1913
Tensor repeat(const Tensor &x, int repeats, int axis, std::string name="T_repeat", std::string tag=kBroadcast)
Creates an operation to repeat elements of an array.
Definition: transform.h:1304
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
DLDevice Device
Definition: ndarray.h:43
A managed object in the TVM runtime.
Type-erased function used across TVM API.
This file defines the TVM global function registry.
Definition: profiling.h:330
Timer timer
Definition: profiling.h:336
std::unordered_map< std::string, ObjectRef > extra_metrics
Definition: profiling.h:338
std::vector< std::pair< MetricCollector, ObjectRef > > extra_collectors
Definition: profiling.h:342
String name
Definition: profiling.h:334
Device dev
Definition: profiling.h:332
Wrapper for Device because Device is not passable across the PackedFunc interface.
Definition: profiling.h:158
TVM_DECLARE_BASE_OBJECT_INFO(DeviceWrapperNode, Object)
Device device
Definition: profiling.h:160
DeviceWrapperNode(Device device)
Definition: profiling.h:163
static constexpr const char * _type_key
Definition: profiling.h:165