From ae6a4af5f3718475dbea7922f575615d92a8af8a Mon Sep 17 00:00:00 2001 From: kingbri Date: Sat, 25 May 2024 21:13:58 -0400 Subject: [PATCH] Tree: Swap from map to list comprehensions List comprehensions are the more "pythonic" way to approach mapping values to a list. They're also more flexible across different collection types rather than the inbuilt map method. It's best to keep one convention rather than splitting down two. Signed-off-by: kingbri --- backends/exllamav2/model.py | 27 +++++++++++---------------- common/downloader.py | 16 +++++++--------- common/sampling.py | 2 +- endpoints/OAI/router.py | 2 +- 4 files changed, 20 insertions(+), 27 deletions(-) diff --git a/backends/exllamav2/model.py b/backends/exllamav2/model.py index 52bc8905..989c80a9 100644 --- a/backends/exllamav2/model.py +++ b/backends/exllamav2/model.py @@ -134,12 +134,9 @@ def progress(loaded_modules: int, total_modules: int, self.gpu_split_auto = gpu_split_auto autosplit_reserve_megabytes = unwrap(kwargs.get("autosplit_reserve"), [96]) - self.autosplit_reserve = list( - map( - lambda value: int(math.ceil(value * 1024**2)), - autosplit_reserve_megabytes, - ) - ) + self.autosplit_reserve = [ + int(math.ceil(value * 1024**2)) for value in autosplit_reserve_megabytes + ] elif gpu_count > 1: # Manual GPU split self.gpu_split = kwargs.get("gpu_split") @@ -681,21 +678,19 @@ def get_special_tokens( } def get_logprobs(self, token_ids: torch.Tensor, token_probs: torch.Tensor): - top_tokens = list( - map( - lambda index: self.tokenizer.extended_id_to_piece.get( - index, self.tokenizer.id_to_piece[index] - ), - token_ids.flatten().tolist(), + top_tokens = [ + self.tokenizer.extended_id_to_piece.get( + index, self.tokenizer.id_to_piece[index] ) - ) + for index in token_ids.flatten().tolist() + ] top_values = torch.log(token_probs).flatten().tolist() # Cannot return -inf in JSON - cleaned_values = list( - map(lambda value: -1000 if value == float("-inf") else value, top_values) - ) + cleaned_values = [ + -1000 if value == float("-inf") else value for value in top_values + ] return dict(zip_longest(top_tokens, cleaned_values)) diff --git a/common/downloader.py b/common/downloader.py index 941485f6..a0b16cb9 100644 --- a/common/downloader.py +++ b/common/downloader.py @@ -63,15 +63,13 @@ def _get_repo_info(repo_id, revision, token): api_client = HfApi() repo_tree = api_client.list_repo_files(repo_id, revision=revision, token=token) - return list( - map( - lambda filename: { - "filename": filename, - "url": hf_hub_url(repo_id, filename, revision=revision), - }, - repo_tree, - ) - ) + return [ + { + "filename": filename, + "url": hf_hub_url(repo_id, filename, revision=revision), + } + for filename in repo_tree + ] def _get_download_folder(repo_id: str, repo_type: str, folder_name: Optional[str]): diff --git a/common/sampling.py b/common/sampling.py index bee40407..bbeddb80 100644 --- a/common/sampling.py +++ b/common/sampling.py @@ -367,7 +367,7 @@ def get_all_presets(): """Fetches all sampler override presets from the overrides directory""" override_directory = pathlib.Path("sampler_overrides") - preset_files = map(lambda file: file.stem, override_directory.glob("*.yml")) + preset_files = [file.stem for file in override_directory.glob("*.yml")] return preset_files diff --git a/endpoints/OAI/router.py b/endpoints/OAI/router.py index b7c594fd..213f446b 100644 --- a/endpoints/OAI/router.py +++ b/endpoints/OAI/router.py @@ -191,7 +191,7 @@ async def unload_model(): @router.get("/v1/template/list", dependencies=[Depends(check_api_key)]) async def get_templates(): templates = get_all_templates() - template_strings = list(map(lambda template: template.stem, templates)) + template_strings = [template.stem for template in templates] return TemplateList(data=template_strings)