MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / get_best_grid

Method get_best_grid

subprojects/llama.cpp/tools/mtmd/clip.cpp:2777–2807  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2775 }
2776
2777 static clip_image_size get_best_grid(const int max_slice_nums, const int multiple, const float log_ratio) {
2778 std::vector<int> candidate_split_grids_nums;
2779 for (int i : {multiple - 1, multiple, multiple + 1}) {
2780 if (i == 1 || i > max_slice_nums) {
2781 continue;
2782 }
2783 candidate_split_grids_nums.push_back(i);
2784 }
2785
2786 std::vector<clip_image_size> candidate_grids;
2787 for (int split_grids_nums : candidate_split_grids_nums) {
2788 int m = 1;
2789 while (m <= split_grids_nums) {
2790 if (split_grids_nums % m == 0) {
2791 candidate_grids.push_back(clip_image_size{m, split_grids_nums / m});
2792 }
2793 ++m;
2794 }
2795 }
2796
2797 clip_image_size best_grid{1, 1};
2798 float min_error = std::numeric_limits<float>::infinity();
2799 for (const auto& grid : candidate_grids) {
2800 float error = std::abs(log_ratio - std::log(1.0 * grid.width / grid.height));
2801 if (error < min_error) {
2802 best_grid = grid;
2803 min_error = error;
2804 }
2805 }
2806 return best_grid;
2807 }
2808};
2809
2810// returns the normalized float tensor for llava-1.5, for spatial_unpad with anyres processing for llava-1.6 it returns the normalized image patch tensors as a vector

Callers

nothing calls this directly

Calls 2

logFunction · 0.85
push_backMethod · 0.45

Tested by

no test coverage detected