ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-cuda / batch-get.cu
| 1 | #include "batch-get.cuh" |
| 2 | #include "common.cuh" |
| 3 | #include "ggml-cpp.h" |
| 4 | |
| 5 | #include <climits> |
| 6 | |
| 7 | #if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA) |
| 8 | struct ggml_cuda_batch_get_descriptor { |
| 9 | const unsigned char * src; |
| 10 | unsigned char * dst; |
| 11 | size_t size; |
| 12 | }; |
| 13 | |
| 14 | static __global__ void ggml_cuda_batch_get_kernel(const ggml_cuda_batch_get_descriptor * copies, size_t count) { |
| 15 | // Blocks along y cooperate on each descriptor. |
| 16 | const size_t index = blockIdx.x; |
| 17 | if (index >= count) { |
| 18 | return; |
| 19 | } |
| 20 | const size_t first = (size_t) blockIdx.y * blockDim.x + threadIdx.x; |
| 21 | const size_t stride = (size_t) gridDim.y * blockDim.x; |
| 22 | const auto copy = copies[index]; |
| 23 | // Use 16-, 8-, 4-, or 2-byte copies when both pointers meet that alignment; threads copy separate chunks. |
| 24 | const uintptr_t alignment = (uintptr_t) copy.src | (uintptr_t) copy.dst; |
| 25 | size_t tail = 0; |
| 26 | if (alignment % 16 == 0) { |
| 27 | for (size_t i = first; i < copy.size / 16; i += stride) { |
| 28 | ggml_cuda_memcpy_1<16>(copy.dst + 16*i, copy.src + 16*i); |
| 29 | } |
| 30 | tail = copy.size / 16 * 16; |
| 31 | } else if (alignment % 8 == 0) { |
| 32 | for (size_t i = first; i < copy.size / 8; i += stride) { |
| 33 | ggml_cuda_memcpy_1<8>(copy.dst + 8*i, copy.src + 8*i); |
| 34 | } |
| 35 | tail = copy.size / 8 * 8; |
| 36 | } else if (alignment % 4 == 0) { |
| 37 | for (size_t i = first; i < copy.size / 4; i += stride) { |
| 38 | ggml_cuda_memcpy_1<4>(copy.dst + 4*i, copy.src + 4*i); |
| 39 | } |
| 40 | tail = copy.size / 4 * 4; |
| 41 | } else if (alignment % 2 == 0) { |
| 42 | for (size_t i = first; i < copy.size / 2; i += stride) { |
| 43 | ggml_cuda_memcpy_1<2>(copy.dst + 2*i, copy.src + 2*i); |
| 44 | } |
| 45 | tail = copy.size / 2 * 2; |
| 46 | } |
| 47 | // Copy trailing bytes, or the entire range when no wider alignment is available. |
| 48 | for (size_t i = tail + first; i < copy.size; i += stride) { |
| 49 | ggml_cuda_memcpy_1<1>(copy.dst + i, copy.src + i); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | struct ggml_cuda_batch_get_mapping { |
| 54 | void * host = nullptr; |
| 55 | unsigned char * device = nullptr; |
| 56 | |
| 57 | ggml_cuda_batch_get_mapping(void * host, size_t size) { |
| 58 | const cudaError_t err = cudaHostRegister(host, size, cudaHostRegisterMapped | cudaHostRegisterPortable); |
| 59 | if (err != cudaSuccess) { |
| 60 | (void) cudaGetLastError(); |
| 61 | GGML_LOG_DEBUG("%s: failed to register %.2f MiB of pinned memory: %s\n", __func__, |
| 62 | size / 1024.0 / 1024.0, cudaGetErrorString(err)); |
| 63 | return; |
| 64 | } |
| 65 | this->host = host; |
| 66 | if (cudaHostGetDevicePointer((void **) &device, host, 0) != cudaSuccess) { |
| 67 | (void) cudaGetLastError(); |
| 68 | device = nullptr; |
| 69 | return; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | ggml_cuda_batch_get_mapping(const ggml_cuda_batch_get_mapping &) = delete; |
| 74 | ggml_cuda_batch_get_mapping & operator=(const ggml_cuda_batch_get_mapping &) = delete; |
| 75 | |
| 76 | ~ggml_cuda_batch_get_mapping() { |
| 77 | if (host) { |
| 78 | // Release only the registration acquired by this object. |
| 79 | CUDA_CHECK(cudaHostUnregister(host)); |
| 80 | } |
| 81 | } |
| 82 | }; |
| 83 | #endif |
| 84 | |
| 85 | bool ggml_cuda_get_tensor_n(int device, |
| 86 | const ggml_tensor * const * tensors, void * const * data, const size_t * offsets, |
| 87 | const size_t * sizes, size_t n_tensors) { |
| 88 | #if defined(GGML_USE_HIP) || defined(GGML_USE_MUSA) |
| 89 | GGML_UNUSED(device); |
| 90 | GGML_UNUSED(tensors); |
| 91 | GGML_UNUSED(data); |
| 92 | GGML_UNUSED(offsets); |
| 93 | GGML_UNUSED(sizes); |
| 94 | GGML_UNUSED(n_tensors); |
| 95 | return false; |
| 96 | #else |
| 97 | // Provisional dispatch values based on experiments. |
| 98 | constexpr size_t min_batch_reads = 128; |
| 99 | constexpr size_t max_gather_read_bytes = 128 * 1024; |
| 100 | // Collect non-empty CPU destination ranges. |
| 101 | std::vector<std::pair<uintptr_t, uintptr_t>> ranges; |
| 102 | ranges.reserve(n_tensors); |
| 103 | size_t payload_bytes = 0; |
| 104 | // Number of non-empty reads whose size is at most max_gather_read_bytes. |
| 105 | size_t n_reads_small = 0; |
| 106 | for (size_t i = 0; i < n_tensors; ++i) { |
| 107 | if (sizes[i] == 0) { |
| 108 | continue; |
| 109 | } |
| 110 | const uintptr_t address = (uintptr_t) data[i]; |
| 111 | GGML_ASSERT(address <= UINTPTR_MAX - sizes[i]); |
| 112 | GGML_ASSERT(payload_bytes <= SIZE_MAX - sizes[i]); |
| 113 | payload_bytes += sizes[i]; |
| 114 | n_reads_small += sizes[i] <= max_gather_read_bytes; |
| 115 | ranges.emplace_back(address, address + sizes[i]); |
| 116 | } |
| 117 | if (ranges.empty()) { |
| 118 | return true; |
| 119 | } |
| 120 | // Total number of non-empty reads. |
| 121 | const size_t n_reads = ranges.size(); |
| 122 | if (n_reads > SIZE_MAX / sizeof(ggml_cuda_batch_get_descriptor) || n_reads > INT_MAX) { |
| 123 | return false; |
| 124 | } |
| 125 | if (n_reads < 2 || (n_reads < min_batch_reads && n_reads_small == n_reads)) { |
| 126 | return false; |
| 127 | } |
| 128 | std::sort(ranges.begin(), ranges.end()); |
| 129 | for (size_t i = 1; i < ranges.size(); ++i) { |
| 130 | // Preserve the ordered copy path when CPU destinations overlap. |
| 131 | if (ranges[i].first < ranges[i - 1].second) { |
| 132 | return false; |
| 133 | } |
| 134 | } |
| 135 | // Find the CPU span to register, including gaps between destinations. |
| 136 | const uintptr_t begin = ranges.front().first; |
| 137 | const uintptr_t end = ranges.back().second; |
| 138 | if (end - begin - payload_bytes > payload_bytes) { |
| 139 | return false; |
| 140 | } |
| 141 | // Register and map the existing CPU destination so the kernel can write to it directly. |
| 142 | ggml_cuda_batch_get_mapping mapping((void *) begin, end - begin); |
| 143 | if (!mapping.host) { |
| 144 | return false; |
| 145 | } |
| 146 | // Reads selected for gather; zero means all reads use DMA. |
| 147 | size_t n_reads_gather = mapping.device && n_reads_small >= min_batch_reads ? n_reads_small : 0; |
| 148 | // Keep descriptor storage alive until the final stream synchronization. |
| 149 | constexpr size_t max_batch_descriptors = 128 * 1024; |
| 150 | const size_t bytes = std::min(n_reads_gather, max_batch_descriptors) * sizeof(ggml_cuda_batch_get_descriptor); |
| 151 | ggml_backend_buffer_ptr descriptors(n_reads_gather ? ggml_backend_buft_alloc_buffer(ggml_backend_cuda_buffer_type(device), bytes) : nullptr); |
| 152 | if (!descriptors) { |
| 153 | n_reads_gather = 0; |
| 154 | } |
| 155 | const auto host_type = ggml_backend_cuda_host_buffer_type(); |
| 156 | ggml_backend_buffer_ptr host_descriptors(n_reads_gather ? ggml_backend_buft_alloc_buffer(host_type, bytes) : nullptr); |
| 157 | if (!host_descriptors || ggml_backend_buffer_get_type(host_descriptors.get()) != host_type) { |
| 158 | n_reads_gather = 0; |
| 159 | } |
| 160 | if (n_reads_gather) { |
| 161 | auto * copies = static_cast<ggml_cuda_batch_get_descriptor *>(ggml_backend_buffer_get_base(host_descriptors.get())); |
| 162 | auto * device_copies = static_cast<ggml_cuda_batch_get_descriptor *>(ggml_backend_buffer_get_base(descriptors.get())); |
| 163 | const int threads = 256; |
| 164 | const size_t nsm = ggml_cuda_info().devices[device].nsm; |
| 165 | size_t i = 0; |
| 166 | for (size_t first = 0; first < n_reads_gather; first += max_batch_descriptors) { |
| 167 | const size_t n_batch = std::min(n_reads_gather - first, max_batch_descriptors); |
| 168 | size_t count = 0; |
| 169 | for (; i < n_tensors && count < n_batch; ++i) { |
| 170 | if (sizes[i] == 0 || sizes[i] > max_gather_read_bytes) { |
| 171 | continue; |
| 172 | } |
| 173 | copies[count++] = {(const unsigned char *) tensors[i]->data + offsets[i], |
| 174 | mapping.device + ((uintptr_t) data[i] - begin), sizes[i]}; |
| 175 | } |
| 176 | const size_t blocks_per_copy = std::min<size_t>(UINT16_MAX, std::max<size_t>(1, (nsm + n_batch - 1) / n_batch)); |
| 177 | const dim3 blocks((unsigned int) n_batch, (unsigned int) blocks_per_copy); |
| 178 | // Stream ordering keeps each kernel ahead of the next descriptor overwrite. |
| 179 | CUDA_CHECK(cudaMemcpyAsync(device_copies, copies, n_batch * sizeof(copies[0]), cudaMemcpyHostToDevice, cudaStreamPerThread)); |
| 180 | ggml_cuda_batch_get_kernel<<<blocks, threads, 0, cudaStreamPerThread>>>(device_copies, n_batch); |
| 181 | CUDA_CHECK(cudaGetLastError()); |
| 182 | if (first + n_batch < n_reads_gather) { |
| 183 | // Wait before refilling the pinned host descriptor buffer. |
| 184 | CUDA_CHECK(cudaStreamSynchronize(cudaStreamPerThread)); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | // Keep the destination pinned for DMA too; overlapping destinations already declined above. |
| 189 | for (size_t i = 0; i < n_tensors; ++i) { |
| 190 | if (sizes[i] && (!n_reads_gather || sizes[i] > max_gather_read_bytes)) { |
| 191 | CUDA_CHECK(cudaMemcpyAsync(data[i], (const char *) tensors[i]->data + offsets[i], sizes[i], cudaMemcpyDeviceToHost, cudaStreamPerThread)); |
| 192 | } |
| 193 | } |
| 194 | // Wait for GPU writes before releasing the CPU mapping and descriptor buffers. |
| 195 | CUDA_CHECK(cudaStreamSynchronize(cudaStreamPerThread)); |
| 196 | return true; |
| 197 | #endif |
| 198 | } |