-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added speed task #70
Merged
Merged
Added speed task #70
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import logging | ||
import platform | ||
import time | ||
from datetime import datetime | ||
from pathlib import Path | ||
from typing import Optional, Union | ||
|
||
import numpy as np | ||
import psutil | ||
import torch | ||
|
||
from seb.interfaces.model import EmbeddingModel | ||
from seb.interfaces.task import Task | ||
from seb.result_dataclasses import TaskResult | ||
from seb.types import DescriptiveDatasetStats, languages_in_seb | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CPUSpeedTask(Task): | ||
reference = "NA" | ||
version = "0.0.1" | ||
task_type = "Speed" | ||
languages = languages_in_seb | ||
main_score = "Inference speed (seconds)" | ||
domain = ["fiction"] # noqa | ||
name = "Speed (CPU)" | ||
description = "Time taken to encode the text 'The Ugly Duckling' split by paragraphs on a CPU." | ||
device = "cpu" | ||
_dataset: Optional[list[str]] = None | ||
|
||
def load_dataset(self) -> list[str]: | ||
file_path = Path(__file__).parent / "the_ugly_duckling.txt" | ||
with file_path.open("r") as f: | ||
text = f.read() | ||
return text.split("\n\n") | ||
|
||
@property | ||
def dataset(self) -> list[str]: | ||
if self._dataset is None: | ||
self._dataset = self.load_dataset() | ||
return self._dataset | ||
|
||
def get_descriptive_stats(self) -> DescriptiveDatasetStats: | ||
dataset = self.load_dataset() | ||
lengths = np.array([len(x) for x in dataset]) | ||
return DescriptiveDatasetStats( | ||
mean_document_length=float(np.mean(lengths)), std_document_length=float(np.std(lengths)), num_documents=len(dataset) | ||
) | ||
|
||
def get_time_taken(self, model: EmbeddingModel) -> float: | ||
dataset = self.load_dataset() | ||
start = time.time() | ||
with torch.no_grad(): | ||
model.encode(dataset, batch_size=1, device=self.device, task=self) | ||
KennethEnevoldsen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
time_taken = time.time() - start | ||
return time_taken | ||
|
||
def evaluate(self, model: EmbeddingModel) -> TaskResult: | ||
model.loader() # ensure model is loaded | ||
|
||
has_to_method = callable(getattr(model._model, "to", None)) | ||
KennethEnevoldsen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if has_to_method: | ||
model = model.to(self.device) # type: ignore | ||
|
||
run_inference = not (self.device == "cuda" and not has_to_method) | ||
if run_inference: | ||
time_taken = self.get_time_taken(model) | ||
else: | ||
logger.warn(f"Could not run inference on {model.meta.name} on {self.device} as it does not have a 'to' method. Skipping") | ||
time_taken = np.nan | ||
|
||
scores: dict[str, Union[str, float]] = {self.main_score: time_taken, **self.get_system_info()} | ||
|
||
return TaskResult( | ||
task_name=self.name, | ||
task_description=self.description, | ||
task_version=self.version, | ||
scores={Language: scores for Language in self.languages}, | ||
time_of_run=datetime.now(), | ||
main_score=self.main_score, | ||
) | ||
|
||
def get_system_info(self) -> dict[str, str]: | ||
""" | ||
Returns a dictionary with system information. | ||
""" | ||
info = {} | ||
info["platform"] = platform.system() | ||
info["platform-release"] = platform.release() | ||
info["platform-version"] = platform.version() | ||
info["architecture"] = platform.machine() | ||
info["processor"] = platform.processor() | ||
info["ram"] = str(round(psutil.virtual_memory().total / (1024.0**3))) + " GB" | ||
info["Physical cores"] = psutil.cpu_count(logical=False) | ||
info["Total cores"] = psutil.cpu_count(logical=True) | ||
return info | ||
|
||
|
||
class GPUSpeedTask(CPUSpeedTask): | ||
name = "Speed (GPU)" | ||
description = "Time taken to encode the text 'The Ugly Duckling' split by paragraphs on a GPU." | ||
device: str = "cuda" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this to appease the type checker?