/* Copyright 2020 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

    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.
==============================================================================*/

#ifndef JAXLIB_DLPACK_H_
#define JAXLIB_DLPACK_H_

#include <cstdint>
#include <optional>
#include <tuple>

#include "absl/status/statusor.h"
#include "include/dlpack/dlpack.h"
#include "nanobind/nanobind.h"
#include "nanobind/ndarray.h"
#include "jaxlib/nb_class_ptr.h"
#include "jaxlib/py_client.h"
#include "xla/python/ifrt/device.h"
#include "xla/xla_data.pb.h"

namespace jax {

inline constexpr std::tuple<int, int> kDlPackVersion =
    std::make_tuple(DLPACK_MAJOR_VERSION, DLPACK_MINOR_VERSION);

// If take_ownership is true, ownership of the buffer is handed to DLPack, and
// the receiver may mutate the buffer as they see fit. Otherwise PjRt retains
// ownership of the buffer and it should be immutable.
//
// stream, if set, is a GPU stream, e.g. cudaStream_t for CUDA GPUs, that should
// be synchronized to the buffer as per
// https://dmlc.github.io/dlpack/latest/python_spec.html#python-specification-for-dlpack.
absl::StatusOr<nanobind::capsule> BufferToDLPackManagedTensor(
    nanobind::handle buffer, std::optional<std::intptr_t> stream,
    std::optional<std::tuple<int64_t, int64_t>> max_version = std::nullopt,
    bool copied = false);

absl::StatusOr<nanobind::object> DLPackManagedTensorToBuffer(
    const nanobind::capsule& tensor, xla::ifrt::Device* device,
    nb_class_ptr<PyClient> client, std::optional<std::intptr_t> stream,
    std::optional<bool> copy,
    std::optional<DLDeviceType> dl_device_type = std::nullopt);

// Converts a PrimitiveType to the nanobind specific implementation of
// DLDataType.
absl::StatusOr<nanobind::dlpack::dtype> PrimitiveTypeToNbDLDataType(
    xla::PrimitiveType type);

}  // namespace jax

#endif  // JAXLIB_DLPACK_H_
