Delete main/src/options.py with huggingface_hub
Browse files- main/src/options.py +0 -131
main/src/options.py
DELETED
|
@@ -1,131 +0,0 @@
|
|
| 1 |
-
from typing import Optional, Any
|
| 2 |
-
import os
|
| 3 |
-
import torch
|
| 4 |
-
import logging
|
| 5 |
-
import platform
|
| 6 |
-
from pprint import pformat
|
| 7 |
-
|
| 8 |
-
logger = logging.getLogger(__name__)
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def get_logging_root(logging_root: Optional[str] = None) -> str:
|
| 12 |
-
"""
|
| 13 |
-
Return the data root directory
|
| 14 |
-
"""
|
| 15 |
-
if logging_root is None:
|
| 16 |
-
logging_root = os.environ.get('LOGGING_ROOT')
|
| 17 |
-
|
| 18 |
-
if logging_root is None:
|
| 19 |
-
if 'Windows' in platform.system():
|
| 20 |
-
logging_root = 'c:/logs/'
|
| 21 |
-
else:
|
| 22 |
-
logging_root = '$HOME/logs/'
|
| 23 |
-
|
| 24 |
-
logging_root = os.path.expandvars(os.path.expanduser(logging_root))
|
| 25 |
-
return logging_root
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
class TrainingParameters:
|
| 29 |
-
"""
|
| 30 |
-
Define here specific training parameters
|
| 31 |
-
"""
|
| 32 |
-
def __init__(self, num_epochs: int, mixed_precision_enabled: bool = False, gradient_update_frequency: int = 1):
|
| 33 |
-
self.num_epochs = num_epochs
|
| 34 |
-
|
| 35 |
-
self.gradient_scaler = None
|
| 36 |
-
if mixed_precision_enabled:
|
| 37 |
-
try:
|
| 38 |
-
from torch.cuda.amp import GradScaler
|
| 39 |
-
self.gradient_scaler = GradScaler()
|
| 40 |
-
|
| 41 |
-
except Exception as e:
|
| 42 |
-
# mixed precision is not enabled
|
| 43 |
-
logger.error(f'Mixed precision not enabled! Exception={e}')
|
| 44 |
-
self.gradient_update_frequency = gradient_update_frequency
|
| 45 |
-
|
| 46 |
-
def __repr__(self) -> str:
|
| 47 |
-
return pformat(vars(self), indent=3, width=1)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
class WorkflowOptions:
|
| 51 |
-
"""
|
| 52 |
-
Define here workflow options
|
| 53 |
-
"""
|
| 54 |
-
def __init__(self, logging_directory: str, device: torch.device):
|
| 55 |
-
# expand special characters/variables
|
| 56 |
-
logging_directory = os.path.expandvars(os.path.expanduser(logging_directory))
|
| 57 |
-
|
| 58 |
-
self.device: torch.device = device
|
| 59 |
-
self.train_split: str = 'train'
|
| 60 |
-
self.logging_directory: str = logging_directory
|
| 61 |
-
self.current_logging_directory: str = logging_directory
|
| 62 |
-
self.trainer_run: int = 0
|
| 63 |
-
self.sql_database_view_path: Optional[str] = None
|
| 64 |
-
self.sql_database_path: Optional[str] = None
|
| 65 |
-
self.sql_database: Optional[Any] = None
|
| 66 |
-
|
| 67 |
-
def __repr__(self) -> str:
|
| 68 |
-
return pformat(vars(self), indent=3, width=1)
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
class Runtime:
|
| 72 |
-
"""
|
| 73 |
-
Define here the runtime configuration
|
| 74 |
-
"""
|
| 75 |
-
def __init__(self):
|
| 76 |
-
pass
|
| 77 |
-
|
| 78 |
-
def __repr__(self) -> str:
|
| 79 |
-
return pformat(vars(self), indent=3, width=1)
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
class Options:
|
| 83 |
-
"""
|
| 84 |
-
Create default options for the training and evaluation process.
|
| 85 |
-
"""
|
| 86 |
-
def __init__(self,
|
| 87 |
-
logging_directory: Optional[str] = None,
|
| 88 |
-
num_epochs: int = 50,
|
| 89 |
-
device: Optional[torch.device] = None,
|
| 90 |
-
mixed_precision_enabled: bool = False,
|
| 91 |
-
gradient_update_frequency: int =1):
|
| 92 |
-
"""
|
| 93 |
-
|
| 94 |
-
Args:
|
| 95 |
-
logging_directory: the base directory where the logs will be exported for each trained model.
|
| 96 |
-
If None and if the environment variable `LOGGING_DIRECTORY` exists, it will be used as root
|
| 97 |
-
directory. Else a default folder will be used
|
| 98 |
-
|
| 99 |
-
num_epochs: the number of epochs
|
| 100 |
-
device: the device to train the model on. If `None`, we will try first any available GPU then
|
| 101 |
-
revert to CPU
|
| 102 |
-
mixed_precision_enabled: if `True`, enable mixed precision for the training
|
| 103 |
-
gradient_update_frequency: defines how often (every `X` batches) the gradient is updated. This
|
| 104 |
-
is done to simulate a larger effective batch size (e.g., batch_size = 64 and gradient_update_frequency = 3
|
| 105 |
-
the effective batch size is gradient_update_frequency * batch_size)
|
| 106 |
-
"""
|
| 107 |
-
logging_directory = get_logging_root(logging_directory)
|
| 108 |
-
|
| 109 |
-
if device is None:
|
| 110 |
-
if torch.cuda.device_count() > 0:
|
| 111 |
-
env_device = os.environ.get('DEVICE')
|
| 112 |
-
if env_device is not None:
|
| 113 |
-
device = torch.device(env_device)
|
| 114 |
-
else:
|
| 115 |
-
device = torch.device('cuda:0')
|
| 116 |
-
else:
|
| 117 |
-
device = torch.device('cpu')
|
| 118 |
-
|
| 119 |
-
self.training_parameters: TrainingParameters = TrainingParameters(
|
| 120 |
-
num_epochs=num_epochs,
|
| 121 |
-
mixed_precision_enabled=mixed_precision_enabled,
|
| 122 |
-
gradient_update_frequency=gradient_update_frequency
|
| 123 |
-
)
|
| 124 |
-
self.workflow_options: WorkflowOptions = WorkflowOptions(
|
| 125 |
-
logging_directory=logging_directory,
|
| 126 |
-
device=device
|
| 127 |
-
)
|
| 128 |
-
self.runtime: Runtime = Runtime()
|
| 129 |
-
|
| 130 |
-
def __repr__(self) -> str:
|
| 131 |
-
return pformat(vars(self), indent=3, width=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|