Skip to content

Commit

Permalink
[Metal] Support metal device profiling
Browse files Browse the repository at this point in the history
Enable native metal device profiling through API `sampleTimestamps`
  • Loading branch information
Hzfengsy committed May 24, 2024
1 parent b1951a7 commit 62579c9
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/runtime/metal/metal_device_api.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* \file metal_device_api.mm
*/
#include <dmlc/thread_local.h>
#include <tvm/runtime/profiling.h>
#include <tvm/runtime/registry.h>
#include "metal_common.h"

Expand Down Expand Up @@ -366,6 +367,42 @@ int GetWarpSize(id<MTLDevice> dev) {
MetalWorkspace::Global()->ReinitializeDefaultStreams();
});

class MetalTimerNode : public TimerNode {
public:
MetalTimerNode() {}
explicit MetalTimerNode(Device dev) : dev_(dev) {
mtl_dev_ = MetalWorkspace::Global()->GetDevice(dev_);
}

virtual void Start() {
[mtl_dev_ sampleTimestamps:&start_cpu_time_ gpuTimestamp:&start_gpu_time_];
}
virtual void Stop() {
auto ws = MetalWorkspace::Global();
ws->StreamSync(dev_, ws->GetCurrentStream(dev_));
[mtl_dev_ sampleTimestamps:&stop_cpu_time_ gpuTimestamp:&stop_gpu_time_];
}
virtual int64_t SyncAndGetElapsedNanos() { return stop_gpu_time_ - start_gpu_time_; }

static constexpr const char* _type_key = "MetalTimerNode";
TVM_DECLARE_FINAL_OBJECT_INFO(MetalTimerNode, TimerNode);

private:
Device dev_;
id<MTLDevice> mtl_dev_;

MTLTimestamp start_cpu_time_;
MTLTimestamp start_gpu_time_;
MTLTimestamp stop_cpu_time_;
MTLTimestamp stop_gpu_time_;
};

TVM_REGISTER_OBJECT_TYPE(MetalTimerNode);

TVM_REGISTER_GLOBAL("profiling.timer.metal").set_body_typed([](Device dev) {
return Timer(make_object<MetalTimerNode>(dev));
});

} // namespace metal
} // namespace runtime
} // namespace tvm

0 comments on commit 62579c9

Please sign in to comment.