Delete main/src/store.py with huggingface_hub
Browse files- main/src/store.py +0 -112
main/src/store.py
DELETED
|
@@ -1,112 +0,0 @@
|
|
| 1 |
-
from abc import ABC, abstractmethod
|
| 2 |
-
from typing import Dict, Any, Sequence, List
|
| 3 |
-
|
| 4 |
-
from basic_typing import History
|
| 5 |
-
|
| 6 |
-
from params import HyperParameters
|
| 7 |
-
import pickle
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
Metrics = Dict[str, float]
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
class RunResult:
|
| 14 |
-
"""
|
| 15 |
-
Represent the result of a run
|
| 16 |
-
"""
|
| 17 |
-
def __init__(self, metrics: Metrics, hyper_parameters: HyperParameters, history: History, info: Any = None):
|
| 18 |
-
"""
|
| 19 |
-
Args:
|
| 20 |
-
metrics: the metrics to be recorded
|
| 21 |
-
info: additional info related to the run
|
| 22 |
-
hyper_parameters: hyper parameter that led to these metrics
|
| 23 |
-
"""
|
| 24 |
-
self.info = info
|
| 25 |
-
self.hyper_parameters = hyper_parameters
|
| 26 |
-
self.metrics = metrics
|
| 27 |
-
self.history = history
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
class RunStore(ABC):
|
| 31 |
-
def __init__(self):
|
| 32 |
-
pass
|
| 33 |
-
|
| 34 |
-
@abstractmethod
|
| 35 |
-
def close(self) -> None:
|
| 36 |
-
"""
|
| 37 |
-
Close the store
|
| 38 |
-
"""
|
| 39 |
-
pass
|
| 40 |
-
|
| 41 |
-
@abstractmethod
|
| 42 |
-
def save_run(self, run_result: RunResult) -> None:
|
| 43 |
-
"""
|
| 44 |
-
Save the results of a run
|
| 45 |
-
|
| 46 |
-
Args:
|
| 47 |
-
run_result: the results to record
|
| 48 |
-
"""
|
| 49 |
-
|
| 50 |
-
@abstractmethod
|
| 51 |
-
def load_all_runs(self) -> Sequence[RunResult]:
|
| 52 |
-
"""
|
| 53 |
-
Load all the runs
|
| 54 |
-
"""
|
| 55 |
-
|
| 56 |
-
def __enter__(self):
|
| 57 |
-
return self
|
| 58 |
-
|
| 59 |
-
def __exit__(self, exc_type, exc_val, exc_tb):
|
| 60 |
-
self.close()
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
class RunStoreFile(RunStore):
|
| 64 |
-
"""
|
| 65 |
-
Linear file based store.
|
| 66 |
-
|
| 67 |
-
Notes:
|
| 68 |
-
* we don't keep the file open, since we may have
|
| 69 |
-
several stores reading from this location (single writer
|
| 70 |
-
but many reader). Reading and writing must happen on the same
|
| 71 |
-
thread
|
| 72 |
-
"""
|
| 73 |
-
def __init__(self, store_location: str, serializer=pickle):
|
| 74 |
-
super().__init__()
|
| 75 |
-
self.serializer = serializer
|
| 76 |
-
self.store_location = store_location
|
| 77 |
-
|
| 78 |
-
# do not keep file descriptor open BUT at least create an empty file,
|
| 79 |
-
# meaning the store exists but is currently empty.
|
| 80 |
-
with open(self.store_location, mode='ab') as f:
|
| 81 |
-
pass
|
| 82 |
-
|
| 83 |
-
def close(self) -> None:
|
| 84 |
-
"""
|
| 85 |
-
Close the store
|
| 86 |
-
"""
|
| 87 |
-
pass
|
| 88 |
-
|
| 89 |
-
def save_run(self, run_result: RunResult) -> None:
|
| 90 |
-
"""
|
| 91 |
-
Save the results of a run
|
| 92 |
-
|
| 93 |
-
Args:
|
| 94 |
-
run_result: the results to record
|
| 95 |
-
"""
|
| 96 |
-
with open(self.store_location, mode='ab') as f:
|
| 97 |
-
self.serializer.dump(run_result, f)
|
| 98 |
-
|
| 99 |
-
def load_all_runs(self) -> List[RunResult]:
|
| 100 |
-
"""
|
| 101 |
-
Load all the runs
|
| 102 |
-
"""
|
| 103 |
-
results = []
|
| 104 |
-
with open(self.store_location, mode='rb') as f:
|
| 105 |
-
while True:
|
| 106 |
-
try:
|
| 107 |
-
r = self.serializer.load(f)
|
| 108 |
-
results.append(r)
|
| 109 |
-
except EOFError:
|
| 110 |
-
break
|
| 111 |
-
|
| 112 |
-
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|