Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging system #57

Merged
merged 5 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 41 additions & 9 deletions doc/source/how_to.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,47 @@ Global configuration
Verbosity
---------

Tensorflow is very verbose by default.
When ``vegasflow`` is imported the environment variable ``TF_CPP_MIN_LOG_LEVEL``
is set to 1, hiding most warnings.
If you want to recover the usual Tensorflow logging level you can
set your enviroment to ``export TF_CPP_MIN_LOG_LEVEL=0``.
``VegasFlow`` uses the internal logging capabilities of python by
creating a new logger handled named ``vegasflow``.
You can modify the behavior of the logger as with any sane python library with the following lines:

.. code-block:: python

import logging
log_dict = {
"0" : logging.ERROR,
"1" : logging.WARNING,
"2" : logging.INFO,
"3" : logging.DEBUG
}
logger_vegasflow = logging.getLogger('vegasflow')
logger_vegasflow.setLevel(log_dict["0"])

Where the where the log level can be any level defined in the ``log_dict`` dictionary.
scarlehoff marked this conversation as resolved.
Show resolved Hide resolved

Since ``VegasFlow`` is to be interfaced with non-python code it is also
possible to control the behaviour through the environment variable ``VEGASFLOW_LOG_LEVEL``, in that case any of the keys in ``log_dict`` can be used. For instance:

.. code-block:: bash

export VEGASFLOW_LOG_LEVEL=1

will suppress all logger information other than ``WARNING`` and ``ERROR``.


Environment
-----------

``VegasFlow`` is based on ``TensorFlow`` and as such all environment variable that
have an effect on ``TensorFlow``s behavior will also have an effect on ``VegasFlow``.

Here we describe only some of what we found to be the most useful variables.
For a complete description on the variables controlling the GPU-behavior of ``TensorFlow`` please refer to
the `nvidia official documentation <https://docs.nvidia.com/deeplearning/frameworks/tensorflow-user-guide/index.html#variablestf>`_.

- ``TF_CPP_MIN_LOG_LEVEL``: controls the ``TensorFlow`` logging level. It is set to 1 by default so that only errors are printed.
- ``VEGASFLOW_LOG_LEVEL``: controls the ``VegasFlow`` logging level. Set to 3 by default so that everything is printed.


Choosing integration device
---------------------------
Expand Down Expand Up @@ -166,7 +202,3 @@ Note that here we are only filling one histograms and so the histogram tuple con


We ship an example of an integrand which generates histograms in the github repository: `here <https://github.com/N3PDF/vegasflow/blob/master/examples/histogram_ex.py>`_.




69 changes: 49 additions & 20 deletions src/vegasflow/configflow.py
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']