From 5a6781d7e169c12a0543660ce21a455d9bffbb72 Mon Sep 17 00:00:00 2001 From: lonexreb Date: Fri, 10 Jul 2026 18:46:16 -0500 Subject: [PATCH] [None][fix] avoid UnboundLocalError in CnnDailymail.compute_score When outputs[0].outputs is empty the beam loop never executes, so 'return rouge1' referenced an unbound variable. Initialize rouge1 = 0.0 before the loop. Signed-off-by: lonexreb --- tensorrt_llm/evaluate/cnn_dailymail.py | 1 + .../others/test_cnn_dailymail_score.py | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 tests/unittest/others/test_cnn_dailymail_score.py diff --git a/tensorrt_llm/evaluate/cnn_dailymail.py b/tensorrt_llm/evaluate/cnn_dailymail.py index f46cf65cc4c3..b39944c9f77a 100644 --- a/tensorrt_llm/evaluate/cnn_dailymail.py +++ b/tensorrt_llm/evaluate/cnn_dailymail.py @@ -65,6 +65,7 @@ def generate_samples(self) -> Iterable[tuple]: def compute_score(self, outputs: List[RequestOutput], references: List[str]) -> float: + rouge1 = 0.0 for beam_idx in range(len(outputs[0].outputs)): metrics = self.rouge.compute( predictions=[output.outputs[0].text for output in outputs], diff --git a/tests/unittest/others/test_cnn_dailymail_score.py b/tests/unittest/others/test_cnn_dailymail_score.py new file mode 100644 index 000000000000..95916fa4e030 --- /dev/null +++ b/tests/unittest/others/test_cnn_dailymail_score.py @@ -0,0 +1,26 @@ +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from types import SimpleNamespace + +from tensorrt_llm.evaluate.cnn_dailymail import CnnDailymail + + +def test_compute_score_with_no_beams_returns_zero() -> None: + # When outputs[0].outputs is empty the beam loop never runs; rouge1 was + # then returned while unbound, raising UnboundLocalError. It should now + # return the 0.0 default. + evaluator = CnnDailymail.__new__(CnnDailymail) # skip heavy __init__ + empty_request = SimpleNamespace(outputs=[]) + assert evaluator.compute_score([empty_request], references=[]) == 0.0