mtmd: fix LFM2 image tiling threshold (#27057)
* mtmd: fix LFM2 image tiling threshold * refactor testing * fix * fix on windows --------- Co-authored-by: Xuan Son Nguyen <son@huggingface.co>
This commit is contained in:
co-authored by
Xuan Son Nguyen
parent
1511ce3bc3
commit
7acdbb1f19
@@ -310,6 +310,9 @@ llama_build_and_test(test-mtmd-c-api.c)
|
||||
target_link_libraries(${LLAMA_TEST_NAME} PRIVATE mtmd)
|
||||
unset(LLAMA_TEST_NAME)
|
||||
|
||||
llama_build_and_test(test-mtmd-impl.cpp)
|
||||
target_link_libraries(test-mtmd-impl PRIVATE mtmd)
|
||||
|
||||
# GGUF model data fetcher library for tests that need real model metadata
|
||||
# Only compile when cpp-httplib has SSL support (CPPHTTPLIB_OPENSSL_SUPPORT)
|
||||
if (TARGET cpp-httplib)
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
#include "testing.h"
|
||||
|
||||
#include "mtmd-image.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
// this test file contains:
|
||||
// 1. test cases for mtmd helpers
|
||||
// 2. test cases for internal mtmd components
|
||||
// internal headers can be included here
|
||||
|
||||
struct test_registry {
|
||||
using fn_t = void (*)(testing &);
|
||||
|
||||
struct entry {
|
||||
std::string name;
|
||||
fn_t fn;
|
||||
};
|
||||
|
||||
static std::vector<entry> & all() {
|
||||
static std::vector<entry> entries;
|
||||
return entries;
|
||||
}
|
||||
|
||||
test_registry(const char * name, fn_t fn) {
|
||||
all().push_back({ name, fn });
|
||||
}
|
||||
};
|
||||
|
||||
#define MAKE_TEST(name) \
|
||||
static void name(testing & t); \
|
||||
static const test_registry test_registry_ ## name(#name, &name); \
|
||||
static void name(testing & t)
|
||||
|
||||
|
||||
//
|
||||
// mtmd_image
|
||||
//
|
||||
|
||||
MAKE_TEST(test_image_preprocessor_lfm2) {
|
||||
clip_hparams hparams;
|
||||
hparams.patch_size = 16;
|
||||
hparams.n_merge = 2;
|
||||
hparams.set_limit_image_tokens(64, 256);
|
||||
|
||||
// { image size, expected tiling }
|
||||
const std::vector<std::pair<clip_image_size, bool>> cases = {
|
||||
{ { 704, 704 }, false },
|
||||
// 720 / (patch_size * n_merge) is exactly 22.5, so this only matches HF
|
||||
// if round_by_factor rounds half to even (22) instead of away from zero (23)
|
||||
{ { 720, 720 }, false },
|
||||
{ { 736, 736 }, true },
|
||||
{ { 1024, 977 }, true },
|
||||
{ { 1056, 384 }, false },
|
||||
};
|
||||
|
||||
for (const auto & [size, expected] : cases) {
|
||||
const bool actual = mtmd_image_preprocessor_lfm2::should_tile(hparams, size);
|
||||
|
||||
t.assert_equal(
|
||||
"tiling for " + std::to_string(size.width) + "x" + std::to_string(size.height),
|
||||
std::string(expected ? "tiled" : "single"),
|
||||
std::string(actual ? "tiled" : "single"));
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// main
|
||||
//
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
testing t(std::cout);
|
||||
t.verbose = true;
|
||||
|
||||
// usage: test-mtmd-impl [filter_regex]
|
||||
for (int i = 1; i < argc; i++) {
|
||||
t.set_filter(argv[i]);
|
||||
}
|
||||
|
||||
for (const auto & e : test_registry::all()) {
|
||||
t.test(e.name, e.fn);
|
||||
}
|
||||
|
||||
return t.summary();
|
||||
}
|
||||
Reference in New Issue
Block a user