225 lines
5.6 KiB
Python
Raw Normal View History

# Copyright 2018 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.
# JAX is Autograd and XLA
load(
"//jaxlib:jax.bzl",
"nanobind_extension",
"py_library_providing_imports_info",
"pytype_library",
)
load("//jaxlib:symlink_files.bzl", "symlink_files")
2019-11-24 13:13:39 -05:00
licenses(["notice"])
package(
default_applicable_licenses = [],
default_visibility = ["//jax:internal"],
)
# This makes xla_extension module accessible from jax._src.lib.
genrule(
name = "xla_extension_py",
outs = ["xla_extension.py"],
cmd = "echo 'from xla.xla.python.xla_extension import *\n' > $@",
)
py_library_providing_imports_info(
name = "jaxlib",
srcs = [
"gpu_common_utils.py",
"gpu_linalg.py",
"gpu_prng.py",
"gpu_rnn.py",
"gpu_solver.py",
"gpu_sparse.py",
"gpu_triton.py",
"hlo_helpers.py",
"init.py",
"lapack.py",
"plugin_support.py",
":version",
":xla_client",
":xla_extension_py",
],
data = [":ffi_headers"],
lib_rule = pytype_library,
deps = [
":cpu_feature_guard",
Add a C++ implementation of safe map. Before (argument names reversed, oops, fixed in code): ``` name time/op safe_map/num_args:0/arg_lengths:1 1.43µs ± 1% safe_map/num_args:1/arg_lengths:1 1.61µs ± 1% safe_map/num_args:2/arg_lengths:1 1.72µs ± 0% safe_map/num_args:5/arg_lengths:1 2.14µs ± 1% safe_map/num_args:10/arg_lengths:1 2.87µs ± 1% safe_map/num_args:100/arg_lengths:1 15.6µs ± 1% safe_map/num_args:0/arg_lengths:2 1.65µs ± 0% safe_map/num_args:1/arg_lengths:2 1.83µs ± 1% safe_map/num_args:2/arg_lengths:2 1.97µs ± 1% safe_map/num_args:5/arg_lengths:2 2.41µs ± 1% safe_map/num_args:10/arg_lengths:2 3.22µs ± 2% safe_map/num_args:100/arg_lengths:2 17.0µs ± 2% safe_map/num_args:0/arg_lengths:3 1.83µs ± 1% safe_map/num_args:1/arg_lengths:3 2.02µs ± 1% safe_map/num_args:2/arg_lengths:3 2.16µs ± 1% safe_map/num_args:5/arg_lengths:3 2.63µs ± 1% safe_map/num_args:10/arg_lengths:3 3.48µs ± 1% safe_map/num_args:100/arg_lengths:3 18.1µs ± 1% ``` After: ``` name time/op safe_map/num_args:0/arg_lengths:1 409ns ± 1% safe_map/num_args:1/arg_lengths:1 602ns ± 5% safe_map/num_args:2/arg_lengths:1 777ns ± 4% safe_map/num_args:5/arg_lengths:1 1.21µs ± 3% safe_map/num_args:10/arg_lengths:1 1.93µs ± 2% safe_map/num_args:100/arg_lengths:1 14.7µs ± 0% safe_map/num_args:0/arg_lengths:2 451ns ± 1% safe_map/num_args:1/arg_lengths:2 652ns ± 0% safe_map/num_args:2/arg_lengths:2 850ns ± 4% safe_map/num_args:5/arg_lengths:2 1.32µs ± 3% safe_map/num_args:10/arg_lengths:2 2.11µs ± 2% safe_map/num_args:100/arg_lengths:2 16.0µs ± 1% safe_map/num_args:0/arg_lengths:3 496ns ± 1% safe_map/num_args:1/arg_lengths:3 718ns ± 5% safe_map/num_args:2/arg_lengths:3 919ns ± 4% safe_map/num_args:5/arg_lengths:3 1.43µs ± 2% safe_map/num_args:10/arg_lengths:3 2.30µs ± 2% safe_map/num_args:100/arg_lengths:3 17.3µs ± 1% ``` PiperOrigin-RevId: 523263207
2023-04-10 18:09:24 -07:00
":utils",
"//jaxlib/cpu:_lapack",
"//jaxlib/mlir",
"//jaxlib/mlir:arithmetic_dialect",
"//jaxlib/mlir:builtin_dialect",
Migrate from MLIR-HLO's CHLO to StableHLO's CHLO Unlike StableHLO which is meant to coexist with MHLO, StableHLO's CHLO is meant to replace MLIR-HLO's CHLO. This change is the final step towards enabling adoption of StableHLO. If we keep two copies of CHLO, then some users won't be able to depend on both MLIR-HLO and StableHLO, and that is a useful possibility to enable both in the short and in the long term. C++: 1) C++ dependency changes from `//third_party/tensorflow/compiler/xla/mlir_hlo` (includes CHLO, among other things) to `//third_party/tensorflow/compiler/xla/mlir_hlo/stablehlo:chlo_ops` (in CMake, from `ChloDialect` to `ChloOps`). 2) .h include changes from `#include "third_party/tensorflow/compiler/xla/mlir_hlo/include/mlir-hlo/Dialect/mhlo/IR/chlo_ops.h"` to `#include "third_party/tensorflow/compiler/xla/mlir_hlo/stablehlo/stablehlo/dialect/ChloOps.h"`. 3) To register the CHLO dialect in C++, you'll need to depend on `//third_party/tensorflow/compiler/xla/mlir_hlo/stablehlo:register`, include `#include "third_party/tensorflow/compiler/xla/mlir_hlo/stablehlo/stablehlo/dialect/Register.h"` and call `mlir::stablehlo::registerAllDialects(registry)`. 4) C++ usage doesn't change - StableHLO's CHLO is an exact copy of MLIR-HLO's CHLO. Python: 5) Python dependency changes from `//third_party/py/mlir:mhlo_dialect` (includes CHLO, among other things) to `//third_party/py/mlir:chlo_dialect` (in CMake, from `MLIRHLOPythonModules` to `StablehloUnifiedPythonModules`). 6) Python imports don't change. 7) To register the CHLO dialect in Python, you'll need to change `chlo.register_chlo_dialect(context)` to `chlo.register_dialect(context)`. 8) Python usage doesn't change - StableHLO's CHLO is an exact copy of MLIR-HLO's CHLO. PiperOrigin-RevId: 470265566
2022-08-26 09:34:46 -07:00
"//jaxlib/mlir:chlo_dialect",
"//jaxlib/mlir:func_dialect",
"//jaxlib/mlir:gpu_dialect",
"//jaxlib/mlir:ir",
"//jaxlib/mlir:llvm_dialect",
"//jaxlib/mlir:math_dialect",
"//jaxlib/mlir:memref_dialect",
"//jaxlib/mlir:mhlo_dialect",
"//jaxlib/mlir:nvgpu_dialect",
"//jaxlib/mlir:nvvm_dialect",
"//jaxlib/mlir:pass_manager",
"//jaxlib/mlir:scf_dialect",
#sdy Initial set of changes to allow for lowering to the Shardy dialect. The OpenXLA project is working on an open source, MLIR, named-axis based propagation (and in the future SP<D partitioning) system that will be dialect agnostic (would work for any dialect - MHLO, StableHLO, YourDialect). We plan on having frontends like JAX and PyTorch target this when using XLA and wanting SPMD propagation/partitioning. See www.github.com/openxla/shardy for more info. Currently Shardy is implemented inside the XLA compiler, requiring us to round-trip between StableHLO and HLO with `mhlo.sharding`s. But we will eventually make Shardy the first pass in the XLA pipeline while it's still working on StableHLO. Partitioning (the system that adds the collectives like all-gathers/all-reduces) will still be the GSPMD Partitioner, but next year the Shardy partitioner will be developed, allowing for propagation and partitioning to be completely in MLIR and the first pass in the pipeline. So then we'd have: 1. Traced jaxpr 2. Jaxpr -> StableHLO 3. StableHLO with Shardy propagation 4. StableHLO with Shardy partitioning 5. StableHLO -> HLO 6. XLA optimizations The following test: ```py def test_sdy_lowering(self): mesh = jtu.create_global_mesh((4, 2), ('x', 'y')) np_inp = np.arange(16).reshape(8, 2) s = jax.sharding.NamedSharding(mesh, P('x', 'y')) arr = jax.device_put(np_inp, s) @partial(jax.jit, out_shardings=s) def f(x): return x * 2 print(f.lower(arr).as_text()) ``` outputs: ``` module @jit_f attributes {mhlo.num_partitions = 8 : i32, mhlo.num_replicas = 1 : i32} { sdy.mesh @mesh = <"x"=4, "y"=2> func.func public @main(%arg0: tensor<8x2xi64> {mhlo.layout_mode = "{1,0}", sdy.sharding = #sdy.sharding<@mesh, [{"x"}, {"y"}]>}) -> (tensor<8x2xi64> {jax.result_info = "", mhlo.layout_mode = "default", sdy.sharding = #sdy.sharding<@mesh, [{"x"}, {"y"}]>}) { %c = stablehlo.constant dense<2> : tensor<i64> %0 = stablehlo.broadcast_in_dim %c, dims = [] : (tensor<i64>) -> tensor<8x2xi64> %1 = stablehlo.multiply %arg0, %0 : tensor<8x2xi64> return %1 : tensor<8x2xi64> } } ``` Shardy will be hidden behind the `jax_use_shardy_partitioner` flag initially before becoming enabled by default in the future. PiperOrigin-RevId: 655127611
2024-07-23 05:31:15 -07:00
"//jaxlib/mlir:sdy_dialect",
"//jaxlib/mlir:sparse_tensor_dialect",
Migrate from MLIR-HLO's CHLO to StableHLO's CHLO Unlike StableHLO which is meant to coexist with MHLO, StableHLO's CHLO is meant to replace MLIR-HLO's CHLO. This change is the final step towards enabling adoption of StableHLO. If we keep two copies of CHLO, then some users won't be able to depend on both MLIR-HLO and StableHLO, and that is a useful possibility to enable both in the short and in the long term. C++: 1) C++ dependency changes from `//third_party/tensorflow/compiler/xla/mlir_hlo` (includes CHLO, among other things) to `//third_party/tensorflow/compiler/xla/mlir_hlo/stablehlo:chlo_ops` (in CMake, from `ChloDialect` to `ChloOps`). 2) .h include changes from `#include "third_party/tensorflow/compiler/xla/mlir_hlo/include/mlir-hlo/Dialect/mhlo/IR/chlo_ops.h"` to `#include "third_party/tensorflow/compiler/xla/mlir_hlo/stablehlo/stablehlo/dialect/ChloOps.h"`. 3) To register the CHLO dialect in C++, you'll need to depend on `//third_party/tensorflow/compiler/xla/mlir_hlo/stablehlo:register`, include `#include "third_party/tensorflow/compiler/xla/mlir_hlo/stablehlo/stablehlo/dialect/Register.h"` and call `mlir::stablehlo::registerAllDialects(registry)`. 4) C++ usage doesn't change - StableHLO's CHLO is an exact copy of MLIR-HLO's CHLO. Python: 5) Python dependency changes from `//third_party/py/mlir:mhlo_dialect` (includes CHLO, among other things) to `//third_party/py/mlir:chlo_dialect` (in CMake, from `MLIRHLOPythonModules` to `StablehloUnifiedPythonModules`). 6) Python imports don't change. 7) To register the CHLO dialect in Python, you'll need to change `chlo.register_chlo_dialect(context)` to `chlo.register_dialect(context)`. 8) Python usage doesn't change - StableHLO's CHLO is an exact copy of MLIR-HLO's CHLO. PiperOrigin-RevId: 470265566
2022-08-26 09:34:46 -07:00
"//jaxlib/mlir:stablehlo_dialect",
"//jaxlib/mlir:vector_dialect",
"//jaxlib/mosaic",
"//jaxlib/triton",
"@xla//xla/python:xla_extension",
],
)
symlink_files(
name = "version",
srcs = ["//jax:version.py"],
dst = ".",
flatten = True,
)
symlink_files(
name = "xla_client",
srcs = ["@xla//xla/python:xla_client"],
dst = ".",
flatten = True,
)
symlink_files(
name = "ffi_headers",
srcs = ["@xla//xla/ffi/api:all_headers"],
dst = "include/xla/ffi/api",
flatten = True,
)
exports_files([
"README.md",
"setup.py",
])
cc_library(
name = "absl_status_casters",
hdrs = ["absl_status_casters.h"],
copts = [
"-fexceptions",
"-fno-strict-aliasing",
],
features = ["-use_header_modules"],
deps = [
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
],
)
cc_library(
name = "ffi_helpers",
hdrs = ["ffi_helpers.h"],
features = ["-use_header_modules"],
deps = [
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings:str_format",
"@xla//xla/ffi/api:c_api",
"@xla//xla/ffi/api:ffi",
],
)
cc_library(
name = "kernel_nanobind_helpers",
hdrs = ["kernel_nanobind_helpers.h"],
copts = [
"-fexceptions",
"-fno-strict-aliasing",
],
features = ["-use_header_modules"],
deps = [
":kernel_helpers",
"@com_google_absl//absl/base",
"@nanobind",
"@xla//xla/ffi/api:c_api",
"@xla//xla/tsl/python/lib/core:numpy",
],
)
cc_library(
name = "kernel_helpers",
hdrs = ["kernel_helpers.h"],
copts = [
"-fexceptions",
"-fno-strict-aliasing",
],
2019-11-26 17:06:57 -08:00
features = ["-use_header_modules"],
deps = [
"@com_google_absl//absl/base",
"@com_google_absl//absl/status:statusor",
],
)
cc_library(
name = "pass_boilerplate",
hdrs = ["pass_boilerplate.h"],
# compatible with libtpu
deps = [
"@llvm-project//mlir:IR",
"@llvm-project//mlir:Pass",
"@llvm-project//mlir:Support",
],
)
cc_library(
name = "handle_pool",
hdrs = ["handle_pool.h"],
copts = [
"-fexceptions",
"-fno-strict-aliasing",
],
features = ["-use_header_modules"],
deps = [
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/synchronization",
],
)
# This isn't a CPU kernel. This exists to catch cases where jaxlib is built for the wrong
# target architecture.
nanobind_extension(
name = "cpu_feature_guard",
srcs = ["cpu_feature_guard.c"],
module_name = "cpu_feature_guard",
deps = [
"@xla//third_party/python_runtime:headers",
],
)
nanobind_extension(
Add a C++ implementation of safe map. Before (argument names reversed, oops, fixed in code): ``` name time/op safe_map/num_args:0/arg_lengths:1 1.43µs ± 1% safe_map/num_args:1/arg_lengths:1 1.61µs ± 1% safe_map/num_args:2/arg_lengths:1 1.72µs ± 0% safe_map/num_args:5/arg_lengths:1 2.14µs ± 1% safe_map/num_args:10/arg_lengths:1 2.87µs ± 1% safe_map/num_args:100/arg_lengths:1 15.6µs ± 1% safe_map/num_args:0/arg_lengths:2 1.65µs ± 0% safe_map/num_args:1/arg_lengths:2 1.83µs ± 1% safe_map/num_args:2/arg_lengths:2 1.97µs ± 1% safe_map/num_args:5/arg_lengths:2 2.41µs ± 1% safe_map/num_args:10/arg_lengths:2 3.22µs ± 2% safe_map/num_args:100/arg_lengths:2 17.0µs ± 2% safe_map/num_args:0/arg_lengths:3 1.83µs ± 1% safe_map/num_args:1/arg_lengths:3 2.02µs ± 1% safe_map/num_args:2/arg_lengths:3 2.16µs ± 1% safe_map/num_args:5/arg_lengths:3 2.63µs ± 1% safe_map/num_args:10/arg_lengths:3 3.48µs ± 1% safe_map/num_args:100/arg_lengths:3 18.1µs ± 1% ``` After: ``` name time/op safe_map/num_args:0/arg_lengths:1 409ns ± 1% safe_map/num_args:1/arg_lengths:1 602ns ± 5% safe_map/num_args:2/arg_lengths:1 777ns ± 4% safe_map/num_args:5/arg_lengths:1 1.21µs ± 3% safe_map/num_args:10/arg_lengths:1 1.93µs ± 2% safe_map/num_args:100/arg_lengths:1 14.7µs ± 0% safe_map/num_args:0/arg_lengths:2 451ns ± 1% safe_map/num_args:1/arg_lengths:2 652ns ± 0% safe_map/num_args:2/arg_lengths:2 850ns ± 4% safe_map/num_args:5/arg_lengths:2 1.32µs ± 3% safe_map/num_args:10/arg_lengths:2 2.11µs ± 2% safe_map/num_args:100/arg_lengths:2 16.0µs ± 1% safe_map/num_args:0/arg_lengths:3 496ns ± 1% safe_map/num_args:1/arg_lengths:3 718ns ± 5% safe_map/num_args:2/arg_lengths:3 919ns ± 4% safe_map/num_args:5/arg_lengths:3 1.43µs ± 2% safe_map/num_args:10/arg_lengths:3 2.30µs ± 2% safe_map/num_args:100/arg_lengths:3 17.3µs ± 1% ``` PiperOrigin-RevId: 523263207
2023-04-10 18:09:24 -07:00
name = "utils",
srcs = ["utils.cc"],
module_name = "utils",
deps = [
"@com_google_absl//absl/cleanup",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
Add a C++ implementation of safe map. Before (argument names reversed, oops, fixed in code): ``` name time/op safe_map/num_args:0/arg_lengths:1 1.43µs ± 1% safe_map/num_args:1/arg_lengths:1 1.61µs ± 1% safe_map/num_args:2/arg_lengths:1 1.72µs ± 0% safe_map/num_args:5/arg_lengths:1 2.14µs ± 1% safe_map/num_args:10/arg_lengths:1 2.87µs ± 1% safe_map/num_args:100/arg_lengths:1 15.6µs ± 1% safe_map/num_args:0/arg_lengths:2 1.65µs ± 0% safe_map/num_args:1/arg_lengths:2 1.83µs ± 1% safe_map/num_args:2/arg_lengths:2 1.97µs ± 1% safe_map/num_args:5/arg_lengths:2 2.41µs ± 1% safe_map/num_args:10/arg_lengths:2 3.22µs ± 2% safe_map/num_args:100/arg_lengths:2 17.0µs ± 2% safe_map/num_args:0/arg_lengths:3 1.83µs ± 1% safe_map/num_args:1/arg_lengths:3 2.02µs ± 1% safe_map/num_args:2/arg_lengths:3 2.16µs ± 1% safe_map/num_args:5/arg_lengths:3 2.63µs ± 1% safe_map/num_args:10/arg_lengths:3 3.48µs ± 1% safe_map/num_args:100/arg_lengths:3 18.1µs ± 1% ``` After: ``` name time/op safe_map/num_args:0/arg_lengths:1 409ns ± 1% safe_map/num_args:1/arg_lengths:1 602ns ± 5% safe_map/num_args:2/arg_lengths:1 777ns ± 4% safe_map/num_args:5/arg_lengths:1 1.21µs ± 3% safe_map/num_args:10/arg_lengths:1 1.93µs ± 2% safe_map/num_args:100/arg_lengths:1 14.7µs ± 0% safe_map/num_args:0/arg_lengths:2 451ns ± 1% safe_map/num_args:1/arg_lengths:2 652ns ± 0% safe_map/num_args:2/arg_lengths:2 850ns ± 4% safe_map/num_args:5/arg_lengths:2 1.32µs ± 3% safe_map/num_args:10/arg_lengths:2 2.11µs ± 2% safe_map/num_args:100/arg_lengths:2 16.0µs ± 1% safe_map/num_args:0/arg_lengths:3 496ns ± 1% safe_map/num_args:1/arg_lengths:3 718ns ± 5% safe_map/num_args:2/arg_lengths:3 919ns ± 4% safe_map/num_args:5/arg_lengths:3 1.43µs ± 2% safe_map/num_args:10/arg_lengths:3 2.30µs ± 2% safe_map/num_args:100/arg_lengths:3 17.3µs ± 1% ``` PiperOrigin-RevId: 523263207
2023-04-10 18:09:24 -07:00
"@com_google_absl//absl/container:inlined_vector",
"@com_google_absl//absl/synchronization",
"@nanobind",
"@xla//third_party/python_runtime:headers",
Add a C++ implementation of safe map. Before (argument names reversed, oops, fixed in code): ``` name time/op safe_map/num_args:0/arg_lengths:1 1.43µs ± 1% safe_map/num_args:1/arg_lengths:1 1.61µs ± 1% safe_map/num_args:2/arg_lengths:1 1.72µs ± 0% safe_map/num_args:5/arg_lengths:1 2.14µs ± 1% safe_map/num_args:10/arg_lengths:1 2.87µs ± 1% safe_map/num_args:100/arg_lengths:1 15.6µs ± 1% safe_map/num_args:0/arg_lengths:2 1.65µs ± 0% safe_map/num_args:1/arg_lengths:2 1.83µs ± 1% safe_map/num_args:2/arg_lengths:2 1.97µs ± 1% safe_map/num_args:5/arg_lengths:2 2.41µs ± 1% safe_map/num_args:10/arg_lengths:2 3.22µs ± 2% safe_map/num_args:100/arg_lengths:2 17.0µs ± 2% safe_map/num_args:0/arg_lengths:3 1.83µs ± 1% safe_map/num_args:1/arg_lengths:3 2.02µs ± 1% safe_map/num_args:2/arg_lengths:3 2.16µs ± 1% safe_map/num_args:5/arg_lengths:3 2.63µs ± 1% safe_map/num_args:10/arg_lengths:3 3.48µs ± 1% safe_map/num_args:100/arg_lengths:3 18.1µs ± 1% ``` After: ``` name time/op safe_map/num_args:0/arg_lengths:1 409ns ± 1% safe_map/num_args:1/arg_lengths:1 602ns ± 5% safe_map/num_args:2/arg_lengths:1 777ns ± 4% safe_map/num_args:5/arg_lengths:1 1.21µs ± 3% safe_map/num_args:10/arg_lengths:1 1.93µs ± 2% safe_map/num_args:100/arg_lengths:1 14.7µs ± 0% safe_map/num_args:0/arg_lengths:2 451ns ± 1% safe_map/num_args:1/arg_lengths:2 652ns ± 0% safe_map/num_args:2/arg_lengths:2 850ns ± 4% safe_map/num_args:5/arg_lengths:2 1.32µs ± 3% safe_map/num_args:10/arg_lengths:2 2.11µs ± 2% safe_map/num_args:100/arg_lengths:2 16.0µs ± 1% safe_map/num_args:0/arg_lengths:3 496ns ± 1% safe_map/num_args:1/arg_lengths:3 718ns ± 5% safe_map/num_args:2/arg_lengths:3 919ns ± 4% safe_map/num_args:5/arg_lengths:3 1.43µs ± 2% safe_map/num_args:10/arg_lengths:3 2.30µs ± 2% safe_map/num_args:100/arg_lengths:3 17.3µs ± 1% ``` PiperOrigin-RevId: 523263207
2023-04-10 18:09:24 -07:00
],
)