diff --git a/examples/simple_layer/main.zig b/examples/simple_layer/main.zig index 6f61c0b0..6535ba95 100644 --- a/examples/simple_layer/main.zig +++ b/examples/simple_layer/main.zig @@ -2,13 +2,27 @@ const std = @import("std"); const zml = @import("zml"); const asynk = @import("async"); +pub const std_options = .{ + .log_level = .info, + .logFn = asynk.logFn(std.log.defaultLog), +}; + /// Model definition const Layer = struct { bias: ?zml.Tensor = null, weight: zml.Tensor, pub fn forward(self: Layer, x: zml.Tensor) zml.Tensor { - var y = self.weight.mul(x); + // var y = self.weight.mul(x); + var y = zml.ops.triton( + \\ module { + \\ func.func @my_mul(%arg0: tensor<1xf16>, %arg1: tensor<1xf16>) -> tensor<1xf16> { + \\ %0 = arith.mulf %arg0, %arg1 : tensor<1xf16> + \\ return %0 : tensor<1xf16> + \\ } + \\ } + , .{ self.weight, x }, x.shape()); + if (self.bias) |bias| { y = y.add(bias); } @@ -34,7 +48,11 @@ pub fn asyncMain() !void { var context = try zml.Context.init(); defer context.deinit(); - const platform = context.autoPlatform(.{}); + const platform = context.autoPlatform(.{}).withCompilationOptions(.{ + .xla_dump_to = "/tmp/zml/simple_layer", + .xla_dump_fusion_visualization = true, + .xla_dump_hlo_pass_re = "fusion", + }); context.printAvailablePlatforms(platform); // Our weights and bias to use diff --git a/runtimes/cuda/BUILD.bazel b/runtimes/cuda/BUILD.bazel index a7f8bf94..f72502ef 100644 --- a/runtimes/cuda/BUILD.bazel +++ b/runtimes/cuda/BUILD.bazel @@ -39,3 +39,8 @@ filegroup( srcs = [], visibility = ["//visibility:public"], ) + +exports_files( + ["libpjrt_cuda.so"], + visibility = ["//visibility:public"], +) diff --git a/runtimes/cuda/libpjrt_cuda.BUILD.bazel b/runtimes/cuda/libpjrt_cuda.BUILD.bazel index 95ea1992..62f24dd1 100644 --- a/runtimes/cuda/libpjrt_cuda.BUILD.bazel +++ b/runtimes/cuda/libpjrt_cuda.BUILD.bazel @@ -25,7 +25,7 @@ copy_to_directory( cc_import( name = "libpjrt_cuda", data = [":sandbox"], - shared_library = "libpjrt_cuda.so", + shared_library = "@zml//runtimes/cuda:libpjrt_cuda.so", add_needed = ["libzmlxcuda.so.0"], rename_dynamic_symbols = { "dlopen": "zmlxcuda_dlopen", diff --git a/zml/module.zig b/zml/module.zig index 453f0cb0..aaac42fa 100644 --- a/zml/module.zig +++ b/zml/module.zig @@ -913,7 +913,6 @@ fn compileModuleToPjrtExecutable(arena: std.mem.Allocator, platform: Platform, m .cuda => { // NVIDIA recommends these settings // https://github.com/NVIDIA/JAX-Toolbox?tab=readme-ov-file#environment-variables - setFlag(&options, "xla_gpu_enable_triton_gemm", false); setFlag(&options, "xla_gpu_enable_latency_hiding_scheduler", true); setFlag(&options, "xla_gpu_enable_llvm_module_compilation_parallelism", true); setFlag(&options, "xla_gpu_enable_libnvptxcompiler", true); diff --git a/zml/ops.zig b/zml/ops.zig index c73dc689..aa8472e3 100644 --- a/zml/ops.zig +++ b/zml/ops.zig @@ -1155,6 +1155,29 @@ fn scatterPrepareIndices( return Tensor.stack(indices.constSlice(), .last, .coord); } +/// Generate an MLIR call to the given member function with the given tensors. +pub fn triton(triton_ir: [:0]const u8, args: anytype, res_shape: Shape) Tensor { + const ctx = CompilationContext.current(); + + var values: [args.len]mlir.Value = undefined; + ctx.extractValues(&args, &values); + + const op = dialect.stablehlo.custom_call( + ctx.mlirCtx(), + &values, + .{ + .call_target_name = "triton::snippet", + .backend_config = triton_ir, + .api_version = 2, + .has_side_effect = false, + .output_operand_aliases = &.{}, + }, + &.{mlir.ext.mlirType(ctx.mlirCtx(), res_shape)}, + ctx.mlirCtx().location(@src()), + ); + return Tensor._result(res_shape, op.result(0)); +} + inline fn toI64(values: anytype) []i64 { var res: [Tensor.MAX_RANK]i64 = undefined; for (values, 0..) |val, i| res[i] = @intCast(val);