24 #ifndef TVM_RUNTIME_PROFILING_H_
25 #define TVM_RUNTIME_PROFILING_H_
27 #include <tvm/ffi/container/array.h>
28 #include <tvm/ffi/container/map.h>
29 #include <tvm/ffi/function.h>
38 #include <unordered_map>
88 class Timer :
public ObjectRef {
159 namespace profiling {
190 ffi::Array<ffi::Map<ffi::String, ffi::Any>>
calls;
223 ffi::String
AsTable(
bool sort =
true,
bool aggregate =
true,
bool compute_col_sums =
true)
const;
267 explicit Report(ffi::Array<ffi::Map<ffi::String, ffi::Any>> calls,
268 ffi::Map<ffi::String, ffi::Map<ffi::String, ffi::Any>> device_metrics,
269 ffi::Map<ffi::String, ffi::Any> configuration);
303 virtual void Init(ffi::Array<DeviceWrapper> devs) = 0;
316 virtual ffi::Map<ffi::String, ffi::Any>
Stop(ffi::ObjectRef obj) = 0;
380 explicit Profiler(std::vector<Device> devs, std::vector<MetricCollector> metric_collectors,
381 std::unordered_map<ffi::String, ffi::Any> configuration = {});
403 std::unordered_map<std::string, ffi::Any> extra_metrics = {});
408 void StopCall(std::unordered_map<std::string, ffi::Any> extra_metrics = {});
421 std::vector<Device> devs_;
422 bool is_running_{
false};
423 std::vector<CallFrame> calls_;
424 std::stack<CallFrame> in_flight_;
425 std::vector<MetricCollector> collectors_;
426 std::unordered_map<ffi::String, ffi::Any> configuration_;
532 ffi::Array<MetricCollector> collectors);
582 int min_repeat_ms,
int limit_zero_time_iterations,
583 int cooldown_interval_ms,
int repeats_to_cooldown,
584 int cache_flush_bytes = 0,
ffi::Function f_preproc =
nullptr);
Managed Tensor. The array is backed by reference counted blocks.
Definition: tensor.h:53
Base class for all implementations.
Definition: profiling.h:52
virtual int64_t SyncAndGetElapsedNanos()=0
Synchronize timer state and return elapsed time between Start and Stop.
TVM_FFI_DECLARE_OBJECT_INFO("runtime.TimerNode", TimerNode, Object)
virtual void Stop()=0
Stop the timer.
virtual void Start()=0
Start the timer.
static constexpr const bool _type_mutable
Definition: profiling.h:78
virtual ~TimerNode()
Definition: profiling.h:76
Timer for a specific device.
Definition: profiling.h:88
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Timer, ObjectRef, TimerNode)
static Timer Start(Device dev)
Get a device specific timer.
Definition: profiling.h:456
CountNode(int64_t a)
Definition: profiling.h:464
int64_t value
Definition: profiling.h:459
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("runtime.profiling.Count", CountNode, Object)
Wrapper for Device.
Definition: profiling.h:173
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(DeviceWrapper, ObjectRef, DeviceWrapperNode)
DeviceWrapper(Device dev)
Definition: profiling.h:175
Definition: profiling.h:430
double microseconds
Definition: profiling.h:433
DurationNode(double a)
Definition: profiling.h:438
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("runtime.profiling.Duration", DurationNode, Object)
Interface for user defined profiling metric collection.
Definition: profiling.h:297
TVM_FFI_DECLARE_OBJECT_INFO("runtime.profiling.MetricCollector", MetricCollectorNode, Object)
virtual ObjectRef Start(Device dev)=0
Start colling metrics for a function call.
static constexpr const bool _type_mutable
Definition: profiling.h:320
virtual ffi::Map< ffi::String, ffi::Any > Stop(ffi::ObjectRef obj)=0
Stop collecting metrics.
virtual ~MetricCollectorNode()
Definition: profiling.h:318
virtual void Init(ffi::Array< DeviceWrapper > devs)=0
Initialization call. Called before profiling has started. Any expensive precomputation should happen ...
Wrapper for MetricCollectorNode.
Definition: profiling.h:325
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(MetricCollector, ObjectRef, MetricCollectorNode)
Definition: profiling.h:443
PercentNode(double a)
Definition: profiling.h:451
double percent
Definition: profiling.h:446
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("runtime.profiling.Percent", PercentNode, Object)
Definition: profiling.h:365
bool IsRunning() const
Check if the profiler is currently running.
Definition: profiling.h:418
Profiler(std::vector< Device > devs, std::vector< MetricCollector > metric_collectors, std::unordered_map< ffi::String, ffi::Any > configuration={})
void Stop()
Stop the profiler.
void StopCall(std::unordered_map< std::string, ffi::Any > extra_metrics={})
Stop the last StartCall.
void StartCall(ffi::String name, Device dev, std::unordered_map< std::string, ffi::Any > extra_metrics={})
Start a function call.
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:469
RatioNode(double a)
Definition: profiling.h:477
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("runtime.profiling.Ratio", RatioNode, Object)
double ratio
Definition: profiling.h:472
Data collected from a profiling run. Includes per-call metrics and per-device metrics.
Definition: profiling.h:181
ffi::String AsJSON() const
Convert this report to JSON.
ffi::Map< ffi::String, ffi::Map< ffi::String, ffi::Any > > device_metrics
Metrics collected for the entire run of the model on a per-device basis.
Definition: profiling.h:198
ffi::String AsTable(bool sort=true, bool aggregate=true, bool compute_col_sums=true) const
Create a human readable table of profiling metrics.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("runtime.profiling.Report", ReportNode, Object)
ffi::Array< ffi::Map< ffi::String, ffi::Any > > calls
A list of function calls and the metrics recorded for that call.
Definition: profiling.h:190
ffi::Map< ffi::String, ffi::Any > configuration
Definition: profiling.h:203
ffi::String AsCSV() const
Output calls in CSV format.
Definition: profiling.h:260
static Report FromJSON(ffi::String json)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Report, ObjectRef, ReportNode)
Report(ffi::Array< ffi::Map< ffi::String, ffi::Any >> calls, ffi::Map< ffi::String, ffi::Map< ffi::String, ffi::Any >> device_metrics, ffi::Map< ffi::String, ffi::Any > configuration)
Abstract device memory management API.
ffi::Function ProfileFunction(ffi::Module mod, std::string func_name, int device_type, int device_id, int warmup_iters, ffi::Array< MetricCollector > collectors)
Collect performance information of a function execution. Usually used with a compiled PrimFunc (via t...
ffi::String ShapeString(const std::vector< Tensor > &shapes)
ffi::String representation of an array of Tensor shapes
ffi::Function WrapTimeEvaluator(ffi::Function 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, ffi::Function 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:1061
constexpr const char * device_type
The device type.
Definition: stmt.h:1063
tvm::PrimExpr mod(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:308
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:1960
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:1349
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
DLDevice Device
Definition: device_api.h:42
A managed object in the TVM runtime.
Runtime container of the functions generated by TVM, This is used to support dynamically link,...
A device-independent managed Tensor abstraction.
Definition: profiling.h:331
Timer timer
Definition: profiling.h:337
std::vector< std::pair< MetricCollector, ObjectRef > > extra_collectors
Definition: profiling.h:343
Device dev
Definition: profiling.h:333
std::unordered_map< std::string, ffi::Any > extra_metrics
Definition: profiling.h:339
ffi::String name
Definition: profiling.h:335
Wrapper for Device because Device is not passable across the ffi::Function interface.
Definition: profiling.h:163
Device device
Definition: profiling.h:165
DeviceWrapperNode(Device device)
Definition: profiling.h:168
TVM_FFI_DECLARE_OBJECT_INFO("runtime.profiling.DeviceWrapper", DeviceWrapperNode, Object)