interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-cuda / mmid.cu

179 lines · 9016 bytes Raw Blame History
1#include "common.cuh"
2#include "mmid.cuh"
3
4// To reduce shared memory use, store "it" and "iex_used" with 22/10 bits each.
5struct mm_ids_helper_store {
6 uint32_t data;
7
8 __device__ mm_ids_helper_store(const uint32_t it, const uint32_t iex_used) {
9 data = (it & 0x003FFFFF) | (iex_used << 22);
10 }
11
12 __device__ uint32_t it() const {
13 return data & 0x003FFFFF;
14 }
15
16 __device__ uint32_t iex_used() const {
17 return data >> 22;
18 }
19};
20static_assert(sizeof(mm_ids_helper_store) == 4, "unexpected size for mm_ids_helper_store");
21
22// the generic path passes 0, which needs no padding since it never groups lanes by token
23template <int n> struct mm_ids_pow2 { static constexpr int value = 2*mm_ids_pow2<(n + 1)/2>::value; };
24template <> struct mm_ids_pow2<1> { static constexpr int value = 1; };
25template <> struct mm_ids_pow2<0> { static constexpr int value = 1; };
26
27// Helper function for mul_mat_id, converts ids to a more convenient format.
28// ids_src1 describes how to permute the flattened column indices of src1 in order to get a compact src1 tensor sorted by expert.
29// ids_dst describes the same mapping but for the dst tensor.
30// The upper and lower bounds for the ith expert in the compact src1 tensor are stored in expert_bounds[i:i+1].
31template <int n_expert_used_template>
32__launch_bounds__(ggml_cuda_get_physical_warp_size(), 1)
33static __global__ void mm_ids_helper(
34 const int32_t * __restrict__ ids, int32_t * __restrict__ ids_src1, int32_t * __restrict__ ids_dst, int32_t * __restrict__ expert_bounds,
35 const int n_tokens, const int n_expert_used_var, const int nchannels_y, const int si1, const int sis1, const bool write_inverse) {
36 constexpr int warp_size = ggml_cuda_get_physical_warp_size();
37 const int n_expert_used = n_expert_used_template == 0 ? n_expert_used_var : n_expert_used_template;
38 const int expert = blockIdx.x;
39
40 extern __shared__ char data_mm_ids_helper[];
41 mm_ids_helper_store * store = (mm_ids_helper_store *) data_mm_ids_helper;
42
43 int nex_prev = 0; // Number of columns for experts with a lower index.
44 int it_compact = 0; // Running index for the compact slice of this expert.
45
46 if constexpr (n_expert_used_template == 0) {
47 // Generic implementation:
48 for (int it = 0; it < n_tokens; ++it) {
49 int iex_used = -1; // The index at which the expert is used, if any.
50 for (int iex = threadIdx.x; iex < n_expert_used; iex += warp_size) {
51 const int expert_used = ids[it*si1 + iex];
52 nex_prev += expert_used < expert;
53 if (expert_used == expert) {
54 iex_used = iex;
55 }
56 }
57
58 if (iex_used != -1) {
59 store[it_compact] = mm_ids_helper_store(it, iex_used);
60 }
61
62 if (warp_reduce_any<warp_size>(iex_used != -1)) {
63 it_compact++;
64 }
65 }
66 } else {
67 // Implementation optimized for specific numbers of experts used:
68 // a warp holds a whole number of token slots, so the slot count is padded to a power of 2
69 constexpr int neu_padded = mm_ids_pow2<n_expert_used_template>::value;
70 static_assert(neu_padded <= warp_size && warp_size % neu_padded == 0, "bad n_expert_used");
71 for (int it0 = 0; it0 < n_tokens; it0 += warp_size/neu_padded) {
72 const int it = it0 + threadIdx.x / neu_padded;
73
74 const int iex = threadIdx.x % neu_padded; // The index at which the expert is used, if any.
75 const int expert_used = (neu_padded == n_expert_used || iex < n_expert_used) && it < n_tokens ?
76 ids[it*si1 + iex] : INT_MAX;
77 const int iex_used = expert_used == expert ? iex : -1;
78 nex_prev += expert_used < expert;
79
80 // Whether the threads at this token position have used the expert:
81 const int it_compact_add_self = warp_reduce_any<neu_padded>(iex_used != -1);
82
83 // Do a scan over threads at lower token positions in warp to get the correct index for writing data:
84 int it_compact_add_lower = 0;
85#pragma unroll
86 for (int offset = neu_padded; offset < warp_size; offset += neu_padded) {
87 const int tmp = __shfl_up_sync(0xFFFFFFFF, it_compact_add_self, offset, warp_size);
88 if (threadIdx.x >= static_cast<unsigned int>(offset)) {
89 it_compact_add_lower += tmp;
90 }
91 }
92
93 if (iex_used != -1) {
94 store[it_compact + it_compact_add_lower] = mm_ids_helper_store(it, iex_used);
95 }
96
97 // The thread with the highest index in the warp always has the sum over the whole warp, use it to increment all threads:
98 it_compact += __shfl_sync(0xFFFFFFFF, it_compact_add_lower + it_compact_add_self, warp_size - 1, warp_size);
99 }
100 }
101 nex_prev = warp_reduce_sum<warp_size>(nex_prev);
102 ggml_cuda_syncwarp();
103
104 for (int itc = threadIdx.x; itc < it_compact; itc += warp_size) {
105 const mm_ids_helper_store store_it = store[itc];
106 const int it = store_it.it();
107 const int iex_used = store_it.iex_used();
108 ids_dst[nex_prev + itc] = it*n_expert_used + iex_used;
109 // ids_src1 holds the forward map, or the inverse map (token slot -> compact row) for quant dedup
110 if (write_inverse) {
111 ids_src1[it*n_expert_used + iex_used] = nex_prev + itc;
112 } else {
113 ids_src1[nex_prev + itc] = it*sis1 + iex_used % nchannels_y;
114 }
115 }
116
117 if (threadIdx.x != 0) {
118 return;
119 }
120
121 expert_bounds[expert] = nex_prev;
122
123 if (expert < static_cast<int>(gridDim.x) - 1) {
124 return;
125 }
126
127 expert_bounds[gridDim.x] = nex_prev + it_compact;
128}
129
130template <int n_expert_used_template>
131static void launch_mm_ids_helper(
132 const int32_t * __restrict__ ids, int32_t * __restrict__ ids_src1, int32_t * __restrict__ ids_dst, int32_t * __restrict__ expert_bounds,
133 const int n_experts, const int n_tokens, const int n_expert_used_var, const int nchannels_y, const int si1, const int sis1, const bool write_inverse, cudaStream_t stream) {
134 GGML_ASSERT(n_tokens < (1 << 22) && "too few bits in mm_ids_helper_store");
135 GGML_ASSERT(n_expert_used_var < (1 << 10) && "too few bits in mm_ids_helper_store");
136
137 const int id = ggml_cuda_get_device();
138 const int warp_size = ggml_cuda_info().devices[id].warp_size;
139 const size_t smpbo = ggml_cuda_info().devices[id].smpbo;
140 CUDA_SET_SHARED_MEMORY_LIMIT(mm_ids_helper<n_expert_used_template>, smpbo);
141
142 const dim3 num_blocks(n_experts, 1, 1);
143 const dim3 block_size(warp_size, 1, 1);
144 const size_t nbytes_shared = n_tokens*sizeof(mm_ids_helper_store);
145 GGML_ASSERT(nbytes_shared <= smpbo);
146 mm_ids_helper<n_expert_used_template><<<num_blocks, block_size, nbytes_shared, stream>>>
147 (ids, ids_src1, ids_dst, expert_bounds, n_tokens, n_expert_used_var, nchannels_y, si1, sis1, write_inverse);
148}
149
150void ggml_cuda_launch_mm_ids_helper(
151 const int32_t * __restrict__ ids, int32_t * __restrict__ ids_src1, int32_t * __restrict__ ids_dst, int32_t * __restrict__ expert_bounds,
152 const int n_experts, const int n_tokens, const int n_expert_used, const int nchannels_y, const int si1, const int sis1, const bool write_inverse, cudaStream_t stream) {
153 switch (n_expert_used) {
154 case 2:
155 launch_mm_ids_helper< 2>(ids, ids_src1, ids_dst, expert_bounds, n_experts, n_tokens, n_expert_used, nchannels_y, si1, sis1, write_inverse, stream);
156 break;
157 case 4:
158 launch_mm_ids_helper< 4>(ids, ids_src1, ids_dst, expert_bounds, n_experts, n_tokens, n_expert_used, nchannels_y, si1, sis1, write_inverse, stream);
159 break;
160 case 6:
161 launch_mm_ids_helper< 6>(ids, ids_src1, ids_dst, expert_bounds, n_experts, n_tokens, n_expert_used, nchannels_y, si1, sis1, write_inverse, stream);
162 break;
163 case 8:
164 launch_mm_ids_helper< 8>(ids, ids_src1, ids_dst, expert_bounds, n_experts, n_tokens, n_expert_used, nchannels_y, si1, sis1, write_inverse, stream);
165 break;
166 case 10:
167 launch_mm_ids_helper<10>(ids, ids_src1, ids_dst, expert_bounds, n_experts, n_tokens, n_expert_used, nchannels_y, si1, sis1, write_inverse, stream);
168 break;
169 case 16:
170 launch_mm_ids_helper<16>(ids, ids_src1, ids_dst, expert_bounds, n_experts, n_tokens, n_expert_used, nchannels_y, si1, sis1, write_inverse, stream);
171 break;
172 case 32:
173 launch_mm_ids_helper<32>(ids, ids_src1, ids_dst, expert_bounds, n_experts, n_tokens, n_expert_used, nchannels_y, si1, sis1, write_inverse, stream);
174 break;
175 default:
176 launch_mm_ids_helper< 0>(ids, ids_src1, ids_dst, expert_bounds, n_experts, n_tokens, n_expert_used, nchannels_y, si1, sis1, write_inverse, stream);
177 break;
178 }
179}