<commit_before>import numpy

import chainer
from chainer import cuda
from chainer import function_node
from chainer.functions.array import broadcast
from chainer.functions.math import sum as sum_
from chainer.utils import type_check

if cuda.cudnn_enabled:
    cudnn = cuda.cudnn
    libcudnn = cudnn.cudnn
    _algorithm = libcudnn.CUDNN_SOFTMAX_ACCURATE
    _mode = libcudnn.CUDNN_SOFTMAX_MODE_CHANNEL


def _get_tensor4d_shape(axis, shape):
    left_shape = numpy.prod(shape[slice(0, axis)], dtype=numpy.int)
    center_shape = shape[axis]
    right_shape = numpy.prod(
        shape[slice(axis + 1, len(shape))], dtype=numpy.int)
    return left_shape, center_shape, right_shape, 1


class Softmax(function_node.FunctionNode):

    """Softmax activation function."""

    def __init__(self, axis=1):
        self.axis = axis

    def check_type_forward(self, in_types):
        type_check.expect(in_types.size() == 1)
        x_type, = in_types

        type_check.expect(
            x_type.dtype.kind == 'f',
            x_type.ndim > 1,
            self.axis < x_type.ndim
        )

    def forward(self, x):
        xp = cuda.get_array_module(*x)
        if xp is not numpy and chainer.should_use_cudnn('>=auto'):
            oz_dtype = 'd' if x[0].dtype == 'd' else 'f'
            one = numpy.array(1, dtype=oz_dtype).ctypes
            zero = numpy.array(0, dtype=oz_dtype).ctypes
            handle = cudnn.get_handle()
            x_tensor4d = cuda.cupy.ascontiguousarray(
                x[0].reshape(_get_tensor4d_shape(self.axis, x[0].shape)))
            desc = cudnn.create_tensor_descriptor(x_tensor4d)
            y = xp.empty_like(x[0])
            libcudnn.softmaxForward(
                handle, _algorithm, _mode, one.data, desc.value,
                x_tensor4d.data.ptr, zero.data, desc.value,
                y.data.ptr)
        else:
            y = x[0] - x[0].max(axis=self.axis, keepdims=True)
            xp.exp(y, out=y)
            y /= y.sum(axis=self.axis, keepdims=True)

        self._x_shape = x[0].shape
        self.retain_outputs((0,))
        return y,

    def backward(self, indexes, grad_outputs):
        y = self.get_retained_outputs()[0]
        gy, = grad_outputs<commit_msg>Remove unnecessary line.<commit_after>import numpy

import chainer
from chainer import cuda
from chainer import function_node
from chainer.functions.array import broadcast
from chainer.functions.math import sum as sum_
from chainer.utils import type_check

if cuda.cudnn_enabled:
    cudnn = cuda.cudnn
    libcudnn = cudnn.cudnn
    _algorithm = libcudnn.CUDNN_SOFTMAX_ACCURATE
    _mode = libcudnn.CUDNN_SOFTMAX_MODE_CHANNEL


def _get_tensor4d_shape(axis, shape):
    left_shape = numpy.prod(shape[slice(0, axis)], dtype=numpy.int)
    center_shape = shape[axis]
    right_shape = numpy.prod(
        shape[slice(axis + 1, len(shape))], dtype=numpy.int)
    return left_shape, center_shape, right_shape, 1


class Softmax(function_node.FunctionNode):

    """Softmax activation function."""

    def __init__(self, axis=1):
        self.axis = axis

    def check_type_forward(self, in_types):
        type_check.expect(in_types.size() == 1)
        x_type, = in_types

        type_check.expect(
            x_type.dtype.kind == 'f',
            x_type.ndim > 1,
            self.axis < x_type.ndim
        )

    def forward(self, x):
        xp = cuda.get_array_module(*x)
        if xp is not numpy and chainer.should_use_cudnn('>=auto'):
            oz_dtype = 'd' if x[0].dtype == 'd' else 'f'
            one = numpy.array(1, dtype=oz_dtype).ctypes
            zero = numpy.array(0, dtype=oz_dtype).ctypes
            handle = cudnn.get_handle()
            x_tensor4d = cuda.cupy.ascontiguousarray(
                x[0].reshape(_get_tensor4d_shape(self.axis, x[0].shape)))
            desc = cudnn.create_tensor_descriptor(x_tensor4d)
            y = xp.empty_like(x[0])
            libcudnn.softmaxForward(
                handle, _algorithm, _mode, one.data, desc.value,
                x_tensor4d.data.ptr, zero.data, desc.value,
                y.data.ptr)
        else:
            y = x[0] - x[0].max(axis=self.axis, keepdims=True)
            xp.exp(y, out=y)
            y /= y.sum(axis=self.axis, keepdims=True)

        self.retain_outputs((0,))
        return y,

    def backward(self, indexes, grad_outputs):
        y = self.get_retained_outputs()[0]
        gy, = grad_outputs
{'fraction_non_alphanumeric': 0.11156278229448961, 'fraction_numerical': 0.00971093044263776, 'mean_word_length': 2.452065471551052, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '2124594', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1511, 'n_words': 295}