diff --git a/bazel/zig.bzl b/bazel/zig.bzl deleted file mode 100644 index 26773a5e..00000000 --- a/bazel/zig.bzl +++ /dev/null @@ -1,63 +0,0 @@ -load("@rules_cc//cc:cc_binary.bzl", "cc_binary") -load("@rules_cc//cc:cc_test.bzl", "cc_test") -load("@rules_zig//zig:defs.bzl", "BINARY_KIND", "zig_binary") - -def zig_cc_binary( - name, - copts = [], - args = None, - env = None, - data = [], - deps = [], - tags = [], - visibility = None, - **kwargs): - zig_binary( - name = "{}_lib".format(name), - kind = BINARY_KIND.static_lib, - copts = copts + ["-lc"], - deps = deps, - visibility = visibility, - **kwargs - ) - cc_binary( - name = name, - args = args, - env = env, - data = data, - deps = [":{}_lib".format(name)], - tags = tags, - visibility = visibility, - ) - -def zig_cc_test( - name, - copts = [], - env = None, - data = [], - deps = [], - test_runner = None, - tags = [], - visibility = None, - **kwargs): - zig_binary( - name = "{}_test_lib".format(name), - kind = BINARY_KIND.test_lib, - test_runner = test_runner, - tags = tags, - copts = copts + ["-lc"], - deps = deps + [ - "@rules_zig//zig/lib:libc", - ], - visibility = visibility, - **kwargs - ) - cc_test( - name = name, - env = env, - data = data, - deps = [":{}_test_lib".format(name)], - tags = tags, - visibility = visibility, - linkstatic = True, - ) diff --git a/bazel/zig_srcs.bzl b/bazel/zig_srcs.bzl index 2aa79568..2ee8ba79 100644 --- a/bazel/zig_srcs.bzl +++ b/bazel/zig_srcs.bzl @@ -1,5 +1,5 @@ load("@aspect_bazel_lib//lib:tar.bzl", "mtree_spec", "tar") -load("@rules_zig//zig:defs.bzl", "zig_binary", "BINARY_KIND") +load("@rules_zig//zig:defs.bzl", "zig_binary") def zig_srcs(name, zig_bin="", zig_lib=""): """For a given zig_library, recursively extract all zig sources into a tarball. @@ -12,7 +12,6 @@ def zig_srcs(name, zig_bin="", zig_lib=""): zig_bin = "{}_bin".format(name) zig_binary( name = zig_bin, - kind = BINARY_KIND.bc, tags = ["manual", "@rules_zig//zig/lib:libc"], deps = [zig_lib], ) diff --git a/docs/howtos/add_weights.md b/docs/howtos/add_weights.md index 99b30b00..df1836d1 100644 --- a/docs/howtos/add_weights.md +++ b/docs/howtos/add_weights.md @@ -5,7 +5,7 @@ Our [first model](../tutorials/write_first_model.md) did not need any weights fi We just created weights and biases at runtime. But real-world models typically need weights files, and maybe some other -supporting files. +supporting files. We recommend, for easy deployments, you upload those files. In many instances, you will use a site like [🤗 Hugging Face](https://huggingface.co). @@ -51,7 +51,7 @@ Then, in your `BUILD.bazel`, you can refer to the files you defined above, in the following way: ```python -zig_cc_binary( +zig_binary( name = "mnist", args = [ "$(location @com_github_zml_cdn_mnist//file)", @@ -74,4 +74,3 @@ See how: - we use `data = [ ... ]` to reference the files in `weights.bzl` - we use `args = [ ... ]` to pass the files as command-line arguments to the MNIST executable at runtime, automatically. - diff --git a/docs/howtos/dockerize_models.md b/docs/howtos/dockerize_models.md index 7bce7b08..92dda5b5 100644 --- a/docs/howtos/dockerize_models.md +++ b/docs/howtos/dockerize_models.md @@ -41,9 +41,9 @@ use their rules to define our 5 additional targets: load("@aspect_bazel_lib//lib:tar.bzl", "mtree_spec", "tar") load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup") load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load", "oci_push") -load("@zml//bazel:zig.bzl", "zig_cc_binary") +load("@rules_zig//zig:defs.bzl", "zig_binary") -zig_cc_binary( +zig_binary( name = "simple_layer", main = "main.zig", deps = [ @@ -249,10 +249,10 @@ load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template") load("@aspect_bazel_lib//lib:tar.bzl", "mtree_spec", "tar") load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup") load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load", "oci_push") -load("@zml//bazel:zig.bzl", "zig_cc_binary") +load("@rules_zig//zig:defs.bzl", "zig_binary") # The executable -zig_cc_binary( +zig_binary( name = "mnist", args = [ "$(location @com_github_ggerganov_ggml_mnist//file)", @@ -375,4 +375,3 @@ MNIST model, including weights and dataset, to the docker registry: ``` bazel run //mnist:push --@zml//runtimes:cuda=true -- --repository index.docker.io/my_org/zml_mnist ``` - diff --git a/docs/howtos/howto_torch2zml.md b/docs/howtos/howto_torch2zml.md index a5785f44..026e2013 100644 --- a/docs/howtos/howto_torch2zml.md +++ b/docs/howtos/howto_torch2zml.md @@ -150,9 +150,9 @@ pub fn asyncMain() !void { And add a `zig_cc_binary` target in `my_project/BUILD.bazel`: ```python -load("@zml//bazel:zig.bzl", "zig_cc_binary") +load("@rules_zig//zig:defs.bzl", "zig_binary") -zig_cc_binary( +zig_binary( name = "torch2zml", main = "torch2zml.zig", deps = [ @@ -246,7 +246,7 @@ We can test the MLP layer with the `zml.testing.testLayer` utility: ```zig pub fn asyncMain() !void { ... - + var ctx = try zml.Context.init(); defer ctx.deinit(); const platform = ctx.autoPlatform(.{}); @@ -293,5 +293,5 @@ During this phase, you have three kinds of errors that can appear: "C,W,H" of tensors, you can port this to actual tensor attributes using `x.withTags(.{.c, .w, .h})`, and use those tags (eg `.c`) to refer to axes instead of offsets. E.g. in Pytorch: `x.sum(0) # reduce over channel axis` - becomes `x.sum(.c)`. More on this topic in + becomes `x.sum(.c)`. More on this topic in ["Working with tensors"](../tutorials/working_with_tensors.md). diff --git a/docs/tutorials/write_first_model.md b/docs/tutorials/write_first_model.md index 4d755d1d..6aa62b92 100644 --- a/docs/tutorials/write_first_model.md +++ b/docs/tutorials/write_first_model.md @@ -352,9 +352,9 @@ As mentioned already, ZML uses Bazel; so to build our model, we just need to create a simple `BUILD.bazel` file, next to the `main.zig` file, like this: ```python -load("@zml//bazel:zig.bzl", "zig_cc_binary") +load("@rules_zig//zig:defs.bzl", "zig_binary") -zig_cc_binary( +zig_binary( name = "simple_layer", main = "main.zig", deps = [ diff --git a/examples/.bazelrc b/examples/.bazelrc index d41c85ec..6bbfddcf 100644 --- a/examples/.bazelrc +++ b/examples/.bazelrc @@ -34,14 +34,13 @@ build --enable_runfiles build --incompatible_strict_action_env # We don't to check the built files in the sandbox or repositories -build --experimental_check_output_files=false -fetch --experimental_check_output_files=false -query --experimental_check_output_files=false +common --experimental_check_output_files=false # Performance build --legacy_external_runfiles=false build --experimental_output_directory_naming_scheme=diff_against_dynamic_baseline + # Mount the zig cache into the sandboxes build:macos --sandbox_add_mount_pair=/var/tmp build:linux --sandbox_add_mount_pair=/tmp @@ -61,7 +60,6 @@ common --experimental_cc_shared_library common --incompatible_use_cc_configure_from_rules_cc common --experimental_starlark_cc_import - common --experimental_platform_in_output_dir # Faster sandbox speeds @@ -80,11 +78,11 @@ build:macos --action_env=MACOSX_DEPLOYMENT_TARGET=13.0 build:macos --macos_sdk_version=13.0 # Used by the tsl overlay in python_init_repositories for the bazelified xla module 20241025.0-4663f04 and later -build --repo_env=HERMETIC_PYTHON_VERSION=3.11 +common --repo_env=HERMETIC_PYTHON_VERSION=3.11 # Don't detect the local C++ toolchain, be fully hermetic # This is done only on Linux for now since we use the local toolchain on macOS -build:linux --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 +common:linux --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 # Print test errors in the console test --test_output=errors @@ -97,8 +95,6 @@ common:silent --logging=0 common --registry=https://bazel-registry.zml.ai common --registry=https://bcr.bazel.build -common --@rules_zig//zig/settings:copt=-fPIC -common --@rules_zig//zig/settings:copt=-fcompiler-rt common --@toolchains_llvm_bootstrapped//config:experimental_stub_libgcc_s=True @@ -112,8 +108,13 @@ common:debug --strategy=ZigBuildTestLib=local # --config=alldebug build everything in debug mode, including the backend. common:alldebug --compilation_mode=dbg common:alldebug --@rules_zig//zig/settings:mode=debug +common:alldebug --spawn_strategy=local common:alldebug --strategy=local common:release --compilation_mode=opt common:release --copt=-Ofast common:release --@rules_zig//zig/settings:mode=release_safe + +common --override_module=rules_zig=%workspace%/../../../rules_zig + +common --@rules_zig//zig/settings:linkmode=cc diff --git a/examples/MODULE.bazel.lock b/examples/MODULE.bazel.lock index 151cc08e..5b937986 100644 --- a/examples/MODULE.bazel.lock +++ b/examples/MODULE.bazel.lock @@ -155,6 +155,7 @@ "https://bazel-registry.zml.ai/modules/rules_python/0.40.0/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/rules_python/1.3.0/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/rules_rust/0.60.0/MODULE.bazel": "not found", + "https://bazel-registry.zml.ai/modules/rules_rust/0.62.0/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/rules_shell/0.2.0/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/rules_shell/0.3.0/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/rules_shell/0.4.1/MODULE.bazel": "not found", @@ -366,7 +367,8 @@ "https://bcr.bazel.build/modules/rules_python/1.3.0/MODULE.bazel": "8361d57eafb67c09b75bf4bbe6be360e1b8f4f18118ab48037f2bd50aa2ccb13", "https://bcr.bazel.build/modules/rules_python/1.3.0/source.json": "25932f917cd279c7baefa6cb1d3fa8750a7a29de522024449b19af6eab51f4a0", "https://bcr.bazel.build/modules/rules_rust/0.60.0/MODULE.bazel": "911ff2a12d01ac574fd6dfec0b05fa976ff8693d8c2420db637a9f98f697b0ae", - "https://bcr.bazel.build/modules/rules_rust/0.60.0/source.json": "2b17f77e27489aa1b86b765a141642a1966a2a35fed0207277f3327fd09ef3d4", + "https://bcr.bazel.build/modules/rules_rust/0.62.0/MODULE.bazel": "6a15b57982e278793c684f426e19166e62e73f1bd45fe3b6bcedd0b901177b37", + "https://bcr.bazel.build/modules/rules_rust/0.62.0/source.json": "1b3a6551a585ee47cafa21550c5eb87c6f3a56bb9761f9e3421ff1102f220437", "https://bcr.bazel.build/modules/rules_shell/0.2.0/MODULE.bazel": "fda8a652ab3c7d8fee214de05e7a9916d8b28082234e8d2c0094505c5268ed3c", "https://bcr.bazel.build/modules/rules_shell/0.3.0/MODULE.bazel": "de4402cd12f4cc8fda2354fce179fdb068c0b9ca1ec2d2b17b3e21b24c1a937b", "https://bcr.bazel.build/modules/rules_shell/0.4.1/MODULE.bazel": "00e501db01bbf4e3e1dd1595959092c2fadf2087b2852d3f553b5370f5633592", @@ -4681,7 +4683,7 @@ }, "@@rules_rust+//crate_universe:extensions.bzl%crate": { "general": { - "bzlTransitiveDigest": "w9dJZZyTXj/SlBYlitGslCqlLdGrS0oO0DngHjXtA7s=", + "bzlTransitiveDigest": "YWlcbEZ/1fdqy0HNBaFalAKz/RswQbhU7oDvWUZm7cQ=", "usagesDigest": "641905SjTVLGtibJNQNQklx/63ZDVAy9joJU2P7P4ho=", "recordedFileInputs": { "@@zml+//zml/tokenizer/hftokenizers/Cargo.lock": "93198160b29c4607994b405ffa0ad1c29b08df1ad22ec9a711874914bbaadbcb", @@ -6086,11 +6088,6 @@ "rules_rust+", "rules_rust", "rules_rust+" - ], - [ - "rules_rust+", - "rust_host_tools", - "rules_rust++rust_host_tools+rust_host_tools" ] ] } diff --git a/examples/benchmark/BUILD.bazel b/examples/benchmark/BUILD.bazel index 7088dae8..680a2aa9 100644 --- a/examples/benchmark/BUILD.bazel +++ b/examples/benchmark/BUILD.bazel @@ -1,6 +1,6 @@ -load("@zml//bazel:zig.bzl", "zig_cc_binary") +load("@rules_zig//zig:defs.bzl", "zig_binary") -zig_cc_binary( +zig_binary( name = "benchmark", main = "main.zig", deps = [ diff --git a/examples/llama/BUILD.bazel b/examples/llama/BUILD.bazel index d657f4a1..5aa795bc 100644 --- a/examples/llama/BUILD.bazel +++ b/examples/llama/BUILD.bazel @@ -5,9 +5,9 @@ load("@bazel_skylib//rules:native_binary.bzl", "native_test") load("@bazel_skylib//rules:write_file.bzl", "write_file") load("@rules_cc//cc:cc_binary.bzl", "cc_binary") load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load", "oci_push") -load("@zml//bazel:zig.bzl", "zig_cc_binary") +load("@rules_zig//zig:defs.bzl", "zig_binary") -zig_cc_binary( +zig_binary( name = "llama", srcs = [ "llama.zig", @@ -37,7 +37,7 @@ cc_binary( tags = [ "manual", ], - deps = [":llama_lib"], + deps = [":llama"], ) cc_binary( @@ -56,7 +56,7 @@ cc_binary( tags = [ "manual", ], - deps = [":llama_lib"], + deps = [":llama"], ) cc_binary( @@ -75,7 +75,7 @@ cc_binary( tags = [ "manual", ], - deps = [":llama_lib"], + deps = [":llama"], ) cc_binary( @@ -94,7 +94,7 @@ cc_binary( tags = [ "manual", ], - deps = [":llama_lib"], + deps = [":llama"], ) cc_binary( @@ -112,7 +112,7 @@ cc_binary( "@Karpathy-TinyLlama-Stories15M//file", "@Karpathy-TinyLlama-Tokenizer//file", ], - deps = [":llama_lib"], + deps = [":llama"], ) write_file( @@ -121,7 +121,7 @@ write_file( content = ['{"bos_token_id":1,"eos_token_id":2,"hidden_act":"silu","hidden_size":288,"intermediate_size":768,"max_position_embeddings":256,"model_type":"llama","num_attention_heads":6,"num_hidden_layers":6,"num_key_value_heads":6,"rms_norm_eps":1e-05,"hf_rope_impl":false,"rope_scaling":null,"rope_theta":10000.0}'], ) -zig_cc_binary( +zig_binary( name = "test-implementation", srcs = ["llama.zig"], args = [ diff --git a/examples/llama/main.zig b/examples/llama/main.zig index 0b121e65..ed31cd4f 100644 --- a/examples/llama/main.zig +++ b/examples/llama/main.zig @@ -1,19 +1,19 @@ +const std = @import("std"); + const asynk = @import("async"); const clap = @import("clap"); -const std = @import("std"); const stdx = @import("stdx"); const zml = @import("zml"); +const Buffer = zml.Buffer; +const Tensor = zml.Tensor; +const ShapeOf = zml.ShapeOf; const llama = @import("llama.zig"); - const LlamaLM = llama.LlamaLM; const Llama = llama.Llama; const KvCache = llama.KvCache; const TransformerLayer = llama.TransformerLayer; const SelfAttn = llama.SelfAttn; -const Buffer = zml.Buffer; -const Tensor = zml.Tensor; -const ShapeOf = zml.ShapeOf; const log = std.log.scoped(.llama); @@ -170,7 +170,7 @@ pub fn main() !void { } pub fn asyncMain() !void { - log.info(" LLama was compiled with {}", .{@import("builtin").mode}); + log.info(" LLama was compiled with {}", .{@import("builtin").mode}); const allocator = std.heap.c_allocator; diff --git a/examples/loader/BUILD.bazel b/examples/loader/BUILD.bazel index a80b40b4..6fd0d515 100644 --- a/examples/loader/BUILD.bazel +++ b/examples/loader/BUILD.bazel @@ -1,6 +1,6 @@ -load("@zml//bazel:zig.bzl", "zig_cc_binary") +load("@rules_zig//zig:defs.bzl", "zig_binary") -zig_cc_binary( +zig_binary( name = "safetensors", main = "main.zig", deps = [ diff --git a/examples/mnist/BUILD.bazel b/examples/mnist/BUILD.bazel index be8b8e95..da536bf1 100644 --- a/examples/mnist/BUILD.bazel +++ b/examples/mnist/BUILD.bazel @@ -2,10 +2,10 @@ load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template") load("@aspect_bazel_lib//lib:tar.bzl", "mtree_spec", "tar") load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup") load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load", "oci_push") -load("@zml//bazel:zig.bzl", "zig_cc_binary") +load("@rules_zig//zig:defs.bzl", "zig_binary") # Executable -zig_cc_binary( +zig_binary( name = "mnist", args = [ "$(location @com_github_ggerganov_ggml_mnist//file)", diff --git a/examples/modernbert/BUILD.bazel b/examples/modernbert/BUILD.bazel index f2b09ad6..ca42ec4b 100644 --- a/examples/modernbert/BUILD.bazel +++ b/examples/modernbert/BUILD.bazel @@ -1,7 +1,7 @@ load("@rules_cc//cc:cc_binary.bzl", "cc_binary") -load("@zml//bazel:zig.bzl", "zig_cc_binary") +load("@rules_zig//zig:defs.bzl", "zig_binary") -zig_cc_binary( +zig_binary( name = "modernbert", srcs = ["modernbert.zig"], main = "main.zig", @@ -28,7 +28,7 @@ cc_binary( tags = [ "manual", ], - deps = [":modernbert_lib"], + deps = [":modernbert"], ) cc_binary( @@ -46,10 +46,10 @@ cc_binary( tags = [ "manual", ], - deps = [":modernbert_lib"], + deps = [":modernbert"], ) -zig_cc_binary( +zig_binary( name = "test-implementation", srcs = ["modernbert.zig"], args = [ diff --git a/examples/simple_layer/BUILD.bazel b/examples/simple_layer/BUILD.bazel index 8d747677..e51d41e0 100644 --- a/examples/simple_layer/BUILD.bazel +++ b/examples/simple_layer/BUILD.bazel @@ -1,9 +1,20 @@ load("@aspect_bazel_lib//lib:tar.bzl", "mtree_spec", "tar") load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup") load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load", "oci_push") -load("@zml//bazel:zig.bzl", "zig_cc_binary") +load("@rules_zig//zig:defs.bzl", "zig_asm_binary", "zig_binary") -zig_cc_binary( +zig_asm_binary( + name = "kernel", + main = "kernel.zig", + extension = ".ptx", + copts = [ + "-target", + "nvptx64-cuda-none", + "-mcpu=sm_87", + ], +) + +zig_binary( name = "simple_layer", main = "main.zig", deps = [ diff --git a/examples/simple_layer/main.zig b/examples/simple_layer/main.zig index 3ffc1471..0f83898c 100644 --- a/examples/simple_layer/main.zig +++ b/examples/simple_layer/main.zig @@ -1,6 +1,7 @@ const std = @import("std"); -const zml = @import("zml"); + const asynk = @import("async"); +const zml = @import("zml"); /// Model definition const Layer = struct { diff --git a/examples/tools/buildifier.sh b/examples/tools/buildifier.sh index 0e2b7ad7..5e32f8ef 100755 --- a/examples/tools/buildifier.sh +++ b/examples/tools/buildifier.sh @@ -1,4 +1,3 @@ #!/bin/bash -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(bazel info workspace)" -exec bazel run -- @buildifier_prebuilt//:buildifier "$@" +cd "$(dirname "${BASH_SOURCE[0]}")"/.. +exec bazel run --experimental_convenience_symlinks=ignore -- @buildifier_prebuilt//:buildifier "$@" diff --git a/examples/tools/zls.sh b/examples/tools/zls.sh index 86827c87..5524a7e5 100755 --- a/examples/tools/zls.sh +++ b/examples/tools/zls.sh @@ -1,4 +1,3 @@ #!/bin/bash -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(bazel info workspace)" -exec bazel run -- @zml//:zls "${@}" +cd "$(dirname "${BASH_SOURCE[0]}")/.." +exec bazel run --experimental_convenience_symlinks=ignore -- @zml//:zls "${@}" diff --git a/mlir/BUILD.bazel b/mlir/BUILD.bazel index 63b9ede6..faf05caa 100644 --- a/mlir/BUILD.bazel +++ b/mlir/BUILD.bazel @@ -1,8 +1,6 @@ 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_srcs.bzl", "zig_srcs") -load("//bazel:zig.bzl", "zig_cc_test") cc_library( name = "c", @@ -28,17 +26,17 @@ zig_library( ], ) -zig_cc_test( +zig_test( name = "test", deps = [":mlir"], ) cc_static_library( - name="mlir_static", - deps = ["c"] + name = "mlir_static", + deps = ["c"], ) zig_srcs( name = "sources", - zig_bin = ":test_test_lib", + zig_bin = ":test", ) diff --git a/mlir/dialects/BUILD.bazel b/mlir/dialects/BUILD.bazel index bcb3a1bb..4f954fa4 100644 --- a/mlir/dialects/BUILD.bazel +++ b/mlir/dialects/BUILD.bazel @@ -1,5 +1,4 @@ -load("@rules_zig//zig:defs.bzl", "zig_library") -load("//bazel:zig.bzl", "zig_cc_test") +load("@rules_zig//zig:defs.bzl", "zig_library", "zig_test") load("//bazel:zig_srcs.bzl", "zig_srcs") zig_library( @@ -20,22 +19,22 @@ zig_library( ], ) -zig_cc_test( +zig_test( name = "test", deps = [":dialects"], ) zig_srcs( name = "sources", - zig_bin = ":test_test_lib", + zig_bin = ":test", ) cc_static_library( - name="mlir_static", + name = "mlir_static", deps = [ "//mlir:c", "@stablehlo//:stablehlo_dialect_capi", - ] + ], ) zig_library( @@ -51,7 +50,7 @@ zig_library( ], ) -zig_cc_test( +zig_test( name = "stablehlo_test", deps = [":stablehlo"], ) diff --git a/pjrt/BUILD.bazel b/pjrt/BUILD.bazel index 682b569c..726015e1 100644 --- a/pjrt/BUILD.bazel +++ b/pjrt/BUILD.bazel @@ -1,7 +1,6 @@ -load("@rules_zig//zig:defs.bzl", "zig_library") -load("@zml//bazel:zig.bzl", "zig_cc_binary") -load("@zml//bazel:zig_srcs.bzl", "zig_srcs") +load("@rules_zig//zig:defs.bzl", "zig_binary", "zig_library") load("@zml//bazel:zig_proto_library.bzl", "zig_proto_library") +load("@zml//bazel:zig_srcs.bzl", "zig_srcs") zig_library( name = "pjrt", @@ -50,7 +49,7 @@ zig_proto_library( deps = ["@xla//third_party/tsl/tsl/profiler/protobuf:trace_events_proto"], ) -zig_cc_binary( +zig_binary( name = "xspace_to_json", srcs = [ "convert/trace_container.zig", diff --git a/runtimes/cuda/BUILD.bazel b/runtimes/cuda/BUILD.bazel index 0913c058..f16e84a0 100644 --- a/runtimes/cuda/BUILD.bazel +++ b/runtimes/cuda/BUILD.bazel @@ -1,5 +1,5 @@ load("@rules_cc//cc:cc_library.bzl", "cc_library") -load("@rules_zig//zig:defs.bzl", "zig_library") +load("@rules_zig//zig:defs.bzl", "zig_library", "zig_shared_library") cc_library( name = "zmlxcuda_lib", @@ -7,6 +7,13 @@ cc_library( visibility = ["@libpjrt_cuda//:__subpackages__"], ) +zig_shared_library( + name = "zmlxcuda", + main = "zmlxcuda.zig", + copts = ["-lc"], + visibility = ["@libpjrt_cuda//:__subpackages__"], +) + cc_library( name = "empty", ) diff --git a/runtimes/neuron/neuron.zig b/runtimes/neuron/neuron.zig index 9fdbcc4c..e9b8eb76 100644 --- a/runtimes/neuron/neuron.zig +++ b/runtimes/neuron/neuron.zig @@ -1,5 +1,5 @@ -const builtin = @import("builtin"); const std = @import("std"); +const builtin = @import("builtin"); const asynk = @import("async"); const bazel_builtin = @import("bazel_builtin"); @@ -23,7 +23,7 @@ fn isRunningOnEC2() !bool { var f = try asynk.File.open("/sys/devices/virtual/dmi/id/sys_vendor", .{ .mode = .read_only }); defer f.close() catch {}; - var buf = [_]u8{0} ** AmazonEC2.len; + var buf: [AmazonEC2.len]u8 = undefined; _ = try f.reader().readAll(&buf); return std.mem.eql(u8, &buf, AmazonEC2); diff --git a/runtimes/rocm/BUILD.bazel b/runtimes/rocm/BUILD.bazel index bab048b1..2d3b08e1 100644 --- a/runtimes/rocm/BUILD.bazel +++ b/runtimes/rocm/BUILD.bazel @@ -1,5 +1,5 @@ -load("@rules_zig//zig:defs.bzl", "zig_library") load("@aspect_bazel_lib//lib:tar.bzl", "mtree_spec", "tar") +load("@rules_zig//zig:defs.bzl", "zig_library", "zig_shared_library") cc_library( name = "zmlxrocm_lib", @@ -11,6 +11,16 @@ cc_library( visibility = ["@libpjrt_rocm//:__subpackages__"], ) +zig_shared_library( + name = "zmlxrocm", + main = "zmlxrocm.zig", + copts = ["-lc"], + deps = [ + "//stdx", + ], + visibility = ["@libpjrt_rocm//:__subpackages__"], +) + alias( name = "hipblaslt", actual = "@libpjrt_rocm//:hipblaslt", diff --git a/runtimes/rocm/libpjrt_rocm.BUILD.bazel b/runtimes/rocm/libpjrt_rocm.BUILD.bazel index 27e05a6a..8eeeb210 100644 --- a/runtimes/rocm/libpjrt_rocm.BUILD.bazel +++ b/runtimes/rocm/libpjrt_rocm.BUILD.bazel @@ -92,6 +92,7 @@ copy_to_directory( "rocblas": "lib", "opt/amdgpu/lib/x86_64-linux-gnu": "lib", "libdrm-amdgpu-amdgpu1": "lib", + "opt/amdgpu/share": "share", }, add_directory_to_runfiles = True, include_external_repositories = ["**"], diff --git a/runtimes/rocm/rocm.bzl b/runtimes/rocm/rocm.bzl index 889687f6..0d94f075 100644 --- a/runtimes/rocm/rocm.bzl +++ b/runtimes/rocm/rocm.bzl @@ -15,7 +15,7 @@ _UBUNTU_PACKAGES = { packages.patchelf( name = "libelf1", shared_library = "usr/lib/x86_64-linux-gnu/libelf.so.1", - set_rpath = '$ORIGIN', + set_rpath = "$ORIGIN", ), ]), "libdrm-amdgpu-common": packages.filegroup(name = "amdgpu_ids", srcs = ["opt/amdgpu/share/libdrm/amdgpu.ids"]), @@ -26,7 +26,10 @@ _UBUNTU_PACKAGES = { packages.patchelf( name = "libdrm-amdgpu-amdgpu1", shared_library = "opt/amdgpu/lib/x86_64-linux-gnu/libdrm_amdgpu.so.1", - set_rpath = '$ORIGIN', + set_rpath = "$ORIGIN", + rename_dynamic_symbols = { + "open": "zmlxrocm_open", + }, ), ]), "libtinfo6": packages.filegroup(name = "libtinfo6", srcs = ["lib/x86_64-linux-gnu/libtinfo.so.6"]), diff --git a/runtimes/rocm/rocm.zig b/runtimes/rocm/rocm.zig index 98be6031..05739241 100644 --- a/runtimes/rocm/rocm.zig +++ b/runtimes/rocm/rocm.zig @@ -1,5 +1,5 @@ -const builtin = @import("builtin"); const std = @import("std"); +const builtin = @import("builtin"); const asynk = @import("async"); const bazel_builtin = @import("bazel_builtin"); @@ -10,20 +10,6 @@ const stdx = @import("stdx"); const log = std.log.scoped(.@"zml/runtime/rocm"); -const ROCmEnvEntry = struct { - name: [:0]const u8, - rpath: []const u8, - dirname: bool, - mandatory: bool, -}; - -const rocm_env_entries: []const ROCmEnvEntry = &.{ - .{ .name = "HIPBLASLT_EXT_OP_LIBRARY_PATH", .rpath = "/lib/hipblaslt/library/hipblasltExtOpLibrary.dat", .dirname = false, .mandatory = false }, - .{ .name = "HIPBLASLT_TENSILE_LIBPATH", .rpath = "/lib/hipblaslt/library/TensileManifest.txt", .dirname = true, .mandatory = false }, - .{ .name = "ROCBLAS_TENSILE_LIBPATH", .rpath = "/lib/rocblas/library/TensileManifest.txt", .dirname = true, .mandatory = true }, - .{ .name = "ROCM_PATH", .rpath = "/", .dirname = false, .mandatory = true }, -}; - pub fn isEnabled() bool { return @hasDecl(c, "ZML_RUNTIME_ROCM"); } @@ -35,23 +21,13 @@ fn hasRocmDevices() bool { return true; } -fn setupRocmEnv(allocator: std.mem.Allocator, rocm_data_dir: []const u8) !void { - for (rocm_env_entries) |entry| { - var real_path: []const u8 = std.fmt.allocPrintZ(allocator, "{s}/{s}", .{ rocm_data_dir, entry.rpath }) catch null orelse { - if (entry.mandatory) { - stdx.debug.panic("Unable to find {s} in {s}\n", .{ entry.name, bazel_builtin.current_repository }); - } - continue; - }; +fn setupRocmEnv(rocm_data_dir: []const u8) !void { + var buf: [std.fs.max_path_bytes]u8 = undefined; - if (entry.dirname) { - real_path = std.fs.path.dirname(real_path) orelse { - stdx.debug.panic("Unable to dirname on {s}", .{real_path}); - }; - } - - _ = c.setenv(entry.name, try allocator.dupeZ(u8, real_path), 1); - } + _ = c.setenv("ROCM_PATH", try stdx.fs.path.bufJoinZ(&buf, &.{rocm_data_dir}), 1); // need to be zero terminated + _ = c.setenv("ROCBLAS_TENSILE_LIBPATH", try stdx.fs.path.bufJoinZ(&buf, &.{ rocm_data_dir, "lib", "rocblas", "library" }), 1); + _ = c.setenv("HIPBLASLT_TENSILE_LIBPATH", try stdx.fs.path.bufJoinZ(&buf, &.{ rocm_data_dir, "lib", "hipblaslt", "library" }), 1); + _ = c.setenv("HIPBLASLT_EXT_OP_LIBRARY_PATH", try stdx.fs.path.bufJoinZ(&buf, &.{ rocm_data_dir, "lib", "hipblaslt", "library", "hipblasltExtOpLibrary.dat" }), 1); } pub fn load() !*const pjrt.Api { @@ -81,7 +57,7 @@ pub fn load() !*const pjrt.Api { return error.FileNotFound; }; - try setupRocmEnv(arena.allocator(), sandbox_path); + try setupRocmEnv(sandbox_path); var lib_path_buf: [std.fs.max_path_bytes]u8 = undefined; const lib_path = try stdx.fs.path.bufJoinZ(&lib_path_buf, &.{ sandbox_path, "lib", "libpjrt_rocm.so" }); diff --git a/runtimes/tpu/tpu.zig b/runtimes/tpu/tpu.zig index dc12b437..641cdc4b 100644 --- a/runtimes/tpu/tpu.zig +++ b/runtimes/tpu/tpu.zig @@ -1,12 +1,12 @@ -const builtin = @import("builtin"); const std = @import("std"); +const builtin = @import("builtin"); const asynk = @import("async"); -const pjrt = @import("pjrt"); -const c = @import("c"); -const stdx = @import("stdx"); const bazel_builtin = @import("bazel_builtin"); +const c = @import("c"); +const pjrt = @import("pjrt"); const runfiles = @import("runfiles"); +const stdx = @import("stdx"); const log = std.log.scoped(.@"zml/runtime/tpu"); @@ -25,7 +25,7 @@ fn isOnGCP() !bool { var f = try asynk.File.open("/sys/devices/virtual/dmi/id/product_name", .{ .mode = .read_only }); defer f.close() catch {}; - var buf = [_]u8{0} ** GoogleComputeEngine.len; + var buf: [GoogleComputeEngine.len]u8 = undefined; _ = try f.reader().readAll(&buf); return std.mem.eql(u8, &buf, GoogleComputeEngine); diff --git a/stdx/fs.zig b/stdx/fs.zig index 3f278e32..ae807fa5 100644 --- a/stdx/fs.zig +++ b/stdx/fs.zig @@ -1,13 +1,34 @@ const std = @import("std"); +extern fn dladdr(addr: *anyopaque, info: *Dl_info) c_int; + +const Dl_info = extern struct { + dli_fname: [*c]const u8, + dli_fbase: *anyopaque, + dli_sname: [*c]const u8, + dli_saddr: *anyopaque, +}; + +fn selfSharedObjectPathImpl(addr: usize) []const u8 { + var info: Dl_info = undefined; + _ = dladdr(@ptrFromInt(addr), &info); + return std.mem.span(info.dli_fname); +} + +pub fn selfSharedObjectPath() []const u8 { + return selfSharedObjectPathImpl(@returnAddress()); +} + +pub fn selfSharedObjectDirPath() []const u8 { + return std.fs.path.dirname(selfSharedObjectPathImpl(@returnAddress())).?; +} + pub const path = struct { pub fn bufJoin(buf: []u8, paths: []const []const u8) ![]u8 { - var fa: std.heap.FixedBufferAllocator = .init(buf); - return try std.fs.path.join(fa.allocator(), paths); + return try std.fmt.bufPrint(buf, "{f}", .{std.fs.path.fmtJoin(paths)}); } pub fn bufJoinZ(buf: []u8, paths: []const []const u8) ![:0]u8 { - var fa: std.heap.FixedBufferAllocator = .init(buf); - return try std.fs.path.joinZ(fa.allocator(), paths); + return try std.fmt.bufPrintZ(buf, "{f}", .{std.fs.path.fmtJoin(paths)}); } }; diff --git a/stdx/math.zig b/stdx/math.zig index da0bec79..1e1e1318 100644 --- a/stdx/math.zig +++ b/stdx/math.zig @@ -21,3 +21,7 @@ pub inline fn roundeven(x: anytype) @TypeOf(x) { extern fn @"llvm.roundeven"(@TypeOf(x)) @TypeOf(x); }.@"llvm.roundeven"(x); } + +pub fn cdiv(numerator: anytype, denominator: anytype) @TypeOf(numerator) { + return (numerator + denominator - 1) / denominator; +} diff --git a/third_party/modules/zig-protobuf/20250318.0-930153e/overlay/BUILD.bazel b/third_party/modules/zig-protobuf/20250318.0-930153e/overlay/BUILD.bazel index 832c7591..1fa764b0 100644 --- a/third_party/modules/zig-protobuf/20250318.0-930153e/overlay/BUILD.bazel +++ b/third_party/modules/zig-protobuf/20250318.0-930153e/overlay/BUILD.bazel @@ -1,5 +1,5 @@ load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain") -load("@rules_zig//zig:defs.bzl", "BINARY_KIND", "zig_binary", "zig_library") +load("@rules_zig//zig:defs.bzl", "zig_binary", "zig_library") zig_library( name = "protobuf", @@ -15,7 +15,6 @@ zig_binary( "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"], diff --git a/tools/buildifier.sh b/tools/buildifier.sh index 0e2b7ad7..077504cd 100755 --- a/tools/buildifier.sh +++ b/tools/buildifier.sh @@ -1,4 +1,3 @@ #!/bin/bash cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(bazel info workspace)" -exec bazel run -- @buildifier_prebuilt//:buildifier "$@" +exec bazel run --experimental_convenience_symlinks=ignore -- @buildifier_prebuilt//:buildifier "$@" diff --git a/tools/zls.sh b/tools/zls.sh index 86827c87..52bd023a 100755 --- a/tools/zls.sh +++ b/tools/zls.sh @@ -1,4 +1,3 @@ #!/bin/bash cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(bazel info workspace)" -exec bazel run -- @zml//:zls "${@}" +exec bazel run --experimental_convenience_symlinks=ignore -- @zml//:zls "${@}" diff --git a/zml/BUILD.bazel b/zml/BUILD.bazel index 218c484f..b55e1d16 100644 --- a/zml/BUILD.bazel +++ b/zml/BUILD.bazel @@ -1,6 +1,5 @@ load("@rules_cc//cc:defs.bzl", "cc_library") -load("@rules_zig//zig:defs.bzl", "zig_library") -load("//bazel:zig.bzl", "zig_cc_test") +load("@rules_zig//zig:defs.bzl", "zig_library", "zig_test") load("//bazel:zig_proto_library.bzl", "zig_proto_library") load("//bazel:zig_srcs.bzl", "zig_srcs") @@ -44,10 +43,9 @@ zig_proto_library( deps = ["@xla//xla/pjrt/proto:compile_options_proto"], ) - # All ZML Tests -zig_cc_test( +zig_test( name = "test", data = [ "aio/torch/simple.pt", @@ -65,5 +63,5 @@ filegroup( zig_srcs( name = "sources", - zig_bin = ":test_test_lib", + zig_bin = ":test", ) diff --git a/zml/exe.zig b/zml/exe.zig index 0923f17c..d5c26795 100644 --- a/zml/exe.zig +++ b/zml/exe.zig @@ -251,7 +251,7 @@ pub const BaseExe = struct { for (events[0..sharding.num_partitions]) |e| { if (e) |ev| { - ev.await_(self.platform.pjrt_api) catch unreachable; + ev.deinit(self.platform.pjrt_api); } } } diff --git a/zml/module.zig b/zml/module.zig index 8ab1aedc..dcb647fc 100644 --- a/zml/module.zig +++ b/zml/module.zig @@ -901,6 +901,7 @@ fn compileModuleToPjrtExecutable(arena: std.mem.Allocator, platform: Platform, m // Use lld from libllvm instead of invoking the ld.lld binary. // This saves us from having to sandbox it. setFlag(&options, "xla_gpu_use_inprocess_lld", true); + setFlag(&options, "xla_gpu_autotune_level", 0); }, else => {}, } diff --git a/zml/nn.zig b/zml/nn.zig index ffdb7a9c..adc06237 100644 --- a/zml/nn.zig +++ b/zml/nn.zig @@ -175,17 +175,23 @@ pub const RopeOpts = struct { /// Read a Rope scaling config from HF config.json format. pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !Scaling { - const content = try std.json.Value.jsonParse(allocator, source, options); - if (content == .null) return .default; + return jsonParseFromValue( + allocator, + try std.json.innerParse(std.json.Value, allocator, source, options), + options, + ); + } - if (content != .object) return error.InvalidEnumTag; + pub fn jsonParseFromValue(allocator: std.mem.Allocator, source: std.json.Value, options: std.json.ParseOptions) std.json.ParseFromValueError!Scaling { + if (source == .null) return .default; - const obj = content.object; + if (source != .object) return error.InvalidEnumTag; + + const obj = source.object; const impl = obj.get("rope_type") orelse return error.MissingField; if (impl != .string) return error.InvalidEnumTag; if (std.mem.eql(u8, impl.string, "llama3")) { - // Note: leaky is fine here cause Llama3 struct don't need to allocate memory. - return .{ .llama3 = try std.json.parseFromValueLeaky(Llama3, undefined, content, .{ .ignore_unknown_fields = true }) }; + return .{ .llama3 = try std.json.innerParseFromValue(Llama3, allocator, source, options) }; } else { log.warn("Unsupported Rope implementation: {s}, will use the default one which will produce altered results", .{impl.string}); return .{ .default = {} }; diff --git a/zml/pjrtx.zig b/zml/pjrtx.zig index 1795efc3..9e3143d6 100644 --- a/zml/pjrtx.zig +++ b/zml/pjrtx.zig @@ -272,14 +272,14 @@ pub const LoadedExecutable = opaque { }; pub fn execute(self: *const LoadedExecutable, api: *const Api, args: ExecuteArgs) ExecuteError!void { - try asynk.callBlocking(pjrt.LoadedExecutable.execute, .{ self.inner(), api, pjrt.LoadedExecutable.ExecuteArgs{ + try self.inner().execute(api, pjrt.LoadedExecutable.ExecuteArgs{ .num_args = args.num_args, .arguments = @ptrCast(args.arguments), .results = @ptrCast(args.results), .events = @ptrCast(args.events), .non_donatable_input_indices = args.non_donatable_input_indices, .context = args.context, - } }); + }); } pub fn getExecutable(self: *LoadedExecutable, api: *const Api) ApiError!*Executable { diff --git a/zml/tokenizer/BUILD.bazel b/zml/tokenizer/BUILD.bazel index d1bb0def..4489d774 100644 --- a/zml/tokenizer/BUILD.bazel +++ b/zml/tokenizer/BUILD.bazel @@ -1,5 +1,4 @@ -load("@rules_zig//zig:defs.bzl", "zig_library") -load("@zml//bazel:zig.bzl", "zig_cc_binary") +load("@rules_zig//zig:defs.bzl", "zig_binary", "zig_library") zig_library( name = "tokenizer", @@ -15,7 +14,7 @@ zig_library( ], ) -zig_cc_binary( +zig_binary( name = "main", main = "main.zig", visibility = ["//visibility:public"], diff --git a/zml/tools/tracer.zig b/zml/tools/tracer.zig index 06bcd6fa..3d0c1b31 100644 --- a/zml/tools/tracer.zig +++ b/zml/tools/tracer.zig @@ -1,9 +1,10 @@ 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 => FakeTracer, else => FakeTracer, }; @@ -83,6 +84,10 @@ const FakeTracer = struct { return .{}; } + pub fn deinit(self: *const FakeTracer) void { + _ = self; + } + pub fn event(self: *const FakeTracer, message: [:0]const u8) void { _ = self; _ = message;