2023-05-24 12:09:57 -07:00
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2023-06-22 04:56:43 -07:00
|
|
|
#include <tuple>
|
2023-05-24 12:09:57 -07:00
|
|
|
#include <vector>
|
|
|
|
|
2023-05-25 10:25:11 -07:00
|
|
|
#include "pybind11/pybind11.h"
|
2023-06-22 04:56:43 -07:00
|
|
|
#include "pybind11/pytypes.h"
|
2023-05-25 10:25:11 -07:00
|
|
|
#include "pybind11/stl.h"
|
2023-05-24 12:09:57 -07:00
|
|
|
#include "absl/status/statusor.h"
|
2023-05-25 10:25:11 -07:00
|
|
|
#include "jaxlib/gpu/gpu_kernel_helpers.h"
|
2023-06-22 04:56:43 -07:00
|
|
|
#include "jaxlib/gpu/triton.pb.h"
|
2023-07-03 06:51:45 -07:00
|
|
|
#include "jaxlib/gpu/triton_kernels.h"
|
2023-08-16 02:53:05 -07:00
|
|
|
#include "jaxlib/gpu/triton_utils.h"
|
2023-05-24 12:09:57 -07:00
|
|
|
#include "jaxlib/gpu/vendor.h"
|
2023-06-21 10:37:15 -07:00
|
|
|
#include "jaxlib/kernel_pybind11_helpers.h"
|
2023-05-24 12:09:57 -07:00
|
|
|
#include "pybind11_abseil/status_casters.h" // IWYU pragma: keep
|
|
|
|
|
2023-05-25 10:25:11 -07:00
|
|
|
#define CUDA_RETURN_IF_ERROR(expr) JAX_RETURN_IF_ERROR(JAX_AS_STATUS(expr))
|
2023-05-24 12:09:57 -07:00
|
|
|
|
2023-06-22 04:56:43 -07:00
|
|
|
|
2023-05-24 12:09:57 -07:00
|
|
|
namespace py = pybind11;
|
|
|
|
|
2023-05-25 10:25:11 -07:00
|
|
|
namespace jax::JAX_GPU_NAMESPACE {
|
2023-05-24 12:09:57 -07:00
|
|
|
|
|
|
|
PYBIND11_MODULE(_triton, m) {
|
2023-06-22 04:56:43 -07:00
|
|
|
py::class_<Kernel>(m, "TritonKernel")
|
|
|
|
.def(py::init<std::string, uint32_t, uint32_t, std::string, std::string,
|
|
|
|
int>());
|
|
|
|
|
|
|
|
py::class_<KernelCall::Parameter>(m, "TritonParameter");
|
|
|
|
|
|
|
|
m.def("create_array_parameter",
|
|
|
|
[](size_t bytes_to_zero, size_t ptr_divisibility) {
|
|
|
|
return KernelCall::Parameter{
|
|
|
|
KernelCall::Parameter::Array{bytes_to_zero, ptr_divisibility}};
|
|
|
|
});
|
|
|
|
|
|
|
|
m.def("create_scalar_parameter",
|
|
|
|
[](py::bool_ value,
|
|
|
|
std::string_view dtype) -> absl::StatusOr<KernelCall::Parameter> {
|
2023-07-28 22:56:36 -07:00
|
|
|
if ((dtype == "i1") || (dtype == "B")) {
|
2023-06-22 04:56:43 -07:00
|
|
|
return KernelCall::Parameter{static_cast<bool>(value)};
|
|
|
|
} else {
|
|
|
|
return absl::InvalidArgumentError(std::string("unknown dtype: ") +
|
|
|
|
dtype.data());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
m.def("create_scalar_parameter",
|
|
|
|
[](py::int_ value,
|
|
|
|
std::string_view dtype) -> absl::StatusOr<KernelCall::Parameter> {
|
|
|
|
if (dtype == "i32") {
|
|
|
|
return KernelCall::Parameter{static_cast<int32_t>(value)};
|
|
|
|
} else if (dtype == "u32") {
|
|
|
|
return KernelCall::Parameter{static_cast<uint32_t>(value)};
|
|
|
|
} else if (dtype == "i64") {
|
|
|
|
return KernelCall::Parameter{static_cast<int64_t>(value)};
|
|
|
|
} else if (dtype == "u64") {
|
|
|
|
return KernelCall::Parameter{static_cast<uint64_t>(value)};
|
|
|
|
} else {
|
|
|
|
return absl::InvalidArgumentError(std::string("unknown dtype: ") +
|
|
|
|
dtype.data());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-07-28 22:56:36 -07:00
|
|
|
m.def("create_scalar_parameter",
|
|
|
|
[](py::float_ value,
|
|
|
|
std::string_view dtype) -> absl::StatusOr<KernelCall::Parameter> {
|
|
|
|
if (dtype == "fp32") {
|
|
|
|
return KernelCall::Parameter{static_cast<float>(value)};
|
|
|
|
} else if (dtype == "fp64") {
|
|
|
|
return KernelCall::Parameter{static_cast<double>(value)};
|
|
|
|
} else {
|
|
|
|
return absl::InvalidArgumentError(std::string("unknown dtype: ") +
|
|
|
|
dtype.data());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-22 04:56:43 -07:00
|
|
|
py::class_<KernelCall>(m, "TritonKernelCall")
|
|
|
|
.def(py::init<Kernel, uint32_t, uint32_t, uint32_t,
|
|
|
|
std::vector<KernelCall::Parameter>>())
|
2023-08-16 02:53:05 -07:00
|
|
|
.def("to_proto", [](const KernelCall& kernel_call, std::string name,
|
|
|
|
std::string metadata) {
|
2023-07-03 06:51:45 -07:00
|
|
|
jax_triton::TritonAnyKernelCall proto;
|
2023-06-30 03:22:48 -07:00
|
|
|
*proto.mutable_kernel_call() = kernel_call.ToProto();
|
2023-08-16 02:53:05 -07:00
|
|
|
proto.set_name(std::move(name));
|
2023-06-30 03:22:48 -07:00
|
|
|
proto.set_metadata(std::move(metadata));
|
|
|
|
return py::bytes(proto.SerializeAsString());
|
|
|
|
});
|
2023-06-22 04:56:43 -07:00
|
|
|
|
|
|
|
py::class_<AutotunedKernelCall>(m, "TritonAutotunedKernelCall")
|
2023-05-24 12:09:57 -07:00
|
|
|
.def(py::init<>([](std::string name,
|
2023-06-22 04:56:43 -07:00
|
|
|
std::vector<std::pair<KernelCall, std::string>>
|
2023-05-24 12:09:57 -07:00
|
|
|
calls_and_descriptions,
|
|
|
|
std::vector<std::tuple<size_t, size_t, size_t>>
|
|
|
|
input_output_aliases) {
|
2023-06-22 04:56:43 -07:00
|
|
|
std::vector<AutotunedKernelCall::Config> configs;
|
2023-05-24 12:09:57 -07:00
|
|
|
configs.reserve(calls_and_descriptions.size());
|
|
|
|
for (auto& [kernel_call, desc] : calls_and_descriptions) {
|
|
|
|
configs.push_back({std::move(kernel_call), std::move(desc)});
|
|
|
|
}
|
2023-06-22 04:56:43 -07:00
|
|
|
return std::make_unique<AutotunedKernelCall>(
|
2023-05-24 12:09:57 -07:00
|
|
|
std::move(name), std::move(configs),
|
|
|
|
std::move(input_output_aliases));
|
|
|
|
}))
|
2023-08-16 02:53:05 -07:00
|
|
|
.def("to_proto", [](const AutotunedKernelCall& kernel_call,
|
|
|
|
std::string name, std::string metadata) {
|
|
|
|
jax_triton::TritonAnyKernelCall proto;
|
|
|
|
*proto.mutable_autotuned_kernel_call() = kernel_call.ToProto();
|
|
|
|
proto.set_name(std::move(name));
|
|
|
|
proto.set_metadata(std::move(metadata));
|
|
|
|
return py::bytes(proto.SerializeAsString());
|
|
|
|
});
|
2023-05-24 12:09:57 -07:00
|
|
|
|
2023-06-21 10:37:15 -07:00
|
|
|
m.def("get_custom_call",
|
2023-07-03 06:51:45 -07:00
|
|
|
[] { return EncapsulateFunction(&TritonKernelCall); });
|
2023-05-24 12:09:57 -07:00
|
|
|
|
|
|
|
m.def("get_compute_capability", [](int device) -> absl::StatusOr<int> {
|
|
|
|
int major, minor;
|
|
|
|
CUDA_RETURN_IF_ERROR(cuInit(device));
|
|
|
|
CUDA_RETURN_IF_ERROR(cuDeviceGetAttribute(
|
|
|
|
&major, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, device));
|
|
|
|
CUDA_RETURN_IF_ERROR(cuDeviceGetAttribute(
|
|
|
|
&minor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, device));
|
|
|
|
return major * 10 + minor;
|
|
|
|
});
|
2023-06-30 03:22:48 -07:00
|
|
|
|
2023-08-16 02:53:05 -07:00
|
|
|
m.def("get_serialized_metadata",
|
|
|
|
[](absl::string_view opaque) -> absl::StatusOr<py::bytes> {
|
|
|
|
JAX_ASSIGN_OR_RETURN(std::string metadata,
|
|
|
|
GetTritonKernelCallSerializedMetadata(opaque));
|
|
|
|
return py::bytes(metadata);
|
|
|
|
});
|
2023-05-24 12:09:57 -07:00
|
|
|
}
|
|
|
|
|
2023-07-03 06:51:45 -07:00
|
|
|
} // namespace jax::JAX_GPU_NAMESPACE
|