Allanatrix commited on
Commit
df4a67f
·
verified ·
1 Parent(s): d8a4422

Publish tokenizer_kernel kernel and performance card

Browse files

CUDA source, short description, performance table, and plot.

Files changed (3) hide show
  1. README.md +40 -0
  2. kernel.cu +248 -0
  3. performance.svg +8 -0
README.md ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ tags:
4
+ - cuda
5
+ - kernel
6
+ - gpu-optimization
7
+ - hpc
8
+ ---
9
+
10
+ # tokenizer_kernel
11
+
12
+ Experimental tokenizer/matrix CUDA kernel file.
13
+
14
+ This repository contains the standalone CUDA source for the `tokenizer_kernel` lane from
15
+ the PyC kernel lab. It is a source artifact for inspection and benchmarking;
16
+ it is not a precompiled binary and the result below is not a universal ranking.
17
+
18
+ ## Performance
19
+
20
+ | Kernel | GPU / architecture | Shape | Best recorded result | Evidence |
21
+ |---|---|---|---|---|
22
+ | `tokenizer_kernel` | not recorded | not recorded | Not measured in the published campaign | No published performance receipt was found for this lane. |
23
+
24
+ ![Performance plot](performance.svg)
25
+
26
+ The result is reported with the original campaign's timing and correctness
27
+ context. Compare kernels only when GPU, CUDA version, matrix shape, warmup,
28
+ repeats, and reference/correctness mode match.
29
+
30
+ ## Source
31
+
32
+ - `kernel.cu` — copied from `kernels/prototypes/experimental/tokenizer_matmul/kernel.cu`.
33
+ - Original lane tags: `cuda, tokenizer, experimental`.
34
+
35
+ ## Build/run contract
36
+
37
+ ```text
38
+ {nvcc} -O2 -c {source} -o {build_dir}/{name}.o
39
+ (compile-only)
40
+ ```
kernel.cu ADDED
@@ -0,0 +1,248 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <cuda_runtime.h>
2
+ #include <stdio.h>
3
+ #include <ctype.h>
4
+
5
+ #define MAX_TOKENS 1024
6
+
7
+ // Enhanced token types
8
+ typedef enum {
9
+ TOKEN_IDENTIFIER = 0,
10
+ TOKEN_NUMBER = 1,
11
+ TOKEN_OPERATOR = 2,
12
+ TOKEN_KEYWORD = 3,
13
+ TOKEN_STRING = 4,
14
+ TOKEN_COMMENT = 5,
15
+ TOKEN_PREPROCESSOR = 6,
16
+ TOKEN_PUNCTUATION = 7
17
+ } TokenType;
18
+
19
+ // Add token metadata
20
+ typedef struct {
21
+ TokenType type;
22
+ int start_pos;
23
+ int end_pos;
24
+ int length;
25
+ int line;
26
+ int column;
27
+ char lexeme[256];
28
+ unsigned int hash;
29
+ } EnhancedTokenGPU;
30
+
31
+ typedef struct {
32
+ int type; // 0: identifier, 1: number, 2: operator
33
+ int start_pos;
34
+ int end_pos;
35
+ int length;
36
+ } TokenGPU;
37
+
38
+ // Add shared memory optimization
39
+ __shared__ char shared_input[1024];
40
+ __shared__ int shared_token_count;
41
+
42
+ // Enhanced tokenization kernel with better pattern matching
43
+ __global__ void enhanced_tokenize_kernel(const char* input, size_t input_length,
44
+ EnhancedTokenGPU* tokens, int* token_count,
45
+ bool enable_comments, bool enable_preprocessing) {
46
+ int idx = blockIdx.x * blockDim.x + threadIdx.x;
47
+ if (idx >= input_length) return;
48
+
49
+ // Load chunk into shared memory
50
+ int local_idx = threadIdx.x;
51
+ if (local_idx < 1024 && idx < input_length) {
52
+ shared_input[local_idx] = input[idx];
53
+ }
54
+ __syncthreads();
55
+
56
+ // Enhanced token detection with more patterns
57
+ if (idx > 0 && (isalnum(shared_input[local_idx-1]) && isalnum(shared_input[local_idx]))) return;
58
+
59
+ int tcount = atomicAdd(token_count, 0);
60
+ if (tcount >= MAX_TOKENS) return;
61
+
62
+ EnhancedTokenGPU token;
63
+ token.start_pos = idx;
64
+ token.hash = 0;
65
+
66
+ // Calculate line and column
67
+ int line = 1, column = 1;
68
+ for (int i = 0; i < idx; i++) {
69
+ if (input[i] == '\n') {
70
+ line++;
71
+ column = 1;
72
+ } else {
73
+ column++;
74
+ }
75
+ }
76
+ token.line = line;
77
+ token.column = column;
78
+
79
+ // Enhanced pattern matching
80
+ if (isalpha(shared_input[local_idx]) || shared_input[local_idx] == '_') {
81
+ // Handle identifiers and keywords
82
+ int end = local_idx;
83
+ while (end < 1024 && (isalnum(shared_input[end]) || shared_input[end] == '_')) {
84
+ token.hash = token.hash * 31 + shared_input[end];
85
+ end++;
86
+ }
87
+ token.type = TOKEN_IDENTIFIER;
88
+ token.end_pos = idx + (end - local_idx) - 1;
89
+ token.length = end - local_idx;
90
+ }
91
+ // ... Add more token pattern matching ...
92
+
93
+ // Store token if valid
94
+ if (token.length > 0) {
95
+ int new_count = atomicAdd(token_count, 1);
96
+ if (new_count < MAX_TOKENS) {
97
+ tokens[new_count] = token;
98
+ }
99
+ }
100
+ }
101
+
102
+ __global__ void tokenize_kernel(const char* input, size_t input_length, TokenGPU* tokens, int* token_count) {
103
+ int idx = blockIdx.x * blockDim.x + threadIdx.x;
104
+ if (idx >= input_length) return;
105
+
106
+ // Skip if not at token boundary
107
+ if (idx > 0 && (isalnum(input[idx-1]) && isalnum(input[idx]))) return;
108
+
109
+ int tcount = *token_count;
110
+ if (tcount >= MAX_TOKENS) return;
111
+
112
+ if (isalpha(input[idx])) {
113
+ int end = idx;
114
+ while (end < input_length && isalnum(input[end])) end++;
115
+ int new_count = atomicAdd(token_count, 1);
116
+ if (new_count < MAX_TOKENS) {
117
+ tokens[new_count].type = 0;
118
+ tokens[new_count].start_pos = idx;
119
+ tokens[new_count].end_pos = end - 1;
120
+ tokens[new_count].length = end - idx;
121
+ }
122
+ } else if (isdigit(input[idx])) {
123
+ int end = idx;
124
+ while (end < input_length && isdigit(input[end])) end++;
125
+ int new_count = atomicAdd(token_count, 1);
126
+ if (new_count < MAX_TOKENS) {
127
+ tokens[new_count].type = 1;
128
+ tokens[new_count].start_pos = idx;
129
+ tokens[new_count].end_pos = end - 1;
130
+ tokens[new_count].length = end - idx;
131
+ }
132
+ } else if (input[idx] == '+' || input[idx] == '-' || input[idx] == '*' || input[idx] == '/') {
133
+ int new_count = atomicAdd(token_count, 1);
134
+ if (new_count < MAX_TOKENS) {
135
+ tokens[new_count].type = 2;
136
+ tokens[new_count].start_pos = idx;
137
+ tokens[new_count].end_pos = idx;
138
+ tokens[new_count].length = 1;
139
+ }
140
+ }
141
+ }
142
+
143
+ __global__ void matrix_mult_kernel(float* a, float* b, float* c, int m, int n, int k) {
144
+ int row = blockIdx.y * blockDim.y + threadIdx.y;
145
+ int col = blockIdx.x * blockDim.x + threadIdx.x;
146
+ if (row < m && col < n) {
147
+ float sum = 0.0f;
148
+ for (int i = 0; i < k; i++) {
149
+ sum += a[row * k + i] * b[i * n + col];
150
+ }
151
+ c[row * n + col] = sum;
152
+ }
153
+ }
154
+
155
+ // Add parallel matrix operations
156
+ __global__ void enhanced_matrix_mult_kernel(float* a, float* b, float* c,
157
+ int m, int n, int k,
158
+ bool use_shared_memory) {
159
+ // ... existing matrix multiplication code ...
160
+
161
+ // Add shared memory optimization
162
+ __shared__ float shared_a[16][16];
163
+ __shared__ float shared_b[16][16];
164
+
165
+ // ... implement block matrix multiplication ...
166
+ }
167
+
168
+ // Add new CUDA utilities
169
+ void initialize_cuda_context(void) {
170
+ cudaFree(0); // Force context initialization
171
+ }
172
+
173
+ void optimize_kernel_launch(dim3* blocks, dim3* threads, size_t shared_memory_size) {
174
+ int device;
175
+ cudaGetDevice(&device);
176
+ cudaDeviceProp props;
177
+ cudaGetDeviceProperties(&props, device);
178
+
179
+ // Optimize launch configuration based on device properties
180
+ // ... implementation ...
181
+ }
182
+
183
+ void cuda_tokenize(const char* input, TokenGPU* tokens, int* token_count) {
184
+ size_t input_length = strlen(input);
185
+ char* d_input;
186
+ TokenGPU* d_tokens;
187
+ int* d_token_count;
188
+
189
+ cudaMalloc(&d_input, input_length + 1);
190
+ cudaMalloc(&d_tokens, MAX_TOKENS * sizeof(TokenGPU));
191
+ cudaMalloc(&d_token_count, sizeof(int));
192
+ cudaMemcpy(d_input, input, input_length + 1, cudaMemcpyHostToDevice);
193
+ cudaMemset(d_token_count, 0, sizeof(int));
194
+
195
+ int threads = 256;
196
+ int blocks = (input_length + threads - 1) / threads;
197
+ tokenize_kernel<<<blocks, threads>>>(d_input, input_length, d_tokens, d_token_count);
198
+
199
+ cudaMemcpy(token_count, d_token_count, sizeof(int), cudaMemcpyDeviceToHost);
200
+ cudaMemcpy(tokens, d_tokens, *token_count * sizeof(TokenGPU), cudaMemcpyDeviceToHost);
201
+
202
+ cudaFree(d_input);
203
+ cudaFree(d_tokens);
204
+ cudaFree(d_token_count);
205
+ }
206
+
207
+ void cuda_matrix_mult(float* a, float* b, float* c, int m, int n, int k) {
208
+ float *d_a, *d_b, *d_c;
209
+ cudaMalloc(&d_a, m * k * sizeof(float));
210
+ cudaMalloc(&d_b, k * n * sizeof(float));
211
+ cudaMalloc(&d_c, m * n * sizeof(float));
212
+ cudaMemcpy(d_a, a, m * k * sizeof(float), cudaMemcpyHostToDevice);
213
+ cudaMemcpy(d_b, b, k * n * sizeof(float), cudaMemcpyHostToDevice);
214
+
215
+ dim3 threads(16, 16);
216
+ dim3 blocks((n + threads.x - 1) / threads.x, (m + threads.y - 1) / threads.y);
217
+ matrix_mult_kernel<<<blocks, threads>>>(d_a, d_b, d_c, m, n, k);
218
+
219
+ cudaMemcpy(c, d_c, m * n * sizeof(float), cudaMemcpyDeviceToHost);
220
+ cudaFree(d_a); cudaFree(d_b); cudaFree(d_c);
221
+ }
222
+ void print_tokens(TokenGPU* tokens, int token_count) {
223
+ for (int i = 0; i < token_count; i++) {
224
+ printf("Token %d: Type %d, Start %d, End %d, Length %d\n",
225
+ i, tokens[i].type, tokens[i].start_pos, tokens[i].end_pos, tokens[i].length);
226
+ }
227
+ }
228
+ int main() {
229
+ const char* input = "int a = 5 + 3;";
230
+ TokenGPU tokens[MAX_TOKENS];
231
+ int token_count;
232
+
233
+ cuda_tokenize(input, tokens, &token_count);
234
+ print_tokens(tokens, token_count);
235
+
236
+ float a[6] = {1, 2, 3, 4, 5, 6};
237
+ float b[6] = {7, 8, 9, 10, 11, 12};
238
+ float c[4] = {0};
239
+
240
+ cuda_matrix_mult(a, b, c, 2, 3, 2);
241
+ for (int i = 0; i < 4; i++) {
242
+ printf("%f ", c[i]);
243
+ }
244
+ printf("\n");
245
+
246
+ return 0;
247
+ }
248
+ // Compile with nvcc -o kernel kernel.cu
performance.svg ADDED