diff --git a/examples/mnist/mnist.zig b/examples/mnist/mnist.zig index 4130bf7a..0e6a07ac 100644 --- a/examples/mnist/mnist.zig +++ b/examples/mnist/mnist.zig @@ -53,6 +53,13 @@ pub fn asyncMain() !void { const platform = context.autoPlatform(.{}); context.printAvailablePlatforms(platform); + var profiler = platform.getProfiler(.{}); + profiler.start(); + defer { + profiler.stop(); + profiler.dumpDataTo(std.heap.c_allocator, std.fs.openDirAbsolute("/home/hugo/zml", .{}) catch unreachable, "prof") catch unreachable; + } + // Parse program args const process_args = try std.process.argsAlloc(allocator); defer std.process.argsFree(allocator, process_args); diff --git a/pjrt/BUILD.bazel b/pjrt/BUILD.bazel index 173bea7e..d1fa2abf 100644 --- a/pjrt/BUILD.bazel +++ b/pjrt/BUILD.bazel @@ -1,16 +1,27 @@ +load("@com_google_protobuf//bazel:upb_proto_library.bzl", "upb_c_proto_library") load("@rules_zig//zig:defs.bzl", "zig_library") zig_library( name = "pjrt", - srcs = ["ffi.zig"], + srcs = [ + "ffi.zig", + "profiler.zig", + ], main = "pjrt.zig", visibility = ["//visibility:public"], deps = [ + ":profiler_options_proto", "//stdx", "@xla//xla/ffi/api:c_api", "@xla//xla/pjrt/c:pjrt_c_api_ffi_extension_hdrs", "@xla//xla/pjrt/c:pjrt_c_api_gpu_extension_hdrs", "@xla//xla/pjrt/c:pjrt_c_api_hdrs", + "@xla//xla/pjrt/c:pjrt_c_api_profiler_extension_hdrs", "@xla//xla/pjrt/c:pjrt_c_api_triton_extension_hdrs", ], ) + +upb_c_proto_library( + name = "profiler_options_proto", + deps = ["@xla//third_party/tsl/tsl/profiler/protobuf:profiler_options_proto"], +) diff --git a/pjrt/pjrt.zig b/pjrt/pjrt.zig index f9f30bda..c710593e 100644 --- a/pjrt/pjrt.zig +++ b/pjrt/pjrt.zig @@ -5,6 +5,7 @@ const c = @import("c"); const stdx = @import("stdx"); pub const ffi = @import("ffi.zig"); +pub const Profiler = @import("profiler.zig").Profiler; const log = std.log.scoped(.pjrt); @@ -390,6 +391,19 @@ pub const Client = opaque { }; } + /// Returns the Profiler for this API. + /// Not all platform have a profiling api, for those the profiler object will do nothing. + /// Platforms with known profiler extensions: cuda, xpu + pub fn getProfiler(self: *const Client, api: *const Api, options: Profiler.Options) Profiler { + if (api.version().minor >= 45) { + if (api.lookupExtension(c.PJRT_Profiler_Extension, c.PJRT_Extension_Type_Profiler)) |ext| { + return Profiler.init(ext.profiler_api.*, options); + } + } + log.warn("No profiler found for platform: {}", .{self}); + return Profiler.init(null, null); + } + pub fn deserializeAndLoad(self: *const Client, api: *const Api, bytes: []const u8) ApiError!*LoadedExecutable { const ret = try api.call(.PJRT_Executable_DeserializeAndLoad, .{ .client = self.inner(), diff --git a/pjrt/profiler.zig b/pjrt/profiler.zig new file mode 100644 index 00000000..6566cf3f --- /dev/null +++ b/pjrt/profiler.zig @@ -0,0 +1,200 @@ +const std = @import("std"); +const c = @import("c"); +// const tsl_proto = @import("//tsl:profiler_options_proto"); +const proto = c.tensorflow_ProfileOptions; + +const log = std.log.scoped(.@"pjrt/profiler"); +// const TraceContainer = @import("convert/trace_container.zig").TraceContainer; + +/// Pjrt Profiler extension +pub const Profiler = struct { + api: ?c.PLUGIN_Profiler_Api, + inner: *c.PLUGIN_Profiler, + last_error: ?*Error = null, + status: Status = .ready, + + pub const Status = enum { ready, started, stopped, done }; + pub const Error = c.PLUGIN_Profiler_Error; + + pub const default_options: Options = .{ + .version = 1, + // .device_type = .UNSPECIFIED, // profile all devices + // .include_dataset_ops = false, // tensorflow specific + // .host_tracer_level = 2, + // .device_tracer_level = 1, + // .python_tracer_level = 0, + // .enable_hlo_proto = true, + // .start_timestamp_ns = 0, + // .duration_ms = 0, + // .repository_path = .Empty, + }; + + pub fn init(api: ?c.PLUGIN_Profiler_Api, options: ?Options) Profiler { + _ = options; // autofix + if (api == null) { + return .{ .api = null, .inner = undefined }; + } + // var options_with_timestamp = options orelse default_options; + // options_with_timestamp.start_timestamp_ns = @truncate(@max(0, std.time.nanoTimestamp())); + + var buffer: [std.fs.max_path_bytes + @sizeOf(Options) * 4]u8 = undefined; + + var fba = std.heap.FixedBufferAllocator.init(&buffer); + // const byte_options = options_with_timestamp.encode(fba.allocator()) catch unreachable; + const byte_options = fba.allocator().alloc(u8, 256) catch unreachable; // placeholder for serialized options + var args: c.PLUGIN_Profiler_Create_Args = .{ + .options = byte_options.ptr, + .options_size = byte_options.len, + .profiler = undefined, // out + }; + var res: Profiler = .{ .api = api, .inner = undefined }; + res.check(api.?.create.?(&args)) catch unreachable; + + res.inner = args.profiler.?; + return res; + } + + fn transition(self: *Profiler, fn_name: []const u8, expected: Status, next: Status) void { + if (self.status == expected) { + self.status = next; + return; + } + std.debug.panic("Profiler can't `{s}()`. Current status: {}, expected: {}", .{ fn_name, self.status, expected }); + } + + pub fn start(self: *Profiler) void { + self.transition("start", .ready, .started); + if (self.api == null) return; + var args: c.PLUGIN_Profiler_Start_Args = .{ .profiler = self.inner }; + self.check(self.api.?.start.?(&args)) catch unreachable; + } + + pub fn stop(self: *Profiler) void { + self.transition("stop", .started, .stopped); + if (self.api == null) return; + + var args: c.PLUGIN_Profiler_Stop_Args = .{ .profiler = self.inner }; + self.check(self.api.?.stop.?(&args)) catch unreachable; + } + + pub fn collectData(self: *Profiler, allocator: std.mem.Allocator) !ProfilingData { + self.transition("collect_data", .stopped, .done); + if (self.api == null) return .{ .external = &.{} }; + + var args: c.PLUGIN_Profiler_CollectData_Args = .{ + .struct_size = c.PLUGIN_Profiler_CollectData_Args_STRUCT_SIZE, + .profiler = self.inner, + .buffer = null, + .buffer_size_in_bytes = 0, + }; + try self.check(self.api.?.collect_data.?(&args)); + std.debug.assert(args.buffer_size_in_bytes > 0); + return if (args.buffer == null) blk: { + log.info("Plugin profiler wants us to allocate {d} bytes for profile data", .{args.buffer_size_in_bytes}); + // The plugin want us to allocate memory for it: + const buffer = try allocator.alloc(u8, args.buffer_size_in_bytes); + args.buffer = buffer.ptr; + try self.check(self.api.?.collect_data.?(&args)); + break :blk .{ .owned = buffer }; + } else blk: { + log.info("Plugin profiler has {d} bytes of profile data", .{args.buffer_size_in_bytes}); + // Drop sentinel. The profiler plugin returns a null terminated string. + // But this is creating issues if we save the sentinel on disk, + // because it will trip up protobuf readers. + var data = args.buffer[0..args.buffer_size_in_bytes]; + data = if (data.len > 0 and data[data.len - 1] == 0) data[0 .. data.len - 1] else data; + break :blk .{ .external = data }; + }; + } + + pub fn dumpDataTo( + self: *Profiler, + allocator: std.mem.Allocator, + dir: std.fs.Dir, + file_name: []const u8, + ) !void { + const profile_data = try self.collectData(allocator); + defer profile_data.free(allocator); + + if (profile_data.items().len == 0) { + log.warn("No profile data was collected: {}", .{self}); + return; + } + + const file = try dir.createFile(file_name, .{ .truncate = true }); + defer file.close(); + log.info("Writing profiling data to {s} ({} bytes)", .{ file_name, profile_data.items().len }); + return try file.writeAll(profile_data.items()); + } + + // pub fn dumpAsJsonTo( + // self: *Profiler, + // allocator: std.mem.Allocator, + // dir: std.fs.Dir, + // file_name: []const u8, + // ) !void { + // log.info("Writing profiling data to {s}", .{file_name}); + // var output_file = try dir.createFile(file_name, .{}); + // defer output_file.close(); + // var buffered_writer = std.io.bufferedWriter(output_file.writer()); + // try self.dumpAsJsonToWriter(allocator, buffered_writer.writer()); + // try buffered_writer.flush(); + // } + + // pub fn dumpAsJsonToWriter( + // self: *Profiler, + // allocator: std.mem.Allocator, + // writer: anytype, + // ) !void { + // const profile_data = try self.collectData(allocator); + // defer profile_data.free(allocator); + + // if (profile_data.items().len == 0) { + // log.warn("No profile data was collected: {}", .{self}); + // return; + // } + + // var converter = TraceContainer.init(allocator); + // defer converter.deinit(); + // try converter.parseXSpaceBytes(profile_data.items(), 1_000_000); + + // try converter.toJson(writer); + // } + + fn check(self: *Profiler, c_error: ?*Error) !void { + if (c_error) |err| { + self.last_error = err; + return error.PjrtProfilerError; + } + } + + pub fn deinit(self: Profiler) void { + switch (self.status) { + .started => log.warn("Profiler was never stopped", .{}), + .stopped => log.warn("Profiler data was never collected", .{}), + else => {}, + } + if (self.api == null) return; + + var args: c.PLUGIN_Profiler_Destroy_Args = .{ .profiler = self.inner }; + _ = self.api.?.destroy.?(&args); + } +}; + +const ProfilingData = union(enum) { + owned: []const u8, + external: []const u8, + + pub fn items(self: ProfilingData) []const u8 { + return switch (self) { + inline else => |x| x, + }; + } + + pub fn free(self: ProfilingData, allocator: std.mem.Allocator) void { + switch (self) { + .owned => |data| allocator.free(data), + .external => {}, + } + } +}; diff --git a/zml/BUILD.bazel b/zml/BUILD.bazel index efe4abb0..a806e77a 100644 --- a/zml/BUILD.bazel +++ b/zml/BUILD.bazel @@ -30,8 +30,8 @@ zig_library( "aio/torch/pickle.zig", "aio/torch/py.zig", "buffer.zig", - "context.zig", "callback.zig", + "context.zig", "dtype.zig", "exe.zig", "floats.zig", @@ -54,12 +54,12 @@ zig_library( "torch.zig", "zml.zig", ], + main = "zml.zig", + visibility = ["//visibility:public"], zigopts = [ "-lc", "-freference-trace=20", ], - main = "zml.zig", - visibility = ["//visibility:public"], deps = [ ":posix", ":xla_compile_options_upb", diff --git a/zml/pjrtx.zig b/zml/pjrtx.zig index f09ffa1e..3ef4e6c3 100644 --- a/zml/pjrtx.zig +++ b/zml/pjrtx.zig @@ -119,6 +119,13 @@ pub const Client = opaque { return try async.callBlocking(compileSync, .{ self, api, allocator, module, compile_options_pb }); } + /// Returns the Profiler for this API. + /// Not all platform have a profiling api, for those the profiler object will do nothing. + /// Platforms with known profiler extensions: cuda, xpu + pub fn getProfiler(self: *const Client, api: *const Api, options: pjrt.Profiler.Options) pjrt.Profiler { + return self.inner().getProfiler(api, options); + } + pub fn addressableMemories(self: *const Client, api: *const Api) []*const Memory { return self.inner().addressableMemories(api); } diff --git a/zml/platform.zig b/zml/platform.zig index c5872a57..f2094688 100644 --- a/zml/platform.zig +++ b/zml/platform.zig @@ -124,6 +124,13 @@ pub const Platform = struct { const device = platform.getDevices()[device_id]; return device.memoryStats(platform.pjrt_api) catch .zeroes; } + + /// Returns the Profiler for this API. + /// Not all platform have a profiling api, for those the profiler object will do nothing. + /// Platforms with known profiler extensions: cuda, xpu + pub fn getProfiler(self: Platform, options: ?pjrt.Profiler.Options) pjrt.Profiler { + return self.pjrt_client.getProfiler(self.pjrt_api, options orelse pjrt.Profiler.default_options); + } }; const _CreateOptions = struct {