def add(*args, **kwargs): """ This has multiple overloads .. function:: add(values: Dict[str, any]) :noindex: .. function:: add(name: str, value: any) :noindex: .. function:: add(**kwargs: any) :noindex: """ assert len(args) <= 2 if len(args) == 0: _add_dict(kwargs) elif len(args) == 1: assert not kwargs assert isinstance(args[0], dict) _add_dict(args[0]) elif len(args) == 2: assert not kwargs assert isinstance(args[0], str) _internal().store(args[0], args[1])
def log(*args, is_new_line: bool = True, is_reset: bool = True): r""" This has multiple overloads .. function:: log(message: str, *, is_new_line=True) :noindex: .. function:: log(message: str, color: StyleCode, *, is_new_line=True) :noindex: .. function:: log(message: str, colors: List[StyleCode], *, is_new_line=True) :noindex: .. function:: log(messages: List[Union[str, Tuple[str, StyleCode]]], *, is_new_line=True) :noindex: .. function:: log(*args: Union[str, Tuple[str, StyleCode]], is_new_line: bool = True) :noindex: Arguments: message (str): string to be printed color (StyleCode): color/style of the message colors (List[StyleCode]): list of colors/styles for the message args (Union[str, Tuple[str, StyleCode]]): list of messages Each element should be either a string or a tuple of string and styles. messages (List[Union[str, Tuple[str, StyleCode]]]): a list of messages. Each element should be either a string or a tuple of string and styles. Keyword Arguments: is_new_line (bool): whether to print a new line at the end Example:: >>> logger.log("test") """ from labml.internal.logger import logger_singleton as _internal if len(args) == 0: assert is_new_line _internal().log([], is_new_line=is_new_line, is_reset=is_reset) elif len(args) == 1: message = args[0] if isinstance(message, str): _internal().log([(message, None)], is_new_line=is_new_line, is_reset=is_reset) elif isinstance(message, list): _internal().log(message, is_new_line=is_new_line, is_reset=is_reset) else: raise TypeError(f'Unrecognized type: {type(message)}', message) elif len(args) == 2 and isinstance(args[0], str) and isinstance( args[1], StyleCode): _internal().log([(args[0], args[1])], is_new_line=is_new_line, is_reset=is_reset) else: _internal().log(args, is_new_line=is_new_line, is_reset=is_reset)
def enum(name, iterable: Sized, *, is_silent: bool = False, is_timed: bool = True): return _internal().enum(name, iterable, is_silent=is_silent, is_timed=is_timed)
def inspect(*args, **kwargs): """ Pretty prints a set of values. This has multiple overloads .. function:: inspect(items: Dict) :noindex: .. function:: inspect(items: List) :noindex: .. function:: inspect(*items: any) :noindex: """ from labml.internal.logger import logger_singleton as _internal _internal().info(*args, **kwargs)
def loop(iterator_: Union[range, int], *, is_print_iteration_time: bool = True): """ This has multiple overloads .. function:: loop(iterator_: range, *,is_print_iteration_time=True) :noindex: .. function:: loop(iterator_: int, *,is_print_iteration_time=True) :noindex: """ if type(iterator_) == int: return _internal().loop( range(iterator_), is_print_iteration_time=is_print_iteration_time) else: return _internal().loop( iterator_, is_print_iteration_time=is_print_iteration_time)
def iterate(name, iterable: Union[Iterable, Sized, int], total_steps: Optional[int] = None, *, is_silent: bool = False, is_timed: bool = True): return _internal().iterate(name, iterable, total_steps, is_silent=is_silent, is_timed=is_timed)
def section(name, *, is_silent: bool = False, is_timed: bool = True, is_partial: bool = False, is_new_line: bool = True, total_steps: float = 1.0): return _internal().section(name, is_silent=is_silent, is_timed=is_timed, is_partial=is_partial, total_steps=total_steps, is_new_line=is_new_line)
def save(*args, **kwargs): r""" This has multiple overloads .. function:: save() :noindex: .. function:: save(global_step: int) :noindex: .. function:: save(values: Dict[str, any]) :noindex: .. function:: save(name: str, value: any) :noindex: .. function:: save(**kwargs: any) :noindex: .. function:: save(global_step: int, values: Dict[str, any]) :noindex: .. function:: save(global_step: int, name: str, value: any) :noindex: .. function:: save(global_step: int, **kwargs: any) :noindex: """ if len(args) > 0 and type(args[0]) == int: _internal().set_global_step(args[0]) args = args[1:] if len(args) > 0 or len(kwargs.keys()) > 0: add(*args, **kwargs) _internal().write()
def set_scalar(name: str, is_print: bool = False): from labml.internal.logger.store.indicators.numeric import Scalar _internal().add_indicator(Scalar(name, is_print))
def reset(): r""" Reset indicators, for a new experiment """ _internal().reset_store()
def set_histogram(name: str, is_print: bool = False): from labml.internal.logger.store.indicators.numeric import Histogram _internal().add_indicator(Histogram(name, is_print))
def set_tensor(name: str): from labml.internal.logger.store.artifacts import Tensor _internal().add_artifact(Tensor(name))
def namespace(name: str): r""" Set a namespace for tracking """ return _internal().store_namespace(name)
def progress(steps: float): _internal().progress(steps)
def set_indexed_text(name: str, title: Optional[str] = None, is_print: bool = False): from labml.internal.logger.store.indicators.artifacts import IndexedText _internal().add_indicator(IndexedText(name, title, is_print))
def set_tensor(name: str, is_once: bool = False): from labml.internal.logger.store.indicators.artifacts import Tensor _internal().add_indicator(Tensor(name, is_once=is_once))
def set_indexed_scalar(name: str): from labml.internal.logger.store.indicators.indexed import IndexedScalar _internal().add_indicator(IndexedScalar(name))
def fail(): _internal().set_successful(False)
def set_text(name: str, is_print: bool = False): from labml.internal.logger.store.indicators.artifacts import Text _internal().add_indicator(Text(name, is_print))
def finish_loop(): _internal().finish_loop()
def set_global_step(global_step: Optional[int]): _internal().set_global_step(global_step)
def get_global_step() -> int: return _internal().global_step
def _add_dict(values: Dict[str, any]): for k, v in values.items(): _internal().store(k, v)
def set_queue(name: str, queue_size: int = 10, is_print: bool = False): from labml.internal.logger.store.indicators.numeric import Queue _internal().add_indicator(Queue(name, queue_size, is_print))
def add_global_step(increment_global_step: int = 1): _internal().add_global_step(int(increment_global_step))
def set_image(name: str, is_print: bool = False): from labml.internal.logger.store.artifacts import Image _internal().add_artifact(Image(name, is_print))