-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathstringzillas.c
More file actions
2068 lines (1806 loc) · 82.7 KB
/
stringzillas.c
File metadata and controls
2068 lines (1806 loc) · 82.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file stringzillas.c
* @brief Very light-weight CPython wrapper for StringZillas advanced algorithms,
* supporting bulk operations for edit distances, sequence alignment, and fingerprinting.
* @author Ash Vardanian
* @date December 15, 2024
* @copyright Copyright (c) 2024
*
* - Doesn't use PyBind11, NanoBind, Boost.Python, or any other high-level libs, only CPython API.
* - To minimize latency this implementation avoids `PyArg_ParseTupleAndKeywords` calls.
* - Uses manual argument parsing for performance on hot paths.
* - Returns & accepts NumPy arrays when available, avoiding memory-scattered Python lists.
*/
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#define NOMINMAX
#include <windows.h>
#else
#include <fcntl.h> // `O_RDNLY`
#include <sys/mman.h> // `mmap`
#include <sys/stat.h> // `stat`
#include <sys/types.h>
#endif
#ifdef _MSC_VER
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#else
#include <limits.h> // `SSIZE_MAX`
#include <unistd.h> // `ssize_t`
#endif
// It seems like some Python versions forget to include a header, so we should:
// https://github.com/ashvardanian/StringZilla/actions/runs/7706636733/job/21002535521
#ifndef SSIZE_MAX
#define SSIZE_MAX (SIZE_MAX / 2)
#endif
#include <errno.h> // `errno`
#include <stdio.h> // `fopen`
#include <stdlib.h> // `rand`, `srand`
#include <time.h> // `time`
#include <Python.h> // CPython API
#include <numpy/arrayobject.h> // NumPy C API
#include <stringzillas/stringzillas.h>
/**
* @brief Set appropriate Python exception based on StringZilla status code and error detail.
* @param[in] status The StringZilla status code
* @param[in] error_detail Detailed error message from StringZilla (never NULL)
* @param[in] context Context string describing the operation (e.g., "Levenshtein initialization")
*/
static void set_stringzilla_error(sz_status_t status, char const *error_detail, char const *context) {
switch (status) {
case sz_bad_alloc_k: PyErr_Format(PyExc_MemoryError, "%s: %s", context, error_detail); break;
case sz_invalid_utf8_k: PyErr_Format(PyExc_ValueError, "%s: %s", context, error_detail); break;
case sz_overflow_risk_k: PyErr_Format(PyExc_OverflowError, "%s: %s", context, error_detail); break;
case sz_unexpected_dimensions_k: PyErr_Format(PyExc_ValueError, "%s: %s", context, error_detail); break;
case sz_missing_gpu_k:
case sz_device_code_mismatch_k:
case sz_device_memory_mismatch_k:
default: PyErr_Format(PyExc_RuntimeError, "%s: %s", context, error_detail); break;
}
}
#pragma region Forward Declarations
/**
* @brief Creates a Python tuple from capabilities mask.
* @param[in] caps Capabilities mask
* @return New reference to Python tuple, or NULL on error
*/
static PyObject *capabilities_to_tuple(sz_capability_t caps) {
char const *cap_strings[SZ_CAPABILITIES_COUNT];
sz_size_t cap_count = sz_capabilities_to_strings_implementation_(caps, cap_strings, SZ_CAPABILITIES_COUNT);
PyObject *caps_tuple = PyTuple_New(cap_count);
if (!caps_tuple) return NULL;
for (sz_size_t i = 0; i < cap_count; i++) {
PyObject *cap_str = PyUnicode_FromString(cap_strings[i]);
if (!cap_str) {
Py_DECREF(caps_tuple);
return NULL;
}
PyTuple_SET_ITEM(caps_tuple, i, cap_str);
}
return caps_tuple;
}
// Try to import NumPy, and fail if it's not available
static int numpy_available = 0;
static PyObject *numpy_module = NULL;
static PyTypeObject DeviceScopeType;
static PyTypeObject LevenshteinDistancesType;
static PyTypeObject LevenshteinDistancesUTF8Type;
static PyTypeObject NeedlemanWunschType;
static PyTypeObject SmithWatermanType;
static PyTypeObject FingerprintsType;
// Function pointers for stringzilla functions imported from capsules
static sz_bool_t (*sz_py_export_string_like)(PyObject *, sz_cptr_t *, sz_size_t *) = NULL;
static sz_bool_t (*sz_py_export_strings_as_sequence)(PyObject *, sz_sequence_t *) = NULL;
static sz_bool_t (*sz_py_export_strings_as_u32tape)(PyObject *, sz_cptr_t *, sz_u32_t const **, sz_size_t *) = NULL;
static sz_bool_t (*sz_py_export_strings_as_u64tape)(PyObject *, sz_cptr_t *, sz_u64_t const **, sz_size_t *) = NULL;
static sz_bool_t (*sz_py_replace_strings_allocator)(PyObject *, sz_memory_allocator_t *) = NULL;
// Default device scope that can be safely reused across calls
// The underlying implementation is stateless and thread-safe
static szs_device_scope_t default_device_scope = NULL;
// Static variable to store hardware capabilities
static sz_capability_t default_hardware_capabilities = 0;
// Static unified memory allocator for GPU compatibility
static sz_memory_allocator_t unified_allocator;
// Default CPU-side allocator for buffer-based flows
static sz_memory_allocator_t default_allocator;
typedef struct PyAPI {
sz_bool_t (*sz_py_export_string_like)(PyObject *, sz_cptr_t *, sz_size_t *);
sz_bool_t (*sz_py_export_strings_as_sequence)(PyObject *, sz_sequence_t *);
sz_bool_t (*sz_py_export_strings_as_u32tape)(PyObject *, sz_cptr_t *, sz_u32_t const **, sz_size_t *);
sz_bool_t (*sz_py_export_strings_as_u64tape)(PyObject *, sz_cptr_t *, sz_u64_t const **, sz_size_t *);
sz_bool_t (*sz_py_replace_strings_allocator)(PyObject *, sz_memory_allocator_t *);
} PyAPI;
// Method flags
#define SZ_METHOD_FLAGS METH_VARARGS | METH_KEYWORDS
/**
* @brief Helper function to automatically swap a Strs object's allocator to unified memory for GPU kernels.
* @param[in] strs_obj The Strs object to swap allocator for
* @return sz_true_k on success, sz_false_k on failure
* @note Sets Pythonic error on failure.
*/
static inline sz_bool_t try_swap_to_unified_allocator(PyObject *strs_obj) {
if (!strs_obj || !sz_py_replace_strings_allocator) return sz_false_k;
// Try to swap to unified allocator - this will be a no-op if already using it
sz_bool_t success = sz_py_replace_strings_allocator(strs_obj, &unified_allocator);
if (!success) {
// Always fatal: GPU kernels require unified/device-accessible memory
PyErr_SetString(PyExc_RuntimeError,
"Device memory mismatch: GPU kernels require unified/device-accessible memory. "
"Consider reducing input size, freeing memory, or using CPU capabilities.");
return sz_false_k;
}
return sz_true_k;
}
/**
* @brief Helper function to determine if unified memory is required based on capabilities and device scope.
* @param[in] capabilities The capabilities bitmask of the current engine.
*/
static inline sz_bool_t requires_unified_memory(sz_capability_t capabilities) {
return (capabilities & sz_cap_cuda_k) != 0;
}
#pragma endregion
#pragma region DeviceScope
/**
* @brief Device scope for controlling execution context (CPU cores or GPU device).
*/
typedef struct {
PyObject ob_base;
szs_device_scope_t handle;
char description[32];
} DeviceScope;
static void DeviceScope_dealloc(DeviceScope *self) {
if (self->handle) {
szs_device_scope_free(self->handle);
self->handle = NULL;
}
Py_TYPE(self)->tp_free((PyObject *)self);
}
static PyObject *DeviceScope_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
DeviceScope *self = (DeviceScope *)type->tp_alloc(type, 0);
if (self != NULL) {
self->handle = NULL;
self->description[0] = '\0';
}
return (PyObject *)self;
}
static int DeviceScope_init(DeviceScope *self, PyObject *args, PyObject *kwargs) {
sz_size_t cpu_cores = 0;
sz_size_t gpu_device = 0;
PyObject *cpu_cores_obj = NULL;
PyObject *gpu_device_obj = NULL;
static char *kwlist[] = {"cpu_cores", "gpu_device", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", kwlist, &cpu_cores_obj, &gpu_device_obj)) return -1;
sz_status_t status;
char const *error_detail = NULL;
if (cpu_cores_obj != NULL && gpu_device_obj != NULL) {
PyErr_SetString(PyExc_ValueError, "Cannot specify both cpu_cores and gpu_device");
return -1;
}
else if (cpu_cores_obj != NULL) {
if (!PyLong_Check(cpu_cores_obj)) {
PyErr_SetString(PyExc_TypeError, "cpu_cores must be an integer");
return -1;
}
cpu_cores = PyLong_AsSize_t(cpu_cores_obj);
if (cpu_cores == (sz_size_t)-1 && PyErr_Occurred()) { return -1; }
status = szs_device_scope_init_cpu_cores(cpu_cores, &self->handle, &error_detail);
if (cpu_cores == 1) { snprintf(self->description, sizeof(self->description), "default"); }
else if (cpu_cores == 0) { snprintf(self->description, sizeof(self->description), "CPUs:all"); }
else { snprintf(self->description, sizeof(self->description), "CPUs:%zu", cpu_cores); }
}
else if (gpu_device_obj != NULL) {
if (!PyLong_Check(gpu_device_obj)) {
PyErr_SetString(PyExc_TypeError, "gpu_device must be an integer");
return -1;
}
gpu_device = PyLong_AsSize_t(gpu_device_obj);
if (gpu_device == (sz_size_t)-1 && PyErr_Occurred()) { return -1; }
status = szs_device_scope_init_gpu_device(gpu_device, &self->handle, &error_detail);
snprintf(self->description, sizeof(self->description), "GPU:%zu", gpu_device);
}
else {
status = szs_device_scope_init_default(&self->handle, &error_detail);
snprintf(self->description, sizeof(self->description), "default");
}
if (status != sz_success_k) {
set_stringzilla_error(status, error_detail, "DeviceScope initialization");
return -1;
}
return 0;
}
static PyObject *DeviceScope_repr(DeviceScope *self) {
return PyUnicode_FromFormat("DeviceScope(%s)", self->description);
}
static char const doc_DeviceScope[] = //
"DeviceScope(cpu_cores=None, gpu_device=None)\n"
"\n"
"Context for controlling execution on CPU cores or GPU devices.\n"
"\n"
"Args:\n"
" cpu_cores (int, optional): Number of CPU cores to use, or zero for all cores.\n"
" gpu_device (int, optional): GPU device ID to target.\n"
"\n"
"Note: Cannot specify both cpu_cores and gpu_device.";
static PyTypeObject DeviceScopeType = {
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "stringzillas.DeviceScope",
.tp_doc = doc_DeviceScope,
.tp_basicsize = sizeof(DeviceScope),
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = DeviceScope_new,
.tp_init = (initproc)DeviceScope_init,
.tp_dealloc = (destructor)DeviceScope_dealloc,
.tp_repr = (reprfunc)DeviceScope_repr,
};
#pragma endregion
#pragma region Metadata
/**
* @brief Parse capabilities from a Python tuple of strings and intersect with hardware capabilities.
* @param[in] caps_tuple Python tuple containing capability strings (e.g., ('serial', 'haswell')).
* @param[out] result Output capability mask after intersection with hardware capabilities.
* @return 0 on success, -1 on error (with Python exception set).
*/
static int parse_and_intersect_capabilities(PyObject *caps_obj, sz_capability_t *result) {
// Handle `DeviceScope` objects
if (PyObject_IsInstance(caps_obj, (PyObject *)&DeviceScopeType)) {
DeviceScope *device_scope = (DeviceScope *)caps_obj;
// Try to get GPU device
sz_size_t gpu_device;
char const *error_detail_gpu = NULL;
if (szs_device_scope_get_gpu_device(device_scope->handle, &gpu_device, &error_detail_gpu) == sz_success_k) {
if (default_hardware_capabilities & sz_caps_cuda_k) {
*result = sz_caps_cuda_k & default_hardware_capabilities;
return 0;
}
else {
PyErr_SetString(PyExc_RuntimeError, "GPU DeviceScope requested but CUDA not available");
return -1;
}
}
// Try to get CPU cores first
sz_size_t cpu_cores;
char const *error_detail_cpu = NULL;
if (szs_device_scope_get_cpu_cores(device_scope->handle, &cpu_cores, &error_detail_cpu) == sz_success_k) {
*result = sz_caps_cpus_k & default_hardware_capabilities;
return 0;
}
// Default scope - use all available capabilities
*result = default_hardware_capabilities;
return 0;
}
// Handle tuple of capability strings (original behavior)
if (!PyTuple_Check(caps_obj)) {
PyErr_SetString(PyExc_TypeError, "capabilities must be a tuple of strings or a DeviceScope object");
return -1;
}
sz_capability_t requested_caps = 0;
Py_ssize_t n = PyTuple_Size(caps_obj);
for (Py_ssize_t i = 0; i < n; i++) {
PyObject *item = PyTuple_GET_ITEM(caps_obj, i);
if (!PyUnicode_Check(item)) {
PyErr_SetString(PyExc_TypeError, "capabilities must be a tuple of strings");
return -1;
}
char const *cap_str = PyUnicode_AsUTF8(item);
if (!cap_str) return -1;
sz_capability_t flag = sz_capability_from_string_implementation_(cap_str);
if (flag == sz_caps_none_k) {
PyErr_Format(PyExc_ValueError, "Unknown capability: %s", cap_str);
return -1;
}
requested_caps |= flag;
}
// Intersect with hardware capabilities
*result = requested_caps & default_hardware_capabilities;
// If no capabilities match, fall back to serial
if (*result == 0) { *result = sz_cap_serial_k; }
return 0;
}
#pragma endregion
#pragma region LevenshteinDistances
/**
* @brief Levenshtein distance computation engine for binary strings.
*/
typedef struct {
PyObject ob_base;
szs_levenshtein_distances_t handle;
char description[32];
sz_capability_t capabilities;
} LevenshteinDistances;
static void LevenshteinDistances_dealloc(LevenshteinDistances *self) {
if (self->handle) {
szs_levenshtein_distances_free(self->handle);
self->handle = NULL;
}
Py_TYPE(self)->tp_free((PyObject *)self);
}
static PyObject *LevenshteinDistances_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
LevenshteinDistances *self = (LevenshteinDistances *)type->tp_alloc(type, 0);
if (self != NULL) {
self->handle = NULL;
self->description[0] = '\0';
self->capabilities = 0;
}
return (PyObject *)self;
}
static int LevenshteinDistances_init(LevenshteinDistances *self, PyObject *args, PyObject *kwargs) {
int match = 0, mismatch = 1, open = 1, extend = 1;
PyObject *capabilities_tuple = NULL;
sz_capability_t capabilities = default_hardware_capabilities;
static char *kwlist[] = {"match", "mismatch", "open", "extend", "capabilities", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiO", kwlist, &match, &mismatch, &open, &extend,
&capabilities_tuple))
return -1;
// Validate range of values
if (match < -128 || match > 127) {
PyErr_SetString(PyExc_ValueError, "match cost must fit in 8-bit signed integer");
return -1;
}
if (mismatch < -128 || mismatch > 127) {
PyErr_SetString(PyExc_ValueError, "mismatch cost must fit in 8-bit signed integer");
return -1;
}
if (open < -128 || open > 127) {
PyErr_SetString(PyExc_ValueError, "open cost must fit in 8-bit signed integer");
return -1;
}
if (extend < -128 || extend > 127) {
PyErr_SetString(PyExc_ValueError, "extend cost must fit in 8-bit signed integer");
return -1;
}
// Parse capabilities if provided
if (capabilities_tuple) {
if (parse_and_intersect_capabilities(capabilities_tuple, &capabilities) != 0) { return -1; }
}
char const *error_detail = NULL;
sz_status_t status =
szs_levenshtein_distances_init(match, mismatch, open, extend, NULL, capabilities, &self->handle, &error_detail);
if (status != sz_success_k) {
set_stringzilla_error(status, error_detail, "Levenshtein distances initialization");
return -1;
}
snprintf(self->description, sizeof(self->description), "%d,%d,%d,%d", match, mismatch, open, extend);
self->capabilities = capabilities;
return 0;
}
static PyObject *LevenshteinDistances_repr(LevenshteinDistances *self) {
return PyUnicode_FromFormat("LevenshteinDistances(match,mismatch,open,extend=%s)", self->description);
}
static PyObject *LevenshteinDistances_get_capabilities(LevenshteinDistances *self, void *closure) {
return capabilities_to_tuple(self->capabilities);
}
static PyObject *LevenshteinDistances_call(LevenshteinDistances *self, PyObject *args, PyObject *kwargs) {
PyObject *a_obj = NULL, *b_obj = NULL, *device_obj = NULL, *out_obj = NULL;
static char *kwlist[] = {"a", "b", "device", "out", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|OO", kwlist, &a_obj, &b_obj, &device_obj, &out_obj)) return NULL;
DeviceScope *device_scope = NULL;
if (device_obj != NULL && device_obj != Py_None) {
if (!PyObject_TypeCheck(device_obj, &DeviceScopeType)) {
PyErr_SetString(PyExc_TypeError, "device must be a DeviceScope instance");
return NULL;
}
device_scope = (DeviceScope *)device_obj;
}
szs_device_scope_t device_handle = device_scope ? device_scope->handle : default_device_scope;
sz_size_t kernel_input_size = 0;
void *kernel_a_texts_punned = NULL;
void *kernel_b_texts_punned = NULL;
sz_size_t *kernel_results = NULL;
sz_size_t kernel_results_stride = sizeof(sz_size_t);
sz_status_t (*kernel_punned)(szs_levenshtein_distances_t, szs_device_scope_t, void *, void *, sz_size_t *,
sz_size_t, char const **) = NULL;
// Swap allocators only when using CUDA with a GPU device (inputs must be unified)
if (requires_unified_memory(self->capabilities))
if (!try_swap_to_unified_allocator(a_obj) || !try_swap_to_unified_allocator(b_obj)) return NULL;
// Handle 32-bit tape inputs
sz_sequence_u32tape_t a_u32tape, b_u32tape;
sz_bool_t a_is_u32tape = sz_py_export_strings_as_u32tape( //
a_obj, &a_u32tape.data, &a_u32tape.offsets, &a_u32tape.count);
sz_bool_t b_is_u32tape = sz_py_export_strings_as_u32tape( //
b_obj, &b_u32tape.data, &b_u32tape.offsets, &b_u32tape.count);
if (a_is_u32tape && b_is_u32tape) {
if (a_u32tape.count != b_u32tape.count) {
PyErr_SetString(PyExc_ValueError, "Input sequences must have the same length");
return NULL;
}
kernel_input_size = a_u32tape.count;
kernel_punned = szs_levenshtein_distances_u32tape;
kernel_a_texts_punned = &a_u32tape;
kernel_b_texts_punned = &b_u32tape;
}
// Handle 64-bit tape inputs
sz_sequence_u64tape_t a_u64tape, b_u64tape;
sz_bool_t a_is_u64tape = !a_is_u32tape && sz_py_export_strings_as_u64tape( //
a_obj, &a_u64tape.data, &a_u64tape.offsets, &a_u64tape.count);
sz_bool_t b_is_u64tape = !b_is_u32tape && sz_py_export_strings_as_u64tape( //
b_obj, &b_u64tape.data, &b_u64tape.offsets, &b_u64tape.count);
if (a_is_u64tape && b_is_u64tape) {
if (a_u64tape.count != b_u64tape.count) {
PyErr_SetString(PyExc_ValueError, "Input sequences must have the same length");
return NULL;
}
kernel_input_size = a_u64tape.count;
kernel_punned = szs_levenshtein_distances_u64tape;
kernel_a_texts_punned = &a_u64tape;
kernel_b_texts_punned = &b_u64tape;
}
// Handle sequence inputs
sz_sequence_t a_seq, b_seq;
sz_bool_t a_is_sequence = !a_is_u32tape && !a_is_u64tape && sz_py_export_strings_as_sequence(a_obj, &a_seq);
sz_bool_t b_is_sequence = !b_is_u32tape && !b_is_u64tape && sz_py_export_strings_as_sequence(b_obj, &b_seq);
if (a_is_sequence && b_is_sequence) {
if (a_seq.count != b_seq.count) {
PyErr_SetString(PyExc_ValueError, "Input sequences must have the same length");
return NULL;
}
kernel_input_size = a_seq.count;
kernel_punned = szs_levenshtein_distances_sequence;
kernel_a_texts_punned = &a_seq;
kernel_b_texts_punned = &b_seq;
}
// If no valid input types were found, raise an error
if (!kernel_punned) {
PyErr_Format(PyExc_TypeError,
"Expected stringzilla.Strs objects, got %s and %s. "
"Convert using: stringzilla.Strs(your_string_list)",
Py_TYPE(a_obj)->tp_name, Py_TYPE(b_obj)->tp_name);
return NULL;
}
// Make sure the `out` argument is valid NumPy array and extract `kernel_results` and `kernel_results_stride`
// or create a new results array.
PyObject *results_array = NULL;
if (!out_obj || out_obj == Py_None) {
// Create a new NumPy array for results
npy_intp numpy_size = kernel_input_size;
results_array = PyArray_SimpleNew(1, &numpy_size, NPY_UINT64);
if (!results_array) {
PyErr_SetString(PyExc_RuntimeError, "Failed to create NumPy array for results");
goto cleanup;
}
kernel_results = (sz_size_t *)PyArray_DATA((PyArrayObject *)results_array);
kernel_results_stride = sizeof(sz_size_t);
}
else {
// Validate existing NumPy array
if (!PyArray_Check(out_obj)) {
PyErr_SetString(PyExc_TypeError, "out argument must be a NumPy array");
goto cleanup;
}
PyArrayObject *array = (PyArrayObject *)out_obj;
if (PyArray_NDIM(array) != 1) {
PyErr_SetString(PyExc_ValueError, "out array must be 1-dimensional");
goto cleanup;
}
if (PyArray_SIZE(array) < (npy_intp)kernel_input_size) {
PyErr_SetString(PyExc_ValueError, "out array is too small for results");
goto cleanup;
}
if (PyArray_TYPE(array) != NPY_UINT64) {
PyErr_SetString(PyExc_TypeError, "out array must have uint64 dtype");
goto cleanup;
}
kernel_results = (sz_size_t *)PyArray_DATA(array);
kernel_results_stride = PyArray_STRIDE(array, 0);
results_array = out_obj;
Py_INCREF(results_array);
}
char const *error_detail = NULL;
sz_status_t status = kernel_punned( //
self->handle, device_handle, //
kernel_a_texts_punned, kernel_b_texts_punned, //
kernel_results, kernel_results_stride, &error_detail);
if (status != sz_success_k) {
set_stringzilla_error(status, error_detail, "Levenshtein distances computation");
goto cleanup;
}
return results_array;
cleanup:
Py_XDECREF(results_array);
return NULL;
}
static char const doc_LevenshteinDistances[] = //
"LevenshteinDistances(match=0, mismatch=1, open=1, extend=1, capabilities=None)\n"
"\n"
"Compute Levenshtein edit distances between pairs of binary strings.\n"
"\n"
"Args:\n"
" match (int): Cost for matching characters (default: 0).\n"
" mismatch (int): Cost for mismatched characters (default: 1).\n"
" open (int): Cost for opening a gap (default: 1).\n"
" extend (int): Cost for extending a gap (default: 1).\n"
" capabilities (Tuple[str] or DeviceScope, optional): Hardware capabilities to use.\n"
" Can be explicit capabilities like ('serial', 'parallel')\n"
" or a DeviceScope for automatic capability inference.\n"
"\n"
"Call with:\n"
" a (sequence): First sequence of strings.\n"
" b (sequence): Second sequence of strings.\n"
" device (DeviceScope, optional): Device execution context.\n"
" out (array, optional): Output buffer for results.\n"
"\n"
"Examples:\n"
" ```python\n"
" # Minimal CPU example with auto-inferred capabilities\n"
" import stringzilla as sz, stringzillas as szs\n"
" engine = szs.LevenshteinDistances()\n"
" strings_a = sz.Strs(['hello', 'world'])\n"
" strings_b = sz.Strs(['hallo', 'word'])\n"
" distances = engine(strings_a, strings_b)\n"
" \n"
" # GPU example with custom costs and auto-inferred capabilities\n"
" gpu_scope = szs.DeviceScope(gpu_device=0)\n"
" engine = szs.LevenshteinDistances(match=0, mismatch=2, open=3, extend=1, capabilities=gpu_scope)\n"
" distances = engine(strings_a, strings_b, device=gpu_scope)\n"
" ```";
static PyGetSetDef LevenshteinDistances_getsetters[] = {
{"__capabilities__", (getter)LevenshteinDistances_get_capabilities, NULL,
"Hardware capabilities used by this engine", NULL},
{NULL} /* Sentinel */
};
static PyTypeObject LevenshteinDistancesType = {
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "stringzillas.LevenshteinDistances",
.tp_doc = doc_LevenshteinDistances,
.tp_basicsize = sizeof(LevenshteinDistances),
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = LevenshteinDistances_new,
.tp_init = (initproc)LevenshteinDistances_init,
.tp_dealloc = (destructor)LevenshteinDistances_dealloc,
.tp_call = (ternaryfunc)LevenshteinDistances_call,
.tp_repr = (reprfunc)LevenshteinDistances_repr,
.tp_getset = LevenshteinDistances_getsetters,
};
#pragma endregion
#pragma region LevenshteinDistancesUTF8
typedef struct {
PyObject ob_base;
szs_levenshtein_distances_utf8_t handle;
char description[32];
sz_capability_t capabilities;
} LevenshteinDistancesUTF8;
static PyObject *LevenshteinDistancesUTF8_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
LevenshteinDistancesUTF8 *self = (LevenshteinDistancesUTF8 *)type->tp_alloc(type, 0);
if (self != NULL) {
self->handle = NULL;
self->description[0] = '\0';
self->capabilities = 0;
}
return (PyObject *)self;
}
static void LevenshteinDistancesUTF8_dealloc(LevenshteinDistancesUTF8 *self) {
if (self->handle) { szs_levenshtein_distances_utf8_free(self->handle); }
Py_TYPE(self)->tp_free((PyObject *)self);
}
static int LevenshteinDistancesUTF8_init(LevenshteinDistancesUTF8 *self, PyObject *args, PyObject *kwargs) {
int match = 0, mismatch = 1, open = 1, extend = 1;
PyObject *capabilities_tuple = NULL;
sz_capability_t capabilities = default_hardware_capabilities;
static char *kwlist[] = {"match", "mismatch", "open", "extend", "capabilities", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiO", kwlist, &match, &mismatch, &open, &extend,
&capabilities_tuple))
return -1;
// Validate range of values
if (match < -128 || match > 127) {
PyErr_SetString(PyExc_ValueError, "match cost must fit in 8-bit signed integer");
return -1;
}
if (mismatch < -128 || mismatch > 127) {
PyErr_SetString(PyExc_ValueError, "mismatch cost must fit in 8-bit signed integer");
return -1;
}
if (open < -128 || open > 127) {
PyErr_SetString(PyExc_ValueError, "open cost must fit in 8-bit signed integer");
return -1;
}
if (extend < -128 || extend > 127) {
PyErr_SetString(PyExc_ValueError, "extend cost must fit in 8-bit signed integer");
return -1;
}
// Parse capabilities if provided
if (capabilities_tuple) {
if (parse_and_intersect_capabilities(capabilities_tuple, &capabilities) != 0) { return -1; }
}
char const *error_detail = NULL;
sz_status_t status = szs_levenshtein_distances_utf8_init(match, mismatch, open, extend, NULL, capabilities,
&self->handle, &error_detail);
if (status != sz_success_k) {
set_stringzilla_error(status, error_detail, "UTF-8 Levenshtein distances initialization");
return -1;
}
snprintf(self->description, sizeof(self->description), "%d,%d,%d,%d", match, mismatch, open, extend);
self->capabilities = capabilities;
return 0;
}
static PyObject *LevenshteinDistancesUTF8_repr(LevenshteinDistancesUTF8 *self) {
return PyUnicode_FromFormat("LevenshteinDistancesUTF8(match,mismatch,open,extend=%s)", self->description);
}
static PyObject *LevenshteinDistancesUTF8_get_capabilities(LevenshteinDistancesUTF8 *self, void *closure) {
return capabilities_to_tuple(self->capabilities);
}
static PyObject *LevenshteinDistancesUTF8_call(LevenshteinDistancesUTF8 *self, PyObject *args, PyObject *kwargs) {
PyObject *a_obj = NULL, *b_obj = NULL, *device_obj = NULL, *out_obj = NULL;
static char *kwlist[] = {"a", "b", "device", "out", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|OO", kwlist, &a_obj, &b_obj, &device_obj, &out_obj)) return NULL;
DeviceScope *device_scope = NULL;
if (device_obj != NULL && device_obj != Py_None) {
if (!PyObject_TypeCheck(device_obj, &DeviceScopeType)) {
PyErr_SetString(PyExc_TypeError, "device must be a DeviceScope instance");
return NULL;
}
device_scope = (DeviceScope *)device_obj;
}
szs_device_scope_t device_handle = device_scope ? device_scope->handle : default_device_scope;
sz_size_t kernel_input_size = 0;
void *kernel_a_texts_punned = NULL;
void *kernel_b_texts_punned = NULL;
sz_size_t *kernel_results = NULL;
sz_size_t kernel_results_stride = sizeof(sz_size_t);
sz_status_t (*kernel_punned)(szs_levenshtein_distances_t, szs_device_scope_t, void *, void *, sz_size_t *,
sz_size_t, char const **) = NULL;
// Swap allocators when engine supports CUDA
if (requires_unified_memory(self->capabilities))
if (!try_swap_to_unified_allocator(a_obj) || !try_swap_to_unified_allocator(b_obj)) return NULL;
// Handle 32-bit tape inputs
sz_sequence_u32tape_t a_u32tape, b_u32tape;
sz_bool_t a_is_u32tape = sz_py_export_strings_as_u32tape( //
a_obj, &a_u32tape.data, &a_u32tape.offsets, &a_u32tape.count);
sz_bool_t b_is_u32tape = sz_py_export_strings_as_u32tape( //
b_obj, &b_u32tape.data, &b_u32tape.offsets, &b_u32tape.count);
if (a_is_u32tape && b_is_u32tape) {
if (a_u32tape.count != b_u32tape.count) {
PyErr_SetString(PyExc_ValueError, "Input sequences must have the same length");
return NULL;
}
kernel_input_size = a_u32tape.count;
kernel_punned = szs_levenshtein_distances_utf8_u32tape;
kernel_a_texts_punned = &a_u32tape;
kernel_b_texts_punned = &b_u32tape;
}
// Handle 64-bit tape inputs
sz_sequence_u64tape_t a_u64tape, b_u64tape;
sz_bool_t a_is_u64tape = !a_is_u32tape && sz_py_export_strings_as_u64tape( //
a_obj, &a_u64tape.data, &a_u64tape.offsets, &a_u64tape.count);
sz_bool_t b_is_u64tape = !b_is_u32tape && sz_py_export_strings_as_u64tape( //
b_obj, &b_u64tape.data, &b_u64tape.offsets, &b_u64tape.count);
if (a_is_u64tape && b_is_u64tape) {
if (a_u64tape.count != b_u64tape.count) {
PyErr_SetString(PyExc_ValueError, "Input sequences must have the same length");
return NULL;
}
kernel_input_size = a_u64tape.count;
kernel_punned = szs_levenshtein_distances_utf8_u64tape;
kernel_a_texts_punned = &a_u64tape;
kernel_b_texts_punned = &b_u64tape;
}
// Handle sequence inputs
sz_sequence_t a_seq, b_seq;
sz_bool_t a_is_sequence = !a_is_u32tape && !a_is_u64tape && sz_py_export_strings_as_sequence(a_obj, &a_seq);
sz_bool_t b_is_sequence = !b_is_u32tape && !b_is_u64tape && sz_py_export_strings_as_sequence(b_obj, &b_seq);
if (a_is_sequence && b_is_sequence) {
if (a_seq.count != b_seq.count) {
PyErr_SetString(PyExc_ValueError, "Input sequences must have the same length");
return NULL;
}
kernel_input_size = a_seq.count;
kernel_punned = szs_levenshtein_distances_utf8_sequence;
kernel_a_texts_punned = &a_seq;
kernel_b_texts_punned = &b_seq;
}
// If no valid input types were found, raise an error
if (!kernel_punned) {
PyErr_Format(PyExc_TypeError,
"Expected stringzilla.Strs objects, got %s and %s. "
"Convert using: stringzilla.Strs(your_string_list)",
Py_TYPE(a_obj)->tp_name, Py_TYPE(b_obj)->tp_name);
return NULL;
}
// Make sure the `out` argument is valid NumPy array and extract `kernel_results` and `kernel_results_stride`
// or create a new results array.
PyObject *results_array = NULL;
if (!out_obj || out_obj == Py_None) {
// Create a new NumPy array for results
npy_intp numpy_size = kernel_input_size;
results_array = PyArray_SimpleNew(1, &numpy_size, NPY_UINT64);
if (!results_array) {
PyErr_SetString(PyExc_RuntimeError, "Failed to create NumPy array for results");
goto cleanup;
}
kernel_results = (sz_size_t *)PyArray_DATA((PyArrayObject *)results_array);
kernel_results_stride = sizeof(sz_size_t);
}
else {
// Validate existing NumPy array
if (!PyArray_Check(out_obj)) {
PyErr_SetString(PyExc_TypeError, "out argument must be a NumPy array");
goto cleanup;
}
PyArrayObject *array = (PyArrayObject *)out_obj;
if (PyArray_NDIM(array) != 1) {
PyErr_SetString(PyExc_ValueError, "out array must be 1-dimensional");
goto cleanup;
}
if (PyArray_SIZE(array) < (npy_intp)kernel_input_size) {
PyErr_SetString(PyExc_ValueError, "out array is too small for results");
goto cleanup;
}
if (PyArray_TYPE(array) != NPY_UINT64) {
PyErr_SetString(PyExc_TypeError, "out array must have uint64 dtype");
goto cleanup;
}
kernel_results = (sz_size_t *)PyArray_DATA(array);
kernel_results_stride = PyArray_STRIDE(array, 0);
results_array = out_obj;
Py_INCREF(results_array);
}
char const *error_detail = NULL;
sz_status_t status = kernel_punned( //
self->handle, device_handle, //
kernel_a_texts_punned, kernel_b_texts_punned, //
kernel_results, kernel_results_stride, &error_detail);
if (status != sz_success_k) {
set_stringzilla_error(status, error_detail, "Levenshtein distances computation");
goto cleanup;
}
return results_array;
cleanup:
Py_XDECREF(results_array);
return NULL;
}
static char const doc_LevenshteinDistancesUTF8[] = //
"LevenshteinDistancesUTF8(match=0, mismatch=1, open=1, extend=1, capabilities=None)\n"
"\n"
"Vectorized UTF-8 Levenshtein distance calculator with affine gap penalties.\n"
"Computes edit distances between pairs of UTF-8 encoded strings.\n"
"\n"
"Args:\n"
" match (int): Cost of matching characters (default 0).\n"
" mismatch (int): Cost of mismatched characters (default 1).\n"
" open (int): Cost of opening a gap (default 1).\n"
" extend (int): Cost of extending a gap (default 1).\n"
" capabilities (Tuple[str] or DeviceScope, optional): Hardware capabilities to use.\n"
" Can be explicit capabilities like ('serial', 'parallel')\n"
" or a DeviceScope for automatic capability inference.\n"
"\n"
"Call with:\n"
" a (sequence): First sequence of UTF-8 strings.\n"
" b (sequence): Second sequence of UTF-8 strings.\n"
" device (DeviceScope, optional): Device execution context.\n"
" out (array, optional): Output buffer for results.\n"
"\n"
"Examples:\n"
" ```python\n"
" # Minimal CPU example with Unicode strings\n"
" import stringzilla as sz, stringzillas as szs\n"
" engine = szs.LevenshteinDistancesUTF8()\n"
" strings_a = sz.Strs(['café', 'naïve'])\n"
" strings_b = sz.Strs(['caffe', 'naive'])\n"
" distances = engine(strings_a, strings_b)\n"
" \n"
" # GPU example with high mismatch penalty\n"
" gpu_scope = szs.DeviceScope(gpu_device=0)\n"
" engine = szs.LevenshteinDistancesUTF8(mismatch=5, capabilities=gpu_scope)\n"
" distances = engine(strings_a, strings_b, device=gpu_scope)\n"
" ```";
static PyGetSetDef LevenshteinDistancesUTF8_getsetters[] = {
{"__capabilities__", (getter)LevenshteinDistancesUTF8_get_capabilities, NULL,
"Hardware capabilities used by this engine", NULL},
{NULL} /* Sentinel */
};
static PyTypeObject LevenshteinDistancesUTF8Type = {
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "stringzillas.LevenshteinDistancesUTF8",
.tp_doc = doc_LevenshteinDistancesUTF8,
.tp_basicsize = sizeof(LevenshteinDistancesUTF8),
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = LevenshteinDistancesUTF8_new,
.tp_init = (initproc)LevenshteinDistancesUTF8_init,
.tp_dealloc = (destructor)LevenshteinDistancesUTF8_dealloc,
.tp_call = (ternaryfunc)LevenshteinDistancesUTF8_call,
.tp_repr = (reprfunc)LevenshteinDistancesUTF8_repr,
.tp_getset = LevenshteinDistancesUTF8_getsetters,
};
#pragma endregion
#pragma region NeedlemanWunsch
/**
* @brief Needleman-Wunsch global alignment scoring engine.
*/
typedef struct {
PyObject ob_base;
szs_needleman_wunsch_scores_t handle;
char description[32];
sz_capability_t capabilities;
} NeedlemanWunsch;
static void NeedlemanWunsch_dealloc(NeedlemanWunsch *self) {
if (self->handle) {
szs_needleman_wunsch_scores_free(self->handle);
self->handle = NULL;
}
Py_TYPE(self)->tp_free((PyObject *)self);
}
static PyObject *NeedlemanWunsch_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
NeedlemanWunsch *self = (NeedlemanWunsch *)type->tp_alloc(type, 0);
if (self != NULL) {
self->handle = NULL;
self->description[0] = '\0';
self->capabilities = 0;
}
return (PyObject *)self;
}
static int NeedlemanWunsch_init(NeedlemanWunsch *self, PyObject *args, PyObject *kwargs) {
PyObject *substitution_matrix_obj = NULL;
sz_error_cost_t open = -1, extend = -1;
PyObject *capabilities_tuple = NULL;
sz_capability_t capabilities = default_hardware_capabilities;
// Parse arguments: substitution_matrix, open, extend, capabilities
static char *kwlist[] = {"substitution_matrix", "open", "extend", "capabilities", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|iiO", kwlist, &substitution_matrix_obj, &open, &extend,
&capabilities_tuple))
return -1;
// Validate substitution matrix (should be a 256x256 numpy array)
if (!numpy_available || !PyArray_Check(substitution_matrix_obj)) {
PyErr_SetString(PyExc_TypeError, "substitution_matrix must be a NumPy array");
return -1;
}
PyArrayObject *subs_array = (PyArrayObject *)substitution_matrix_obj;
if (PyArray_NDIM(subs_array) != 2 || PyArray_DIM(subs_array, 0) != 256 || PyArray_DIM(subs_array, 1) != 256) {
PyErr_SetString(PyExc_ValueError, "substitution_matrix must be a 256x256 array");
return -1;
}
if (PyArray_TYPE(subs_array) != NPY_INT8) {
PyErr_SetString(PyExc_TypeError, "substitution_matrix must have int8 dtype");
return -1;
}
// Check that array is C-contiguous for safe memory access
if (!PyArray_IS_C_CONTIGUOUS(subs_array)) {
PyErr_SetString(PyExc_ValueError,
"substitution_matrix must be a C-contiguous array. Use np.ascontiguousarray() to convert.");
return -1;
}
// Parse capabilities if provided
if (capabilities_tuple) {
if (parse_and_intersect_capabilities(capabilities_tuple, &capabilities) != 0) { return -1; }
}
// Initialize the engine
sz_error_cost_t *subs_data = (sz_error_cost_t *)PyArray_DATA(subs_array);
// Create a simple checksum of the substitution matrix for the description
sz_u32_t subs_checksum = 0;
for (int i = 0; i < 256; i += 16) // Sample every 16th element
subs_checksum += (sz_u32_t)subs_data[i * 256 + i]; // Diagonal elements
char const *error_detail = NULL;
sz_status_t status =
szs_needleman_wunsch_scores_init(subs_data, open, extend, NULL, capabilities, &self->handle, &error_detail);
if (status != sz_success_k) {
set_stringzilla_error(status, error_detail, "NeedlemanWunsch initialization");
return -1;
}
snprintf(self->description, sizeof(self->description), "%X,%d,%d", subs_checksum & 0xFFFF, open, extend);
self->capabilities = capabilities;
return 0;
}