-
Notifications
You must be signed in to change notification settings - Fork 11.5k
llava : introduce libmtmd #12849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
llava : introduce libmtmd #12849
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
235340d
wip llava2
ngxson 96bf95e
migrated gemma3 to llava2
ngxson 94564ac
Merge branch 'master' into xsn/llava2
ngxson 7cc4108
add timings
ngxson a9ef623
correct pre/postfix
ngxson 3b25bd9
fix missing include
ngxson 1576c82
fix compilation unused var warn
ngxson 117bf73
update llava2_tokenize
ngxson a6625fa
change name llava2 --> mtmd
ngxson 430dbd8
improve api
ngxson 6ed09b7
refine helpers
ngxson aed3216
Update examples/llava/mtmd.cpp
ngxson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
#include "ggml.h" | ||
#include "gguf.h" | ||
|
||
#include "clip.h" | ||
|
||
#include <climits> | ||
#include <cstdarg> | ||
#include <string> | ||
#include <map> | ||
#include <sstream> | ||
#include <vector> | ||
#include <memory> | ||
|
||
// Internal header for clip.cpp | ||
|
||
|
@@ -120,6 +123,23 @@ static projector_type clip_projector_type_from_string(const std::string & str) { | |
return PROJECTOR_TYPE_UNKNOWN; | ||
} | ||
|
||
// RGB uint8 image | ||
struct clip_image_u8 { | ||
int nx; | ||
int ny; | ||
|
||
std::vector<uint8_t> buf; | ||
}; | ||
|
||
// RGB float32 image (NHWC) | ||
// Memory layout: RGBRGBRGB... | ||
struct clip_image_f32 { | ||
int nx; | ||
int ny; | ||
|
||
std::vector<float> buf; | ||
}; | ||
|
||
// | ||
// logging | ||
// | ||
|
@@ -178,6 +198,28 @@ static void clip_log_internal(enum ggml_log_level level, const char * format, .. | |
#define LOG_DBG(...) LOG_TMPL(GGML_LOG_LEVEL_DEBUG, __VA_ARGS__) | ||
#define LOG_CNT(...) LOG_TMPL(GGML_LOG_LEVEL_CONT, __VA_ARGS__) | ||
|
||
// | ||
// cpp wrappers | ||
// | ||
|
||
struct clip_image_u8_deleter { | ||
void operator()(clip_image_u8 * val) { clip_image_u8_free(val); } | ||
}; | ||
|
||
struct clip_image_f32_deleter { | ||
void operator()(clip_image_f32 * val) { clip_image_f32_free(val); } | ||
}; | ||
|
||
struct clip_image_f32_batch_deleter { | ||
void operator()(clip_image_f32_batch * val) { clip_image_f32_batch_free(val); } | ||
}; | ||
|
||
typedef std::unique_ptr<clip_image_u8, clip_image_u8_deleter> clip_image_u8_ptr; | ||
typedef std::unique_ptr<clip_image_f32, clip_image_f32_deleter> clip_image_f32_ptr; | ||
typedef std::unique_ptr<clip_image_f32_batch, clip_image_f32_batch_deleter> clip_image_f32_batch_ptr; | ||
|
||
// TODO @ngxson : we're currently having a naming clash between struct clip_image_size and function clip_image_size() | ||
Comment on lines
+205
to
+221
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what I was talking about in #12834 (comment) In a follow-up PR, I'll use this inside clip.cpp |
||
|
||
// | ||
// common utils | ||
// | ||
|
@@ -214,6 +256,20 @@ static void string_replace_all(std::string & s, const std::string & search, cons | |
s = std::move(builder); | ||
} | ||
|
||
// split string by a `std::string delim` instead of `char delim` | ||
static std::vector<std::string> string_split_str(std::string s, const std::string & delimiter) { | ||
std::vector<std::string> tokens; | ||
size_t pos = 0; | ||
std::string token; | ||
while ((pos = s.find(delimiter)) != std::string::npos) { | ||
token = s.substr(0, pos); | ||
tokens.push_back(token); | ||
s.erase(0, pos + delimiter.length()); | ||
} | ||
tokens.push_back(s); | ||
return tokens; | ||
} | ||
|
||
// | ||
// gguf utils | ||
// | ||
|
@@ -271,3 +327,9 @@ static std::string gguf_kv_to_str(const struct gguf_context * ctx_gguf, int i) { | |
return gguf_data_to_str(type, gguf_get_val_data(ctx_gguf, i), 0); | ||
} | ||
} | ||
|
||
// | ||
// API used internally with mtmd | ||
// | ||
|
||
projector_type clip_get_projector_type(const struct clip_ctx * ctx); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not really sure what is the need for building both static and shared libs here. Probably something to revisit and simplify.