# Copyright 2021 The JAX Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. load("@rules_cc//cc:cc_library.bzl", "cc_library") load("@rules_python//python:defs.bzl", "py_binary", "py_library") load("//jaxlib:jax.bzl", "if_windows", "pytype_library") load( "//jaxlib:pywrap.bzl", "nanobind_pywrap_extension", "pywrap_binaries", "pywrap_library", ) load("//jaxlib:symlink_files.bzl", "symlink_inputs") load("//jaxlib/mlir/_mlir_libs:stubgen.bzl", "py_stubgen") package( default_visibility = [ "//visibility:public", ], licenses = ["notice"], ) COPTS = [ "-fexceptions", "-frtti", ] nanobind_pywrap_extension( name = "_mlir", srcs = [ "@llvm-project//mlir:lib/Bindings/Python/MainModule.cpp", ], copts = COPTS, deps = [ "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:MLIRBindingsPythonCore", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeaders", "@nanobind", ], ) py_stubgen( name = "_mlir_pyi", outs = [ "_mlir/__init__.pyi", "_mlir/ir.pyi", "_mlir/passmanager.pyi", "_mlir/rewrite.pyi", ], module = "jaxlib.mlir._mlir_libs._mlir", ) nanobind_pywrap_extension( name = "_mlirDialectsGPU", srcs = [ "@llvm-project//mlir:lib/Bindings/Python/DialectGPU.cpp", ], copts = COPTS, deps = [ "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPIGPU", "@llvm-project//mlir:CAPIIR", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeaders", "@nanobind", ], ) py_stubgen( name = "_mlirDialectsGPU_pyi", module = "jaxlib.mlir._mlir_libs._mlirDialectsGPU", ) nanobind_pywrap_extension( name = "_mlirGPUPasses", srcs = [ "@llvm-project//mlir:lib/Bindings/Python/GPUPasses.cpp", ], copts = COPTS, deps = [ "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPIGPU", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeaders", "@nanobind", ], ) nanobind_pywrap_extension( name = "_mlirDialectsNVGPU", srcs = [ "@llvm-project//mlir:lib/Bindings/Python/DialectNVGPU.cpp", ], copts = COPTS, deps = [ "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPIIR", "@llvm-project//mlir:CAPINVGPU", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeaders", "@nanobind", ], ) py_stubgen( name = "_mlirDialectsNVGPU_pyi", module = "jaxlib.mlir._mlir_libs._mlirDialectsNVGPU", ) nanobind_pywrap_extension( name = "_mlirDialectsLLVM", srcs = [ "@llvm-project//mlir:lib/Bindings/Python/DialectLLVM.cpp", ], copts = COPTS, deps = [ "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPIIR", "@llvm-project//mlir:CAPILLVM", "@llvm-project//mlir:CAPITarget", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeaders", "@nanobind", ], ) py_stubgen( name = "_mlirDialectsLLVM_pyi", module = "jaxlib.mlir._mlir_libs._mlirDialectsLLVM", ) nanobind_pywrap_extension( name = "_mlirDialectsSparseTensor", srcs = [ "@llvm-project//mlir:lib/Bindings/Python/DialectSparseTensor.cpp", ], copts = COPTS, deps = [ "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPISparseTensor", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeaders", "@nanobind", ], ) py_stubgen( name = "_mlirDialectsSparseTensor_pyi", module = "jaxlib.mlir._mlir_libs._mlirDialectsSparseTensor", ) nanobind_pywrap_extension( name = "_mlirSparseTensorPasses", srcs = [ "@llvm-project//mlir:lib/Bindings/Python/SparseTensorPasses.cpp", ], copts = COPTS, deps = [ "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPISparseTensor", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeaders", "@nanobind", ], ) nanobind_pywrap_extension( name = "_mosaic_gpu_ext", srcs = ["mosaic_gpu_ext.cc"], copts = COPTS, pytype_srcs = ["_mosaic_gpu_ext.pyi"], deps = [ "//jaxlib:hash_util", "//jaxlib/mosaic/dialect/gpu:gpu_dialect_capi", "//jaxlib/mosaic/gpu:dump", "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPIIR", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeadersAndDeps", "@nanobind", ], ) nanobind_pywrap_extension( name = "_thunky_ext", srcs = ["thunky_ext.cc"], copts = COPTS, pytype_srcs = ["_thunky_ext.pyi"], deps = [ "//jaxlib/thunky:mlir_to_proto", "//jaxlib/thunky/dialect:thunky_dialect_capi", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings:string_view", "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPIIR", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:FuncExtensions", "@llvm-project//mlir:IR", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeadersAndDeps", "@nanobind", "@xla//xla/service/gpu:gpu_executable_proto_cc", ], ) # This is here, instead of in jaxlib/mosaic/python, so it's in the same # directory as libjaxlib_mlir_capi.so (produced by # :jaxlib_mlir_capi_shared_library). This ensures that the RPATH works correctly # across platforms. It's not clear if Windows supports RPATH-like functionality # across different directories at all. nanobind_pywrap_extension( name = "_tpu_ext", srcs = ["tpu_ext.cc"], copts = COPTS, pytype_srcs = ["_tpu_ext.pyi"], deps = [ "//jaxlib/mosaic:tpu_dialect_capi", "@com_google_absl//absl/log:check", "@llvm-project//llvm:Support", "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPIIR", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeadersAndDeps", "@nanobind", "@xla//xla/python:nb_numpy", "@xla//xla/tsl/python/lib/core:numpy", ], ) # This target contains the extension and it's Python dependencies, which are not # supported by the `nanobind_pywrap_extension`/`nanobind_extension` macros. py_library( name = "_tpu_ext_lib", deps = [ ":_tpu_ext", "//jaxlib/mlir:ir", "//jaxlib/mosaic/python:layout_defs", ], ) nanobind_pywrap_extension( name = "_triton_ext", srcs = ["triton_ext.cc"], copts = COPTS, pytype_srcs = ["_triton_ext.pyi"], deps = [ "@nanobind", ] + if_windows( [], [ "//jaxlib/triton:triton_dialect_capi", "@llvm-project//mlir:CAPIIR", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeadersAndDeps", "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPITransforms", ], ), ) symlink_inputs( name = "_mlir_libs", rule = pytype_library, symlinked_inputs = {"srcs": { ".": [ "@llvm-project//mlir/python:MlirLibsPyFiles", ], }}, deps = [ ":_jax_mlir_ext", ":_mlir", ], ) # JAX-specific registrations. cc_library( name = "traceback_to_location", srcs = ["traceback_to_location.cc"], hdrs = ["traceback_to_location.h"], copts = ["-fexceptions"], deps = [ "//jaxlib:traceback", "@com_google_absl//absl/base", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/types:span", "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPIIR", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:IR", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeaders", "@nanobind", ], ) nanobind_pywrap_extension( name = "_jax_mlir_ext", srcs = ["jax_mlir_ext.cc"], copts = COPTS, deps = [ ":traceback_to_location", "//jaxlib/mosaic/gpu:mlir_capi", "@com_google_absl//absl/log:check", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", "@com_google_absl//absl/strings:str_format", "@com_google_absl//absl/types:span", "@llvm-project//llvm:Support", "@llvm-project//mlir:ArithDialect", "@llvm-project//mlir:CAPIArith", "@llvm-project//mlir:CAPICF", "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPIGPU", "@llvm-project//mlir:CAPIIR", "@llvm-project//mlir:CAPILLVM", "@llvm-project//mlir:CAPIMath", "@llvm-project//mlir:CAPIMemRef", "@llvm-project//mlir:CAPINVGPU", "@llvm-project//mlir:CAPINVVM", "@llvm-project//mlir:CAPISCF", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:CAPIVector", "@llvm-project//mlir:FuncDialect", "@llvm-project//mlir:IR", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeaders", "@llvm-project//mlir:ShapeDialect", "@llvm-project//mlir:Support", "@llvm-project//mlir:TensorDialect", "@nanobind", "@rules_python//python/cc:current_py_cc_headers", "@shardy//shardy/dialect/sdy/ir:dialect", "@shardy//shardy/integrations/c:sdy_capi", "@stablehlo//:vhlo_ops", "@xla//xla/pjrt:status_casters", "@xla//xla/python:nb_absl_span", "@xla//xla/service/spmd/shardy/integrations/c:xla_sdy_capi", ], ) py_stubgen( name = "_jax_mlir_ext_pyi", module = "jaxlib.mlir._mlir_libs._jax_mlir_ext", ) ##---------------------------------------------------------------------------## # MHLO Extensions ##---------------------------------------------------------------------------## nanobind_pywrap_extension( name = "_mlirHlo", srcs = [ "@xla//xla/mlir_hlo:bindings/python/MlirHloModule.cc", ], copts = COPTS, deps = [ "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPIIR", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeaders", "@nanobind", "@rules_python//python/cc:current_py_cc_headers", "@xla//xla/mlir_hlo:CAPI", ], ) py_stubgen( name = "_mlirHlo_pyi", module = "jaxlib.mlir._mlir_libs._mlirHlo", ) ##---------------------------------------------------------------------------## # Shardy Extensions ##---------------------------------------------------------------------------## nanobind_pywrap_extension( name = "_sdy", srcs = [ "@shardy//shardy/integrations/python/ir:sdy_module.cc", ], copts = COPTS, deps = [ "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPIIR", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:IR", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeaders", "@nanobind", "@rules_python//python/cc:current_py_cc_headers", "@shardy//shardy/integrations/c:sdy_capi", ], ) py_stubgen( name = "_sdy_pyi", module = "jaxlib.mlir._mlir_libs._sdy", ) nanobind_pywrap_extension( name = "_sdyMpmd", srcs = [ "@shardy//shardy/integrations/python/ir/mpmd:mpmd_module.cc", ], copts = COPTS, deps = [ "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPIIR", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:IR", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeaders", "@nanobind", "@rules_python//python/cc:current_py_cc_headers", "@shardy//shardy/integrations/c/mpmd:mpmd_capi", ], ) py_stubgen( name = "_sdyMpmd_pyi", module = "jaxlib.mlir._mlir_libs._sdyMpmd", ) ##---------------------------------------------------------------------------## # Stablehlo Extensions ##---------------------------------------------------------------------------## nanobind_pywrap_extension( name = "_chlo", srcs = [ "@stablehlo//:chlo_py_api_files", ], copts = COPTS, deps = [ "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPIIR", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeaders", "@nanobind", "@rules_python//python/cc:current_py_cc_headers", "@stablehlo//:chlo_capi", ], ) py_stubgen( name = "_chlo_pyi", module = "jaxlib.mlir._mlir_libs._chlo", ) nanobind_pywrap_extension( name = "_stablehlo", srcs = [ "@stablehlo//:stablehlo_py_api_files", ], copts = COPTS, deps = [ "@llvm-project//llvm:Support", "@llvm-project//mlir:CAPIDebug", "@llvm-project//mlir:CAPIIR", "@llvm-project//mlir:CAPITransforms", "@llvm-project//mlir:MLIRBindingsPythonNanobindHeaders", "@nanobind", "@rules_python//python/cc:current_py_cc_headers", "@stablehlo//:stablehlo_capi", ], ) py_stubgen( name = "_stablehlo_pyi", module = "jaxlib.mlir._mlir_libs._stablehlo", ) ##---------------------------------------------------------------------------## # Type stubs ##---------------------------------------------------------------------------## pywrap_library( name = "mlir_for_stubgen", common_lib_def_files_or_filters = { "jaxlib/mlir/_mlir_libs/mlir_for_stubgen_common": "mlir_for_stubgen.json", }, visibility = ["//visibility:private"], deps = [ ":_chlo", ":_jax_mlir_ext", ":_mlir", ":_mlirDialectsGPU", ":_mlirDialectsLLVM", ":_mlirDialectsNVGPU", ":_mlirDialectsSparseTensor", ":_mlirHlo", ":_sdy", ":_sdyMpmd", ":_stablehlo", ], ) pywrap_binaries( name = "mlir_for_stubgen_binaries", dep = ":mlir_for_stubgen", visibility = ["//visibility:private"], ) py_binary( name = "stubgen", srcs = ["stubgen_runner.py"], data = [ ":mlir_for_stubgen_binaries", "@nanobind//:src/stubgen.py", ], main = "stubgen_runner.py", visibility = ["//visibility:private"], deps = [ "@rules_python//python/runfiles", ], ) py_binary( name = "normalize_stubs", srcs = ["normalize_stubs.py"], visibility = ["//visibility:private"], ) filegroup( name = "all_pyi", srcs = [ ":_chlo_pyi", ":_jax_mlir_ext_pyi", ":_mlirDialectsGPU_pyi", ":_mlirDialectsLLVM_pyi", ":_mlirDialectsNVGPU_pyi", ":_mlirDialectsSparseTensor_pyi", ":_mlirHlo_pyi", ":_mlir_pyi", ":_sdyMpmd_pyi", ":_sdy_pyi", ":_stablehlo_pyi", ], )