예제 #1
0
def register_snoop(verbose=False, tensor_format=default_format, numpy_format=default_numpy_format):
    import snoop
    import cheap_repr
    import snoop.configuration
    cheap_repr.register_repr(torch.Tensor)(lambda x, _: tensor_format(x))
    cheap_repr.register_repr(numpy.ndarray)(lambda x, _: numpy_format(x))
    cheap_repr.cheap_repr(torch.zeros(6))
    unwanted = {
        snoop.configuration.len_shape_watch,
        snoop.configuration.dtype_watch,
    }
    snoop.config.watch_extras = tuple(x for x in snoop.config.watch_extras if x not in unwanted)
    if verbose:

        class TensorWrap:

            def __init__(self, tensor):
                self.tensor = tensor

            def __repr__(self):
                return self.tensor.__repr__()

        snoop.config.watch_extras += (
            lambda source, value: ('{}.data'.format(source), TensorWrap(value.data)),
        )
예제 #2
0
def register(verbose=False,
             torch_formatter=None,
             numpy_formatter=None,
             report_gpu_mem=False,
             report_time_cost=False):
    try:
        import numpy
        from snoop_tensor.numpy import TensorFormatter
        numpy_formatter = numpy_formatter or TensorFormatter()
        cheap_repr.register_repr(
            numpy.ndarray)(lambda x, _: numpy_formatter(x))
    except ImportError:
        pass

    try:
        import torch
        from snoop_tensor.torch import TensorFormatter, LayerFormatter
        torch_formatter = torch_formatter or TensorFormatter()
        torch_layer_formatter = LayerFormatter()
        cheap_repr.register_repr(torch.Tensor)(lambda x, _: torch_formatter(x))
        cheap_repr.register_repr(
            torch.nn.Module)(lambda x, _: torch_layer_formatter(x))
    except ImportError:
        pass

    if report_gpu_mem:
        from snoop_tensor.profiler import GPUMemoryWatcher
        snoop.config.watch_extras += (
            GPUMemoryWatcher.create_watcher(report_gpu_mem), )
    if report_time_cost:
        from snoop_tensor.profiler import TimeCostWatcher
        snoop.config.watch_extras += (TimeCostWatcher.create_watcher(), )

    unwanted = {
        snoop.configuration.len_shape_watch,
        snoop.configuration.dtype_watch,
    }
    snoop.config.watch_extras = tuple(x for x in snoop.config.watch_extras
                                      if x not in unwanted)
예제 #3
0
    from collections import ChainMap
except ImportError:
    from chainmap import ChainMap

from cheap_repr import basic_repr, register_repr, cheap_repr, PY2, PY3, ReprSuppressedWarning, find_repr_function, \
    raise_exceptions_from_default_repr, repr_registry

PYPY = 'pypy' in version.lower()


class FakeExpensiveReprClass(object):
    def __repr__(self):
        return 'bad'


register_repr(FakeExpensiveReprClass)(basic_repr)


class ErrorClass(object):
    def __init__(self, error=False):
        self.error = error

    def __repr__(self):
        if self.error:
            raise ValueError()
        return 'bob'


class ErrorClassChild(ErrorClass):
    pass
예제 #4
0
            activity: Activity = day[name]
            activity.start(time, tag, note)
            return activity
        else:
            # Ongoing activity -> stop it and start new activity
            if name == ongoing_activity.name:
                raise ValueError(
                    f'{ongoing_activity.shortrepr()} is already ongoing')
            if ongoing_activity.has_similar_name(name):
                raise ValueError(
                    f'{ongoing_activity.shortrepr()} is ongoing, and has a similar name to {name!r}'
                )
            ongoing_activity.stop(time)
            day = self[time.DDMMYY]
            activity: Activity = day[name]
            activity.start(time, tag, note)
            return activity


if os.getenv('TIMEFRED_BIRDSEYE'):
    import cheap_repr

    cheap_repr.register_repr(Activity)(cheap_repr.normal_repr)

assert Day.__default_factory__ == Activity

assert Work.__default_factory__ == Day
assert Day.__default_factory__ == Activity
assert Work.__default_factory__ == Day
assert Day.__default_factory__ == Activity