2022-09-22 12:26:48 -07:00
|
|
|
# Copyright 2019 The JAX Authors.
|
2019-11-24 13:06:23 -05:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2023-12-11 13:59:29 +00:00
|
|
|
from __future__ import annotations
|
2022-05-05 10:54:53 -07:00
|
|
|
from functools import partial
|
2019-11-24 13:06:23 -05:00
|
|
|
import itertools
|
|
|
|
|
2022-04-06 13:22:25 -07:00
|
|
|
import jaxlib.mlir.ir as ir
|
2019-11-24 13:06:23 -05:00
|
|
|
|
|
|
|
from jaxlib import xla_client
|
|
|
|
|
2022-12-15 20:59:34 -08:00
|
|
|
from .hlo_helpers import custom_call
|
2025-02-27 11:51:39 -08:00
|
|
|
from .plugin_support import import_from_plugin
|
2024-07-31 08:09:28 -07:00
|
|
|
|
2025-02-27 11:51:39 -08:00
|
|
|
_cuda_prng = import_from_plugin("cuda", "_prng")
|
|
|
|
_hip_prng = import_from_plugin("rocm", "_prng")
|
2023-11-06 09:05:08 -08:00
|
|
|
|
|
|
|
if _cuda_prng:
|
2021-09-02 07:52:35 -07:00
|
|
|
for _name, _value in _cuda_prng.registrations().items():
|
2025-02-14 07:24:39 -08:00
|
|
|
# TODO(danfm): remove after JAX 0.5.1 release
|
|
|
|
api_version = 1 if "_ffi" in _name else 0
|
2024-06-28 06:24:01 -07:00
|
|
|
xla_client.register_custom_call_target(_name, _value, platform="CUDA",
|
2025-02-14 07:24:39 -08:00
|
|
|
api_version=api_version)
|
2022-05-05 10:54:53 -07:00
|
|
|
|
2024-06-12 18:45:01 -05:00
|
|
|
if _hip_prng:
|
2022-05-05 10:54:53 -07:00
|
|
|
for _name, _value in _hip_prng.registrations().items():
|
2025-02-14 07:24:39 -08:00
|
|
|
# TODO(danfm): remove after JAX 0.5.1 release
|
2024-06-28 06:24:01 -07:00
|
|
|
api_version = 1 if "_ffi" in _name else 0
|
|
|
|
xla_client.register_custom_call_target(_name, _value, platform="ROCM",
|
|
|
|
api_version=api_version)
|
2019-11-24 13:06:23 -05:00
|
|
|
|
2024-07-31 08:09:28 -07:00
|
|
|
|
2024-06-28 06:24:01 -07:00
|
|
|
def _threefry2x32_lowering(prng, platform: str, keys, data,
|
2023-12-11 13:59:29 +00:00
|
|
|
length: int | ir.Value | None = None,
|
2024-06-28 06:24:01 -07:00
|
|
|
output_shape: ir.Value | None = None,
|
2024-07-31 08:09:28 -07:00
|
|
|
forward_compatibility_mode: bool = False):
|
2023-05-02 22:26:27 -07:00
|
|
|
"""ThreeFry2x32 kernel for GPU.
|
|
|
|
|
|
|
|
In presence of dynamic shapes, `length` is an `ir.Value` and `output_shape`
|
|
|
|
is a 1D tensor describing the shape of the two outputs.
|
|
|
|
"""
|
2024-07-31 08:09:28 -07:00
|
|
|
del forward_compatibility_mode
|
2022-04-06 13:22:25 -07:00
|
|
|
assert len(keys) == 2, keys
|
|
|
|
assert len(data) == 2, data
|
|
|
|
assert (ir.RankedTensorType(keys[0].type).element_type ==
|
|
|
|
ir.IntegerType.get_unsigned(32)), keys[0].type
|
2022-12-27 04:47:48 -08:00
|
|
|
|
2022-04-06 13:22:25 -07:00
|
|
|
typ = keys[0].type
|
|
|
|
dims = ir.RankedTensorType(typ).shape
|
|
|
|
|
|
|
|
for x in itertools.chain(keys, data):
|
|
|
|
assert x.type == typ, (x.type, typ)
|
|
|
|
ndims = len(dims)
|
2022-05-06 14:50:54 -07:00
|
|
|
layout = tuple(range(ndims - 1, -1, -1))
|
2022-12-27 04:47:48 -08:00
|
|
|
operand_layouts = [layout] * 4
|
|
|
|
operands = [keys[0], keys[1], data[0], data[1]]
|
|
|
|
|
2024-06-28 06:24:01 -07:00
|
|
|
opaque = {} # Use if not forward_compatibility_mode to trigger the FFI (v4).
|
2022-12-27 04:47:48 -08:00
|
|
|
if isinstance(length, int):
|
2023-05-02 22:26:27 -07:00
|
|
|
result_shapes = None
|
2022-12-27 04:47:48 -08:00
|
|
|
else:
|
2023-05-02 22:26:27 -07:00
|
|
|
assert output_shape is not None
|
|
|
|
# We also need to pass separately the shapes of the outputs.
|
|
|
|
result_shapes = [output_shape, output_shape]
|
2022-12-27 04:47:48 -08:00
|
|
|
|
2024-07-31 08:09:28 -07:00
|
|
|
custom_call_target = f"{platform}_threefry2x32_ffi"
|
2022-05-06 14:50:54 -07:00
|
|
|
return custom_call(
|
2024-06-28 06:24:01 -07:00
|
|
|
custom_call_target,
|
2024-07-31 08:09:28 -07:00
|
|
|
api_version=4,
|
2023-09-03 13:06:50 -07:00
|
|
|
result_types=[typ, typ],
|
|
|
|
operands=operands,
|
2022-05-06 14:50:54 -07:00
|
|
|
backend_config=opaque,
|
2022-12-27 04:47:48 -08:00
|
|
|
operand_layouts=operand_layouts,
|
2023-05-02 22:26:27 -07:00
|
|
|
result_layouts=[layout] * 2,
|
2023-09-03 13:06:50 -07:00
|
|
|
result_shapes=result_shapes).results
|
2022-05-05 10:54:53 -07:00
|
|
|
|
2022-10-25 07:23:07 -07:00
|
|
|
|
|
|
|
cuda_threefry2x32 = partial(_threefry2x32_lowering, _cuda_prng, "cu")
|
2022-05-09 13:36:48 +00:00
|
|
|
rocm_threefry2x32 = partial(_threefry2x32_lowering, _hip_prng, "hip")
|