2022-09-22 12:26:48 -07:00
|
|
|
# Copyright 2018 The JAX Authors.
|
2021-09-03 10:03:25 -07: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-07-21 14:20:39 -04:00
|
|
|
import numpy as np
|
|
|
|
|
2021-09-03 10:03:25 -07:00
|
|
|
from jaxlib import xla_client
|
|
|
|
|
2024-08-13 02:40:52 -07:00
|
|
|
from .cpu import _lapack
|
2024-08-28 03:53:07 -07:00
|
|
|
from .cpu._lapack import eig
|
2025-02-10 08:26:52 -08:00
|
|
|
from .cpu._lapack import schur
|
2022-05-06 14:50:54 -07:00
|
|
|
|
2021-09-03 10:03:25 -07:00
|
|
|
for _name, _value in _lapack.registrations().items():
|
2024-07-25 09:58:41 -07:00
|
|
|
xla_client.register_custom_call_target(
|
|
|
|
_name,
|
|
|
|
_value,
|
|
|
|
platform="cpu",
|
|
|
|
api_version=(1 if _name.endswith("_ffi") else 0),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2025-02-10 08:26:52 -08:00
|
|
|
EigComputationMode = eig.ComputationMode
|
|
|
|
SchurComputationMode = schur.ComputationMode
|
|
|
|
SchurSort = schur.Sort
|
2024-08-13 02:40:52 -07:00
|
|
|
|
|
|
|
|
2024-07-25 09:58:41 -07:00
|
|
|
LAPACK_DTYPE_PREFIX = {
|
|
|
|
np.float32: "s",
|
|
|
|
np.float64: "d",
|
|
|
|
np.complex64: "c",
|
|
|
|
np.complex128: "z",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-21 05:07:58 -07:00
|
|
|
def prepare_lapack_call(fn_base, dtype):
|
|
|
|
"""Initializes the LAPACK library and returns the LAPACK target name."""
|
|
|
|
_lapack.initialize()
|
|
|
|
return build_lapack_fn_target(fn_base, dtype)
|
|
|
|
|
|
|
|
|
2024-07-25 09:58:41 -07:00
|
|
|
def build_lapack_fn_target(fn_base: str, dtype) -> str:
|
|
|
|
"""Builds the target name for a LAPACK function custom call."""
|
|
|
|
try:
|
|
|
|
prefix = (
|
|
|
|
LAPACK_DTYPE_PREFIX.get(dtype, None) or LAPACK_DTYPE_PREFIX[dtype.type]
|
|
|
|
)
|
|
|
|
return f"lapack_{prefix}{fn_base}"
|
|
|
|
except KeyError as err:
|
|
|
|
raise NotImplementedError(err, f"Unsupported dtype {dtype}.") from err
|