ModularM
Modular17mo ago
8 replies
Martin Dudek

to_numpy with Mojo 24.5

I try to convert the to_numpy method from the Mojo 24.4 based KMeans tutorial https://www.modular.com/blog/fast-k-means-clustering-in-mojo-guide-to-porting-python-to-mojo-for-accelerated-k-means-clustering to Mojo 24.5 but get the following error:

error: 'pop.index_to_pointer' op MLIR verification error: unregistered operation 'pop.index_to_pointer' found in dialect ('pop') that does not allow unknown operations


The orginal method is implemented as follows:
fn to_numpy(self) raises -> PythonObject:
        var np = Python.import_module("numpy")
        var np_arr = np.zeros((self.rows,self.cols))
        var npArrayPtr = DTypePointer[dtype](
        __mlir_op.`pop.index_to_pointer`[
            _type = __mlir_type[`!kgen.pointer<scalar<`, dtype.value, `>>`]
        ](
            SIMD[DType.index,1](np_arr.__array_interface__['data'][0].__index__()).value
        )
    )
    memcpy(npArrayPtr, self._matPtr, len(self))
    return np_arr ^

I replaced DTypePointer[dtype] with UnsafePointer[Scalar[dtype]]

Any advice highly appreciated 😉
There are several clustering algorithms, but k-means — the algorithm we're going to implement from scratch in Python and Mojo🔥 in this blog post — is one of the most popular due to its simplicity and ease of implementation.
Modular: Fast⚡k-means clustering in Mojo🔥: a guide to porting Pytho...
Was this page helpful?