forked from kscience/kmath
Fixed strides flipping
This commit is contained in:
parent
c2db3a23e1
commit
23ea4a95a1
@ -37,9 +37,8 @@ public class IntTensor internal constructor(
|
||||
shape: IntArray,
|
||||
buffer: IntArray,
|
||||
offset: Int = 0
|
||||
) : BufferedTensor<Int>(shape, IntBuffer(buffer), offset)
|
||||
{
|
||||
internal constructor(bufferedTensor: BufferedTensor<Int>):
|
||||
) : BufferedTensor<Int>(shape, IntBuffer(buffer), offset) {
|
||||
internal constructor(bufferedTensor: BufferedTensor<Int>) :
|
||||
this(bufferedTensor.shape, bufferedTensor.buffer.array(), bufferedTensor.bufferStart)
|
||||
}
|
||||
|
||||
@ -47,9 +46,8 @@ public class DoubleTensor internal constructor(
|
||||
shape: IntArray,
|
||||
buffer: DoubleArray,
|
||||
offset: Int = 0
|
||||
) : BufferedTensor<Double>(shape, DoubleBuffer(buffer), offset)
|
||||
{
|
||||
internal constructor(bufferedTensor: BufferedTensor<Double>):
|
||||
) : BufferedTensor<Double>(shape, DoubleBuffer(buffer), offset) {
|
||||
internal constructor(bufferedTensor: BufferedTensor<Double>) :
|
||||
this(bufferedTensor.shape, bufferedTensor.buffer.array(), bufferedTensor.bufferStart)
|
||||
|
||||
override fun toString(): String = toPrettyString()
|
||||
@ -59,10 +57,17 @@ public class DoubleTensor internal constructor(
|
||||
internal inline fun BufferedTensor<Int>.asTensor(): IntTensor = IntTensor(this)
|
||||
internal inline fun BufferedTensor<Double>.asTensor(): DoubleTensor = DoubleTensor(this)
|
||||
|
||||
internal inline fun <T> TensorStructure<T>.copyToBufferedTensor(): BufferedTensor<T> =
|
||||
BufferedTensor(
|
||||
this.shape,
|
||||
TensorLinearStructure(this.shape).indices().map(this::get).toMutableList().asMutableBuffer(), 0
|
||||
)
|
||||
|
||||
internal inline fun <T> TensorStructure<T>.toBufferedTensor(): BufferedTensor<T> = when (this) {
|
||||
is BufferedTensor<T> -> this
|
||||
is MutableBufferND<T> -> BufferedTensor(this.shape, this.mutableBuffer, 0)
|
||||
else -> BufferedTensor(this.shape, this.elements().map{ it.second }.toMutableList().asMutableBuffer(), 0)
|
||||
is MutableBufferND<T> -> if (this.strides.strides.toIntArray() contentEquals TensorLinearStructure(this.shape).strides)
|
||||
BufferedTensor(this.shape, this.mutableBuffer, 0) else this.copyToBufferedTensor()
|
||||
else -> this.copyToBufferedTensor()
|
||||
}
|
||||
|
||||
internal val TensorStructure<Double>.tensor: DoubleTensor
|
||||
@ -77,3 +82,5 @@ internal val TensorStructure<Int>.tensor: IntTensor
|
||||
else -> this.toBufferedTensor().asTensor()
|
||||
}
|
||||
|
||||
public fun TensorStructure<Double>.toTypedTensor(): DoubleTensor = this.tensor
|
||||
public fun TensorStructure<Int>.toTypedTensor(): IntTensor = this.tensor
|
@ -23,62 +23,64 @@ class TestDoubleTensor {
|
||||
|
||||
@Test
|
||||
fun stridesTest() = DoubleTensorAlgebra {
|
||||
val tensor = fromArray(intArrayOf(2,2), doubleArrayOf(3.5,5.8,58.4,2.4))
|
||||
assertEquals(tensor[intArrayOf(0,1)], 5.8)
|
||||
assertTrue(tensor.elements().map{ it.second }.toList().toDoubleArray() contentEquals tensor.buffer.toDoubleArray())
|
||||
val tensor = fromArray(intArrayOf(2, 2), doubleArrayOf(3.5, 5.8, 58.4, 2.4))
|
||||
assertEquals(tensor[intArrayOf(0, 1)], 5.8)
|
||||
assertTrue(
|
||||
tensor.elements().map { it.second }.toList().toDoubleArray() contentEquals tensor.buffer.toDoubleArray()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getTest() = DoubleTensorAlgebra {
|
||||
val tensor = fromArray(intArrayOf(1,2,2), doubleArrayOf(3.5,5.8,58.4,2.4))
|
||||
val tensor = fromArray(intArrayOf(1, 2, 2), doubleArrayOf(3.5, 5.8, 58.4, 2.4))
|
||||
val matrix = tensor[0].as2D()
|
||||
assertEquals(matrix[0,1], 5.8)
|
||||
assertEquals(matrix[0, 1], 5.8)
|
||||
|
||||
val vector = tensor[0][1].as1D()
|
||||
assertEquals(vector[0], 58.4)
|
||||
|
||||
matrix[0,1] = 77.89
|
||||
assertEquals(tensor[intArrayOf(0,0,1)], 77.89)
|
||||
matrix[0, 1] = 77.89
|
||||
assertEquals(tensor[intArrayOf(0, 0, 1)], 77.89)
|
||||
|
||||
vector[0] = 109.56
|
||||
assertEquals(tensor[intArrayOf(0,1,0)], 109.56)
|
||||
assertEquals(tensor[intArrayOf(0, 1, 0)], 109.56)
|
||||
|
||||
tensor.matrixSequence().forEach {
|
||||
val a = it.asTensor()
|
||||
val secondRow = a[1].as1D()
|
||||
val secondColumn = a.transpose(0,1)[1].as1D()
|
||||
val secondColumn = a.transpose(0, 1)[1].as1D()
|
||||
assertEquals(secondColumn[0], 77.89)
|
||||
assertEquals(secondRow[1], secondColumn[1])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun bufferProtocol() {
|
||||
fun noBufferProtocol() {
|
||||
|
||||
// create buffers
|
||||
val doubleBuffer = DoubleBuffer(doubleArrayOf(1.0,2.0,3.0))
|
||||
val doubleList = MutableList(3, doubleBuffer::get)
|
||||
// create buffer
|
||||
val doubleArray = DoubleBuffer(doubleArrayOf(1.0, 2.0, 3.0))
|
||||
|
||||
// create ND buffers
|
||||
val ndBuffer = MutableBufferND(DefaultStrides(intArrayOf(3)), doubleBuffer)
|
||||
val ndList = MutableBufferND(DefaultStrides(intArrayOf(3)), doubleList.asMutableBuffer())
|
||||
// create ND buffers, no data is copied
|
||||
val ndArray = MutableBufferND(DefaultStrides(intArrayOf(3)), doubleArray)
|
||||
|
||||
// map to tensors
|
||||
val bufferedTensorBuffer = ndBuffer.toBufferedTensor() // strides are flipped
|
||||
val tensorBuffer = bufferedTensorBuffer.asTensor() // no data copied
|
||||
val bufferedTensorArray = ndArray.toBufferedTensor() // strides are flipped so data copied
|
||||
val tensorArray = bufferedTensorArray.asTensor() // data not contiguous so copied again
|
||||
|
||||
val bufferedTensorList = ndList.toBufferedTensor() // strides are flipped
|
||||
val tensorList = bufferedTensorList.asTensor() // data copied
|
||||
val tensorArrayPublic = ndArray.toTypedTensor() // public API, data copied twice
|
||||
val sharedTensorArray = tensorArrayPublic.toTypedTensor() // no data copied by matching type
|
||||
|
||||
tensorBuffer[intArrayOf(0)] = 55.9
|
||||
assertEquals(ndBuffer[intArrayOf(0)], 55.9)
|
||||
assertEquals(doubleBuffer[0], 55.9)
|
||||
assertTrue(tensorArray.buffer.array() contentEquals sharedTensorArray.buffer.array())
|
||||
|
||||
tensorList[intArrayOf(0)] = 55.9
|
||||
assertEquals(ndList[intArrayOf(0)], 1.0)
|
||||
assertEquals(doubleList[0], 1.0)
|
||||
tensorArray[intArrayOf(0)] = 55.9
|
||||
assertEquals(tensorArrayPublic[intArrayOf(0)], 1.0)
|
||||
|
||||
tensorArrayPublic[intArrayOf(0)] = 55.9
|
||||
assertEquals(sharedTensorArray[intArrayOf(0)], 55.9)
|
||||
assertEquals(bufferedTensorArray[intArrayOf(0)], 1.0)
|
||||
|
||||
bufferedTensorArray[intArrayOf(0)] = 55.9
|
||||
assertEquals(ndArray[intArrayOf(0)], 1.0)
|
||||
|
||||
ndList[intArrayOf(0)] = 55.9
|
||||
assertEquals(doubleList[0], 55.9)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user