ModularM
Modular•16mo ago•
1 reply
franchesoni

how to use Buffer?

it seems we're still in the dark regarding arrays. Mojo has efficient implementations of max, sum, etc. over Buffers but creating one is a pain. For instance this code breaks on the print, why?

    np = Python.import_module('numpy')
    logits = np.load('logits.npy')  # (C, H, W)

    # create data buffer
    var channels: Int = logits.shape[0]
    var height: Int = logits.shape[1]
    var width: Int = logits.shape[2]
    pointer_to_data = UnsafePointer[Float32].alloc(channels * height * width)
    pointer_to_data.init_pointee_copy(0)  # is this needed? idk, doesn't hurt
    tensor = NDBuffer[DType.float32, 3](pointer_to_data, DimList(channels, height, width))
    var ftensor = tensor.flatten()  # I need to create the NDBuffer first as I can't create buffer with a dynamic size
    var np_pointer = logits.__array_interface__['data'][0].unsafe_get_as_pointer[DType.float32]()  # from some example

    start = time.perf_counter()  # copy np data to tensor data
    for channel in range(channels):
        for row in range(height):
            for col in range(width):
                var posind = channel * height * width + row * width + col
                ftensor[posind] = np_pointer[posind]
    print("Time elapsed: ", time.perf_counter() - start)
    print(ftensor[0])  # breaks here


I have already spent quite a few hours trying to figure out why creating a Buffer is so much trouble. Also, slicing is hard. I have found no good resources on this, kapa tells me there is not enough info in the docs 😦
Was this page helpful?