2019-08-08 11:50:31 -04:00
|
|
|
/* Copyright 2019 Google LLC
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
http://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.
|
|
|
|
==============================================================================*/
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "absl/container/flat_hash_map.h"
|
|
|
|
#include "absl/strings/str_format.h"
|
2020-12-05 00:07:04 +01:00
|
|
|
#include "third_party/gpus/cuda/include/cublas_v2.h"
|
|
|
|
#include "third_party/gpus/cuda/include/cuda.h"
|
|
|
|
#include "third_party/gpus/cuda/include/cuda_runtime_api.h"
|
2021-09-02 07:52:35 -07:00
|
|
|
#include "jaxlib/cublas_kernels.h"
|
2021-08-09 23:44:14 -07:00
|
|
|
#include "jaxlib/kernel_pybind11_helpers.h"
|
|
|
|
#include "include/pybind11/numpy.h"
|
|
|
|
#include "include/pybind11/pybind11.h"
|
|
|
|
#include "include/pybind11/stl.h"
|
2019-08-08 11:50:31 -04:00
|
|
|
|
|
|
|
namespace jax {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
namespace py = pybind11;
|
|
|
|
|
|
|
|
// Converts a NumPy dtype to a Type.
|
2021-09-02 07:52:35 -07:00
|
|
|
CublasType DtypeToCublasType(const py::dtype& np_type) {
|
|
|
|
static auto* types =
|
|
|
|
new absl::flat_hash_map<std::pair<char, int>, CublasType>({
|
|
|
|
{{'f', 4}, CublasType::F32},
|
|
|
|
{{'f', 8}, CublasType::F64},
|
|
|
|
{{'c', 8}, CublasType::C64},
|
|
|
|
{{'c', 16}, CublasType::C128},
|
|
|
|
});
|
2019-08-08 11:50:31 -04:00
|
|
|
auto it = types->find({np_type.kind(), np_type.itemsize()});
|
|
|
|
if (it == types->end()) {
|
|
|
|
throw std::invalid_argument(
|
|
|
|
absl::StrFormat("Unsupported dtype %s", py::repr(np_type)));
|
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the descriptor for a TrsmBatched operation.
|
|
|
|
std::pair<size_t, py::bytes> BuildTrsmBatchedDescriptor(
|
|
|
|
const py::dtype& dtype, int batch, int m, int n, bool left_side, bool lower,
|
|
|
|
bool trans_a, bool conj_a, bool unit_diagonal) {
|
|
|
|
size_t size = batch * sizeof(void*);
|
|
|
|
TrsmBatchedDescriptor desc;
|
2021-09-02 07:52:35 -07:00
|
|
|
desc.type = DtypeToCublasType(dtype);
|
2019-08-08 11:50:31 -04:00
|
|
|
desc.batch = batch;
|
|
|
|
desc.m = m;
|
|
|
|
desc.n = n;
|
|
|
|
desc.side = left_side ? CUBLAS_SIDE_LEFT : CUBLAS_SIDE_RIGHT;
|
|
|
|
desc.uplo = lower ? CUBLAS_FILL_MODE_LOWER : CUBLAS_FILL_MODE_UPPER;
|
|
|
|
desc.trans = trans_a ? (conj_a ? CUBLAS_OP_C : CUBLAS_OP_T) : CUBLAS_OP_N;
|
|
|
|
desc.diag = unit_diagonal ? CUBLAS_DIAG_UNIT : CUBLAS_DIAG_NON_UNIT;
|
|
|
|
return {size, PackDescriptor(desc)};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the descriptor for a GetrfBatched operation.
|
|
|
|
std::pair<size_t, py::bytes> BuildGetrfBatchedDescriptor(const py::dtype& dtype,
|
|
|
|
int b, int n) {
|
2021-09-02 07:52:35 -07:00
|
|
|
CublasType type = DtypeToCublasType(dtype);
|
2019-08-08 11:50:31 -04:00
|
|
|
size_t size = b * sizeof(void*);
|
|
|
|
return {size, PackDescriptor(GetrfBatchedDescriptor{type, b, n})};
|
|
|
|
}
|
|
|
|
|
|
|
|
py::dict Registrations() {
|
|
|
|
py::dict dict;
|
|
|
|
dict["cublas_trsm_batched"] = EncapsulateFunction(TrsmBatched);
|
|
|
|
dict["cublas_getrf_batched"] = EncapsulateFunction(GetrfBatched);
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
2021-09-02 07:52:35 -07:00
|
|
|
PYBIND11_MODULE(_cublas, m) {
|
2019-08-08 11:50:31 -04:00
|
|
|
m.def("registrations", &Registrations);
|
|
|
|
m.def("build_trsm_batched_descriptor", &BuildTrsmBatchedDescriptor);
|
|
|
|
m.def("build_getrf_batched_descriptor", &BuildGetrfBatchedDescriptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2019-11-24 13:06:23 -05:00
|
|
|
} // namespace jax
|