-
Notifications
You must be signed in to change notification settings - Fork 9
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 #57 from N3PDF/logging
Logging system
- Loading branch information
Showing
2 changed files
with
90 additions
and
29 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
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,56 +1,85 @@ | ||
""" | ||
Define some constants, header style | ||
""" | ||
# Most of this can be moved to a yaml file without loss of generality | ||
import os | ||
|
||
import numpy as np | ||
|
||
# Set TF to only log errors | ||
os.environ.setdefault("TF_CPP_MIN_LOG_LEVEL", "1") | ||
# Most of this can be moved to a yaml file without loss of generality | ||
import tensorflow as tf | ||
|
||
# uncomment this line for debugging to avoid compiling any tf.function | ||
# tf.config.experimental_run_functions_eagerly(True) | ||
# tf.config.run_functions_eagerly(True) | ||
|
||
|
||
# Configure logging | ||
def run_eager(flag=True): | ||
""" Wraper around `run_functions_eagerly` """ | ||
tf.config.run_functions_eagerly(flag) | ||
|
||
|
||
# Configure vegasflow logging | ||
import logging | ||
|
||
module_name = __name__.split(".")[0] | ||
logger = logging.getLogger(module_name) | ||
|
||
# Read the log level from environment, 3 (default) == debug, 2 == info, 1 == warning, 0 == error | ||
DEFAULT_LOG_LEVEL = "3" | ||
log_level_idx = os.environ.get("VEGASFLOW_LOG_LEVEL", DEFAULT_LOG_LEVEL) | ||
log_dict = {"0": logging.ERROR, "1": logging.WARNING, "2": logging.INFO, "3": logging.DEBUG} | ||
bad_log_warning = None | ||
if log_level_idx not in log_dict: | ||
bad_log_warning = log_level_idx | ||
log_level_idx = DEFAULT_LOG_LEVEL | ||
log_level = log_dict[log_level_idx] | ||
|
||
# Set level debug for development | ||
logger.setLevel(logging.DEBUG) | ||
logger.setLevel(log_level) | ||
# Create a handler and format it | ||
console_handler = logging.StreamHandler() | ||
console_handler.setLevel(logging.DEBUG) | ||
console_handler.setLevel(log_level) | ||
console_format = logging.Formatter("[%(levelname)s] %(message)s") | ||
console_handler.setFormatter(console_format) | ||
logger.addHandler(console_handler) | ||
|
||
# Define the tf.numberic types | ||
# Now that the logging has been created, warn about the bad logging level | ||
if bad_log_warning is not None: | ||
logger.warning( | ||
"Accepted log levels are: %s, received: %s", list(log_dict.keys()), bad_log_warning | ||
) | ||
logger.warning(f"Setting log level to its default value: {DEFAULT_LOG_LEVEL}") | ||
|
||
# Define the tensorflow number types | ||
DTYPE = tf.float64 | ||
DTYPEINT = tf.int32 | ||
FMAX = tf.constant(np.finfo(np.float64).max, dtype=DTYPE) | ||
|
||
# Define some default parameters for Vegas | ||
BINS_MAX = 50 | ||
ALPHA = 1.5 | ||
|
||
# Set up the logistics of the integration | ||
# Events Limit limits how many events are done in one single run of the event_loop | ||
# set it lower if hitting memory problems | ||
MAX_EVENTS_LIMIT = int(1e6) | ||
# Select the list of devices to look for | ||
DEFAULT_ACTIVE_DEVICES = ["GPU"] # , 'CPU'] | ||
|
||
# Create wrappers in order to have numbers of the correct type | ||
# The wrappers below transform tensors and array to the correct type | ||
def int_me(i): | ||
""" Casts any interger to DTYPEINT """ | ||
""" Cast the input to the `DTYPEINT` type """ | ||
return tf.cast(i, dtype=DTYPEINT) | ||
|
||
|
||
def float_me(i): | ||
""" Cast any float to DTYPE """ | ||
""" Cast the input to the `DTYPE` type """ | ||
return tf.cast(i, dtype=DTYPE) | ||
|
||
|
||
ione = int_me(1) | ||
izero = int_me(0) | ||
fone = float_me(1) | ||
fzero = float_me(0) | ||
|
||
|
||
# Define some default parameters for Vegas | ||
BINS_MAX = 50 | ||
ALPHA = 1.5 | ||
|
||
# Set up the logistics of the integration | ||
# Events Limit limits how many events are done in one single run of the event_loop | ||
# set it lower if hitting memory problems | ||
MAX_EVENTS_LIMIT = int(1e6) | ||
# Select the list of devices to look for | ||
DEFAULT_ACTIVE_DEVICES = ["GPU"] # , 'CPU'] |