<commit_before>import inspect

import chainer


class WrappedFunctionNode(chainer.FunctionNode):
    """Wrap the target function and operate as ``FunctionNode``

    Arguments:
        name (str): name of the function node
        func (func): the target function
        args (list): args for the function
        kwargs (dict): kwargs for the function
        arg_vars (list): list of `chainer.Variable`s in `args` and `kwargs`
        attributes (list): parameters to be set node's attributes
    """

    def __init__(self, name, func, args, kwargs, arg_vars, attributes=None):
        self.custom_function_node_name = name
        self.func = func
        self.args = args
        self.kwargs = kwargs
        self.arg_vars = arg_vars
        self.internal_results = None

        if attributes is not None:
            for k, v in attributes.items():
                setattr(self, k, v)

    def forward(self, xs):
        assert len(xs) == len(self.arg_vars)
        self.xs = xs
        results = self.func(*self.args, **self.kwargs)
        if isinstance(results, (tuple, list)):
            dummy_results = tuple(_unwrap_var(ret) for ret in results)
            if all([_is_var(ret) for ret in results]):
                self.internal_results = tuple(results)
        elif isinstance(results, dict):
            dummy_results = tuple(_unwrap_var(ret) for ret in results.values())
            if all([_is_var(ret) for ret in results.values()]):
                self.internal_results = tuple(results.values())
        else:
            dummy_results = _unwrap_var(results)
            dummy_results = dummy_results,
            if _is_var(results):
                self.internal_results = results,
        if not chainer.is_arrays_compatible(dummy_results):
            raise ValueError(
                'returned values from the function wrapped by \'as_funcnode\' '
                'must consist only array, function name: {}'.format(self.custom_function_node_name))
        return dummy_results

    def backward(self, target_input_indexes, grad_outputs):
        if self.internal_results is None:
            raise ValueError(
                'the target function does not support backward, propagation '
                'is failed')
        grad_inputs = chainer.grad(self.internal_results, self.arg_vars,
                                   grad_outputs=grad_outputs)
        assert len(self.arg_vars) == len(grad_inputs)
        return tuple(grad_input if i in target_input_indexes else None
                     for i, grad_input in enumerate(grad_inputs))<commit_msg>Fix too long line<commit_after>import inspect

import chainer


class WrappedFunctionNode(chainer.FunctionNode):
    """Wrap the target function and operate as ``FunctionNode``

    Arguments:
        name (str): name of the function node
        func (func): the target function
        args (list): args for the function
        kwargs (dict): kwargs for the function
        arg_vars (list): list of `chainer.Variable`s in `args` and `kwargs`
        attributes (list): parameters to be set node's attributes
    """

    def __init__(self, name, func, args, kwargs, arg_vars, attributes=None):
        self.custom_function_node_name = name
        self.func = func
        self.args = args
        self.kwargs = kwargs
        self.arg_vars = arg_vars
        self.internal_results = None

        if attributes is not None:
            for k, v in attributes.items():
                setattr(self, k, v)

    def forward(self, xs):
        assert len(xs) == len(self.arg_vars)
        self.xs = xs
        results = self.func(*self.args, **self.kwargs)
        if isinstance(results, (tuple, list)):
            dummy_results = tuple(_unwrap_var(ret) for ret in results)
            if all([_is_var(ret) for ret in results]):
                self.internal_results = tuple(results)
        elif isinstance(results, dict):
            dummy_results = tuple(_unwrap_var(ret) for ret in results.values())
            if all([_is_var(ret) for ret in results.values()]):
                self.internal_results = tuple(results.values())
        else:
            dummy_results = _unwrap_var(results)
            dummy_results = dummy_results,
            if _is_var(results):
                self.internal_results = results,
        if not chainer.is_arrays_compatible(dummy_results):
            raise ValueError(
                'returned values from the function wrapped by \'as_funcnode\' '
                'must consist only array, function name: {}'.format(
                    self.custom_function_node_name))
        return dummy_results

    def backward(self, target_input_indexes, grad_outputs):
        if self.internal_results is None:
            raise ValueError(
                'the target function does not support backward, propagation '
                'is failed')
        grad_inputs = chainer.grad(self.internal_results, self.arg_vars,
                                   grad_outputs=grad_outputs)
        assert len(self.arg_vars) == len(grad_inputs)
        return tuple(grad_input if i in target_input_indexes else None
                     for i, grad_input in enumerate(grad_inputs))
{'fraction_non_alphanumeric': 0.0847620882296282, 'fraction_numerical': 0.0, 'mean_word_length': 2.2268489745183344, 'pattern_counts': {'":': 0, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3717207', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1351, 'n_words': 415}