pythonnumpycupy

Cupy throwing a unhashable type: 'ndarray' when appending nested tuples to a set


For a project we are trying to convert numpy code to cupy code for the speedup, but we are getting this error:

TypeError: unhashable type: 'ndarray'

When we run

import cupy as cp
newMat = cp.array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.], [ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0., -1.]])
newMatSet = set()
newMatSet.add(tuple(tuple(row) for row in sortBis(newMat)))

With sortBis simply reordering the matrix:

def sortBis(mat: cp.ndarray): 
    colInds = cp.lexsort(mat[:, 1:])
    mat[:, 1:] = mat[:, 1:][:, colInds] 
    return mat

It is my understanding that a nested tuple should be hashable, and this code works perfectly fine with numpy. Does anyone know of a fix?


Solution

  • Cupy was outputting the floats as a cupy.array in the tuple comprehension above whereas numpy outputted the floats as floats. Changing the code to this produced the desired output:

    import cupy as cp
    
    def sortBis(mat: cp.ndarray): 
        colInds = cp.lexsort(mat[:, 1:]) mat[:, 1:] = mat[:, 1:][:, colInds] 
        return mat
    
    newMat = cp.array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.], [ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0., -1.]]
    
    newMatSet.add(tuple(tuple(float(i) for i in row) for row in sortBis(newMat)))