Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f4ee24b

Browse filesBrowse files
breaking : destructors, signature changes and support for ffi tools (#89)
* Add headers for types like int32_t and size_t. This makes it easier to automatically generate bindings from the header. * Add functions for cleaning up clip images. * Consistently use "clip_" as prefix and verb as suffix. * Try to prevent hard to find use after free bugs. * Add clojure bindings to readme under hot topics.
1 parent 8f34872 commit f4ee24b
Copy full SHA for f4ee24b

File tree

Expand file treeCollapse file tree

8 files changed

+48
-14
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+48
-14
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ clip.cpp also has a short startup time compared to large ML frameworks, which ma
2727

2828
## Hot topics
2929

30+
- 01/27/2024: Clojure bindings available, [clip.clj](https://github.com/phronmophobic/clip.clj).
3031
- 09/27/2023: clip.cpp now uses a new model file structure in GGUF format. This is a breaking change. See bwlow for more information.
3132
- 09/14/2023: All functions are C-compatible now. `zsl` example is updated to match Huggingface's zero-shot behavior in the zero-shot pipeline.
3233
- 09/11/2023: Introduce Python bindings.

‎clip.cpp

Copy file name to clipboardExpand all lines: clip.cpp
+26-2Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,9 +678,33 @@ bool clip_tokenize(const clip_ctx * ctx, const char * text, struct clip_tokens *
678678
return true;
679679
}
680680

681-
clip_image_u8 * make_clip_image_u8() { return new clip_image_u8(); }
681+
clip_image_u8 * clip_image_u8_make() { return new clip_image_u8(); }
682682

683-
clip_image_f32 * make_clip_image_f32() { return new clip_image_f32(); }
683+
clip_image_f32 * clip_image_f32_make() { return new clip_image_f32(); }
684+
685+
void clip_image_u8_clean(clip_image_u8* img) {
686+
if (img->data){
687+
delete[] img->data;
688+
img->data = NULL;
689+
}
690+
}
691+
692+
void clip_image_f32_clean(clip_image_f32* res) {
693+
if (res->data){
694+
delete[] res->data;
695+
res->data = NULL;
696+
}
697+
}
698+
699+
void clip_image_u8_free(clip_image_u8* img) {
700+
clip_image_u8_clean(img);
701+
delete img;
702+
}
703+
704+
void clip_image_f32_free(clip_image_f32* res) {
705+
clip_image_f32_clean(res);
706+
delete res;
707+
}
684708

685709
bool clip_image_load_from_file(const char * fname, clip_image_u8 * img) {
686710
int nx, ny, nc;

‎clip.h

Copy file name to clipboardExpand all lines: clip.h
+11-2Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#define CLIP_H
33

44
#include "ggml/ggml.h"
5+
#include <stdint.h>
6+
#include <stddef.h>
57

68
struct clip_ctx;
79

@@ -73,8 +75,15 @@ struct clip_image_f32_batch {
7375

7476
bool clip_tokenize(const struct clip_ctx * ctx, const char * text, struct clip_tokens * tokens);
7577

76-
struct clip_image_u8 * make_clip_image_u8();
77-
struct clip_image_f32 * make_clip_image_f32();
78+
struct clip_image_u8 * clip_image_u8_make();
79+
struct clip_image_f32 * clip_image_f32_make();
80+
81+
void clip_image_u8_clean(struct clip_image_u8 * img);
82+
void clip_image_f32_clean(struct clip_image_f32 * res);
83+
84+
void clip_image_u8_free(struct clip_image_u8 * img);
85+
void clip_image_f32_free(struct clip_image_f32 * res);
86+
7887
bool clip_image_load_from_file(const char * fname, struct clip_image_u8 * img);
7988
bool clip_image_preprocess(const struct clip_ctx * ctx, const struct clip_image_u8 * img, struct clip_image_f32 * res);
8089

‎examples/common-clip.cpp

Copy file name to clipboardExpand all lines: examples/common-clip.cpp
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,15 @@ int writeNpyFile(const char * filename, const float * data, const int * shape, i
262262
}
263263

264264
// Constructor-like function
265-
struct clip_image_u8_batch make_clip_image_u8_batch(std::vector<clip_image_u8> & images) {
265+
struct clip_image_u8_batch clip_image_u8_batch_make(std::vector<clip_image_u8> & images) {
266266
struct clip_image_u8_batch batch;
267267
batch.data = images.data();
268268
batch.size = images.size();
269269
return batch;
270270
}
271271

272272
// Constructor-like function
273-
struct clip_image_f32_batch make_clip_image_f32_batch(std::vector<clip_image_f32> & images) {
273+
struct clip_image_f32_batch clip_image_f32_batch_make(std::vector<clip_image_f32> & images) {
274274
struct clip_image_f32_batch batch;
275275
batch.data = images.data();
276276
batch.size = images.size();

‎examples/common-clip.h

Copy file name to clipboardExpand all lines: examples/common-clip.h
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ int writeNpyFile(const char * filename, const float * data, const int * shape, i
4242
void write_floats_to_file(float * array, int size, char * filename);
4343

4444
// constructor-like functions
45-
struct clip_image_u8_batch make_clip_image_u8_batch(std::vector<clip_image_u8> & images);
46-
struct clip_image_f32_batch make_clip_image_f32_batch(std::vector<clip_image_f32> & images);
45+
struct clip_image_u8_batch clip_image_u8_batch_make(std::vector<clip_image_u8> & images);
46+
struct clip_image_f32_batch clip_image_f32_batch_make(std::vector<clip_image_f32> & images);
4747

4848
// #ifdef __cplusplus
4949
// }

‎examples/image-search/build.cpp

Copy file name to clipboardExpand all lines: examples/image-search/build.cpp
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ int main(int argc, char ** argv) {
169169
fflush(stdout);
170170
}
171171

172-
auto img_inputs_batch = make_clip_image_u8_batch(img_inputs);
173-
auto imgs_resized_batch = make_clip_image_f32_batch(imgs_resized);
172+
auto img_inputs_batch = clip_image_u8_batch_make(img_inputs);
173+
auto imgs_resized_batch = clip_image_f32_batch_make(imgs_resized);
174174

175175
clip_image_batch_preprocess(clip_ctx, params.n_threads, &img_inputs_batch, &imgs_resized_batch);
176176
clip_image_batch_encode(clip_ctx, params.n_threads, &imgs_resized_batch, vec.data(), true);

‎examples/simple.c

Copy file name to clipboardExpand all lines: examples/simple.c
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ int main() {
2121
int vec_dim = clip_get_vision_hparams(ctx)->projection_dim;
2222

2323
// Load image from disk
24-
struct clip_image_u8 * img0 = make_clip_image_u8();
24+
struct clip_image_u8 * img0 = clip_image_u8_make();
2525
if (!clip_image_load_from_file(img_path, img0)) {
2626
fprintf(stderr, "%s: failed to load image from '%s'\n", __func__, img_path);
2727
return 1;
2828
}
2929

3030
// Preprocess image
31-
struct clip_image_f32 * img_res = make_clip_image_f32();
31+
struct clip_image_f32 * img_res = clip_image_f32_make();
3232
if (!clip_image_preprocess(ctx, img0, img_res)) {
3333
fprintf(stderr, "%s: failed to preprocess image\n", __func__);
3434
return 1;

‎tests/benchmark.cpp

Copy file name to clipboardExpand all lines: tests/benchmark.cpp
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ int main(int argc, char ** argv) {
110110
}
111111
}
112112

113-
auto img_inputs_batch = make_clip_image_u8_batch(img_inputs);
114-
auto imgs_resized_batch = make_clip_image_f32_batch(imgs_resized);
113+
auto img_inputs_batch = clip_image_u8_batch_make(img_inputs);
114+
auto imgs_resized_batch = clip_image_f32_batch_make(imgs_resized);
115115

116116
clip_image_batch_preprocess(ctx, n_threads, &img_inputs_batch, &imgs_resized_batch);
117117

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.