|
| 1 | +// Copyright © 2025 Apple Inc. |
| 2 | + |
| 3 | +#include "mlx/backend/webgpu/allocator.h" |
| 4 | + |
| 5 | +#include "mlx/array.h" |
| 6 | +#include "mlx/backend/webgpu/utils.h" |
| 7 | +#include "mlx/primitives.h" |
| 8 | + |
| 9 | +namespace mlx::core { |
| 10 | + |
| 11 | +namespace allocator { |
| 12 | + |
| 13 | +Allocator& allocator() { |
| 14 | + return webgpu::allocator(); |
| 15 | +} |
| 16 | + |
| 17 | +void* Buffer::raw_ptr() { |
| 18 | + return static_cast<webgpu::DoubleBuffer*>(ptr_)->cpu_data(); |
| 19 | +} |
| 20 | + |
| 21 | +} // namespace allocator |
| 22 | + |
| 23 | +namespace webgpu { |
| 24 | + |
| 25 | +DoubleBuffer::DoubleBuffer(size_t size) |
| 26 | + : size_(size), cpu_data_(std::malloc(size)) {} |
| 27 | + |
| 28 | +DoubleBuffer::DoubleBuffer(betann::Device& device, Dtype dtype, size_t size) |
| 29 | + : size_(size), |
| 30 | + gpu_data_(device.CreateBuffer( |
| 31 | + size * gpu_size_factor(dtype), |
| 32 | + betann::BufferUsage::Storage | betann::BufferUsage::CopySrc)) {} |
| 33 | + |
| 34 | +DoubleBuffer::~DoubleBuffer() { |
| 35 | + std::free(cpu_data_); |
| 36 | +} |
| 37 | + |
| 38 | +WgpuAllocator::WgpuAllocator() : device_(webgpu::device(Device::gpu)) {} |
| 39 | + |
| 40 | +Buffer WgpuAllocator::malloc(size_t size, bool allow_swap) { |
| 41 | + return Buffer(new DoubleBuffer(size)); |
| 42 | +} |
| 43 | + |
| 44 | +void WgpuAllocator::free(Buffer buffer) { |
| 45 | + delete static_cast<DoubleBuffer*>(buffer.ptr()); |
| 46 | +} |
| 47 | + |
| 48 | +size_t WgpuAllocator::size(Buffer buffer) const { |
| 49 | + return static_cast<DoubleBuffer*>(buffer.ptr())->size(); |
| 50 | +} |
| 51 | + |
| 52 | +void WgpuAllocator::ensure_cpu_data(array& arr, const void* data) { |
| 53 | + auto* dbuf = static_cast<DoubleBuffer*>(arr.buffer().ptr()); |
| 54 | + if (dbuf->cpu_data() || dbuf->size() == 0) |
| 55 | + return; |
| 56 | + void* cpu_data = std::malloc(dbuf->size()); |
| 57 | + size_t num_elements = dbuf->size() / arr.itemsize(); |
| 58 | + switch (arr.dtype()) { |
| 59 | + case int32: |
| 60 | + case uint32: |
| 61 | + case float16: |
| 62 | + case float32: |
| 63 | + std::memcpy(cpu_data, data, dbuf->size()); |
| 64 | + break; |
| 65 | + case bool_: |
| 66 | + std::transform( |
| 67 | + static_cast<const uint32_t*>(data), |
| 68 | + static_cast<const uint32_t*>(data) + num_elements, |
| 69 | + static_cast<bool*>(cpu_data), |
| 70 | + [](uint32_t e) { return static_cast<bool>(e); }); |
| 71 | + break; |
| 72 | + case uint8: |
| 73 | + std::transform( |
| 74 | + static_cast<const uint32_t*>(data), |
| 75 | + static_cast<const uint32_t*>(data) + num_elements, |
| 76 | + static_cast<uint8_t*>(cpu_data), |
| 77 | + [](uint32_t e) { return static_cast<uint8_t>(e); }); |
| 78 | + break; |
| 79 | + case uint16: |
| 80 | + std::transform( |
| 81 | + static_cast<const uint32_t*>(data), |
| 82 | + static_cast<const uint32_t*>(data) + num_elements, |
| 83 | + static_cast<uint16_t*>(cpu_data), |
| 84 | + [](uint32_t e) { return static_cast<uint16_t>(e); }); |
| 85 | + break; |
| 86 | + case int8: |
| 87 | + std::transform( |
| 88 | + static_cast<const int32_t*>(data), |
| 89 | + static_cast<const int32_t*>(data) + num_elements, |
| 90 | + static_cast<int8_t*>(cpu_data), |
| 91 | + [](int32_t e) { return static_cast<int8_t>(e); }); |
| 92 | + break; |
| 93 | + case int16: |
| 94 | + std::transform( |
| 95 | + static_cast<const int32_t*>(data), |
| 96 | + static_cast<const int32_t*>(data) + num_elements, |
| 97 | + static_cast<int16_t*>(cpu_data), |
| 98 | + [](int32_t e) { return static_cast<int16_t>(e); }); |
| 99 | + break; |
| 100 | + default: |
| 101 | + throw_unsupported_dtype_error(arr.dtype()); |
| 102 | + } |
| 103 | + dbuf->set_cpu_data(cpu_data); |
| 104 | +} |
| 105 | + |
| 106 | +void WgpuAllocator::ensure_gpu_data(array& arr) { |
| 107 | + auto* dbuf = static_cast<DoubleBuffer*>(arr.buffer().ptr()); |
| 108 | + if (dbuf->gpu_data() || dbuf->size() == 0) |
| 109 | + return; |
| 110 | + size_t num_elements = dbuf->size() / arr.itemsize(); |
| 111 | + switch (arr.dtype()) { |
| 112 | + case int32: |
| 113 | + case uint32: |
| 114 | + case float16: |
| 115 | + case float32: |
| 116 | + dbuf->set_gpu_data( |
| 117 | + device_.CreateBufferFromData(dbuf->cpu_data(), dbuf->size())); |
| 118 | + break; |
| 119 | + case bool_: |
| 120 | + dbuf->set_gpu_data(device_.CreateBufferTransformTo<uint32_t>( |
| 121 | + static_cast<bool*>(dbuf->cpu_data()), num_elements)); |
| 122 | + break; |
| 123 | + case uint8: |
| 124 | + dbuf->set_gpu_data(device_.CreateBufferTransformTo<uint32_t>( |
| 125 | + static_cast<uint8_t*>(dbuf->cpu_data()), num_elements)); |
| 126 | + break; |
| 127 | + case uint16: |
| 128 | + dbuf->set_gpu_data(device_.CreateBufferTransformTo<uint32_t>( |
| 129 | + static_cast<uint16_t*>(dbuf->cpu_data()), num_elements)); |
| 130 | + break; |
| 131 | + case int8: |
| 132 | + dbuf->set_gpu_data(device_.CreateBufferTransformTo<int32_t>( |
| 133 | + static_cast<int8_t*>(dbuf->cpu_data()), num_elements)); |
| 134 | + break; |
| 135 | + case int16: |
| 136 | + dbuf->set_gpu_data(device_.CreateBufferTransformTo<int32_t>( |
| 137 | + static_cast<int16_t*>(dbuf->cpu_data()), num_elements)); |
| 138 | + break; |
| 139 | + default: |
| 140 | + throw_unsupported_dtype_error(arr.dtype()); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +Buffer WgpuAllocator::malloc_gpu(array& arr) { |
| 145 | + return malloc_gpu(arr, arr.nbytes()); |
| 146 | +} |
| 147 | + |
| 148 | +Buffer WgpuAllocator::malloc_gpu(array& arr, size_t size) { |
| 149 | + return Buffer(new DoubleBuffer(device_, arr.dtype(), size)); |
| 150 | +} |
| 151 | + |
| 152 | +WgpuAllocator& allocator() { |
| 153 | + static WgpuAllocator allocator_; |
| 154 | + return allocator_; |
| 155 | +} |
| 156 | + |
| 157 | +betann::Device& device(mlx::core::Device) { |
| 158 | + static betann::Device device; |
| 159 | + return device; |
| 160 | +} |
| 161 | + |
| 162 | +betann::Device& device(array& arr) { |
| 163 | + return device(arr.primitive().device()); |
| 164 | +} |
| 165 | + |
| 166 | +} // namespace webgpu |
| 167 | + |
| 168 | +namespace metal { |
| 169 | + |
| 170 | +size_t get_active_memory() { |
| 171 | + return 0; |
| 172 | +} |
| 173 | +size_t get_peak_memory() { |
| 174 | + return 0; |
| 175 | +} |
| 176 | +void reset_peak_memory() {} |
| 177 | +size_t get_cache_memory() { |
| 178 | + return 0; |
| 179 | +} |
| 180 | +size_t set_memory_limit(size_t, bool) { |
| 181 | + return 0; |
| 182 | +} |
| 183 | +size_t set_cache_limit(size_t) { |
| 184 | + return 0; |
| 185 | +} |
| 186 | +size_t set_wired_limit(size_t) { |
| 187 | + return 0; |
| 188 | +} |
| 189 | + |
| 190 | +std::unordered_map<std::string, std::variant<std::string, size_t>> |
| 191 | +device_info() { |
| 192 | + throw std::runtime_error("[webgpu::device_info] Not implemented"); |
| 193 | +}; |
| 194 | + |
| 195 | +void clear_cache() {} |
| 196 | + |
| 197 | +} // namespace metal |
| 198 | + |
| 199 | +} // namespace mlx::core |
0 commit comments