From 103bbc1bc8c7ff4fd2bacce6a65339af00fc8743 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Tue, 25 Mar 2025 13:57:51 +0000 Subject: [PATCH] wip --- async/asyncio.zig | 4 +- runtimes/common/packages.bzl | 17 +++++++++ runtimes/cuda/cuda.bzl | 5 +++ runtimes/cuda/libpjrt_cuda.BUILD.bazel | 1 + zml/ops.zig | 2 + zml/tensor.zig | 4 ++ zml/tools/tracer.zig | 52 ++++++++++++++++++++++++++ 7 files changed, 83 insertions(+), 2 deletions(-) diff --git a/async/asyncio.zig b/async/asyncio.zig index bbc842cd..7bcbb007 100644 --- a/async/asyncio.zig +++ b/async/asyncio.zig @@ -78,7 +78,7 @@ pub fn sleep(exec: *Executor, ms: u64) !void { const Data = XCallback(SleepResult); var data = Data.init(); - const w = try xev.Timer.init(); + var w = try xev.Timer.init(); defer w.deinit(); var c: xev.Completion = .{}; w.run(loop, &c, ms, Data, &data, &Data.callback); @@ -129,7 +129,7 @@ pub const TCP = struct { } pub fn accept(self: Self) !Self { - const AcceptResult = xev.TCP.AcceptError!xev.TCP; + const AcceptResult = xev.AcceptError!xev.TCP; const Data = XCallback(AcceptResult); const loop = self.exec.loop; diff --git a/runtimes/common/packages.bzl b/runtimes/common/packages.bzl index b5e20cf9..effd7f6e 100644 --- a/runtimes/common/packages.bzl +++ b/runtimes/common/packages.bzl @@ -16,6 +16,22 @@ def _filegroup(**kwargs): def _load(bzl, name): return """load({}, {})""".format(repr(bzl), repr(name)) +def _cc_import_with_glob(name, hdrs_glob, shared_library, deps = []): + return """\ +filegroup( + name = "{name}_files", + srcs = glob(["{hdrs_glob}"]), + visibility = ["//visibility:public"], +) +cc_import( + name = "{name}", + shared_library = {shared_library}, + hdrs = [":{name}_files"], + deps = {deps}, + visibility = ["//visibility:public"], +) +""".format(name = name, hdrs_glob = hdrs_glob, shared_library = repr(shared_library), deps = repr(deps)) + def _read(mctx, labels): ret = {} for label in labels: @@ -59,6 +75,7 @@ common_apt_packages = module_extension( packages = struct( read = _read, cc_import = _cc_import, + cc_import_with_glob = _cc_import_with_glob, filegroup = _filegroup, load_ = _load, ) diff --git a/runtimes/cuda/cuda.bzl b/runtimes/cuda/cuda.bzl index 58fec196..c20a894a 100644 --- a/runtimes/cuda/cuda.bzl +++ b/runtimes/cuda/cuda.bzl @@ -85,6 +85,11 @@ CUDA_PACKAGES = { deps = [":cublasLt"], ), ]), + "cuda_nvtx": packages.cc_import_with_glob( + name = "nvtx", + hdrs_glob = "include/nvtx3/**/*.h", + shared_library = "lib/libnvToolsExt.so.1", + ), } CUDNN_PACKAGES = { diff --git a/runtimes/cuda/libpjrt_cuda.BUILD.bazel b/runtimes/cuda/libpjrt_cuda.BUILD.bazel index 4412cd55..8881a016 100644 --- a/runtimes/cuda/libpjrt_cuda.BUILD.bazel +++ b/runtimes/cuda/libpjrt_cuda.BUILD.bazel @@ -35,6 +35,7 @@ cc_import( ":zmlxcuda", "@cuda_cudart//:cudart", "@cuda_cupti//:cupti", + "@cuda_nvtx//:nvtx", "@cuda_nvcc//:nvptxcompiler", "@cuda_nvcc//:nvvm", "@cuda_nvrtc//:nvrtc", diff --git a/zml/ops.zig b/zml/ops.zig index c0499850..bcfaba1a 100644 --- a/zml/ops.zig +++ b/zml/ops.zig @@ -826,6 +826,7 @@ pub const TritonOps = struct { grid: [3]i32, num_stages: i32, num_warps: i32, + output_operand_aliases: []const i64 = &.{}, }; /// Generate an MLIR call to the given member function with the given tensors. @@ -884,6 +885,7 @@ pub fn triton(inputs: anytype, outputs: anytype, opts: TritonOps) [outputs.len]T .api_version = .typed_ffi, .operand_layouts = &operands_layouts, .result_layouts = &results_layouts, + .output_operand_aliases = opts.output_operand_aliases, }, &res_types, ctx.mlirCtx().location(@src()), diff --git a/zml/tensor.zig b/zml/tensor.zig index 83e6c28f..f5e8dc5c 100644 --- a/zml/tensor.zig +++ b/zml/tensor.zig @@ -976,6 +976,10 @@ pub const Tensor = struct { return self.add(Tensor.scalar(b, self.dtype())); } + pub fn subConstant(self: Tensor, b: anytype) Tensor { + return self.sub(Tensor.scalar(b, self.dtype())); + } + /// Returns a Tensor containing the element-wise division of the input Tensor by a constant. pub fn divByConst(self: Tensor, b: anytype) Tensor { return self.div(Tensor.scalar(b, self.dtype())); diff --git a/zml/tools/tracer.zig b/zml/tools/tracer.zig index ed7d7fe1..a8abbb13 100644 --- a/zml/tools/tracer.zig +++ b/zml/tools/tracer.zig @@ -2,9 +2,61 @@ const builtin = @import("builtin"); pub const Tracer = switch (builtin.os.tag) { .macos => MacOsTracer, + .linux => LinuxTracer, else => FakeTracer, }; +const LinuxTracer = struct { + const c = @import("c"); + + extern fn cudaProfilerStart() c_int; + extern fn cudaProfilerStop() c_int; + + extern fn nvtxMarkA(message: [*:0]const u8) void; + extern fn nvtxRangeStartA(message: [*:0]const u8) c_int; + extern fn nvtxRangeEnd(id: c_int) void; + + pub fn init(name: [:0]const u8) LinuxTracer { + _ = name; + _ = cudaProfilerStart(); + return .{}; + } + + pub fn deinit(self: *const LinuxTracer) void { + _ = self; + _ = cudaProfilerStop(); + } + + pub fn event(self: *const LinuxTracer, message: [:0]const u8) void { + _ = self; + nvtxMarkA(message.ptr); + } + + pub fn frameStart(self: *const LinuxTracer, message: [:0]const u8) u64 { + _ = self; + return @intCast(nvtxRangeStartA(message.ptr)); + } + + pub fn frameEnd(self: *const LinuxTracer, interval_id: u64, message: [:0]const u8) void { + _ = self; + _ = message; + nvtxRangeEnd(@intCast(interval_id)); + return; + } + + // zero the structure + // nvtxEventAttributes_t eventAttrib = {0}; + // // set the version and the size information + // eventAttrib.version = NVTX_VERSION; + // eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE; + // // configure the attributes. 0 is the default for all attributes. + // eventAttrib.colorType = NVTX_COLOR_ARGB; + // eventAttrib.color = 0xFF880000; + // eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII; + // eventAttrib.message.ascii = "Example nvtxMarkEx"; + // nvtxMarkEx(&eventAttrib); +}; + const MacOsTracer = struct { const c = @import("c");