diff --git a/.github/workflows/commit.yaml b/.github/workflows/commit.yaml index 790ee3ca..d6af07af 100644 --- a/.github/workflows/commit.yaml +++ b/.github/workflows/commit.yaml @@ -45,6 +45,7 @@ jobs: common --test_output=streamed common --color=yes common --show_timestamps + common --strip=always - name: ZML / bazel mod explain run: bazel mod explain diff --git a/MODULE.bazel b/MODULE.bazel index 45fb05de..bf7e434e 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -6,7 +6,7 @@ bazel_dep(name = "abseil-cpp", version = "20240722.0.bcr.2") bazel_dep(name = "aspect_bazel_lib", version = "2.14.0") bazel_dep(name = "aspect_rules_py", version = "1.3.2") bazel_dep(name = "bazel_skylib", version = "1.7.1") -bazel_dep(name = "libxev", version = "20250313.0-5773f46") +bazel_dep(name = "libxev", version = "20250716.0-7f80318") bazel_dep(name = "patchelf", version = "0.18.0") bazel_dep(name = "pcre2", version = "10.43") bazel_dep(name = "platforms", version = "0.0.11") @@ -23,7 +23,7 @@ bazel_dep(name = "toolchains_llvm_bootstrapped", version = "0.2.3") bazel_dep(name = "toolchains_protoc", version = "0.4.1") bazel_dep(name = "with_cfg.bzl", version = "0.9.1") bazel_dep(name = "xla", version = "20250710.0-22ea002") -bazel_dep(name = "zig-protobuf", version = "20250318.0-930153e") +bazel_dep(name = "zig-protobuf", version = "20250716.0-7627e25") bazel_dep(name = "zig-yaml", version = "20240903.0-83d5fdf") bazel_dep(name = "buildifier_prebuilt", version = "8.0.3", dev_dependency = True) diff --git a/async/asyncio.zig b/async/asyncio.zig index 1f41f90a..7fe05903 100644 --- a/async/asyncio.zig +++ b/async/asyncio.zig @@ -115,10 +115,10 @@ pub const TCP = struct { exec: *Executor, tcp: xev.TCP, - pub usingnamespace Closeable(Self, xev.TCP); - pub usingnamespace Pollable(Self, xev.TCP); - pub usingnamespace Readable(Self, xev.TCP); - pub usingnamespace Writeable(Self, xev.TCP); + pub const close = Closeable(Self, xev.TCP); + pub const poll = Pollable(Self, xev.TCP); + pub const read = Readable(Self, xev.TCP); + pub const write = Writeable(Self, xev.TCP); pub fn init(exec: *Executor, tcp: xev.TCP) Self { return .{ .exec = exec, .tcp = tcp }; @@ -214,11 +214,20 @@ pub const TCP = struct { } }; -fn Closeable(comptime T: type, comptime StreamT: type) type { +pub const AsyncError = error{ + AccessDenied, + EventNotFound, + SystemResources, + ProcessNotFound, + Overflow, + NestedRunsNotAllowed, +}; + +fn Closeable(comptime T: type, comptime StreamT: type) fn (T) anyerror!void { return struct { const Self = T; const CloseResult = xev.CloseError!void; - pub fn close(self: Self) !void { + pub fn close(self: Self) anyerror!void { const ResultT = CloseResult; const Data = struct { result: ResultT = undefined, @@ -251,14 +260,13 @@ fn Closeable(comptime T: type, comptime StreamT: type) type { return data.result; } - }; + }.close; } -fn Pollable(comptime T: type, comptime StreamT: type) type { +fn Pollable(comptime T: type, comptime StreamT: type) fn (T) anyerror!void { return struct { - const Self = T; const PollResult = xev.PollError!void; - pub fn poll(self: Self) !void { + pub fn poll(self: T) anyerror!void { const ResultT = PollResult; const Data = struct { result: ResultT = undefined, @@ -291,14 +299,14 @@ fn Pollable(comptime T: type, comptime StreamT: type) type { return data.result; } - }; + }.poll; } -fn Readable(comptime T: type, comptime StreamT: type) type { +fn Readable(comptime T: type, comptime StreamT: type) fn (T, xev.ReadBuffer) anyerror!usize { return struct { const Self = T; const ReadResult = xev.ReadError!usize; - pub fn read(self: Self, buf: xev.ReadBuffer) !usize { + pub fn read(self: Self, buf: xev.ReadBuffer) anyerror!usize { const ResultT = ReadResult; const Data = struct { result: ResultT = undefined, @@ -333,10 +341,10 @@ fn Readable(comptime T: type, comptime StreamT: type) type { return data.result; } - }; + }.read; } -fn Writeable(comptime T: type, comptime StreamT: type) type { +fn Writeable(comptime T: type, comptime StreamT: type) fn (T, xev.WriteBuffer) anyerror!usize { return struct { const Self = T; const WriteResult = xev.WriteError!usize; @@ -374,7 +382,7 @@ fn Writeable(comptime T: type, comptime StreamT: type) type { try waitForCompletion(self.exec, &c); return data.result; } - }; + }.write; } pub const File = struct { @@ -383,10 +391,10 @@ pub const File = struct { exec: *Executor, file: xev.File, - pub usingnamespace Closeable(Self, xev.File); - pub usingnamespace Pollable(Self, xev.File); - pub usingnamespace Readable(Self, xev.File); - pub usingnamespace Writeable(Self, xev.File); + pub const close = Closeable(Self, xev.File); + pub const poll = Pollable(Self, xev.File); + pub const read = Readable(Self, xev.File); + pub const write = Writeable(Self, xev.File); pub fn init(exec: *Executor, file: xev.File) Self { return .{ .exec = exec, .file = file }; @@ -527,10 +535,8 @@ pub const UDP = struct { exec: *Executor, udp: xev.UDP, - pub usingnamespace Closeable(Self, xev.TCP); - pub usingnamespace Pollable(Self, xev.TCP); - pub usingnamespace Readable(Self, xev.TCP); - pub usingnamespace Writeable(Self, xev.TCP); + pub const close = Closeable(Self, xev.UDP); + pub const poll = Pollable(Self, xev.UDP); pub fn init(exec: *Executor, udp: xev.UDP) Self { return .{ .exec = exec, .udp = udp }; @@ -541,7 +547,7 @@ pub const UDP = struct { } const ReadResult = xev.ReadError!usize; - pub fn read(self: Self, buf: xev.ReadBuffer) !usize { + pub fn read(self: Self, buf: xev.ReadBuffer) anyerror!usize { const ResultT = ReadResult; const Data = struct { result: ResultT = undefined, @@ -582,7 +588,7 @@ pub const UDP = struct { } const WriteResult = xev.WriteError!usize; - pub fn write(self: Self, addr: std.net.Address, buf: xev.WriteBuffer) !usize { + pub fn write(self: Self, addr: std.net.Address, buf: xev.WriteBuffer) anyerror!usize { const ResultT = WriteResult; const Data = struct { result: ResultT = undefined, diff --git a/bazel/zig_srcs.bzl b/bazel/zig_srcs.bzl index 2aa79568..4fcb5698 100644 --- a/bazel/zig_srcs.bzl +++ b/bazel/zig_srcs.bzl @@ -8,6 +8,10 @@ def zig_srcs(name, zig_bin="", zig_lib=""): It's also possible to pass zig_lib instead of zig_bin in which case, The rule takes care of creating an intermediary binary from the lib. """ + # TODO: this forces to build the test target which isn't needed. + # The problem is we need the binary target to get the translate-c output. + # What is missing from rules_zig is a "translate_c" target that we could call directly + # without needing to build the corresponding binary. if zig_bin == "": zig_bin = "{}_bin".format(name) zig_binary( diff --git a/build.zig b/build.zig deleted file mode 120000 index 0468ff14..00000000 --- a/build.zig +++ /dev/null @@ -1 +0,0 @@ -examples/build.zig \ No newline at end of file diff --git a/build.zig b/build.zig new file mode 100644 index 00000000..7777cb05 --- /dev/null +++ b/build.zig @@ -0,0 +1,497 @@ +const std = @import("std"); + +/// !!! This build.zig is experimental !!! +/// +/// It shows how to bridge Bazel and build.zig. +/// It requires the user to have `bazel` and `tar` installed on their machine. +/// +/// Bazel is used to: +/// +/// * compile C and C++ deps into .a files. +/// * call zig-translate C +/// * generating Zig files +/// * tarring Zig sources +/// +/// build.zig finishes the work by: +/// * untarring Zig sources +/// * copying .a files into zig-cache +/// * creating "zig modules" visible to other build.zig. +/// +/// `zig build test --summary all` will run a lot of tests +/// `zig build test-zml` will stick to ZML test suite +/// `zig build -Druntimes:cpu=true -Druntimes:cuda=false test` will enable/disable runtimes +/// +/// Caveats: +/// +/// * requires a local bazel installed and tar utility +/// * only work for the CPU plugins +/// * pjrt_cuda.so fails to load +/// * requires a build of zig with the following patch: +/// https://github.com/ziglang/zig/commit/8eb58132eb02c843a54ec7baa6337f54dd024648 +/// this works around https://github.com/ziglang/zig/issues/23152 +/// +/// Potential follow ups: +/// * The main missing is that Bazel takes care of sandboxing +/// ZML executables and the PJRT runtimes and all the dependencies. +/// This part has not been added to build.zig beyond mininal rPath for the the libpjrt_xxx.so, +/// but for cuda you have more depedencies that would need to be sandboxed. +/// +/// * The module graph of the various protobuffers is alson a bit big, +/// and if it changes it will be tedious to update. Ideally bazel should generate this graph of deps for us. +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + const platforms = Platforms.parse(b); + + const test_step = b.step("test", "Run all tests across ZML and deps"); + + // stdx + const stdx = b.addModule("stdx", .{ + .root_source_file = b.path("stdx/stdx.zig"), + .target = target, + .optimize = optimize, + }); + + const stdx_test = b.addTest(.{ .root_module = stdx }); + const run_stdx_tests = b.addRunArtifact(stdx_test); + test_step.dependOn(&run_stdx_tests.step); + + // mlir + const mlir_srcs = Tarball.sources(b, "mlir/dialects"); + const mlir_c_deps = mlir_srcs.extractModule( + "mlir/dialects", + "mlir/dialects", + "test_test_lib_c.zig", + .{ .link_libcpp = true }, + ); + const mlir_obj = objectFromBazel(b, "//mlir/dialects:mlir_static", "mlir/dialects/libmlir_static.a"); + + const mlir = b.addModule("mlir", .{ + .root_source_file = b.path("mlir/mlir.zig"), + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "c", .module = mlir_c_deps }, + .{ .name = "stdx", .module = stdx }, + }, + }); + + const mlir_test = b.addTest(.{ .root_module = mlir }); + // TODO: I'm not sure what's the best idea: add the object to the compile step or directly to the module. + mlir_test.addObjectFile(mlir_obj); + const run_mlir_tests = b.addRunArtifact(mlir_test); + test_step.dependOn(&run_mlir_tests.step); + + const stablehlo = b.addModule("mlir/dialects/stablehlo", .{ + .root_source_file = b.path("mlir/dialects/stablehlo.zig"), + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "c", .module = mlir_c_deps }, + .{ .name = "mlir", .module = mlir }, + .{ .name = "stdx", .module = stdx }, + }, + }); + + const mlir_dialects = b.addModule("mlir/dialects", .{ + .root_source_file = b.path("mlir/dialects/dialects.zig"), + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "mlir", .module = mlir }, + .{ .name = "mlir/dialects/stablehlo", .module = stablehlo }, + }, + }); + + const mlir_dialects_test = b.addTest(.{ .root_module = mlir_dialects }); + mlir_dialects_test.addObjectFile(mlir_obj); + const run_mlir_dialects_tests = b.addRunArtifact(mlir_dialects_test); + test_step.dependOn(&run_mlir_dialects_tests.step); + + // pjrt + const pjrt_srcs = Tarball.sources(b, "pjrt"); + const pjrt_c_deps = pjrt_srcs.extractModule( + null, + "pjrt", + "sources_bin_c.zig", + .{ .link_libcpp = true }, + ); + + const pjrt = b.addModule("pjrt", .{ + .root_source_file = b.path("pjrt/pjrt.zig"), + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "c", .module = pjrt_c_deps }, + .{ .name = "stdx", .module = stdx }, + }, + }); + + const pjrt_test = b.addTest(.{ .root_module = pjrt }); + const run_pjrt_tests = b.addRunArtifact(pjrt_test); + test_step.dependOn(&run_pjrt_tests.step); + + const zml_sandbox = b.addNamedWriteFiles("zml_sandbox"); + switch (target.result.os.tag) { + .macos => { + if (platforms.cpu) { + if (b.lazyDependency("pjrt_cpu_darwin_arm64", .{})) |dep| { + _ = zml_sandbox.addCopyFile(dep.path("libpjrt_cpu.dylib"), "libpjrt_cpu.dylib"); + } + } + }, + .linux => { + if (platforms.cpu) { + if (b.lazyDependency("pjrt_cpu_linux_amd64", .{})) |dep| { + _ = zml_sandbox.addCopyFile(dep.path("libpjrt_cpu.so"), "libpjrt_cpu.so"); + } + } + + if (platforms.cuda) { + // TODO: this is not enough for libpjrt_cuda, there are a lot more deps needed. + const zmlxcuda = objectFromBazel(b, "@libpjrt_cuda//:zmlxcuda_so", "external/+cuda_packages+libpjrt_cuda/libzmlxcuda.so.0"); + _ = zml_sandbox.addCopyFile(zmlxcuda, "libzmlxcuda.so.0"); + + const plugin = objectFromBazel(b, "//runtimes/cuda:libpjrt_cuda", "runtimes/cuda/libpjrt_cuda.so"); + _ = zml_sandbox.addCopyFile(plugin, "libpjrt_cuda.so"); + } + }, + else => |os| std.debug.panic("Target not supported: {s}", .{@tagName(os)}), + } + + // xev + const async_srcs = Tarball.sources(b, "async"); + const xev = async_srcs.extractModule( + "xev", + "src", + "main.zig", + .{ + .target = target, + .optimize = optimize, + .link_libc = target.result.os.tag == .linux, + }, + ); + + const xev_test = b.addTest(.{ .root_module = xev }); + const run_xev_tests = b.addRunArtifact(xev_test); + test_step.dependOn(&run_xev_tests.step); + + // async + const async_mod = b.addModule( + "async", + .{ + .root_source_file = b.path("async/async.zig"), + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "xev", .module = xev }, + .{ .name = "stdx", .module = stdx }, + }, + }, + ); + + const async_test = b.addTest(.{ .root_module = async_mod }); + const run_async_tests = b.addRunArtifact(async_test); + test_step.dependOn(&run_async_tests.step); + + const zml_deps = Tarball.zmlDepsTarball(b, platforms); + // ffi + const ffi = zml_deps.extractModule( + "ffi", + "ffi", + "ffi.zig", + .{ .target = target, .optimize = optimize }, + ); + + const ffi_test = b.addTest(.{ .root_module = ffi }); + const run_ffi_tests = b.addRunArtifact(ffi_test); + test_step.dependOn(&run_ffi_tests.step); + + // hftokenizers + const hftokenizers = zml_deps.extractModule( + "//zml/tokenizer/hftokenizers", + "zml/tokenizer/hftokenizers", + "hftokenizers.zig", + .{ .target = target, .optimize = optimize }, + ); + + // sentencepiece + const sentencepiece_c_deps_srcs = Tarball.sources(b, "zml/tokenizer/sentencepiece"); + const sentencepiece_c_deps = sentencepiece_c_deps_srcs.extractModule( + null, + "zml/tokenizer/sentencepiece", + "sources_bin_c.zig", + .{ .link_libcpp = true }, + ); + const sentencepiece = zml_deps.extractModule( + "//zml/tokenizer/sentencepiece", + "zml/tokenizer/sentencepiece", + "sentencepiece.zig", + .{ + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "c", .module = sentencepiece_c_deps }, + }, + }, + ); + + // tokenizer + const tokenizer = b.addModule( + "tokenizer", + .{ + .root_source_file = b.path("zml/tokenizer/tokenizer.zig"), + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "ffi", .module = ffi }, + // Note: even though we import sentencepiece and hftokenizers, + // we don't link the corresponding .a cause there is no tests + // for them directly. + .{ .name = "hftokenizers", .module = hftokenizers }, + .{ .name = "sentencepiece", .module = sentencepiece }, + }, + }, + ); + + const tokenizer_test = b.addTest(.{ .root_module = tokenizer }); + const run_tokenizer_tests = b.addRunArtifact(tokenizer_test); + test_step.dependOn(&run_tokenizer_tests.step); + + // proto + const protobuf = zml_deps.extractModule("protobuf", "src", "protobuf.zig", .{}); + const xla_compile_proto = xla_proto: { + // Ideally we should ask bazel to generate this graph of dependencies between the different proto files, + // Or change altogether how protobuf are generated and only create one zig module for all protobuf stuff. + const empty: *std.Build.Module = empty: { + const write = b.addWriteFiles(); + const root = write.add("root.zig", "//! empty module"); + break :empty b.createModule(.{ .root_source_file = root }); + }; + const opts: std.Build.Module.CreateOptions = .{ .imports = &.{.{ .name = "protobuf", .module = protobuf }} }; + + const duration = zml_deps.extractModule(null, googleProtobuf("duration"), "duration.pb.zig", opts); + const any = zml_deps.extractModule(null, googleProtobuf("any"), "any.pb.zig", opts); + const wrappers = zml_deps.extractModule(null, googleProtobuf("wrappers"), "wrappers.pb.zig", opts); + + const data = zml_deps.extractModule(null, "xla", "xla_data.pb.zig", opts); + const service_hlo = zml_deps.extractModule(null, "xla/service", "hlo.pb.zig", opts); + const xla = zml_deps.extractModule(null, "xla", "xla.pb.zig", opts); + xla.addImport("google_protobuf_any_proto", any); + xla.addImport("xla_xla_data_proto", data); + xla.addImport("xla_service_hlo_proto", service_hlo); + xla.addImport("xla_autotune_results_proto", empty); + + const tsl_dnn = zml_deps.extractModule(null, "xla/tsl/protobuf", "dnn.pb.zig", opts); + tsl_dnn.addImport("google_protobuf_wrappers_proto", wrappers); + + const autotuning = zml_deps.extractModule(null, "xla", "autotuning.pb.zig", opts); + autotuning.addImport("google_protobuf_duration_proto", duration); + autotuning.addImport("xla_tsl_protobuf_dnn_proto", tsl_dnn); + const autotune = zml_deps.extractModule(null, "xla", "autotune_results.pb.zig", opts); + autotune.addImport("xla_autotuning_proto", autotuning); + + const stream_executor_cuda_cuda_compute_capability = zml_deps.extractModule(null, "xla/stream_executor/cuda", "cuda_compute_capability.pb.zig", opts); + const stream_executor_device_description = zml_deps.extractModule(null, "xla/stream_executor", "device_description.pb.zig", opts); + stream_executor_device_description.addImport("xla_autotune_results_proto", autotune); + stream_executor_device_description.addImport("xla_stream_executor_cuda_cuda_compute_capability_proto", stream_executor_cuda_cuda_compute_capability); + + const compile = zml_deps.extractModule(null, "xla/pjrt/proto", "compile_options.pb.zig", opts); + compile.addImport("xla_xla_proto", xla); + compile.addImport("xla_xla_data_proto", data); + compile.addImport("xla_stream_executor_device_description_proto", stream_executor_device_description); + + break :xla_proto compile; + }; + + // zml c deps + const zml_c_deps = zml_deps.extractModule( + null, + "zml", + "test_test_lib_c.zig", + .{ .link_libcpp = true }, + ); + + // bazel runfiles + // TODO: should we also generate a "runfiles" folder to provide the expected runtime information ? + const runfiles = zml_deps.extractModule("runfiles", "zig/runfiles", "runfiles.zig", .{}); + + // runtimes + const runtimes = zml_deps.extractModule( + "runtimes", + "runtimes", + "runtimes.zig", + .{ + .imports = &.{ + .{ .name = "c", .module = zml_c_deps }, + .{ .name = "pjrt", .module = pjrt }, + }, + }, + ); + + inline for (@typeInfo(Platforms).@"struct".fields) |field| { + const platform = field.name; + const runtime_path = b.pathJoin(&.{ "runtimes", platform }); + const zig_file = std.mem.concat(b.allocator, u8, &.{ platform, ".zig" }) catch @panic("OOM"); + const builtin_name = std.fmt.allocPrint(b.allocator, "bazel_builtin_A_S_Sruntimes_S{0s}_C{0s}.zig", .{platform}) catch @panic("OOM"); + const bazel_builtin = zml_deps.extractModule(null, runtime_path, builtin_name, .{}); + + const platform_runtime = b.addModule( + runtime_path, + .{ + .root_source_file = b.path(b.pathJoin(&.{ runtime_path, zig_file })), + .imports = &.{ + .{ .name = "c", .module = zml_c_deps }, + .{ .name = "runfiles", .module = runfiles }, + .{ .name = "stdx", .module = stdx }, + .{ .name = "pjrt", .module = pjrt }, + .{ .name = "async", .module = async_mod }, + .{ .name = "bazel_builtin", .module = bazel_builtin }, + }, + }, + ); + const import_name = std.mem.concat(b.allocator, u8, &.{ "runtimes/", platform }) catch @panic("OOM"); + runtimes.addImport(import_name, platform_runtime); + } + + // zml/tools + const zml_tools = b.createModule( + .{ + .root_source_file = b.path("zml/tools/tools.zig"), + .link_libcpp = target.result.os.tag == .macos, + .imports = &.{.{ .name = "c", .module = zml_c_deps }}, + }, + ); + const macos_tools_obj = objectFromBazel(b, "//zml/tools:macos_static_tools", "zml/tools/libmacos_static_tools.a"); + if (target.result.os.tag == .macos) { + zml_tools.addObjectFile(macos_tools_obj); + } + + // zml + const zml = b.addModule( + "zml", + .{ + .root_source_file = b.path("zml/zml.zig"), + .target = target, + .optimize = optimize, + .link_libcpp = true, + .imports = &.{ + .{ .name = "c", .module = zml_c_deps }, + .{ .name = "//xla:xla_proto", .module = xla_compile_proto }, + .{ .name = "async", .module = async_mod }, + .{ .name = "mlir", .module = mlir }, + .{ .name = "mlir/dialects", .module = mlir_dialects }, + .{ .name = "pjrt", .module = pjrt }, + .{ .name = "runtimes", .module = runtimes }, + .{ .name = "stdx", .module = stdx }, + .{ .name = "zml/tokenizer", .module = tokenizer }, + .{ .name = "zml/tools", .module = zml_tools }, + .{ .name = "runfiles", .module = runfiles }, + }, + }, + ); + + const zml_test = b.addTest(.{ + .root_module = zml, + .test_runner = .{ .mode = .simple, .path = b.path("zml/test_runner.zig") }, + }); + // This is where we will put all the .so needed. + zml_test.addRPath(zml_sandbox.getDirectory()); + + const run_zml_tests = b.addRunArtifact(zml_test); + + const zml_test_step = b.step("test-zml", "Run ZML tests (assumes pjrt.dylib are in the path)"); + zml_test_step.dependOn(&run_zml_tests.step); + test_step.dependOn(&run_zml_tests.step); +} + +/// Take the name of a Bazel `cc_static_library` and create a object LazyPath from it. +/// The object need to be added to a Compile step with `step.addObjectFile(obj)`. +fn objectFromBazel(b: *std.Build, target: []const u8, output: []const u8) std.Build.LazyPath { + // TODO: consider parsing bazel target to generate output path. + const bazel_cmd = b.addSystemCommand(&.{ "bazel", "build", "-c", "opt", target }); + const obj_path = b.pathJoin(&.{ "bazel-bin", output }); + + // Copy bazel output into zig-cache, cause bazel may remove the file later. + const cp = b.addWriteFiles(); + cp.step.dependOn(&bazel_cmd.step); + return cp.addCopyFile(b.path(obj_path), output); +} + +const Tarball = struct { + create_cmd: *std.Build.Step.Run, + path: std.Build.LazyPath, + + /// Ask bazel for the full sources of a zig module. + /// This is needed for module that have generated zig sources, + /// like the output of zig translate-c or protobuf generated sources. + pub fn sources(b: *std.Build, name: []const u8) Tarball { + const allocator = b.allocator; + const target = std.mem.concat(allocator, u8, &.{ "//", name, ":sources" }) catch @panic("OOM"); + const bazel_cmd: *std.Build.Step.Run = b.addSystemCommand(&.{ "bazel", "build", target }); + + const srcs_tar = b.path(b.pathJoin(&.{ "bazel-bin", name, "sources.tar" })); + return .{ .create_cmd = bazel_cmd, .path = srcs_tar }; + } + + /// Zml is a bit different from other targets cause the code of the "runtimes/**.zig" + /// will change based on the platforms selected. + pub fn zmlDepsTarball(b: *std.Build, platforms: Platforms) Tarball { + const zml_deps_tar = b.option([]const u8, "zml_deps_tar", "Path to Bazel generated ZML deps (default generate from bazel)"); + + return if (zml_deps_tar) |tarball| .{ + .create_cmd = b.addSystemCommand(&.{ "echo", "Using zml deps tarball", tarball }), + .path = b.path(tarball), + } else { + const bazel_cmd: *std.Build.Step.Run = b.addSystemCommand(&.{ "bazel", "build", "//zml:sources" }); + bazel_cmd.addArg(if (platforms.cpu) "--@zml//runtimes:cpu=true" else "--@zml//runtimes:cpu=false"); + bazel_cmd.addArg(if (platforms.cuda) "--@zml//runtimes:cuda=true" else "--@zml//runtimes:cuda=false"); + + const srcs_tar = b.path(b.pathJoin(&.{ "bazel-bin", "zml", "sources.tar" })); + return .{ .create_cmd = bazel_cmd, .path = srcs_tar }; + }; + } + + pub fn extractModule(self: Tarball, module_name: ?[]const u8, directory: []const u8, root: []const u8, options: std.Build.Module.CreateOptions) *std.Build.Module { + const b = self.create_cmd.step.owner; + const tar_cmd = b.addSystemCommand(&.{ "tar", "-xf" }); + tar_cmd.step.dependOn(&self.create_cmd.step); + tar_cmd.addFileArg(self.path); + tar_cmd.addArg("-C"); + const out_dir = tar_cmd.addOutputDirectoryArg("sources"); + tar_cmd.addArg(directory); + + var opts = options; + if (opts.root_source_file != null) @panic("extractModule is already setting the root_source_file option"); + opts.root_source_file = out_dir.path(b, b.pathJoin(&.{ directory, root })); + return if (module_name) |name| + b.addModule(name, opts) + else + b.createModule(opts); + } +}; + +fn addRuntimeDeps(run_step: *std.Build.Step.Run, libs: []const *std.Build.Step.InstallFile) void { + for (libs) |lib| { + run_step.step.dependOn(&lib.step); + } +} + +fn googleProtobuf(comptime name: []const u8) []const u8 { + return "src/google/protobuf/_virtual_imports/" ++ name ++ "_proto/google/protobuf"; +} + +const Platforms = struct { + cpu: bool, + cuda: bool, + rocm: bool = false, + tpu: bool = false, + neuron: bool = false, + + pub fn parse(b: *std.Build) Platforms { + return .{ + .cpu = b.option(bool, "runtimes:cpu", "Enable cpu runtime (default: true)") orelse true, + .cuda = b.option(bool, "runtimes:cuda", "Enable cuda runtime") orelse false, + }; + } +}; diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 00000000..4bad5174 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,61 @@ +.{ + .name = .zml, + .version = "0.0.0", + + .fingerprint = 0x32700c0d288fdb37, // Changing this has security and trust implications. + + .minimum_zig_version = "0.14.0", + + // This field is optional. + // Each dependency must either provide a `url` and `hash`, or a `path`. + // `zig build --fetch` can be used to fetch all dependencies of a package, recursively. + // Once all dependencies are fetched, `zig build` no longer requires + // internet connectivity. + .dependencies = .{ + .pjrt_cpu_darwin_arm64 = .{ + .url = "https://github.com/zml/pjrt-artifacts/releases/download/v10.0.0/pjrt-cpu_darwin-arm64.tar.gz", + .hash = "N-V-__8AADBK6gU12pCj6Yo3Q5zYgUefD3uH5kShV2pHGyOS", + .lazy = true, + }, + .pjrt_cpu_linux_amd64 = .{ + .url = "https://github.com/zml/pjrt-artifacts/releases/download/v10.0.0/pjrt-cpu_linux-amd64.tar.gz", + .hash = "N-V-__8AAAjaOQyQKeieiaG2csxnfXzIkaPNN4aX7p247PLE", + .lazy = true, + }, + }, + + // Specifies the set of files and directories that are included in this package. + // Only files and directories listed here are included in the `hash` that + // is computed for this package. Only files listed here will remain on disk + // when using the zig package manager. As a rule of thumb, one should list + // files required for compilation plus any license(s). + // Paths are relative to the build root. Use the empty string (`""`) to refer to + // the build root itself. + // A directory listed here means that all files within, recursively, are included. + .paths = .{ + "async", + "bazel", + "ffi", + "mlir", + "pjrt", + "platforms", + "runtimes", + "stdx", + "third_party", + "zml", + "build.zig", + "build.zig.zon", + // bazel files + "BUILD.bazel", + "MODULE.bazel", + "MODULE.bazel.lock", + // symlinks + ".bazelrc", + ".bazelrc-zml", + "examples/.bazelrc", + ".bazelignore", + "examples/.bazelignore", + ".bazelversion", + "examples/.bazelversion", + }, +} diff --git a/examples/mnist/build.zig b/examples/mnist/build.zig new file mode 100644 index 00000000..f53c8c08 --- /dev/null +++ b/examples/mnist/build.zig @@ -0,0 +1,45 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + // Note: setting -Dcuda on CLI fails cause zml_sandbox doesn't yet contains all CUDA deps. + const cuda = b.option(bool, "cuda", "Enable cuda (broken)") orelse false; + const zml_pkg = b.dependency("zml", .{ + .@"runtimes:cuda" = cuda, + .@"runtimes:cpu" = !cuda, + }); + const zml_sandbox = zml_pkg.namedWriteFiles("zml_sandbox"); + + const mnist = b.createModule(.{ + .root_source_file = b.path("mnist.zig"), + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "async", .module = zml_pkg.module("async") }, + .{ .name = "zml", .module = zml_pkg.module("zml") }, + }, + }); + + const exe = b.addExecutable(.{ .name = "mnist", .root_module = mnist }); + exe.addRPath(zml_sandbox.getDirectory()); + b.installArtifact(exe); + + const run_cmd = b.addRunArtifact(exe); + { + const ggml = b.dependency("ggml", .{}); + const mnist_model = ggml.path("examples/mnist/models/mnist/mnist_model.state_dict"); + const mnist_data = ggml.path("examples/mnist/models/mnist/t10k-images.idx3-ubyte"); + + run_cmd.addFileArg(mnist_model); + run_cmd.addFileArg(mnist_data); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/examples/mnist/build.zig.zon b/examples/mnist/build.zig.zon new file mode 100644 index 00000000..a2dd4cc5 --- /dev/null +++ b/examples/mnist/build.zig.zon @@ -0,0 +1,25 @@ +.{ + .name = .mnist, + .version = "0.0.0", + + .fingerprint = 0x6805ab6e16b3f141, // Changing this has security and trust implications. + + .minimum_zig_version = "0.14.0", + .dependencies = .{ + .zml = .{ + .url = "git+https://github.com/zml/zml#e6ec751e6e478c64113169c28aaed278d6ce13e9", + .hash = "zml-0.0.0-N9uPKHO8IgABeyC0QM0tqVA4wyL4lVr8vwCvGEQgrDqU", + }, + // Get MNIST model and data from GGML repo. + .ggml = .{ + .url = "git+https://github.com/ggerganov/ggml#18703ad600cc68dbdb04d57434c876989a841d12", + .hash = "N-V-__8AAGUBBAESVQ00NaQbKKLu9KDR54aQyFBxUmYvJLF4", + }, + }, + + .paths = .{ + "build.zig", + "build.zig.zon", + "mnist.zig", + }, +} diff --git a/third_party/modules/libxev/20250716.0-7f80318/MODULE.bazel b/third_party/modules/libxev/20250716.0-7f80318/MODULE.bazel new file mode 100644 index 00000000..62de098a --- /dev/null +++ b/third_party/modules/libxev/20250716.0-7f80318/MODULE.bazel @@ -0,0 +1,7 @@ +module( + name = "libxev", + version = "20250716.0-7f80318", + compatibility_level = 1, +) + +bazel_dep(name = "rules_zig", version = "20250613.0-567662a") diff --git a/third_party/modules/libxev/20250716.0-7f80318/overlay/BUILD.bazel b/third_party/modules/libxev/20250716.0-7f80318/overlay/BUILD.bazel new file mode 100644 index 00000000..872ef7fb --- /dev/null +++ b/third_party/modules/libxev/20250716.0-7f80318/overlay/BUILD.bazel @@ -0,0 +1,13 @@ +load("@rules_zig//zig:defs.bzl", "zig_library") + +zig_library( + name = "xev", + srcs = glob([ + "src/*.zig", + "src/backend/*.zig", + "src/linux/*.zig", + "src/watcher/*.zig", + ]), + main = "src/main.zig", + visibility = ["//visibility:public"], +) diff --git a/third_party/modules/libxev/20250716.0-7f80318/overlay/MODULE.bazel b/third_party/modules/libxev/20250716.0-7f80318/overlay/MODULE.bazel new file mode 100644 index 00000000..62de098a --- /dev/null +++ b/third_party/modules/libxev/20250716.0-7f80318/overlay/MODULE.bazel @@ -0,0 +1,7 @@ +module( + name = "libxev", + version = "20250716.0-7f80318", + compatibility_level = 1, +) + +bazel_dep(name = "rules_zig", version = "20250613.0-567662a") diff --git a/third_party/modules/libxev/20250716.0-7f80318/source.json b/third_party/modules/libxev/20250716.0-7f80318/source.json new file mode 100644 index 00000000..f578c73c --- /dev/null +++ b/third_party/modules/libxev/20250716.0-7f80318/source.json @@ -0,0 +1,10 @@ +{ + "strip_prefix": "libxev-7f803181b158a10fec8619f793e3b4df515566cb", + "url": "https://github.com/mitchellh/libxev/archive/7f803181b158a10fec8619f793e3b4df515566cb.tar.gz", + "integrity": "sha256-KaozYKEhhT/6sInef7/8O/60LDBJN+8QmdLuNY1Gkmc", + "overlay": { + "MODULE.bazel": "", + "BUILD.bazel": "" + }, + "patch_strip": 1 +} diff --git a/third_party/modules/libxev/metadata.json b/third_party/modules/libxev/metadata.json index 59deb2c9..c897c980 100644 --- a/third_party/modules/libxev/metadata.json +++ b/third_party/modules/libxev/metadata.json @@ -20,6 +20,7 @@ "20250124.0-31eed4e", "20250222.0-07bcffa", "20250313.0-5773f46", + "20250716.0-7f80318", ], "yanked_versions": {} } diff --git a/third_party/modules/zig-protobuf/20250716.0-7627e25/MODULE.bazel b/third_party/modules/zig-protobuf/20250716.0-7627e25/MODULE.bazel new file mode 120000 index 00000000..d2cd2542 --- /dev/null +++ b/third_party/modules/zig-protobuf/20250716.0-7627e25/MODULE.bazel @@ -0,0 +1 @@ +overlay/MODULE.bazel \ No newline at end of file diff --git a/third_party/modules/zig-protobuf/20250716.0-7627e25/overlay/BUILD.bazel b/third_party/modules/zig-protobuf/20250716.0-7627e25/overlay/BUILD.bazel new file mode 100644 index 00000000..832c7591 --- /dev/null +++ b/third_party/modules/zig-protobuf/20250716.0-7627e25/overlay/BUILD.bazel @@ -0,0 +1,32 @@ +load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain") +load("@rules_zig//zig:defs.bzl", "BINARY_KIND", "zig_binary", "zig_library") + +zig_library( + name = "protobuf", + import_name = "protobuf", + main = "src/protobuf.zig", + visibility = ["//visibility:public"], +) + +zig_binary( + name = "generator", + srcs = [ + "bootstrapped-generator/FullName.zig", + "bootstrapped-generator/google/protobuf/compiler/plugin.pb.zig", + "bootstrapped-generator/google/protobuf/descriptor.pb.zig", + ], + kind = BINARY_KIND.exe, + main = "bootstrapped-generator/main.zig", + visibility = ["//visibility:public"], + deps = [":protobuf"], +) + +proto_lang_toolchain( + name = "zig_toolchain", + command_line = "--zig_out=$(OUT)", + output_files = "multiple", + plugin = ":generator", + plugin_format_flag = "--plugin=protoc-gen-zig=%s", + runtime = ":protobuf", + visibility = ["//visibility:public"], +) diff --git a/third_party/modules/zig-protobuf/20250716.0-7627e25/overlay/MODULE.bazel b/third_party/modules/zig-protobuf/20250716.0-7627e25/overlay/MODULE.bazel new file mode 100644 index 00000000..24102b40 --- /dev/null +++ b/third_party/modules/zig-protobuf/20250716.0-7627e25/overlay/MODULE.bazel @@ -0,0 +1,8 @@ +module( + name = "zig-protobuf", + version = "20250716.0-7627e25", + compatibility_level = 1, +) + +bazel_dep(name = "rules_zig", version = "20250613.0-567662a") +bazel_dep(name = "rules_proto", version = "6.0.0-rc1") diff --git a/third_party/modules/zig-protobuf/20250716.0-7627e25/source.json b/third_party/modules/zig-protobuf/20250716.0-7627e25/source.json new file mode 100644 index 00000000..37ecb718 --- /dev/null +++ b/third_party/modules/zig-protobuf/20250716.0-7627e25/source.json @@ -0,0 +1,9 @@ +{ + "strip_prefix": "zig-protobuf-7627e25201c60d38d730eb7a0f3fd492b99c47c0", + "url": "https://github.com/zml/zig-protobuf/archive/7627e25201c60d38d730eb7a0f3fd492b99c47c0.tar.gz", + "integrity": "sha256-eantkVVBROb77HdUxaKY1iMNu1wXpxJ44K2+QzGns2E=", + "overlay": { + "MODULE.bazel": "", + "BUILD.bazel": "" + } +} diff --git a/third_party/modules/zig-protobuf/metadata.json b/third_party/modules/zig-protobuf/metadata.json index 87b4f2ba..efb77d34 100644 --- a/third_party/modules/zig-protobuf/metadata.json +++ b/third_party/modules/zig-protobuf/metadata.json @@ -8,12 +8,13 @@ } ], "repository": [ - "gwenzek/zig-protobuf" + "zml/zig-protobuf" ], "versions": [ "20240722.0-c644d11", "20250213.0-5304067", - "20250318.0-930153e" + "20250318.0-930153e", + "20250716.0-784e5d6", ], "yanked_versions": {} } diff --git a/zml/BUILD.bazel b/zml/BUILD.bazel index 218c484f..f7c78923 100644 --- a/zml/BUILD.bazel +++ b/zml/BUILD.bazel @@ -1,5 +1,5 @@ load("@rules_cc//cc:defs.bzl", "cc_library") -load("@rules_zig//zig:defs.bzl", "zig_library") +load("@rules_zig//zig:defs.bzl", "zig_library", "zig_test") load("//bazel:zig.bzl", "zig_cc_test") load("//bazel:zig_proto_library.bzl", "zig_proto_library") load("//bazel:zig_srcs.bzl", "zig_srcs") diff --git a/zml/module.zig b/zml/module.zig index c6824b4a..b8d4f21f 100644 --- a/zml/module.zig +++ b/zml/module.zig @@ -841,8 +841,6 @@ fn compileModuleToPjrtExecutable(arena: std.mem.Allocator, platform: Platform, m const sharding = platform.sharding(); - // NOTE(Corendos): Hack needed because Protobuf struct are not public. - const DeviceAssignmentProto = @TypeOf(xla_pb.CompileOptionsProto.init().executable_build_options.?.device_assignment.?); var options: xla_pb.CompileOptionsProto = .{ .executable_build_options = .{ .device_ordinal = -1, @@ -852,18 +850,18 @@ fn compileModuleToPjrtExecutable(arena: std.mem.Allocator, platform: Platform, m .device_assignment = .{ .replica_count = sharding.num_replicas, .computation_count = sharding.num_partitions, - .computation_devices = blk: { - var computation_devices = try std.ArrayListUnmanaged(DeviceAssignmentProto.ComputationDevice).initCapacity(arena, sharding.num_partitions); - for (0..sharding.num_partitions) |i| { - var replica_device_ids = std.ArrayListUnmanaged(i64).initCapacity(arena, 1) catch unreachable; - replica_device_ids.appendAssumeCapacity(@intCast(i)); - computation_devices.appendAssumeCapacity(.{ .replica_device_ids = replica_device_ids }); - } - break :blk computation_devices; - }, + // Filled below. + .computation_devices = .{}, }, }, }; + const computation_devices = &options.executable_build_options.?.device_assignment.?.computation_devices; + try computation_devices.ensureTotalCapacity(arena, sharding.num_partitions); + const replica_device_ids = try arena.alloc(i64, sharding.num_partitions); + for (0..sharding.num_partitions) |i| { + replica_device_ids[i] = @intCast(i); + computation_devices.appendAssumeCapacity(.{ .replica_device_ids = .fromOwnedSlice(replica_device_ids[i .. i + 1]) }); + } // Let the arena deinit, zig-protobuf deinit is very slow. try options.env_option_overrides.ensureUnusedCapacity(arena, 16); diff --git a/zml/tools/BUILD.bazel b/zml/tools/BUILD.bazel index 7579fb9d..a99ea26e 100644 --- a/zml/tools/BUILD.bazel +++ b/zml/tools/BUILD.bazel @@ -7,7 +7,6 @@ cc_library( target_compatible_with = [ "@platforms//os:macos", ], - visibility = ["//zml/tools:__subpackages__"], ) zig_library( diff --git a/zml/tools/tracer.zig b/zml/tools/tracer.zig index 05769563..c1000cb8 100644 --- a/zml/tools/tracer.zig +++ b/zml/tools/tracer.zig @@ -1,9 +1,11 @@ const builtin = @import("builtin"); + const c = @import("c"); pub const Tracer = switch (builtin.os.tag) { .macos => MacOsTracer, - .linux => if (@hasDecl(c, "ZML_RUNTIME_CUDA")) CudaTracer else FakeTracer, + // .linux => if (@hasDecl(c, "ZML_RUNTIME_CUDA")) CudaTracer else FakeTracer, + .linux => FakeTracer, else => FakeTracer, };