From e97302737e770333aaa2456cfea00d1623cb9c75 Mon Sep 17 00:00:00 2001 From: kcz358 Date: Sun, 1 Sep 2024 08:20:21 +0000 Subject: [PATCH] Lint --- lmms_eval/models/gpt4v.py | 4 +- lmms_eval/models/mantis.py | 22 ++++---- lmms_eval/models/model_utils/load_video.py | 12 ++--- lmms_eval/tasks/mirb/utils.py | 8 +-- lmms_eval/tasks/refcoco+/utils_rec.py | 52 +++++++++---------- lmms_eval/tasks/refcoco/utils_rec.py | 52 +++++++++---------- lmms_eval/tasks/refcocog/utils_rec.py | 52 +++++++++---------- tools/lite/embed.py | 15 ++++-- tools/lite/embedder/BaseEmbedder.py | 15 ++++-- tools/lite/embedder/ClipBgeEmbedder.py | 19 ++++--- tools/lite/shrink.py | 13 +++-- tools/lite/shrinker/BaseShrinker.py | 29 ++++++----- tools/lite/shrinker/EmbedShrinker.py | 14 ++--- .../sampling_methods/kcenter_greedy.py | 1 - .../shrinker/sampling_methods/sampling_def.py | 1 + 15 files changed, 162 insertions(+), 147 deletions(-) diff --git a/lmms_eval/models/gpt4v.py b/lmms_eval/models/gpt4v.py index 2eebaf3c..aac62a87 100755 --- a/lmms_eval/models/gpt4v.py +++ b/lmms_eval/models/gpt4v.py @@ -109,11 +109,11 @@ def encode_video(self, video_path, for_get_frames_num): vr = VideoReader(video_path, ctx=cpu(0)) total_frame_num = len(vr) uniform_sampled_frames = np.linspace(0, total_frame_num - 1, for_get_frames_num, dtype=int) - + # Ensure the last frame is included if total_frame_num - 1 not in uniform_sampled_frames: uniform_sampled_frames = np.append(uniform_sampled_frames, total_frame_num - 1) - + frame_idx = uniform_sampled_frames.tolist() frames = vr.get_batch(frame_idx).asnumpy() diff --git a/lmms_eval/models/mantis.py b/lmms_eval/models/mantis.py index 7b0a1569..dc3b55f0 100644 --- a/lmms_eval/models/mantis.py +++ b/lmms_eval/models/mantis.py @@ -4,8 +4,15 @@ import copy -from tqdm import tqdm +import warnings from datetime import timedelta +from typing import List, Optional, Tuple, Union + +from accelerate import Accelerator, DistributedType, InitProcessGroupKwargs +from accelerate.state import AcceleratorState +from loguru import logger as eval_logger +from packaging import version +from tqdm import tqdm from lmms_eval import utils from lmms_eval.api.instance import Instance @@ -13,20 +20,13 @@ from lmms_eval.api.registry import register_model from lmms_eval.utils import stop_sequences_criteria -from accelerate import Accelerator, DistributedType, InitProcessGroupKwargs -from accelerate.state import AcceleratorState -from typing import List, Optional, Union, Tuple -from packaging import version -import warnings - -from loguru import logger as eval_logger - warnings.filterwarnings("ignore") try: - from mantis.models.mllava import LlavaForConditionalGeneration, MLlavaProcessor + from mantis.models.conversation import conv_mllava_v1 as default_conv + from mantis.models.conversation import conv_templates from mantis.models.mfuyu import MFuyuForCausalLM, MFuyuProcessor - from mantis.models.conversation import conv_mllava_v1 as default_conv, conv_templates + from mantis.models.mllava import LlavaForConditionalGeneration, MLlavaProcessor except Exception as e: eval_logger.debug("Mantis is not installed. Please install Mantis to use this model.\nError: %s" % e) diff --git a/lmms_eval/models/model_utils/load_video.py b/lmms_eval/models/model_utils/load_video.py index 5bf9b56a..0c4ea23d 100644 --- a/lmms_eval/models/model_utils/load_video.py +++ b/lmms_eval/models/model_utils/load_video.py @@ -38,11 +38,11 @@ def read_video_pyav(video_path, num_frm=8): total_frames = container.streams.video[0].frames sampled_frm = min(total_frames, num_frm) indices = np.linspace(0, total_frames - 1, sampled_frm, dtype=int) - + # Append the last frame index if not already included if total_frames - 1 not in indices: indices = np.append(indices, total_frames - 1) - + frames = record_video_length_stream(container, indices) except: container = av.open(video_path) @@ -50,11 +50,11 @@ def read_video_pyav(video_path, num_frm=8): total_frames = len(frames) sampled_frm = min(total_frames, num_frm) indices = np.linspace(0, total_frames - 1, sampled_frm, dtype=int) - + # Append the last frame index if not already included if total_frames - 1 not in indices: indices = np.append(indices, total_frames - 1) - + frames = [frames[i] for i in indices] else: container = av.open(video_path) @@ -62,10 +62,10 @@ def read_video_pyav(video_path, num_frm=8): total_frames = len(frames) sampled_frm = min(total_frames, num_frm) indices = np.linspace(0, total_frames - 1, sampled_frm, dtype=int) - + # Append the last frame index if not already included if total_frames - 1 not in indices: indices = np.append(indices, total_frames - 1) - + frames = [frames[i] for i in indices] return np.stack([x.to_ndarray(format="rgb24") for x in frames]) diff --git a/lmms_eval/tasks/mirb/utils.py b/lmms_eval/tasks/mirb/utils.py index 3e675d39..bf52ea8f 100644 --- a/lmms_eval/tasks/mirb/utils.py +++ b/lmms_eval/tasks/mirb/utils.py @@ -1,10 +1,10 @@ -from lmms_eval.filters.extraction import ExtendedRegexFilter -from lmms_eval.filters.transformation import MapFilter +import logging import re -import numpy as np +import numpy as np -import logging +from lmms_eval.filters.extraction import ExtendedRegexFilter +from lmms_eval.filters.transformation import MapFilter eval_logger = logging.getLogger("lmms-eval") diff --git a/lmms_eval/tasks/refcoco+/utils_rec.py b/lmms_eval/tasks/refcoco+/utils_rec.py index ec91bf9c..c2df9ba7 100644 --- a/lmms_eval/tasks/refcoco+/utils_rec.py +++ b/lmms_eval/tasks/refcoco+/utils_rec.py @@ -1,5 +1,6 @@ -import re import logging +import re + from datasets import Dataset eval_logger = logging.getLogger("lmms-eval") @@ -14,22 +15,17 @@ def refcoco_bbox_rec_preprocess_dataset(dataset: Dataset): # Original bbox format (top x, top y, width, height) # Convert to (top-left x, top-left y, bottom-right x, bottom-right y) - # Normalize the bounding box coordinates to be between 0 and 1 + # Normalize the bounding box coordinates to be between 0 and 1 # using the image width and height - dataset = dataset.map( - lambda x: {"bbox": [x["bbox"][0] / x["image_width"], - x["bbox"][1] / x["image_height"], - (x["bbox"][0] + x["bbox"][2]) / x["image_width"], - (x["bbox"][1] + x["bbox"][3]) / x["image_height"]]} - ) + dataset = dataset.map(lambda x: {"bbox": [x["bbox"][0] / x["image_width"], x["bbox"][1] / x["image_height"], (x["bbox"][0] + x["bbox"][2]) / x["image_width"], (x["bbox"][1] + x["bbox"][3]) / x["image_height"]]}) # currently, the dataset has `answer` as a list of strings # each answer should be its own row # we will explode the dataset to have one row per answer # duplicate the other columns def explode_answers(example): - answers = example.pop('answer') - return [{'answer': answer, **example} for answer in answers] + answers = example.pop("answer") + return [{"answer": answer, **example} for answer in answers] # Apply the function to each element, collecting the results exploded_rows = [] @@ -50,8 +46,11 @@ def refcoco_bbox_rec_doc_to_visual(doc): def refcoco_bbox_rec_doc_to_text(doc): - assert isinstance(doc['answer'], str), "Answer must be a string" - return "Bounding box coordinates are specified in the format (top-left x, top-left y, bottom-right x, bottom-right y). All values are floating point numbers bounded between 0 and 1. Please provide the bounding box coordinate of the region this sentence describes: " + doc['answer'] + assert isinstance(doc["answer"], str), "Answer must be a string" + return ( + "Bounding box coordinates are specified in the format (top-left x, top-left y, bottom-right x, bottom-right y). All values are floating point numbers bounded between 0 and 1. Please provide the bounding box coordinate of the region this sentence describes: " + + doc["answer"] + ) def parse_float_sequence_within(input_str): @@ -65,15 +64,15 @@ def parse_float_sequence_within(input_str): list: A list of four floats if the pattern is found, or a list of four zeros if the pattern is not found. """ # Define the regex pattern to find the first instance of four floats within square brackets - pattern = r'\[\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)\s*\]' - + pattern = r"\[\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)\s*\]" + # Use re.search to find the first match of the pattern in the input string match = re.search(pattern, input_str) - + # If a match is found, convert the captured groups into a list of floats if match: return [float(match.group(i)) for i in range(1, 5)] - + # If the input does not contain the pattern, return the null float sequence return [0, 0, 0, 0] @@ -89,7 +88,7 @@ def refcoco_bbox_rec_process_result(doc, result): pred = result[0] if len(result) > 0 else "" pred = parse_float_sequence_within(pred) ann_id = doc["question_id"] - data_dict = {"answer": doc["answer"], "pred": pred, "ann_id": ann_id, 'bbox': doc['bbox']} + data_dict = {"answer": doc["answer"], "pred": pred, "ann_id": ann_id, "bbox": doc["bbox"]} return {f"refcoco_{metric}": data_dict for metric in COCO_REC_METRICS} @@ -173,19 +172,19 @@ def refcoco_bbox_rec_aggregation_result(results, metric): - dict: Dictionary containing the aggregated results for the specified metric. """ scorers = { - 'IoU': compute_iou, - 'ACC@0.1': lambda x, y: compute_accuracy(x, y, 0.1), - 'ACC@0.3': lambda x, y: compute_accuracy(x, y, 0.3), - 'ACC@0.5': lambda x, y: compute_accuracy(x, y, 0.5), - 'ACC@0.7': lambda x, y: compute_accuracy(x, y, 0.7), - 'ACC@0.9': lambda x, y: compute_accuracy(x, y, 0.9), - 'Center_ACC': compute_center_accuracy + "IoU": compute_iou, + "ACC@0.1": lambda x, y: compute_accuracy(x, y, 0.1), + "ACC@0.3": lambda x, y: compute_accuracy(x, y, 0.3), + "ACC@0.5": lambda x, y: compute_accuracy(x, y, 0.5), + "ACC@0.7": lambda x, y: compute_accuracy(x, y, 0.7), + "ACC@0.9": lambda x, y: compute_accuracy(x, y, 0.9), + "Center_ACC": compute_center_accuracy, } results_dict = {metric: []} for result in results: # Extract the ground truth and predicted bounding boxes - gt_bbox = result['bbox'] - pred_bbox = result['pred'] + gt_bbox = result["bbox"] + pred_bbox = result["pred"] # Compute the specified metric between the ground truth and predicted bounding boxes score = scorers[metric](gt_bbox, pred_bbox) results_dict[metric].append(score) @@ -201,6 +200,7 @@ def refcoco_bbox_rec_iou(results): def refcoco_bbox_rec_acc01(results): return refcoco_bbox_rec_aggregation_result(results, "ACC@0.1") + def refcoco_bbox_rec_acc03(results): return refcoco_bbox_rec_aggregation_result(results, "ACC@0.3") diff --git a/lmms_eval/tasks/refcoco/utils_rec.py b/lmms_eval/tasks/refcoco/utils_rec.py index ec91bf9c..c2df9ba7 100644 --- a/lmms_eval/tasks/refcoco/utils_rec.py +++ b/lmms_eval/tasks/refcoco/utils_rec.py @@ -1,5 +1,6 @@ -import re import logging +import re + from datasets import Dataset eval_logger = logging.getLogger("lmms-eval") @@ -14,22 +15,17 @@ def refcoco_bbox_rec_preprocess_dataset(dataset: Dataset): # Original bbox format (top x, top y, width, height) # Convert to (top-left x, top-left y, bottom-right x, bottom-right y) - # Normalize the bounding box coordinates to be between 0 and 1 + # Normalize the bounding box coordinates to be between 0 and 1 # using the image width and height - dataset = dataset.map( - lambda x: {"bbox": [x["bbox"][0] / x["image_width"], - x["bbox"][1] / x["image_height"], - (x["bbox"][0] + x["bbox"][2]) / x["image_width"], - (x["bbox"][1] + x["bbox"][3]) / x["image_height"]]} - ) + dataset = dataset.map(lambda x: {"bbox": [x["bbox"][0] / x["image_width"], x["bbox"][1] / x["image_height"], (x["bbox"][0] + x["bbox"][2]) / x["image_width"], (x["bbox"][1] + x["bbox"][3]) / x["image_height"]]}) # currently, the dataset has `answer` as a list of strings # each answer should be its own row # we will explode the dataset to have one row per answer # duplicate the other columns def explode_answers(example): - answers = example.pop('answer') - return [{'answer': answer, **example} for answer in answers] + answers = example.pop("answer") + return [{"answer": answer, **example} for answer in answers] # Apply the function to each element, collecting the results exploded_rows = [] @@ -50,8 +46,11 @@ def refcoco_bbox_rec_doc_to_visual(doc): def refcoco_bbox_rec_doc_to_text(doc): - assert isinstance(doc['answer'], str), "Answer must be a string" - return "Bounding box coordinates are specified in the format (top-left x, top-left y, bottom-right x, bottom-right y). All values are floating point numbers bounded between 0 and 1. Please provide the bounding box coordinate of the region this sentence describes: " + doc['answer'] + assert isinstance(doc["answer"], str), "Answer must be a string" + return ( + "Bounding box coordinates are specified in the format (top-left x, top-left y, bottom-right x, bottom-right y). All values are floating point numbers bounded between 0 and 1. Please provide the bounding box coordinate of the region this sentence describes: " + + doc["answer"] + ) def parse_float_sequence_within(input_str): @@ -65,15 +64,15 @@ def parse_float_sequence_within(input_str): list: A list of four floats if the pattern is found, or a list of four zeros if the pattern is not found. """ # Define the regex pattern to find the first instance of four floats within square brackets - pattern = r'\[\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)\s*\]' - + pattern = r"\[\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)\s*\]" + # Use re.search to find the first match of the pattern in the input string match = re.search(pattern, input_str) - + # If a match is found, convert the captured groups into a list of floats if match: return [float(match.group(i)) for i in range(1, 5)] - + # If the input does not contain the pattern, return the null float sequence return [0, 0, 0, 0] @@ -89,7 +88,7 @@ def refcoco_bbox_rec_process_result(doc, result): pred = result[0] if len(result) > 0 else "" pred = parse_float_sequence_within(pred) ann_id = doc["question_id"] - data_dict = {"answer": doc["answer"], "pred": pred, "ann_id": ann_id, 'bbox': doc['bbox']} + data_dict = {"answer": doc["answer"], "pred": pred, "ann_id": ann_id, "bbox": doc["bbox"]} return {f"refcoco_{metric}": data_dict for metric in COCO_REC_METRICS} @@ -173,19 +172,19 @@ def refcoco_bbox_rec_aggregation_result(results, metric): - dict: Dictionary containing the aggregated results for the specified metric. """ scorers = { - 'IoU': compute_iou, - 'ACC@0.1': lambda x, y: compute_accuracy(x, y, 0.1), - 'ACC@0.3': lambda x, y: compute_accuracy(x, y, 0.3), - 'ACC@0.5': lambda x, y: compute_accuracy(x, y, 0.5), - 'ACC@0.7': lambda x, y: compute_accuracy(x, y, 0.7), - 'ACC@0.9': lambda x, y: compute_accuracy(x, y, 0.9), - 'Center_ACC': compute_center_accuracy + "IoU": compute_iou, + "ACC@0.1": lambda x, y: compute_accuracy(x, y, 0.1), + "ACC@0.3": lambda x, y: compute_accuracy(x, y, 0.3), + "ACC@0.5": lambda x, y: compute_accuracy(x, y, 0.5), + "ACC@0.7": lambda x, y: compute_accuracy(x, y, 0.7), + "ACC@0.9": lambda x, y: compute_accuracy(x, y, 0.9), + "Center_ACC": compute_center_accuracy, } results_dict = {metric: []} for result in results: # Extract the ground truth and predicted bounding boxes - gt_bbox = result['bbox'] - pred_bbox = result['pred'] + gt_bbox = result["bbox"] + pred_bbox = result["pred"] # Compute the specified metric between the ground truth and predicted bounding boxes score = scorers[metric](gt_bbox, pred_bbox) results_dict[metric].append(score) @@ -201,6 +200,7 @@ def refcoco_bbox_rec_iou(results): def refcoco_bbox_rec_acc01(results): return refcoco_bbox_rec_aggregation_result(results, "ACC@0.1") + def refcoco_bbox_rec_acc03(results): return refcoco_bbox_rec_aggregation_result(results, "ACC@0.3") diff --git a/lmms_eval/tasks/refcocog/utils_rec.py b/lmms_eval/tasks/refcocog/utils_rec.py index ec91bf9c..c2df9ba7 100644 --- a/lmms_eval/tasks/refcocog/utils_rec.py +++ b/lmms_eval/tasks/refcocog/utils_rec.py @@ -1,5 +1,6 @@ -import re import logging +import re + from datasets import Dataset eval_logger = logging.getLogger("lmms-eval") @@ -14,22 +15,17 @@ def refcoco_bbox_rec_preprocess_dataset(dataset: Dataset): # Original bbox format (top x, top y, width, height) # Convert to (top-left x, top-left y, bottom-right x, bottom-right y) - # Normalize the bounding box coordinates to be between 0 and 1 + # Normalize the bounding box coordinates to be between 0 and 1 # using the image width and height - dataset = dataset.map( - lambda x: {"bbox": [x["bbox"][0] / x["image_width"], - x["bbox"][1] / x["image_height"], - (x["bbox"][0] + x["bbox"][2]) / x["image_width"], - (x["bbox"][1] + x["bbox"][3]) / x["image_height"]]} - ) + dataset = dataset.map(lambda x: {"bbox": [x["bbox"][0] / x["image_width"], x["bbox"][1] / x["image_height"], (x["bbox"][0] + x["bbox"][2]) / x["image_width"], (x["bbox"][1] + x["bbox"][3]) / x["image_height"]]}) # currently, the dataset has `answer` as a list of strings # each answer should be its own row # we will explode the dataset to have one row per answer # duplicate the other columns def explode_answers(example): - answers = example.pop('answer') - return [{'answer': answer, **example} for answer in answers] + answers = example.pop("answer") + return [{"answer": answer, **example} for answer in answers] # Apply the function to each element, collecting the results exploded_rows = [] @@ -50,8 +46,11 @@ def refcoco_bbox_rec_doc_to_visual(doc): def refcoco_bbox_rec_doc_to_text(doc): - assert isinstance(doc['answer'], str), "Answer must be a string" - return "Bounding box coordinates are specified in the format (top-left x, top-left y, bottom-right x, bottom-right y). All values are floating point numbers bounded between 0 and 1. Please provide the bounding box coordinate of the region this sentence describes: " + doc['answer'] + assert isinstance(doc["answer"], str), "Answer must be a string" + return ( + "Bounding box coordinates are specified in the format (top-left x, top-left y, bottom-right x, bottom-right y). All values are floating point numbers bounded between 0 and 1. Please provide the bounding box coordinate of the region this sentence describes: " + + doc["answer"] + ) def parse_float_sequence_within(input_str): @@ -65,15 +64,15 @@ def parse_float_sequence_within(input_str): list: A list of four floats if the pattern is found, or a list of four zeros if the pattern is not found. """ # Define the regex pattern to find the first instance of four floats within square brackets - pattern = r'\[\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)\s*\]' - + pattern = r"\[\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)\s*\]" + # Use re.search to find the first match of the pattern in the input string match = re.search(pattern, input_str) - + # If a match is found, convert the captured groups into a list of floats if match: return [float(match.group(i)) for i in range(1, 5)] - + # If the input does not contain the pattern, return the null float sequence return [0, 0, 0, 0] @@ -89,7 +88,7 @@ def refcoco_bbox_rec_process_result(doc, result): pred = result[0] if len(result) > 0 else "" pred = parse_float_sequence_within(pred) ann_id = doc["question_id"] - data_dict = {"answer": doc["answer"], "pred": pred, "ann_id": ann_id, 'bbox': doc['bbox']} + data_dict = {"answer": doc["answer"], "pred": pred, "ann_id": ann_id, "bbox": doc["bbox"]} return {f"refcoco_{metric}": data_dict for metric in COCO_REC_METRICS} @@ -173,19 +172,19 @@ def refcoco_bbox_rec_aggregation_result(results, metric): - dict: Dictionary containing the aggregated results for the specified metric. """ scorers = { - 'IoU': compute_iou, - 'ACC@0.1': lambda x, y: compute_accuracy(x, y, 0.1), - 'ACC@0.3': lambda x, y: compute_accuracy(x, y, 0.3), - 'ACC@0.5': lambda x, y: compute_accuracy(x, y, 0.5), - 'ACC@0.7': lambda x, y: compute_accuracy(x, y, 0.7), - 'ACC@0.9': lambda x, y: compute_accuracy(x, y, 0.9), - 'Center_ACC': compute_center_accuracy + "IoU": compute_iou, + "ACC@0.1": lambda x, y: compute_accuracy(x, y, 0.1), + "ACC@0.3": lambda x, y: compute_accuracy(x, y, 0.3), + "ACC@0.5": lambda x, y: compute_accuracy(x, y, 0.5), + "ACC@0.7": lambda x, y: compute_accuracy(x, y, 0.7), + "ACC@0.9": lambda x, y: compute_accuracy(x, y, 0.9), + "Center_ACC": compute_center_accuracy, } results_dict = {metric: []} for result in results: # Extract the ground truth and predicted bounding boxes - gt_bbox = result['bbox'] - pred_bbox = result['pred'] + gt_bbox = result["bbox"] + pred_bbox = result["pred"] # Compute the specified metric between the ground truth and predicted bounding boxes score = scorers[metric](gt_bbox, pred_bbox) results_dict[metric].append(score) @@ -201,6 +200,7 @@ def refcoco_bbox_rec_iou(results): def refcoco_bbox_rec_acc01(results): return refcoco_bbox_rec_aggregation_result(results, "ACC@0.1") + def refcoco_bbox_rec_acc03(results): return refcoco_bbox_rec_aggregation_result(results, "ACC@0.3") diff --git a/tools/lite/embed.py b/tools/lite/embed.py index e8a0c30e..07cb9a9b 100644 --- a/tools/lite/embed.py +++ b/tools/lite/embed.py @@ -1,13 +1,18 @@ -import embedder -from lmms_eval.utils import simple_parse_args_string -from lmms_eval.tasks import initialize_tasks, include_path, get_task_dict, ConfigurableTask -from lmms_eval.api.registry import ALL_TASKS, GROUP_REGISTRY - import argparse import os +import embedder import torch.distributed as dist +from lmms_eval.api.registry import ALL_TASKS, GROUP_REGISTRY +from lmms_eval.tasks import ( + ConfigurableTask, + get_task_dict, + include_path, + initialize_tasks, +) +from lmms_eval.utils import simple_parse_args_string + def rank0_print(*args): if dist.is_initialized(): diff --git a/tools/lite/embedder/BaseEmbedder.py b/tools/lite/embedder/BaseEmbedder.py index 88031fa4..3c75c34a 100644 --- a/tools/lite/embedder/BaseEmbedder.py +++ b/tools/lite/embedder/BaseEmbedder.py @@ -1,12 +1,17 @@ -from abc import ABC, abstractmethod -from typing import List, Optional, Union, Dict import os - -from lmms_eval.tasks import initialize_tasks, include_path, get_task_dict, ConfigurableTask -from lmms_eval.api.registry import ALL_TASKS +from abc import ABC, abstractmethod +from typing import Dict, List, Optional, Union import torch.distributed as dist +from lmms_eval.api.registry import ALL_TASKS +from lmms_eval.tasks import ( + ConfigurableTask, + get_task_dict, + include_path, + initialize_tasks, +) + def rank0_print(*args): if dist.is_initialized(): diff --git a/tools/lite/embedder/ClipBgeEmbedder.py b/tools/lite/embedder/ClipBgeEmbedder.py index 11973df4..5651c199 100644 --- a/tools/lite/embedder/ClipBgeEmbedder.py +++ b/tools/lite/embedder/ClipBgeEmbedder.py @@ -1,21 +1,20 @@ +import json +import os from copy import deepcopy from datetime import timedelta -from typing import Optional, Union, List, Sequence, Dict -import os -import json - -from .BaseEmbedder import BaseEmbedder +from typing import Dict, List, Optional, Sequence, Union +import numpy as np +import torch from accelerate import Accelerator, DistributedType, InitProcessGroupKwargs from accelerate.state import AcceleratorState from accelerate.utils import gather_object - -import numpy as np -from transformers import CLIPModel, CLIPProcessor +from PIL import Image from sentence_transformers import SentenceTransformer from tqdm import tqdm -import torch -from PIL import Image +from transformers import CLIPModel, CLIPProcessor + +from .BaseEmbedder import BaseEmbedder class ClipBgeEmbedder(BaseEmbedder): diff --git a/tools/lite/shrink.py b/tools/lite/shrink.py index 23cba7a4..6f87ad90 100644 --- a/tools/lite/shrink.py +++ b/tools/lite/shrink.py @@ -1,19 +1,18 @@ import argparse +import datetime +import hashlib import importlib -import os -import yaml import json +import os from pathlib import Path -import hashlib +import numpy as np +import shrinker as shrinker_module +import yaml from accelerate import Accelerator from accelerate.utils import InitProcessGroupKwargs -import numpy as np -import datetime from lmms_eval.utils import simple_parse_args_string -import shrinker as shrinker_module - AVAILABEL_SHRINKER = {"embed": "Embed_Shrinker"} diff --git a/tools/lite/shrinker/BaseShrinker.py b/tools/lite/shrinker/BaseShrinker.py index 742650d3..c530b9b6 100644 --- a/tools/lite/shrinker/BaseShrinker.py +++ b/tools/lite/shrinker/BaseShrinker.py @@ -1,29 +1,34 @@ +import json +import logging +import os +import random from abc import ABC, abstractmethod +from distutils.dir_util import copy_tree from glob import glob -import os -import json +from typing import Dict, List, Union + +import matplotlib.pyplot as plt import numpy as np +import torch from numpy.polynomial.polynomial import polyfit -import matplotlib.pyplot as plt from sklearn.metrics import mean_squared_error -from typing import Dict, List, Union -from distutils.dir_util import copy_tree -import random -import torch + import lmms_eval -from lmms_eval.evaluator import evaluate -from lmms_eval.tasks import initialize_tasks, include_path, get_task_dict, ConfigurableTask from lmms_eval.api.registry import ALL_TASKS -import logging +from lmms_eval.evaluator import evaluate +from lmms_eval.tasks import ( + ConfigurableTask, + get_task_dict, + include_path, + initialize_tasks, +) from lmms_eval.utils import simple_parse_args_string - eval_logger = logging.getLogger("lmms-eval") class BaseShrinker(ABC): def __init__(self, task: str, num_items: Union[int, float], name: str, push_to_hub: bool = True) -> None: - super().__init__() self.name = name self.task = task diff --git a/tools/lite/shrinker/EmbedShrinker.py b/tools/lite/shrinker/EmbedShrinker.py index 0d4f892a..dd67a667 100644 --- a/tools/lite/shrinker/EmbedShrinker.py +++ b/tools/lite/shrinker/EmbedShrinker.py @@ -1,12 +1,14 @@ -from .BaseShrinker import BaseShrinker +import os import sys -from .sampling_methods import AVAILABEL_METHODS -from lmms_eval.tasks import initialize_tasks +from typing import Dict, List, Union -import torch -from typing import List, Dict, Union import numpy as np -import os +import torch + +from lmms_eval.tasks import initialize_tasks + +from .BaseShrinker import BaseShrinker +from .sampling_methods import AVAILABEL_METHODS sys.path.append("..") from embedder import BaseEmbedder diff --git a/tools/lite/shrinker/sampling_methods/kcenter_greedy.py b/tools/lite/shrinker/sampling_methods/kcenter_greedy.py index 69d993fa..9ecef91d 100644 --- a/tools/lite/shrinker/sampling_methods/kcenter_greedy.py +++ b/tools/lite/shrinker/sampling_methods/kcenter_greedy.py @@ -2,7 +2,6 @@ from sklearn.metrics import pairwise_distances from tqdm import tqdm - from .sampling_def import SamplingMethod diff --git a/tools/lite/shrinker/sampling_methods/sampling_def.py b/tools/lite/shrinker/sampling_methods/sampling_def.py index b7d8a40c..aecf9c47 100644 --- a/tools/lite/shrinker/sampling_methods/sampling_def.py +++ b/tools/lite/shrinker/sampling_methods/sampling_def.py @@ -20,6 +20,7 @@ """ import abc + import numpy as np