bytedeco / javacpp-presets

The missing Java distribution of native C++ libraries
Other
2.66k stars 741 forks source link

[pytorch] where is the "CPUFloatType" class for tensor datatype to match ? and the tensor convert to Buffer is null #1275

Open mullerhai opened 1 year ago

mullerhai commented 1 year ago

HI , In scala ,I want to match the tensor datatype to operate something, when I debug ,I find the Tensor type is [CPUFloatType ], But I can not find it ? how to security compare the tensor datatype with declare ,is instance of something? and If the tensor is FloatType ,after I invoke the tensor.data_ptr_byte() or tensor.data_ptr_double() or tensor.data_ptr_int(), the process will crash !!! ,how to security invoke it??

import org.bytedeco.pytorch.global.torch

val tensor = torch.randn(34,34)  //as you know  the tensor  is CPUFloatType 

if(tensor.data_ptr().isInstanceOf[BytePointer] ||tensor.data_ptr().equals(tensor.data_ptr_byte()) ){
      println(" data_ptr_byte") // here will crash first 
    }else if (tensor.data_ptr.isInstanceOf[IntPointer]||tensor.data_ptr().equals(tensor.data_ptr_int())){
      println(" data_ptr_int ") // here will crash second 
    }else if (tensor.data_ptr.isInstanceOf[FloatPointer]||tensor.data_ptr().equals(tensor.data_ptr_float())){
      println(" data_ptr_float ") // can not come here !!
    }else if (tensor.data_ptr.isInstanceOf[DoublePointer]||tensor.data_ptr().equals(tensor.data_ptr_double())){
      println(" data_ptr_double ")
    }else{
      println(" data_ptr_bool")
    }

finally  ,I want to know how to get  or convert to java  data buffer from  javacpp pytorch tensor?
I try to this ,but crash ,it is null

val tensor = torch.randn(34,34) val arr = tensor.data_ptr_float().asInstanceOf[FloatPointer].asBuffer().array() //will crash array is null

val flat = tensor.data_ptr_float() val buffer = DataBuffers.of(flat.asBuffer()) // buffer is null

mullerhai commented 1 year ago

I also found "class org.bytedeco.javacpp.Pointer cannot be cast to class org.bytedeco.javacpp.FloatPointer " why?

val floatTensor= tensor.data_ptr.asInstanceOf[FloatPointer]

how to convert it

mullerhai commented 1 year ago

only tensor.toType(torch.ScalarType.Byte) , but I need to compare the datatype will BytePointer or CPUByteType

saudet commented 1 year ago

Have you tried Tensor.createBuffer() and found something missing? If so, please explain more clearly what you need to do exactly that you are not able to do with that method: http://bytedeco.org/javacpp-presets/pytorch/apidocs/org/bytedeco/pytorch/AbstractTensor.html#createBuffer--

mullerhai commented 1 year ago

http://bytedeco.org/javacpp-presets/pytorch/apidocs/org/bytedeco/pytorch/AbstractTensor.html#createBuffer-- when I use Tensor.createBuffer() ,also crash, If you success use Tensor.createBuffer() ,please give me Tensor.createBuffer() a code example to do, thanks

var tensor2 = torch.randn(34,34)
val buffer2 = tensor2.createBuffer[FloatBuffer]()
val array = buffer2.array() // here will crash 
saudet commented 1 year ago

Those buffers don't have an array, so don't call array() on them.

mullerhai commented 1 year ago

Those buffers don't have an array, so don't call array() on them.

how to convert the buffer2 to java arrayList or Array?

mullerhai commented 1 year ago

Those buffers don't have an array, so don't call array() on them.

how to see and get the real float data num from those buffers

saudet commented 1 year ago

Everything you need to know about Buffer is documented in the JDK: https://docs.oracle.com/javase/8/docs/api/java/nio/Buffer.html

mullerhai commented 1 year ago

I found the tensor.createBuffer is DirectBuffer ,is out jvm buffer. But I need to read the buffer inner data to array, I can not get the suitable method .

saudet commented 1 year ago

We can copy the data with one of the get() methods