Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metal shader profiling support #62

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/hello_world/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,22 @@ int main(int argc, char **argv) {
}
Tensor input = createTensor(ctx, Shape{N}, kf32, inputArr.data());
Tensor output = createTensor(ctx, Shape{N}, kf32);

MetalShaderProfiler profiler; // Add an instance of MetalShaderProfiler

std::promise<void> promise;
std::future<void> future = promise.get_future();

profiler.startCapture(); // Call startCapture before dispatching the kernel

Kernel op = createKernel(ctx, {kGelu, 256, kf32},
Bindings{input, output},
/* nWorkgroups */ {cdiv(N, 256), 1, 1});
dispatchKernel(ctx, op, promise);
wait(ctx, future);

profiler.stopCapture(); // Call stopCapture after waiting for the kernel to finish

toCPU(ctx, output, outputArr.data(), sizeof(outputArr));
for (int i = 0; i < 12; ++i) {
printf(" gelu(%.2f) = %.2f\n", inputArr[i], outputArr[i]);
Expand Down
27 changes: 27 additions & 0 deletions gpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,33 @@ inline void dispatchKernel(Context &ctx, Kernel &kernel,
&promise);
}

#ifdef __APPLE__
#include <Metal/Metal.h>
#include <QuartzCore/CAMetalLayer.h>

class MetalShaderProfiler {
public:
MetalShaderProfiler() : device(nil), captureManager(nil), captureScope(nil) {
device = MTLCreateSystemDefaultDevice();
captureManager = MTLCaptureManager::sharedCaptureManager();
captureScope = [captureManager newCaptureScopeWithDevice:device];
}

void startCapture() {
[captureScope beginScope];
}

void stopCapture() {
[captureScope endScope];
}

private:
id<MTLDevice> device;
MTLCaptureManager *captureManager;
id<MTLCaptureScope> captureScope;
};
#endif

} // namespace gpu

#endif // GPU_H
Loading