generated from dataforgoodfr/d4g-project-template
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from dataforgoodfr/feat/model-repository
Add model repository
- Loading branch information
Showing
8 changed files
with
194 additions
and
52 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,31 @@ | ||
from typing import Union | ||
|
||
from pydantic import BaseModel | ||
|
||
ENERGY_PROFILE = 1.17e-4 | ||
GPU_ENERGY_ALPHA = 8.91e-5 | ||
GPU_ENERGY_BETA = 1.43e-3 | ||
|
||
|
||
class Impacts(BaseModel): | ||
energy: float | ||
energy_range: tuple[float, float] | ||
energy_unit: str = "Wh" | ||
|
||
|
||
def compute_llm_impact( | ||
model_parameter_count: float, | ||
model_parameter_count: Union[float, tuple[float, float]], | ||
output_token_count: int, | ||
) -> Impacts: | ||
return Impacts(energy=ENERGY_PROFILE * model_parameter_count * output_token_count) | ||
if isinstance(model_parameter_count, (tuple, list)): | ||
# TODO: check tuple or list validity (min, max) | ||
energy_min = output_token_count * (GPU_ENERGY_ALPHA * model_parameter_count[0] + GPU_ENERGY_BETA) | ||
energy_max = output_token_count * (GPU_ENERGY_ALPHA * model_parameter_count[1] + GPU_ENERGY_BETA) | ||
energy_avg = (energy_min + energy_max) / 2 | ||
else: | ||
energy_avg = output_token_count * (GPU_ENERGY_ALPHA * model_parameter_count + GPU_ENERGY_BETA) | ||
energy_min = energy_avg | ||
energy_max = energy_avg | ||
return Impacts( | ||
energy=energy_avg, | ||
energy_range=(energy_min, energy_max) | ||
) |
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,35 @@ | ||
provider,name,total_parameters,active_parameters,warnings,sources | ||
openai,gpt-3.5-turbo,20;70,20;70,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit | ||
openai,gpt-3.5-turbo-0125,20;70,20;70,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit | ||
openai,gpt-3.5-turbo-0301,20;70,20;70,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit | ||
openai,gpt-3.5-turbo-0613,20;70,20;70,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit | ||
openai,gpt-3.5-turbo-1106,20;70,20;70,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit | ||
openai,gpt-3.5-turbo-16k,20;70,20;70,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit | ||
openai,gpt-3.5-turbo-16k-0613,20;70,20;70,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit | ||
openai,gpt-3.5-turbo-instruct,20;70,20;70,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit | ||
openai,gpt-3.5-turbo-instruct-0914,20;70,20;70,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit | ||
openai,gpt-4,1760,220;880,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit | ||
openai,gpt-4-0125-preview,1760,220;880,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit | ||
openai,gpt-4-0613,1760,220;880,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit | ||
openai,gpt-4-1106-preview,1760,220;880,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit | ||
openai,gpt-4-turbo-preview,1760,220;880,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit | ||
openai,gpt-4-vision-preview,1760,220;880,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit | ||
mistralai,mistral-large-2402,540,135;540,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit?usp=sharing | ||
mistralai,mistral-large-latest,540,135;540,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit?usp=sharing | ||
mistralai,mistral-medium,70;180,45;180,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit?usp=sharing | ||
mistralai,mistral-medium-2312,70;180,45;180,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit?usp=sharing | ||
mistralai,mistral-medium-latest,70;180,45;180,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit?usp=sharing | ||
mistralai,mistral-small,46.7,12.9,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit?usp=sharing | ||
mistralai,mistral-small-2312,46.7,12.9,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit?usp=sharing | ||
mistralai,mistral-small-2402,46.7,12.9,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit?usp=sharing | ||
mistralai,mistral-small-latest,46.7,12.9,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit?usp=sharing | ||
mistralai,mistral-tiny,7.3,7.3,,https://docs.mistral.ai/models/#sizes | ||
mistralai,mistral-tiny-2312,7.3,7.3,,https://docs.mistral.ai/models/#sizes | ||
mistralai,open-mistral-7b,7.3,7.3,,https://docs.mistral.ai/models/#sizes | ||
mistralai,open-mixtral-8x7b,46.7,46.7,,https://docs.mistral.ai/models/#sizes | ||
anthropic,claude-3-opus-20240229,2000,250;1000,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit?usp=sharing | ||
anthropic,claude-3-sonnet-20240229,800,100;400,model_architecture_not_released, | ||
anthropic,claude-3-haiku-20240307,300,75;150,model_architecture_not_released, | ||
anthropic,claude-2.1,130,130,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit?usp=sharing | ||
anthropic,claude-2.0,130,130,model_architecture_not_released,https://docs.google.com/spreadsheets/d/1O5KVQW1Hx5ZAkcg8AIRjbQLQzx2wVaLl0SqUu-ir9Fs/edit?usp=sharing | ||
anthropic,claude-instant-1.2,20;70,20;70,model_architecture_not_released, |
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,84 @@ | ||
import os | ||
from csv import DictReader | ||
from dataclasses import dataclass | ||
from enum import Enum | ||
from typing import Optional | ||
|
||
|
||
class Providers(Enum): | ||
anthropic = "anthropic" | ||
mistralai = "mistralai" | ||
openai = "openai" | ||
|
||
|
||
class Warnings(Enum): | ||
model_architecture_not_released = "model_architecture_not_released" | ||
|
||
|
||
@dataclass | ||
class Model: | ||
provider: str | ||
name: str | ||
total_parameters: Optional[float] = None | ||
active_parameters: Optional[float] = None | ||
total_parameters_range: Optional[tuple[float, float]] = None | ||
active_parameters_range: Optional[tuple[float, float]] = None | ||
warnings: Optional[list[str]] = None | ||
sources: Optional[list[str]] = None | ||
|
||
|
||
class ModelRepository: | ||
|
||
def __init__(self, models: list[Model]) -> None: | ||
self.__models = models | ||
|
||
def find_model(self, provider: str, model_name: str) -> Optional[Model]: | ||
for model in self.__models: | ||
if model.provider == provider and model_name == model.name: | ||
return model | ||
return None | ||
|
||
@classmethod | ||
def from_csv(cls, filepath: Optional[str] = None) -> "ModelRepository": | ||
if filepath is None: | ||
filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data", "models.csv") | ||
models = [] | ||
with open(filepath) as fd: | ||
csv = DictReader(fd) | ||
for row in csv: | ||
total_parameters = None | ||
total_parameters_range = None | ||
if ";" in row["total_parameters"]: | ||
total_parameters_range = [float(p) for p in row["total_parameters"].split(";")] | ||
elif row["total_parameters"] != "": | ||
total_parameters = float(row["total_parameters"]) | ||
|
||
active_parameters = None | ||
active_parameters_range = None | ||
if ";" in row["active_parameters"]: | ||
active_parameters_range = [float(p) for p in row["active_parameters"].split(";")] | ||
elif row["active_parameters"] != "": | ||
active_parameters = float(row["active_parameters"]) | ||
|
||
warnings = None | ||
if row["warnings"] != "": | ||
warnings = [Warnings(w).name for w in row["warnings"].split(";")] | ||
|
||
sources = None | ||
if row["sources"] != "": | ||
sources = row["sources"].split(";") | ||
|
||
models.append(Model( | ||
provider=Providers(row["provider"]).name, | ||
name=row["name"], | ||
total_parameters=total_parameters, | ||
active_parameters=active_parameters, | ||
total_parameters_range=total_parameters_range, | ||
active_parameters_range=active_parameters_range, | ||
warnings=warnings, | ||
sources=sources | ||
)) | ||
return cls(models) | ||
|
||
|
||
models = ModelRepository.from_csv() |
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 |
---|---|---|
|
@@ -89,7 +89,8 @@ ignore = [ | |
"PLR0913", | ||
"RET504", | ||
"RET505", | ||
"COM812" | ||
"COM812", | ||
"PTH" | ||
] | ||
|
||
fixable = [ | ||
|
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,24 @@ | ||
from genai_impact.model_repository import ModelRepository, Model | ||
|
||
|
||
def test_create_model_repository_default(): | ||
models = ModelRepository.from_csv() | ||
assert isinstance(models, ModelRepository) | ||
assert models.find_model(provider="openai", model_name="gpt-3.5-turbo") is not None | ||
|
||
|
||
def test_create_model_repository_from_scratch(): | ||
models = ModelRepository([ | ||
Model(provider="provider-test", name="model-test") | ||
]) | ||
assert models.find_model(provider="provider-test", model_name="model-test") | ||
|
||
|
||
def test_find_unknown_provider(): | ||
models = ModelRepository.from_csv() | ||
assert models.find_model(provider="provider-test", model_name="gpt-3.5-turbo") is None | ||
|
||
|
||
def test_find_unknown_model_name(): | ||
models = ModelRepository.from_csv() | ||
assert models.find_model(provider="openai", model_name="model-test") is None |