<commit_before>"""Common routines to use CuDNN."""

import atexit, ctypes, os
import libcudnn
import numpy

def get_ptr(x):
    return ctypes.c_void_p(int(x.gpudata))

class Auto(object):
    """Object to be destoryed automatically."""
    def __init__(self, value, destroyer):
        self.value = value
        self.destroyer = destroyer

    def __del__(self):
        try:
            self.destroyer(self.value)
        except:
            pass

_handle = None
_pid    = None

def get_default_handle():
    """Get the default handle of CuDNN."""

    global _handle, _pid
    pid = os.getpid()
    if _pid == pid:  # already initialized
        return _handle

    _handle = libcudnn.cudnnCreate()
    atexit.register(shutdown)

    _pid = pid  # mark as initialized
    return _handle

def shutdown():
    global _handle, _pid

    pid = os.getpid()
    if _pid != pid:  # not initialized
        return

    libcudnn.cudnnDestroy(_handle)
    _handle = None
    _pid    = None  # mark as uninitialized

_dtypes = {numpy.dtype('float32'): libcudnn.cudnnDataType['CUDNN_DATA_FLOAT'],
           numpy.dtype('float64'): libcudnn.cudnnDataType['CUDNN_DATA_DOUBLE']}

def get_tensor_desc(x, h, w, form='CUDNN_TENSOR_NCHW'):
    """Create a tensor descriptor for given settings."""
    n = x.shape[0]
    c = x.size / (n * h * w)
    desc = libcudnn.cudnnCreateTensorDescriptor()
    libcudnn.cudnnSetTensor4dDescriptor(
        desc, libcudnn.cudnnTensorFormat[form], _dtypes[x.dtype], n, c, h, w)<commit_msg>Create different handle for each device<commit_after>"""Common routines to use CuDNN."""

import atexit, ctypes, os
import libcudnn
import numpy

import cuda

def get_ptr(x):
    return ctypes.c_void_p(int(x.gpudata))

class Auto(object):
    """Object to be destoryed automatically."""
    def __init__(self, value, destroyer):
        self.value = value
        self.destroyer = destroyer

    def __del__(self):
        try:
            self.destroyer(self.value)
        except:
            pass

_handles = {}
_pid     = None

def get_default_handle():
    """Get the default handle of CuDNN."""

    global _handles, _pid

    pid = os.getpid()
    if _pid != pid:  # not initialized yet
        _handles = {}
        atexit.register(shutdown)
        _pid     = pid

    device = cuda.Context.get_device()
    if device in _handles:
        return _handles[device]

    handle = libcudnn.cudnnCreate()
    _handles[device] = handle

    return handle

def shutdown():
    global _handles, _pid

    pid = os.getpid()
    if _pid != pid:  # not initialized
        return

    for handle in _handles.itervalues():
        libcudnn.cudnnDestroy(handle)

    _handles = {}
    _pid     = None  # mark as uninitialized

_dtypes = {numpy.dtype('float32'): libcudnn.cudnnDataType['CUDNN_DATA_FLOAT'],
           numpy.dtype('float64'): libcudnn.cudnnDataType['CUDNN_DATA_DOUBLE']}

def get_tensor_desc(x, h, w, form='CUDNN_TENSOR_NCHW'):
    """Create a tensor descriptor for given settings."""
    n = x.shape[0]
    c = x.size / (n * h * w)
    desc = libcudnn.cudnnCreateTensorDescriptor()
    libcudnn.cudnnSetTensor4dDescriptor(
        desc, libcudnn.cudnnTensorFormat[form], _dtypes[x.dtype], n, c, h, w)
{'fraction_non_alphanumeric': 0.11142147734326505, 'fraction_numerical': 0.0037243947858473, 'mean_word_length': 2.8645083932853717, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2707290', 'n_tokens_mistral': 1116, 'n_tokens_neox': 1087, 'n_words': 269}