<commit_before>from numpy import prod
import contextlib

from .. import util
from ..train import Trainer
from ..exceptions import ShapeError
from ..ops import NumpyOps
from ..mem import Memory
from ..util import get_ops
from ... import check
from ... import describe


class Model(object):
    '''Model base class.'''
    name = 'model'
    id = 0
    ops = NumpyOps()
    Trainer = Trainer
    descriptions = []
    on_data_hooks = []
    on_init_hooks = [] # Use this to add layers
    _operators = {}

    @classmethod
    @contextlib.contextmanager
    def define_operators(cls, operators):
        '''Bind operators to specified functions for the scope of the context:

        Example
        -------

            model = Model()
            other = Model()
            with Model.use_operators({"+": lambda self, other: "plus"}):
                print(model + other)
                # "plus"
            print(model + other)
            # Raises TypeError --- binding limited to scope of with block.
        '''
        old_ops = dict(cls._operators)
        for op, func in operators.items():
            cls._operators[op] = func
        yield
        cls._operators = old_ops

    @classmethod
    @contextlib.contextmanager
    def use_device(cls, device):
        '''Change the device to execute on for the scope of the block.'''
        if device == cls.ops.device:
            yield
        else:
            curr_ops = cls.ops
            cls.ops = get_ops(device)
            yield
            cls.ops = curr_ops

    @property
    def input_shape(self):
        raise NotImplementedError

    @property
    def output_shape(self):
        raise NotImplementedError

    def __init__(self, *args, **kwargs):
        Model.id += 1
        self.id = Model.id
        self.name = self.__class__.name
        kwargs = self._update_defaults(args, kwargs)
        self._mem = Memory(self.ops)
        self._dims = {}
        if not hasattr(self, '_layers'):
            self._layers = []
        self.descriptions = dict(self.descriptions)
        self.on_init_hooks = list(self.on_init_hooks)
        self.on_data_hooks = list(self.on_data_hooks)

        for attr, install in self.descriptions.items():
            install(attr, self)
        for hook in self.on_init_hooks:
            hook(self, *args, **kwargs)

    def _update_defaults(self, args, kwargs):
        new_kwargs = {}
        for key, value in kwargs.items():
            if hasattr(self, key):
                setattr(self, key, value)
            else:
                new_kwargs[key] = value
        return new_kwargs

    @check.args_equal_length((1, 2))
    @check.arg_is_sequence(1)
    def begin_training(self, train_X, train_y=None):
        for hook in self.on_data_hooks:
            hook(self, train_X, train_y)
        return self.Trainer(self, train_X, train_y)
 
    #@check.arg_is_float(2)
    @check.arg_has_shape(X=('nB', 'nI'))
    def begin_update(self, X, drop=0.0):
        raise NotImplementedError

    @check.arg_has_shape(X=('nB', 'nI'))
    def predict(self, X):
        y, _ = self.begin_update(X)
        return y

    @check.arg_has_shape(x=('nI',))
    def predict_one(self, x):
        X = self.ops.expand_dims(x, axis=0)
        return self.predict(X)[0]

    @contextlib.contextmanager<commit_msg>Update decorator names<commit_after>from numpy import prod
import contextlib

from .. import util
from ..train import Trainer
from ..exceptions import ShapeError
from ..ops import NumpyOps
from ..mem import Memory
from ..util import get_ops
from ... import check
from ... import describe


class Model(object):
    '''Model base class.'''
    name = 'model'
    id = 0
    ops = NumpyOps()
    Trainer = Trainer
    descriptions = []
    on_data_hooks = []
    on_init_hooks = [] # Use this to add layers
    _operators = {}

    @classmethod
    @contextlib.contextmanager
    def define_operators(cls, operators):
        '''Bind operators to specified functions for the scope of the context:

        Example
        -------

            model = Model()
            other = Model()
            with Model.use_operators({"+": lambda self, other: "plus"}):
                print(model + other)
                # "plus"
            print(model + other)
            # Raises TypeError --- binding limited to scope of with block.
        '''
        old_ops = dict(cls._operators)
        for op, func in operators.items():
            cls._operators[op] = func
        yield
        cls._operators = old_ops

    @classmethod
    @contextlib.contextmanager
    def use_device(cls, device):
        '''Change the device to execute on for the scope of the block.'''
        if device == cls.ops.device:
            yield
        else:
            curr_ops = cls.ops
            cls.ops = get_ops(device)
            yield
            cls.ops = curr_ops

    @property
    def input_shape(self):
        raise NotImplementedError

    @property
    def output_shape(self):
        raise NotImplementedError

    def __init__(self, *args, **kwargs):
        Model.id += 1
        self.id = Model.id
        self.name = self.__class__.name
        kwargs = self._update_defaults(args, kwargs)
        self._mem = Memory(self.ops)
        self._dims = {}
        if not hasattr(self, '_layers'):
            self._layers = []
        self.descriptions = dict(self.descriptions)
        self.on_init_hooks = list(self.on_init_hooks)
        self.on_data_hooks = list(self.on_data_hooks)

        for attr, install in self.descriptions.items():
            install(attr, self)
        for hook in self.on_init_hooks:
            hook(self, *args, **kwargs)

    def _update_defaults(self, args, kwargs):
        new_kwargs = {}
        for key, value in kwargs.items():
            if hasattr(self, key):
                setattr(self, key, value)
            else:
                new_kwargs[key] = value
        return new_kwargs

    @check.args_equal_length((1, 2))
    @check.is_sequence(train_x=True, train_y=True)
    def begin_training(self, train_X, train_y=None):
        for hook in self.on_data_hooks:
            hook(self, train_X, train_y)
        return self.Trainer(self, train_X, train_y)

    @check.arg_is_float(X=True)
    @check.has_shape(X=('nB', 'nI'))
    def begin_update(self, X, drop=0.0):
        raise NotImplementedError

    @check.has_shape(X=('nB', 'nI'))
    def predict(self, X):
        y, _ = self.begin_update(X)
        return y

    @check.has_shape(x=('nI',))
    def predict_one(self, x):
        X = self.ops.expand_dims(x, axis=0)
        return self.predict(X)[0]

    @contextlib.contextmanager
{'fraction_non_alphanumeric': 0.10856798674898359, 'fraction_numerical': 0.002710435175425388, 'mean_word_length': 2.0764242704956, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1054439', 'n_tokens_mistral': 2012, 'n_tokens_neox': 1998, 'n_words': 516}