Search code examples
swiftmultidimensional-arraycoreml

Split a large [MLMultiArray] into smaller chunks [[MLMultiArray]]?


I have a large MLMultiArray of length 15360 values.

Sample:

Float32 1 x 15360
[14.78125,-0.6308594,5.609375,13.57812,-1.871094,-19.65625,9.5625,8.640625,-2.728516,3.654297,-3.189453,-1.740234...]

Is there a way I can convert this huge array into 120 small MLMultiArrays with 128 elements each without changing the sequence of this array and in the most efficient way possible?

The entire array of 15360 elements is available in this Link


Solution

  • let chunkSize = 2
    let shape = yourLargeArray.shape
    let chunkedArrays = try? stride(from: 0, to: shape.count, by: chunkSize)
        .map { offset -> MLMultiArray in
            let startIndex = shape.index(shape.startIndex, offsetBy: offset)
            let endIndex = shape.index(startIndex, offsetBy: chunkSize, limitedBy: shape.endIndex) ?? shape.endIndex
    
            return try MLMultiArray(
                shape: Array(shape[startIndex ..< endIndex]), 
                dataType: yourLargeArray.dataType
            )
        }