Skip to content

[Bug] in getResizedImageSize #81

Closed

Description

@lix19937

Describe the bug

Wrong implementation of c++ function roundByFactor (https://github.com/NVIDIA/TensorRT-Edge-LLM/blob/release/0.7.0/cpp/multimodal/qwenViTRunner.cpp#L373-L375), which refer round_by_factor in https://github.com/QwenLM/Qwen3-VL/blob/main/qwen-vl-utils/src/qwen_vl_utils/vision_process.py#L41-L43

GT

https://github.com/QwenLM/Qwen3-VL/blob/main/qwen-vl-utils/src/qwen_vl_utils/vision_process.py#L41-L43

def round_by_factor(number: int, factor: int) -> int:
    """Returns the closest integer to 'number' that is divisible by 'factor'."""
    return round(number / factor) * factor
    
factor=32

h_bar = max(factor, round_by_factor(720, factor))
print(h_bar) 
 

Impl of v0.7.0

https://github.com/NVIDIA/TensorRT-Edge-LLM/blob/release/0.7.0/cpp/multimodal/qwenViTRunner.cpp#L373-L375

Modified impl

auto my_round_by_factor = [](int64_t value, int64_t factor) -> int64_t {
  if (factor == 0) return int64_t(0); 

  double div = static_cast<double>(value) / factor;
  double rounded = std::nearbyint(div);    
  return static_cast<int64_t>(rounded) * factor;
};

Test

#include <stdio.h>
#include <cmath>

int64_t factor = 32;

int main(){
  auto roundByFactor = [](int64_t value, int64_t factor) -> int64_t {
    return std::round(static_cast<double>(value) / factor) * factor;
  };

  auto a = roundByFactor(720, factor);
  printf("%d\n", a);
  
  //////////////////////////////////////////////////////////////////////
  auto my_round_by_factor = [](int64_t value, int64_t factor) -> int64_t {
    if (factor == 0) return int64_t(0);     
    double div = static_cast<double>(value) / factor;
    double rounded = std::nearbyint(div);    
    return static_cast<int64_t>(rounded) * factor;
  };
  
  auto b = my_round_by_factor(720, factor);
  printf("%d\n", b);
  
  return 0;
}

Activity

  1. nvamberl commented on May 13, 2026

    @nvamberl

    Thanks @lix19937 for the detailed report and reproducer — confirmed. std::round rounds half-away-from-zero while Python 3's round uses banker's rounding (round-half-to-even), so the C++ and HF outputs diverge on any .5 boundary (e.g. 720 / 32 → 736 in C++ vs 704 in Python).

    A fix has been merged internally and will ship in the next release. We're using an integer banker's-round implementation instead of std::nearbyint to avoid any dependency on the global FP rounding mode, but the end behavior matches Python round(v/f)*f exactly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions