include/brotensor/kernel_interop.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | // Zero-copy interop between brotensor::Tensor and external compute kernels | ||
| 3 | // (e.g. brokernel CPU/SPIR-V kernels): | ||
| 4 | // - tensor buffer views (host pointer or Vulkan BDA device address) | ||
| 5 | // - byte-level H2D / D2H transfers preserving existing Tensor storage | ||
| 6 | // - push-constant SPIR-V compute dispatch on brotensor's Vulkan stream | ||
| 7 | |||
| 8 | #include "tensor.h" | ||
| 9 | |||
| 10 | #include <cstddef> | ||
| 11 | #include <cstdint> | ||
| 12 | #include <string> | ||
| 13 | #include <vector> | ||
| 14 | |||
| 15 | namespace brotensor::interop { | ||
| 16 | |||
| 17 | 1 | struct TensorBufferView { | |
| 18 | 1 | void* host_ptr = nullptr; | |
| 19 | 1 | std::uint64_t device_address = 0; | |
| 20 | 1 | std::size_t bytes = 0; | |
| 21 | 1 | std::size_t numel = 0; | |
| 22 | 1 | Dtype dtype = Dtype::FP32; | |
| 23 | 1 | Device device = Device::CPU; | |
| 24 | |||
| 25 | 1 | bool is_gpu() const noexcept { return device.is_gpu(); } | |
| 26 | bool is_vulkan() const noexcept { return device.is_vulkan(); } | ||
| 27 | bool is_cuda() const noexcept { return device.is_cuda(); } | ||
| 28 | bool is_metal() const noexcept { return device.is_metal(); } | ||
| 29 | }; | ||
| 30 | |||
| 31 | /// Returns true when `dev` supports IEEE 754 float64 arithmetic in compute kernels | ||
| 32 | /// (CPU: true, CUDA: true, Metal: false, Vulkan: physical device `shaderFloat64`). | ||
| 33 | bool device_has_float64(Device dev); | ||
| 34 | |||
| 35 | /// Returns a zero-copy buffer view of `t` on its owning device. | ||
| 36 | /// For CPU tensors, `host_ptr` is `t.data`; for Vulkan tensors, `device_address` | ||
| 37 | /// is the buffer device address (`VkDeviceAddress`) carried in `t.data`. | ||
| 38 | TensorBufferView view(const Tensor& t); | ||
| 39 | |||
| 40 | /// Copies `nbytes` from host memory `src_host` into `dst` in-place (preserving | ||
| 41 | /// `dst.data` allocation/device address). Requires `dst.data != nullptr` and `nbytes <= dst.bytes()`. | ||
| 42 | bool copy_bytes_to_tensor(const void* src_host, std::size_t nbytes, Tensor& dst, std::string* err = nullptr); | ||
| 43 | |||
| 44 | /// Copies `nbytes` from `src` (CPU or GPU, synced) into host memory `dst_host`. | ||
| 45 | bool copy_bytes_from_tensor(const Tensor& src, void* dst_host, std::size_t nbytes, std::string* err = nullptr); | ||
| 46 | |||
| 47 | struct SpirvDispatchDesc { | ||
| 48 | const std::uint32_t* words = nullptr; | ||
| 49 | std::size_t word_count = 0; | ||
| 50 | const char* entry_name = "main"; | ||
| 51 | std::uint32_t local_size[3] = {64, 1, 1}; | ||
| 52 | std::uint32_t groups[3] = {1, 1, 1}; | ||
| 53 | const void* push_data = nullptr; | ||
| 54 | std::uint32_t push_bytes = 0; | ||
| 55 | bool local_size_spec_constants = true; | ||
| 56 | }; | ||
| 57 | |||
| 58 | /// Returns true when `dev` is a Vulkan device ready for `dispatch_spirv`. | ||
| 59 | bool vulkan_spirv_dispatch_available(Device dev = Device::vulkan(0)); | ||
| 60 | |||
| 61 | /// Dispatches an arbitrary BDA push-constant SPIR-V kernel directly on `dev`'s | ||
| 62 | /// brotensor Vulkan stream (ordered with preceding/subsequent brotensor ops). | ||
| 63 | bool dispatch_spirv(Device dev, const SpirvDispatchDesc& desc, std::string* err = nullptr); | ||
| 64 | |||
| 65 | struct PtxDispatchDesc { | ||
| 66 | const char* ptx = nullptr; | ||
| 67 | std::size_t ptx_bytes = 0; | ||
| 68 | const char* entry_name = nullptr; | ||
| 69 | std::uint32_t local_size[3] = {64, 1, 1}; | ||
| 70 | std::uint32_t groups[3] = {1, 1, 1}; | ||
| 71 | void** kernel_params = nullptr; | ||
| 72 | std::uint32_t shared_bytes = 0; | ||
| 73 | }; | ||
| 74 | |||
| 75 | /// Returns true when `dev` is a CUDA device ready for zero-copy `dispatch_ptx`. | ||
| 76 | bool cuda_ptx_dispatch_available(Device dev = Device::cuda(0)); | ||
| 77 | |||
| 78 | /// JIT-compiles (cached) and launches a PTX kernel directly on `dev`'s | ||
| 79 | /// brotensor CUDA stream (ordered with preceding/subsequent brotensor ops). | ||
| 80 | bool dispatch_ptx(Device dev, const PtxDispatchDesc& desc, std::string* err = nullptr); | ||
| 81 | |||
| 82 | struct MslDispatchDesc { | ||
| 83 | const char* msl = nullptr; | ||
| 84 | std::size_t msl_bytes = 0; | ||
| 85 | const char* entry_name = nullptr; | ||
| 86 | std::uint32_t local_size[3] = {64, 1, 1}; | ||
| 87 | std::uint32_t groups[3] = {1, 1, 1}; | ||
| 88 | const void* const* buffer_ptrs = nullptr; | ||
| 89 | std::uint32_t buffer_count = 0; | ||
| 90 | const void* scalar_data = nullptr; | ||
| 91 | std::uint32_t scalar_bytes = 0; | ||
| 92 | /// Buffer binding index for `scalar_data`. When 0, defaults to `buffer_count`. | ||
| 93 | std::uint32_t scalar_buffer_index = 0; | ||
| 94 | }; | ||
| 95 | |||
| 96 | /// Returns true when `dev` is a Metal device ready for zero-copy `dispatch_msl`. | ||
| 97 | bool metal_msl_dispatch_available(Device dev = Device::Metal); | ||
| 98 | |||
| 99 | /// Compiles (cached) and dispatches an MSL compute kernel directly on `dev`'s | ||
| 100 | /// brotensor Metal command queue (ordered with preceding/subsequent brotensor ops). | ||
| 101 | bool dispatch_msl(Device dev, const MslDispatchDesc& desc, std::string* err = nullptr); | ||
| 102 | |||
| 103 | /// Stores a raw binary artifact under `(kind, signature)` in brotensor's | ||
| 104 | /// persistent on-disk kernel cache (returns false when the cache is disabled). | ||
| 105 | bool store_cached_blob(const char* kind, const char* signature, const void* data, std::size_t bytes); | ||
| 106 | |||
| 107 | /// Loads a raw binary artifact under `(kind, signature)` from brotensor's | ||
| 108 | /// persistent on-disk kernel cache (returns false on miss or when disabled). | ||
| 109 | bool load_cached_blob(const char* kind, const char* signature, std::vector<std::uint8_t>& out); | ||
| 110 | |||
| 111 | namespace detail { | ||
| 112 | using VulkanDispatchHook = bool (*)(int device_index, const SpirvDispatchDesc& desc, std::string* err); | ||
| 113 | using VulkanAvailableHook = bool (*)(int device_index); | ||
| 114 | using VulkanFloat64Hook = bool (*)(int device_index); | ||
| 115 | void register_vulkan_interop_hooks(VulkanAvailableHook avail_fn, VulkanDispatchHook dispatch_fn); | ||
| 116 | void register_vulkan_float64_hook(VulkanFloat64Hook f64_fn); | ||
| 117 | |||
| 118 | using CudaDispatchHook = bool (*)(int device_index, const PtxDispatchDesc& desc, std::string* err); | ||
| 119 | using CudaAvailableHook = bool (*)(int device_index); | ||
| 120 | void register_cuda_interop_hooks(CudaAvailableHook avail_fn, CudaDispatchHook dispatch_fn); | ||
| 121 | |||
| 122 | using MetalDispatchHook = bool (*)(int device_index, const MslDispatchDesc& desc, std::string* err); | ||
| 123 | using MetalAvailableHook = bool (*)(int device_index); | ||
| 124 | void register_metal_interop_hooks(MetalAvailableHook avail_fn, MetalDispatchHook dispatch_fn); | ||
| 125 | } // namespace detail | ||
| 126 | |||
| 127 | } // namespace brotensor::interop | ||
| 128 |