-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiff.c
More file actions
1438 lines (1201 loc) · 52.3 KB
/
diff.c
File metadata and controls
1438 lines (1201 loc) · 52.3 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
/*
* EmbeddingBridge - Embedding Comparison Implementation
* Copyright (C) 2024 ProgramComputer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <sys/stat.h>
#include <dirent.h>
#include <linux/limits.h> // For PATH_MAX on Linux
#include <npy_array.h>
#include <ctype.h>
/* Core includes - order matters */
#include "../core/types.h"
#include "../core/error.h"
#include "../core/debug.h"
#include "../core/embedding.h"
#include "../core/store.h"
#include "../core/path_utils.h"
/* CLI includes */
#include "cli.h"
#include "colors.h"
#include "debug.h"
/* Function declarations */
void cli_info(const char* format, ...);
static bool is_hex_string(const char* str);
static bool has_multiple_models(const char* repo_root, const char* file_path);
static char* get_available_models(const char* repo_root, const char* file_path);
static const char* get_default_model_for_file(const char* repo_root, const char* file_path);
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
/* Forward declarations */
static float* load_npy_embedding(const char* filepath, size_t* dims);
static float* load_bin_embedding(const char* filepath, size_t* dims);
static float* load_stored_embedding(const char* hash, size_t* dims);
static float* load_embedding(const char* path_or_hash, size_t* dims);
static char* resolve_hash(const char* input_hash);
static bool is_valid_hash(const char* str);
static int ends_with(const char* str, const char* suffix);
static int check_invalid_values(const float* embedding, size_t dims);
static float cosine_similarity(const float* vec1, const float* vec2, size_t dims);
static float euclidean_distance(const float* vec1, const float* vec2, size_t dims);
static float euclidean_similarity(const float* vec1, const float* vec2, size_t dims);
static const char* DIFF_USAGE =
"Usage: embr diff [options] <input1> <input2>\n"
"\n"
"Compare two embeddings and show their similarity\n"
"\n"
"Arguments:\n"
" <input1> First embedding (hash, file, or source file)\n"
" <input2> Second embedding (hash, file, or source file)\n"
"\n"
"Options:\n"
" --models <model1>[,<model2>] Specify models to use (required for multi-model repos)\n"
" --model <model> Shorthand to use the same model for both inputs\n"
"\n"
"Examples:\n"
" embr diff 7d39a15 9f3e8c2 # Compare using short hashes (7 chars)\n"
" embr diff 7d39a15cb74e02f1a0a4e5842b1b1d5c3e2a98765434abcdef 9f3e8c2a3b4c5d6e\n"
" # Compare using full or partial hashes\n"
" embr diff file1.npy file2.npy # Compare two .npy files\n"
" embr diff file1.bin file2.bin # Compare two binary files\n"
" embr diff doc1.txt doc2.txt # Compare source files (looks for associated files)\n"
" embr diff --model voyage-2 file.txt # Compare latest vs. previous for voyage-2\n"
" embr diff --models openai-3,voyage-2 file1.txt file2.txt\n"
" # Compare file1 with openai-3 and file2 with voyage-2\n";
/* Calculate cosine similarity between two float vectors */
static float cosine_similarity(const float *vec1, const float *vec2, size_t dims)
{
double dot_product = 0.0;
double norm1 = 0.0;
double norm2 = 0.0;
DEBUG_PRINT("Calculating similarity for %zu dimensions", dims);
// Calculate dot product and norms in one pass
for (size_t i = 0; i < dims; i++) {
// Check for invalid values
if (isnan(vec1[i]) || isnan(vec2[i]) ||
isinf(vec1[i]) || isinf(vec2[i])) {
DEBUG_PRINT("Invalid value detected at index %zu: vec1=%f, vec2=%f",
i, vec1[i], vec2[i]);
return 0.0f;
}
dot_product += (double)vec1[i] * (double)vec2[i];
norm1 += (double)vec1[i] * (double)vec1[i];
norm2 += (double)vec2[i] * (double)vec2[i];
}
DEBUG_PRINT("Raw calculations: dot_product=%f, norm1=%f, norm2=%f",
dot_product, norm1, norm2);
if (norm1 <= 0.0 || norm2 <= 0.0) {
DEBUG_PRINT("Zero norm detected: norm1=%f, norm2=%f", norm1, norm2);
return 0.0f;
}
// Calculate cosine similarity
float similarity = (float)(dot_product / (sqrt(norm1) * sqrt(norm2)));
DEBUG_PRINT("Calculated cosine similarity: %f", similarity);
return similarity;
}
/* Calculate Euclidean distance between two float vectors */
static float euclidean_distance(const float *vec1, const float *vec2, size_t dims)
{
double sum = 0.0;
DEBUG_PRINT("Calculating Euclidean distance for %zu dimensions", dims);
for (size_t i = 0; i < dims; i++) {
// Check for invalid values
if (isnan(vec1[i]) || isnan(vec2[i]) ||
isinf(vec1[i]) || isinf(vec2[i])) {
DEBUG_PRINT("Invalid value detected at index %zu: vec1=%f, vec2=%f",
i, vec1[i], vec2[i]);
return INFINITY;
}
double diff = (double)vec1[i] - (double)vec2[i];
sum += diff * diff;
}
DEBUG_PRINT("Euclidean distance squared: %f", sum);
return (float)sqrt(sum);
}
/* Calculate normalized Euclidean similarity (0 to 1 scale, where 1 is identical) */
static float euclidean_similarity(const float *vec1, const float *vec2, size_t dims)
{
float distance = euclidean_distance(vec1, vec2, dims);
if (isinf(distance) || isnan(distance)) {
return 0.0f;
}
// Normalize to [0,1] range where 1 means identical
// Using a common approach: 1 / (1 + distance)
float similarity = 1.0f / (1.0f + distance);
DEBUG_PRINT("Calculated normalized Euclidean similarity: %f", similarity);
return similarity;
}
/* Load embedding from .npy file */
static float* load_npy_embedding(const char *filepath, size_t *dims)
{
npy_array_t *arr = npy_array_load(filepath);
if (!arr) {
cli_error("Cannot read NumPy file: %s", filepath);
return NULL;
}
DEBUG_PRINT("Loading .npy file with %d dimensions", arr->ndim);
DEBUG_PRINT("NPY array info:");
DEBUG_PRINT(" Type: %c", arr->typechar);
DEBUG_PRINT(" Dimensions: %d", arr->ndim);
for (int i = 0; i < arr->ndim; i++) {
DEBUG_PRINT(" Shape[%d]: %zu", i, arr->shape[i]);
}
DEBUG_PRINT(" Element size: %zu", arr->elem_size);
DEBUG_PRINT(" Fortran order: %s", arr->fortran_order ? "true" : "false");
// Verify it's a float array (float32 or float64)
if (arr->typechar != 'f') {
cli_error("Invalid .npy format - expected float32/float64 array, got type '%c'", arr->typechar);
npy_array_free(arr);
return NULL;
}
// Get total size
size_t total_elements = 1;
for (int i = 0; i < arr->ndim; i++) {
total_elements *= arr->shape[i];
}
*dims = total_elements;
// Allocate and copy the data
float *data = malloc(total_elements * sizeof(float));
if (!data) {
cli_error("Out of memory");
npy_array_free(arr);
return NULL;
}
// If float64, we need to convert to float32
if (arr->elem_size == 8) {
double *src = (double*)arr->data;
for (size_t i = 0; i < total_elements; i++) {
data[i] = (float)src[i];
}
} else {
memcpy(data, arr->data, total_elements * sizeof(float));
}
DEBUG_PRINT("First 5 values from .npy file:");
for (size_t i = 0; i < 5 && i < total_elements; i++) {
DEBUG_PRINT("[%zu]: %f", i, data[i]);
}
npy_array_free(arr);
return data;
}
/* Helper function to check if string ends with suffix */
static int ends_with(const char *str, const char *suffix) {
size_t str_len = strlen(str);
size_t suffix_len = strlen(suffix);
if (str_len < suffix_len) return 0;
return strcmp(str + str_len - suffix_len, suffix) == 0;
}
/* Load embedding from binary file */
static float* load_bin_embedding(const char *filepath, size_t *dims)
{
struct stat st;
FILE *f;
float *data = NULL;
if (stat(filepath, &st) != 0) {
cli_error("Cannot access binary file: %s", filepath);
return NULL;
}
*dims = st.st_size / sizeof(float);
f = fopen(filepath, "rb");
if (!f) {
cli_error("Cannot open binary file: %s", filepath);
return NULL;
}
data = malloc(st.st_size);
if (!data) {
cli_error("Out of memory");
fclose(f);
return NULL;
}
if (fread(data, 1, (size_t)st.st_size, f) != (size_t)st.st_size) {
cli_error("Failed to read binary file: %s", filepath);
free(data);
fclose(f);
return NULL;
}
DEBUG_PRINT("First 5 values from binary file:");
for (size_t i = 0; i < 5 && i < *dims; i++) {
DEBUG_PRINT("[%zu]: %f", i, data[i]);
}
fclose(f);
return data;
}
static bool is_valid_hash(const char* str) {
if (!str || strlen(str) != 64) return false;
for (int i = 0; str[i]; i++) {
if (!isxdigit(str[i])) return false;
}
return true;
}
/* Helper function to resolve hash */
static char* resolve_hash(const char* input_hash) {
DEBUG_PRINT("resolve_hash: Starting resolution for hash: %s\n", input_hash);
char* repo_root = find_repo_root(".");
if (!repo_root) {
DEBUG_PRINT("resolve_hash: Failed to find repository root\n");
cli_error("Not in an eb repository");
return NULL;
}
DEBUG_PRINT("resolve_hash: Found repository root: %s\n", repo_root);
eb_store_t* store;
eb_store_config_t config = { .root_path = repo_root };
if (eb_store_init(&config, &store) != EB_SUCCESS) {
DEBUG_PRINT("resolve_hash: Failed to initialize store\n");
free(repo_root);
return NULL;
}
DEBUG_PRINT("resolve_hash: Successfully initialized store\n");
char* full_hash = malloc(65); // 64 chars + null terminator
if (!full_hash) {
DEBUG_PRINT("resolve_hash: Failed to allocate memory for full hash\n");
eb_store_destroy(store);
free(repo_root);
return NULL;
}
eb_status_t status = eb_store_resolve_hash(store, input_hash, full_hash, 65);
DEBUG_PRINT("resolve_hash: Resolution status: %d\n", status);
eb_store_destroy(store);
free(repo_root);
if (status != EB_SUCCESS) {
DEBUG_PRINT("resolve_hash: Resolution failed, freeing memory\n");
free(full_hash);
return NULL;
}
DEBUG_PRINT("resolve_hash: Successfully resolved to: %s\n", full_hash);
return full_hash;
}
/* Modified load_stored_embedding to handle multiple file types */
static float* load_embedding(const char* path_or_hash, size_t *dims)
{
DEBUG_PRINT("Attempting to load: %s\n", path_or_hash);
// First try to resolve if it looks like a hash (4-64 hex chars)
if (strlen(path_or_hash) >= 4 && strlen(path_or_hash) <= 64) {
bool looks_like_hash = true;
for (const char* p = path_or_hash; *p; p++) {
if (!isxdigit(*p)) {
looks_like_hash = false;
DEBUG_PRINT("Input contains non-hex character: %c\n", *p);
break;
}
}
DEBUG_PRINT("Input %s looks like a hash: %s\n", path_or_hash, looks_like_hash ? "yes" : "no");
if (looks_like_hash) {
DEBUG_PRINT("Attempting to resolve hash: %s\n", path_or_hash);
char* resolved = resolve_hash(path_or_hash);
if (resolved) {
DEBUG_PRINT("Successfully resolved hash %s to %s\n", path_or_hash, resolved);
float* result = load_stored_embedding(resolved, dims);
free(resolved);
return result;
} else {
DEBUG_PRINT("Failed to resolve hash: %s\n", path_or_hash);
}
}
} else {
DEBUG_PRINT("Input length %zu is outside hash length range (4-64)\n", strlen(path_or_hash));
}
// If not a hash or hash resolution failed, try as direct file
if (strstr(path_or_hash, ".npy")) {
DEBUG_PRINT("Loading direct .npy file: %s\n", path_or_hash);
return load_npy_embedding(path_or_hash, dims);
}
if (strstr(path_or_hash, ".bin")) {
DEBUG_PRINT("Loading direct .bin file: %s\n", path_or_hash);
return load_bin_embedding(path_or_hash, dims);
}
cli_error("Unsupported file format or invalid hash: %s", path_or_hash);
return NULL;
}
static int check_invalid_values(const float *embedding, size_t dims)
{
DEBUG_PRINT("Checking %zu dimensions for invalid values", dims);
for (size_t i = 0; i < dims; i++) {
if (isnan(embedding[i]) || isinf(embedding[i])) {
DEBUG_PRINT("Found invalid value at index %zu", i);
return 1;
}
}
return 0;
}
static float* load_stored_embedding(const char* hash, size_t *dims)
{
DEBUG_PRINT("Loading stored embedding with hash: %s\n", hash);
// Find repository root
char *repo_root = find_repo_root(".");
if (!repo_root) {
cli_error("Not in an eb repository");
return NULL;
}
// Try using the store API first, which handles decompression properly
eb_store_t *store = NULL;
eb_store_config_t config = { .root_path = repo_root };
if (eb_store_init(&config, &store) == EB_SUCCESS) {
void *decompressed_data = NULL;
size_t decompressed_size = 0;
eb_object_header_t header;
eb_status_t status = read_object(store, hash, &decompressed_data, &decompressed_size, &header);
if (status == EB_SUCCESS && decompressed_data) {
// Dump the first bytes to help debug format
DEBUG_PRINT("First bytes in hex:");
char debug_buf[100] = {0};
for (size_t i = 0; i < 16 && i < decompressed_size; i++) {
sprintf(debug_buf + (i*3), "%02x ", ((unsigned char*)decompressed_data)[i]);
}
DEBUG_PRINT("%s", debug_buf);
// Check for NumPy magic string '\x93NUMPY'
if (decompressed_size >= 10 && memcmp(decompressed_data, "\x93NUMPY", 6) == 0) {
DEBUG_PRINT("Detected NumPy array format (.npy) in decompressed data");
// Extract header size from NumPy format (stored at offset 8 as uint16)
uint16_t header_size = *((const uint16_t*)((const uint8_t*)decompressed_data + 8));
// Calculate data offset
size_t data_offset = 10 + header_size;
if (decompressed_size > data_offset) {
// Set dimensions - for numpy arrays, this will typically be the first dimension
// in the shape array, which is the number of elements in the array
float* float_data = (float*)((uint8_t*)decompressed_data + data_offset);
*dims = (decompressed_size - data_offset) / sizeof(float);
DEBUG_PRINT("NumPy array has %zu dimensions/elements", *dims);
// Make a copy of the float data to return
float *data_copy = malloc(*dims * sizeof(float));
if (!data_copy) {
cli_error("Out of memory");
free(decompressed_data);
eb_store_destroy(store);
free(repo_root);
return NULL;
}
memcpy(data_copy, float_data, *dims * sizeof(float));
// Debug first few values
DEBUG_PRINT("First 5 values from numpy array:");
for (size_t i = 0; i < 5 && i < *dims; i++) {
DEBUG_PRINT("[%zu]: %f", i, data_copy[i]);
}
free(decompressed_data);
eb_store_destroy(store);
free(repo_root);
return data_copy;
}
}
// Check specifically for binary format with dimension header
if (decompressed_size >= 4) {
uint32_t dim_header = 0;
memcpy(&dim_header, decompressed_data, sizeof(uint32_t));
DEBUG_PRINT("Possible dimension header: %u (0x%08x)", dim_header, dim_header);
// If this looks like a valid dimension count (1536 for OpenAI embeddings)
if (dim_header == 1536 || (dim_header > 100 && dim_header < 10000)) {
DEBUG_PRINT("Found valid dimension header: %u", dim_header);
// Set dimensions from header
*dims = dim_header;
// Skip the header and copy only the actual float data
size_t data_size = dim_header * sizeof(float);
float *data_copy = malloc(data_size);
if (!data_copy) {
cli_error("Out of memory");
free(decompressed_data);
eb_store_destroy(store);
free(repo_root);
return NULL;
}
// Copy data, skipping the 4-byte header
memcpy(data_copy, (uint8_t*)decompressed_data + sizeof(uint32_t), data_size);
DEBUG_PRINT("Successfully extracted %u-dimension embedding", dim_header);
free(decompressed_data);
eb_store_destroy(store);
free(repo_root);
return data_copy;
}
}
// If no valid header found, use the original approach
// Determine the number of dimensions
*dims = decompressed_size / sizeof(float);
DEBUG_PRINT("Successfully read and decompressed embedding: %zu bytes, %zu dimensions",
decompressed_size, *dims);
// Dump the first bytes to help debug format
DEBUG_PRINT("First bytes in hex:");
for (size_t i = 0; i < 32 && i < decompressed_size; i++) {
DEBUG_PRINT("%02x ", ((unsigned char*)decompressed_data)[i]);
}
// Make a copy of the decompressed data to return
float *data_copy = malloc(decompressed_size);
if (!data_copy) {
cli_error("Out of memory");
free(decompressed_data);
eb_store_destroy(store);
free(repo_root);
return NULL;
}
memcpy(data_copy, decompressed_data, decompressed_size);
// Debug first few values
DEBUG_PRINT("First 5 values from decompressed data:");
for (size_t i = 0; i < 5 && i < *dims; i++) {
DEBUG_PRINT("[%zu]: %f", i, data_copy[i]);
}
free(decompressed_data);
eb_store_destroy(store);
free(repo_root);
return data_copy;
} else {
DEBUG_PRINT("Failed to read object using store API: %d", status);
// Even if the store API fails, we can try to read and decompress the file directly
// Construct path to raw file
char raw_path[PATH_MAX];
snprintf(raw_path, sizeof(raw_path), "%s/.embr/objects/%s.raw", repo_root, hash);
// Try to read and decompress the file directly
FILE *f = fopen(raw_path, "rb");
if (f) {
DEBUG_PRINT("Trying to read the object file directly: %s", raw_path);
// Read the object header
eb_object_header_t header;
if (fread(&header, sizeof(header), 1, f) == 1) {
DEBUG_PRINT("Read object header: magic=0x%08x, version=%u, flags=0x%08x, size=%u",
header.magic, header.version, header.flags, header.size);
// Get data size
fseek(f, 0, SEEK_END);
long file_size = ftell(f);
size_t data_size = file_size - sizeof(header);
fseek(f, sizeof(header), SEEK_SET);
// Read the compressed data
void *compressed_data = malloc(data_size);
if (compressed_data && fread(compressed_data, 1, data_size, f) == data_size) {
// Try to decompress it using ZSTD
void *decompressed_data = NULL;
size_t decompressed_size = 0;
if (eb_decompress_zstd(compressed_data, data_size,
&decompressed_data, &decompressed_size) == EB_SUCCESS) {
DEBUG_PRINT("Successfully decompressed data directly: %zu bytes", decompressed_size);
// Check if it's a NumPy file
if (decompressed_size >= 10 && memcmp(decompressed_data, "\x93NUMPY", 6) == 0) {
DEBUG_PRINT("Detected NumPy array format (.npy) in directly decompressed data");
// Extract header size from NumPy format (stored at offset 8 as uint16)
uint16_t header_size = *((const uint16_t*)((const uint8_t*)decompressed_data + 8));
// Calculate data offset
size_t data_offset = 10 + header_size;
if (decompressed_size > data_offset) {
// Set dimensions
float* float_data = (float*)((uint8_t*)decompressed_data + data_offset);
*dims = (decompressed_size - data_offset) / sizeof(float);
DEBUG_PRINT("NumPy array has %zu dimensions/elements", *dims);
// Make a copy of the float data to return
float *data_copy = malloc(*dims * sizeof(float));
if (data_copy) {
memcpy(data_copy, float_data, *dims * sizeof(float));
DEBUG_PRINT("Successfully extracted NumPy array from direct file");
free(decompressed_data);
free(compressed_data);
fclose(f);
eb_store_destroy(store);
free(repo_root);
return data_copy;
}
}
}
// Check if it has the dimension header format
if (decompressed_size >= 4) {
uint32_t dim_header = 0;
memcpy(&dim_header, decompressed_data, sizeof(uint32_t));
DEBUG_PRINT("Possible dimension header: %u (0x%08x)", dim_header, dim_header);
// If this looks like a valid dimension count (1536 for OpenAI embeddings)
if (dim_header == 1536 || (dim_header > 100 && dim_header < 10000)) {
DEBUG_PRINT("Found valid dimension header: %u", dim_header);
// Set dimensions from header
*dims = dim_header;
// Skip the header and copy only the actual float data
size_t data_size = dim_header * sizeof(float);
float *data_copy = malloc(data_size);
if (data_copy) {
// Copy data, skipping the 4-byte header
memcpy(data_copy, (uint8_t*)decompressed_data + sizeof(uint32_t), data_size);
DEBUG_PRINT("Successfully extracted %u-dimension embedding from direct file", dim_header);
free(decompressed_data);
free(compressed_data);
fclose(f);
eb_store_destroy(store);
free(repo_root);
return data_copy;
}
}
}
free(decompressed_data);
}
free(compressed_data);
}
}
fclose(f);
}
}
} else {
DEBUG_PRINT("Failed to initialize store, falling back to direct file reading");
}
// Original implementation as fallback
// Construct path to raw file
char raw_path[PATH_MAX];
snprintf(raw_path, sizeof(raw_path), "%s/.embr/objects/%s.raw", repo_root, hash);
// Try loading as npy file first
npy_array_t *arr = npy_array_load(raw_path);
if (arr) {
DEBUG_PRINT("Successfully loaded as .npy file");
DEBUG_PRINT("NPY array info:");
DEBUG_PRINT(" Type: %c", arr->typechar);
DEBUG_PRINT(" Dimensions: %d", arr->ndim);
for (int i = 0; i < arr->ndim; i++) {
DEBUG_PRINT(" Shape[%d]: %zu", i, arr->shape[i]);
}
DEBUG_PRINT(" Element size: %zu", arr->elem_size);
DEBUG_PRINT(" Fortran order: %s", arr->fortran_order ? "true" : "false");
// Get dimensions and allocate buffer
*dims = arr->shape[0];
float *data = malloc(*dims * sizeof(float));
if (!data) {
cli_error("Out of memory");
npy_array_free(arr);
free(repo_root);
return NULL;
}
// Copy data
memcpy(data, arr->data, *dims * sizeof(float));
// Debug first few values
DEBUG_PRINT("First 5 values:");
for (size_t i = 0; i < 5 && i < *dims; i++) {
DEBUG_PRINT("[%zu]: %f", i, data[i]);
}
npy_array_free(arr);
free(repo_root);
return data;
}
DEBUG_PRINT("Not a .npy file, trying as raw float data");
// Open and read file as raw float data
FILE *f = fopen(raw_path, "rb");
if (!f) {
cli_error("Cannot open raw file: %s", raw_path);
free(repo_root);
return NULL;
}
// Get file size for raw float data
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);
// Calculate number of floats
*dims = size / sizeof(float);
// Allocate buffer
float *data = malloc(size);
if (!data) {
cli_error("Out of memory");
fclose(f);
free(repo_root);
return NULL;
}
// Read data
if (fread(data, 1, size, f) != (size_t)size) {
cli_error("Failed to read raw file");
free(data);
fclose(f);
free(repo_root);
return NULL;
}
// Debug first few values
DEBUG_PRINT("First 5 values from raw data:");
for (size_t i = 0; i < 5 && i < *dims; i++) {
DEBUG_PRINT("[%zu]: %f", i, data[i]);
}
fclose(f);
free(repo_root);
return data;
}
/* Load embedding with a specific model */
static float* load_embedding_with_model(const char* path_or_hash, const char* model, size_t *dims)
{
DEBUG_PRINT("Attempting to load with model %s: %s\n", model ? model : "NULL", path_or_hash);
// First try to resolve if it looks like a hash (4-64 hex chars)
if (strlen(path_or_hash) >= 4 && strlen(path_or_hash) <= 64) {
bool looks_like_hash = true;
for (const char* p = path_or_hash; *p; p++) {
if (!isxdigit(*p)) {
looks_like_hash = false;
DEBUG_PRINT("Input contains non-hex character: %c\n", *p);
break;
}
}
DEBUG_PRINT("Input %s looks like a hash: %s\n", path_or_hash, looks_like_hash ? "yes" : "no");
if (looks_like_hash) {
DEBUG_PRINT("Attempting to resolve hash: %s\n", path_or_hash);
char* resolved = resolve_hash(path_or_hash);
if (resolved) {
DEBUG_PRINT("Successfully resolved hash %s to %s\n", path_or_hash, resolved);
float* result = load_stored_embedding(resolved, dims);
free(resolved);
return result;
} else {
DEBUG_PRINT("Failed to resolve hash: %s\n", path_or_hash);
}
}
} else {
DEBUG_PRINT("Input length %zu is outside hash length range (4-64)\n", strlen(path_or_hash));
}
// If not a hash or hash resolution failed, try as direct file
if (strstr(path_or_hash, ".npy")) {
DEBUG_PRINT("Loading direct .npy file: %s\n", path_or_hash);
return load_npy_embedding(path_or_hash, dims);
}
if (strstr(path_or_hash, ".bin")) {
DEBUG_PRINT("Loading direct .bin file: %s\n", path_or_hash);
return load_bin_embedding(path_or_hash, dims);
}
// If not a direct file, try to get the hash for the file and model
char repo_root[PATH_MAX];
char* root_dir = find_repo_root(".");
if (root_dir) {
strncpy(repo_root, root_dir, sizeof(repo_root) - 1);
repo_root[sizeof(repo_root) - 1] = '\0';
free(root_dir);
char hash[65];
char rel_path[PATH_MAX];
// Get relative path if needed
if (path_or_hash[0] == '/') {
char* relative = get_relative_path(path_or_hash, repo_root);
if (relative) {
strncpy(rel_path, relative, sizeof(rel_path) - 1);
rel_path[sizeof(rel_path) - 1] = '\0';
free(relative);
} else {
strncpy(rel_path, path_or_hash, sizeof(rel_path) - 1);
rel_path[sizeof(rel_path) - 1] = '\0';
}
} else {
strncpy(rel_path, path_or_hash, sizeof(rel_path) - 1);
rel_path[sizeof(rel_path) - 1] = '\0';
}
// For file paths, be explicit about model requirements
if (!model) {
// Check if multiple models exist for this file
if (has_multiple_models(repo_root, rel_path)) {
cli_error("Multiple models exist for '%s'. Please specify a model with --models",
rel_path);
cli_info("Available models: %s", get_available_models(repo_root, rel_path));
return NULL;
}
// If only one model exists, use it (with a debug message)
model = get_default_model_for_file(repo_root, rel_path);
DEBUG_PRINT("Using default model '%s' for file %s", model ? model : "NULL", rel_path);
}
DEBUG_PRINT("Looking for hash with model %s for file: %s\n", model ? model : "NULL", rel_path);
if (model && get_current_hash_with_model(repo_root, rel_path, model, hash, sizeof(hash)) == EB_SUCCESS) {
// For diff command, we want to use the current hash from the model ref file or index,
// which has already been properly updated by rollback and other commands.
// This ensures we're comparing the correct version that the user intends to use,
// not necessarily the most recent one in the log.
DEBUG_PRINT("Using hash %s for file %s with model %s\n", hash, rel_path, model);
char* resolved = resolve_hash(hash);
if (resolved) {
DEBUG_PRINT("Successfully resolved hash %s to %s\n", hash, resolved);
float* result = load_stored_embedding(resolved, dims);
free(resolved);
return result;
} else {
DEBUG_PRINT("Failed to resolve hash: %s\n", hash);
}
} else {
DEBUG_PRINT("No hash found for file %s with model %s\n", rel_path, model ? model : "NULL");
// Provide a more helpful error message
if (model) {
cli_error("No embedding found for '%s' with model '%s'", rel_path, model);
cli_info("Try using 'eb store --model %s %s' first", model, rel_path);
} else {
cli_error("No embedding found for '%s'", rel_path);
cli_info("Try using 'eb store' to create an embedding first");
}
return NULL;
}
} else {
DEBUG_PRINT("Failed to get repo root\n");
}
// Provide more specific error messages based on context
if (strlen(path_or_hash) >= 4 && strlen(path_or_hash) <= 64 && is_hex_string(path_or_hash)) {
cli_error("Invalid hash: '%s'", path_or_hash);
} else {
cli_error("Unsupported file format or invalid hash: %s", path_or_hash);
cli_info("Supported formats: .npy, .bin, or tracked files");
}
return NULL;
}
/* Check if a string contains only hexadecimal characters */
static bool is_hex_string(const char* str) {
if (!str) return false;
for (const char* p = str; *p; p++) {
if (!isxdigit(*p)) return false;
}
return true;
}
/* Check if a file has embeddings from multiple models */
static bool has_multiple_models(const char* repo_root, const char* file_path) {
char* log_path = get_current_set_log_path();
FILE* fp = fopen(log_path, "r");
free(log_path);
if (!fp) return false;
char line[1024];
int model_count = 0;
char models[10][64] = {0}; // Store up to 10 different models
while (fgets(line, sizeof(line), fp)) {
// Remove newline
size_t len = strlen(line);
if (len > 0 && line[len-1] == '\n') line[len-1] = '\0';
// Parse line to extract file path and model
char* line_file_path = NULL;
char* model = NULL;
// Check for newer format (timestamp hash file model)
char* token = strtok(line, " ");
if (token && (isdigit(token[0]) || token[0] == '-')) {
// Skip timestamp and hash
token = strtok(NULL, " "); // hash
if (!token) continue;
token = strtok(NULL, " "); // file path
if (!token) continue;
line_file_path = token;
token = strtok(NULL, " "); // model
if (!token) continue;
model = token;
} else {
// Older format (file hash timestamp model)
line_file_path = token;
// Skip hash and timestamp
token = strtok(NULL, " "); // hash
if (!token) continue;
token = strtok(NULL, " "); // timestamp
if (!token) continue;
token = strtok(NULL, " "); // model
if (!token) continue;
model = token;
}
// Check if this is for our file
if (line_file_path && strcmp(line_file_path, file_path) == 0) {
// Check if we've seen this model before
bool found = false;
for (int i = 0; i < model_count; i++) {
if (strcmp(models[i], model) == 0) {
found = true;
break;
}
}
// If not, add it to our list
if (!found && model_count < 10) {
strncpy(models[model_count], model, 63);
models[model_count][63] = '\0';
model_count++;
}
}
}
fclose(fp);
return model_count > 1;
}
/* Get a comma-separated list of available models for a file */
static char* get_available_models(const char* repo_root, const char* file_path) {
static char model_list[512] = {0};
model_list[0] = '\0';
char* log_path = get_current_set_log_path();
FILE* fp = fopen(log_path, "r");
free(log_path);
if (!fp) return model_list;
char line[1024];
int model_count = 0;
char models[10][64] = {0}; // Store up to 10 different models
while (fgets(line, sizeof(line), fp)) {
// Remove newline
size_t len = strlen(line);
if (len > 0 && line[len-1] == '\n') line[len-1] = '\0';
// Parse line to extract file path and model
char* line_file_path = NULL;
char* model = NULL;
// Check for newer format (timestamp hash file model)
char* token = strtok(line, " ");
if (token && (isdigit(token[0]) || token[0] == '-')) {
// Skip timestamp and hash
token = strtok(NULL, " "); // hash
if (!token) continue;
token = strtok(NULL, " "); // file path
if (!token) continue;
line_file_path = token;
token = strtok(NULL, " "); // model
if (!token) continue;
model = token;
} else {
// Older format (file hash timestamp model)
line_file_path = token;
// Skip hash and timestamp
token = strtok(NULL, " "); // hash
if (!token) continue;