From c7a8590899269ea6473f25f835e3372abf2bbfae Mon Sep 17 00:00:00 2001 From: ylfeng Date: Wed, 18 Sep 2024 05:53:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=83=8C=E5=8C=85=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E5=8F=AF=E8=83=BD=E4=BA=A7=E7=94=9F=E7=9A=84=E6=AD=BB?= =?UTF-8?q?=E5=BE=AA=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/llamafactory/data/processors/processor_utils.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/llamafactory/data/processors/processor_utils.py b/src/llamafactory/data/processors/processor_utils.py index 8e13d100bc..b7297df34f 100644 --- a/src/llamafactory/data/processors/processor_utils.py +++ b/src/llamafactory/data/processors/processor_utils.py @@ -28,6 +28,8 @@ def greedy_knapsack(numbers: List[int], capacity: int) -> List[List[int]]: r""" An efficient greedy algorithm with binary search for the knapsack problem. """ + # filter out numbers that are larger than the capacity + numbers = [number for number in numbers if number <= capacity] numbers.sort() # sort numbers in ascending order for binary search knapsacks = [] @@ -43,6 +45,10 @@ def greedy_knapsack(numbers: List[int], capacity: int) -> List[List[int]]: remaining_capacity -= numbers[index] # update the remaining capacity current_knapsack.append(numbers.pop(index)) # add the number to knapsack + # avoid endless loop + if remaining_capacity == capacity: + break + knapsacks.append(current_knapsack) return knapsacks