Skip to content

Commit 4e55a03

Browse files
committed
Implement low level XXXArray classes holding custom types. Uses with TensordataFactory.
Related-To: #97, #95
1 parent c1a24c2 commit 4e55a03

16 files changed

Lines changed: 1581 additions & 123 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
GROUP=sk.ainet.core
2-
VERSION_NAME=0.0.1.1
2+
VERSION_NAME=0.0.1.2
33

44
POM_DESCRIPTION=SKaiNET
55

skainet-lang/skainet-lang-api/src/commonMain/kotlin/sk/ainet/lang/tensor/Shape.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public data class Shape(val dimensions: IntArray) {
88
}
99

1010
val volume: Int
11-
get() = dimensions.fold(1) { a, x -> a * x }
11+
get() = dimensions.fold(if (dimensions.isNotEmpty()) 1 else 0) { a, x -> a * x }
1212

1313
val rank: Int
1414
get() = dimensions.size

skainet-lang/skainet-lang-api/src/commonMain/kotlin/sk/ainet/lang/tensor/data/TensorData.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import sk.ainet.lang.types.DType
77
* A fundamental data structure interface that provides indexed access to elements.
88
*
99
* This interface serves as the base for all data structures that need to provide
10-
* element access through multi-dimensional indexing. It is designed to support
10+
* element access through multidimensional indexing. It is designed to support
1111
* efficient access patterns commonly used in neural network computations.
1212
*
1313
* @param T the type of elements that can be accessed
1414
*/
1515
public interface ItemsAccessor<T> {
1616
/**
17-
* Retrieves an element at the specified multi-dimensional indices.
17+
* Retrieves an element at the specified multidimensional indices.
1818
*
1919
* This operator function allows accessing elements using bracket notation
2020
* with variable number of indices, supporting tensors of any dimensionality.
@@ -24,6 +24,12 @@ public interface ItemsAccessor<T> {
2424
* @throws IndexOutOfBoundsException if any index is out of bounds
2525
*/
2626
public operator fun get(vararg indices: Int): T
27+
28+
29+
/**
30+
* Setter
31+
*/
32+
public operator fun set(vararg indices: Int, value: T)
2733
}
2834

2935
/**
@@ -61,4 +67,6 @@ public interface TensorData<T : DType, V> : ItemsAccessor<V> {
6167
* @return the Shape object describing this tensor's dimensional structure
6268
*/
6369
public val shape: Shape
70+
71+
public companion object
6472
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package sk.ainet.lang.tensor.data
2+
3+
import sk.ainet.lang.types.DType
4+
5+
6+
public fun <T: DType,V> TensorData<T,V>.pprint(): String {
7+
return when (this.shape.rank) {
8+
0 -> {
9+
// Scalar - single value
10+
this.toString()
11+
}
12+
1 -> {
13+
// Vector - horizontal representation with parentheses
14+
val sb = StringBuilder()
15+
sb.append("[ ")
16+
for (i in 0 until this.shape[0]) {
17+
if (i > 0) sb.append(", ")
18+
sb.append(this[i].toString())
19+
}
20+
sb.append(" ]")
21+
sb.toString()
22+
}
23+
2 -> {
24+
// Matrix - vertical representation with Unicode brackets
25+
val sb = StringBuilder()
26+
val rows = this.shape[0]
27+
val cols = this.shape[1]
28+
29+
for (i in 0 until rows) {
30+
// Left bracket
31+
val leftBracket = when {
32+
rows == 1 -> "( "
33+
i == 0 -> ""
34+
i == rows - 1 -> ""
35+
else -> ""
36+
}
37+
sb.append(leftBracket)
38+
39+
// Matrix elements
40+
for (j in 0 until cols) {
41+
if (j > 0) sb.append(", ")
42+
sb.append(this[i, j].toString())
43+
}
44+
45+
// Right bracket
46+
val rightBracket = when {
47+
rows == 1 -> " )"
48+
i == 0 -> ""
49+
i == rows - 1 -> ""
50+
else -> ""
51+
}
52+
sb.append(rightBracket)
53+
54+
if (i < rows - 1) sb.append("\n")
55+
}
56+
sb.toString()
57+
}
58+
else -> {
59+
// Higher rank tensors - use toString
60+
this.toString()
61+
}
62+
}
63+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# skainet-lang-memory
2+
3+
## Overview
4+
5+
## memory layout
6+
7+
row major **BCWH**
8+
9+
$$
10+
\text{stride}[i] = \prod_{j=i+1}^{n-1} \text{dims}[j]
11+
$$
12+
13+
## Classes
14+
15+
DenseXXXTensorDataMemory implement dense linear memoty holding tensors data.

0 commit comments

Comments
 (0)