Hi everyone. ```TfLiteTensorCopyFromBuffer(input_t...
# kotlin-native
m
Hi everyone.
Copy code
TfLiteTensorCopyFromBuffer(input_tensor, inputs as CValuesRef<*>, inputs.size as ULong)
"inputs" is of type Array<Any>. I want to extract the data from this "inputs" and feed it to tensorflow function(I am using cinterop to access tensorflow C API) but in runtime it throws me error as Array<Any> cannot be typecasted to CValuesRef<*>. I am asssuming there must be some function like "inputs.data" or something which can do the job. could anyone help me here please?
a
Hello, @Mananpoddarm! I think something like this could help here - try using
StableRef
class to create a reference onto your array, and then send it to C. Also, remember to dispose StableRef instance when it will become unnecessary.
m
Thanks @Artyom Degtyarev [JB] I think it worked. Also, in kotin .size method returns just the count of elements, is there a way to get number of bytes used by a variable like Array<Any> in kotlin? (regarding third argument in the question)
a
It depends on its elements. Not sure there is a built-in function to count byte-size of this data structure. If this array consists of homogeneous content, why don’t you use a more specific type? I’ve seen using
ByteArray
for similar purposes, which is a) easier to get size measured with ordinary
.size()
and b) can be converted to COpaquePointer by
ByteArray.toCValues()
without StableRef usage.
m
@Artyom Degtyarev [JB] I am writing native definitions in kotlin multiplatform. fun Collection<Byte>.toByteArray(): ByteArray Above is what I got from the documentation, now I am getting inputs:Array<Any> in the arguments of the function, how should I convert this inputs to a byteArray exactly?
a
I meant that, if possible, you would be better not using
Array<Any>
here. If there is no such option, please tell me more about this collection content. Maybe the simplest would be just multiplying its byte size over array size.
m
Array<Array<Array<IntArray>>> is the actual collection (Array<Any>). This holds the input data that I am sending to the tensorflow model
Copy code
val stableRef1 = StableRef.create(inputs)
        val voidPtr1 = stableRef1.asCPointer()
        TfLiteTensorCopyFromBuffer(input_tensor, voidPtr1 , 5u)
inputs is "Array<Any>" here. I am using 5u just to run the code but actually inputs.size() should go there and that size should be of type ULong and I think it should be the size of bytes inputs holds
a
I think, you should simplify the data structure using this approach: https://kotlinlang.org/docs/reference/collection-transformations.html#flattening. For example, I’ve made this snippet, flattening like:
Copy code
val a1: IntArray = intArrayOf(0,1)
    val a2: Array<IntArray> = arrayOf(a1, a1)
    val a3: Array<Array<IntArray>> = arrayOf(a2, a2)
    val a4: Array<Array<Array<IntArray>>> = arrayOf(a3, a3)
    val flatty: IntArray = a4.flatMap { return@flatMap it.map { it.map{it.toList()} } }.flatten().flatten().toIntArray()
    println(flatty)
Then I was able to measure array size in bytes(as the documentation states here, Kotlin Int has size 4). Also, IntArray has
.toCValues()
method, which might help.
👍 1