Package: libMHCUDA
MinHashCudaThis project is the reimplementation of Weighted MinHash calculation fromekzhu/datasketchin NVIDIA CUDA and thus
brings 600-1000x speedup over numpy withMKL(Titan X 2016 vs 12-core Xeon E5-1650).
It supports running on multiple GPUs to be even faster, e.g., processing 10Mx12M
matrix with sparsity 0.0014 takes 40 minutes using two Titan Xs.
The produced results are bit-to-bit identical to the reference implementation.
Read thearticle.The input format is 32-bit floatCSRmatrix.
The code is optimized for low memory consumption and speed.What is Weighted MinHashMinHash can be used to compress unweighted set or binary vector, and estimate
unweighted Jaccard similarity.
It is possible to modify MinHash forweighted Jaccardby expanding each item (or dimension) by its weight.
However this approach does not support real number weights, and
doing so can be very expensive if the weights are very large.Weighted MinHashis created by Sergey Ioffe, and its performance does not depend on the weights - as
long as the universe of all possible items (or dimension for vectors) is known.
This makes it unsuitable for stream processing, when the knowledge of unseen
items cannot be assumed.Buildingcmake -DCMAKE_BUILD_TYPE=Release . && makeIt requires cudart, curand >=8.0, OpenMP 4.0 compatible compiler (that is, not gcc <=4.8) and
cmake >= 3.2.
Ifnumpyheaders are not found,
specify the includes path with definingNUMPY_INCLUDES.
If you do not want to build the Python native module, add-D DISABLE_PYTHON=y.
If CUDA is not automatically found, add-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-8.0(change the path to the actual one).
If you are building in a Docker container you may encounter the following error:Could NOT find CUDA (missing: CUDA_TOOLKIT_ROOT_DIR CUDA_INCLUDE_DIRS CUDA_CUDART_LIBRARY)This means you need to install the rest of the CUDA toolkit, which can be installed like in thenvidia/cuda:8.0-devrelDockerfile.
If you still run intoCould NOT find CUDA (missing: CUDA_INCLUDE_DIRS)then run:ln -s /usr/local/cuda/targets/x86_64-linux/include/* /usr/local/cuda/include/Python users: if you are using Linux x86-64 and CUDA 8.0, then you can
install this easily:pip install libMHCUDAOtherwise, you'll have to install it from source:pip install git+https://github.com/src-d/minhashcuda.gitBuilding in Python virtual environments, e.g. pyenv or conda is officially not supported.You can still submit patches to fix the related problems.Testingtest.pycontains the unit tests based onunittest.
They requiredatasketchandscipy.Contributions...are welcome! SeeCONTRIBUTINGandcode of conduct.LicenseApache 2.0Python exampleimportlibMHCUDAimportnumpyfromscipy.sparseimportcsr_matrix# Prepare the rowsnumpy.random.seed(1)data=numpy.random.randint(0,100,(6400,130))mask=numpy.random.randint(0,5,data.shape)data*=(mask>=4)delmaskm=csr_matrix(data,dtype=numpy.float32)deldata# We've got 80% sparse matrix 6400 x 130# Initialize the hasher aka "generator" with 128 hash samples for every rowgen=libMHCUDA.minhash_cuda_init(m.shape[-1],128,seed=1,verbosity=1)# Calculate the hashes. Can be executed several times with different number of rowshashes=libMHCUDA.minhash_cuda_calc(gen,m)# Free the resourceslibMHCUDA.minhash_cuda_fini(gen)The functions can be easily wrapped into a class (not included).Python APIImport "libMHCUDA".defminhash_cuda_init(dim,samples,seed=time(),deferred=False,devices=0,verbosity=0)Creates the hasher.diminteger, the number of dimensions in the input. In other words, length of each weight vector.
Must be less than 2³².samplesinteger, the number of hash samples. The more the value, the more precise are the estimates,
but the larger the hash size and the longer to calculate (linear). Must not be prime
for performance considerations and less than 2¹⁶.seedinteger, the random generator seed for reproducible results.deferredboolean, if True, disables the initialization of WMH parameters with
random numbers. In that case, the user is expected to call
minhash_cuda_assign_random_vars() afterwards.devicesinteger, bitwise OR-ed CUDA device indices, e.g. 1 means first device, 2 means second device,
3 means using first and second device. Special value 0 enables all available devices.
Default value is 0.verbosityinteger, 0 means complete silence, 1 means mere progress logging,
2 means lots of output.returninteger, pointer to generator struct (opaque).defminhash_cuda_calc(gen,matrix,row_start=0,row_finish=0xffffffff)Calculates Weighted MinHash-es. May reallocate memory on GPU but does it's best to reuse the buffers.geninteger, pointer to generator struct obtained from init().matrixscipy.sparse.csr_matrixinstance, the number of columns must matchdim.
The number of rows must be less than 2³¹.row_startinteger, slice start offset (the index of the first row to process).
Enables efficient zero-copy sparse matrix slicing.row_finishinteger, slice finish offset (the index of the row after the last
one to process). The resulting matrix row slice is [row-start:row_finish].returnnumpy.ndarrayof shape (number of matrix rows,samples, 2) and dtype uint32.defminhash_cuda_fini(gen)Disposes any resources allocated by init() and subsequent calc()-s. Generator pointer is invalidated.geninteger, pointer to generator struct obtained from init().C APIInclude "minhashcuda.h".MinhashCudaGenerator*mhcuda_init(uint32_tdim,uint16_tsamples,uint32_tseed,intdeferred,uint32_tdevices,intverbosity,MHCUDAResult*status)Initializes the Weighted MinHash generator.dimthe number of dimensions in the input. In other words, length of each weight vector.sampleshe number of hash samples. The more the value, the more precise are the estimates,
but the larger the hash size and the longer to calculate (linear). Must not be prime
for performance considerations.seedthe random generator seed for reproducible results.deferredif set to anything except 0, disables the initialization of WMH parameters with
random numbers. In that case, the user is expected to call
mhcuda_assign_random_vars() afterwards.devicesbitwise OR-ed CUDA device indices, e.g. 1 means first device, 2 means second device,
3 means using first and second device. Special value 0 enables all available devices.verbosity0 means complete silence, 1 means mere progress logging, 2 means lots of output.statuspointer to the reported return code. May be nullptr. In case of any error, the
returned result is nullptr and the code is stored into *status (with nullptr check).returnpointer to the allocated generator opaque struct.MHCUDAResultmhcuda_calc(constMinhashCudaGenerator*gen,constfloat*weights,constuint32_t*cols,constuint32_t*rows,uint32_tlength,uint32_t*output)Calculates the Weighted MinHash-es for the specified CSR matrix.genpointer to the generator opaque struct obtained from mhcuda_init().weightssparse matrix's values.colssparse matrix's column indices, must be the same size as weights.rowssparse matrix's row indices. The first element is always 0, the last is
effectively the size of weights and cols.lengththe number of rows. "rows" argument must have the size (rows + 1) because of
the leading 0.outputresulting hashes array of size rows x samples x 2.returnthe status code.MHCUDAResultmhcuda_fini(MinhashCudaGenerator*gen);Frees any resources allocated by mhcuda_init() and mhcuda_calc(), including device buffers.
Generator pointer is invalidated.genpointer to the generator opaque struct obtained from mhcuda_init().returnthe status code.README {#ignore_this_doxygen_anchor}