Skip to content

Commit 6b227d1

Browse files
committed
Continue refactoring for creating common executionContext providing required data in various context
Related-To #137
1 parent 119088a commit 6b227d1

12 files changed

Lines changed: 132 additions & 255 deletions

File tree

skainet-lang/skainet-lang-core/src/commonMain/kotlin/sk/ainet/context/ExecutionContext.kt

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,3 @@ public interface ExecutionContext<V> {
88
public val ops: TensorOps<V>
99
public val tensorDataFactory: TensorDataFactory
1010
}
11-
12-
13-
/**
14-
* Memory usage information
15-
*/
16-
public data class MemoryInfo(
17-
/**
18-
* Total memory available on the device
19-
*/
20-
public val totalMemory: Long,
21-
22-
/**
23-
* Memory currently in use
24-
*/
25-
public val usedMemory: Long,
26-
27-
/**
28-
* Free memory available
29-
*/
30-
public val freeMemory: Long = totalMemory - usedMemory,
31-
32-
/**
33-
* Memory usage as percentage
34-
*/
35-
public val usagePercentage: Double = (usedMemory.toDouble() / totalMemory) * 100.0
36-
)
37-
38-
/**
39-
* Execution statistics
40-
*/
41-
public data class ExecutionStats(
42-
/**
43-
* Total number of operations executed
44-
*/
45-
public val operationsExecuted: Long = 0,
46-
47-
/**
48-
* Total execution time in milliseconds
49-
*/
50-
public val totalExecutionTime: Long = 0,
51-
52-
/**
53-
* Average execution time per operation
54-
*/
55-
public val averageExecutionTime: Double =
56-
if (operationsExecuted > 0) totalExecutionTime.toDouble() / operationsExecuted else 0.0,
57-
58-
/**
59-
* Number of tensors created
60-
*/
61-
public val tensorsCreated: Long = 0,
62-
63-
/**
64-
* Peak memory usage
65-
*/
66-
public val peakMemoryUsage: Long = 0
67-
)
68-

skainet-lang/skainet-lang-core/src/commonMain/kotlin/sk/ainet/lang/graph/ExecutionContext.kt renamed to skainet-lang/skainet-lang-core/src/commonMain/kotlin/sk/ainet/context/GraphExecutionContext.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
package sk.ainet.lang.graph
2-
3-
import sk.ainet.context.ExecutionContext
4-
import sk.ainet.context.ExecutionStats
5-
import sk.ainet.context.MemoryInfo
6-
import sk.ainet.lang.tensor.Tensor
7-
import sk.ainet.lang.types.DType
1+
package sk.ainet.context
82

3+
import sk.ainet.lang.graph.ExecutionTape
4+
import sk.ainet.lang.graph.TapeStack
95

106
/**
117
* Context for managing execution state, including mode switching,
@@ -49,4 +45,4 @@ public interface GraphExecutionContext<V> : ExecutionContext<V> {
4945
* Reset execution statistics
5046
*/
5147
public fun resetExecutionStats()
52-
}
48+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package sk.ainet.context
2+
3+
/**
4+
* Memory usage information
5+
*/
6+
public data class MemoryInfo(
7+
/**
8+
* Total memory available on the device
9+
*/
10+
public val totalMemory: Long,
11+
12+
/**
13+
* Memory currently in use
14+
*/
15+
public val usedMemory: Long,
16+
17+
/**
18+
* Free memory available
19+
*/
20+
public val freeMemory: Long = totalMemory - usedMemory,
21+
22+
/**
23+
* Memory usage as percentage
24+
*/
25+
public val usagePercentage: Double = (usedMemory.toDouble() / totalMemory) * 100.0
26+
)
27+
28+
/**
29+
* Execution statistics
30+
*/
31+
public data class ExecutionStats(
32+
/**
33+
* Total number of operations executed
34+
*/
35+
public val operationsExecuted: Long = 0,
36+
37+
/**
38+
* Total execution time in milliseconds
39+
*/
40+
public val totalExecutionTime: Long = 0,
41+
42+
/**
43+
* Average execution time per operation
44+
*/
45+
public val averageExecutionTime: Double =
46+
if (operationsExecuted > 0) totalExecutionTime.toDouble() / operationsExecuted else 0.0,
47+
48+
/**
49+
* Number of tensors created
50+
*/
51+
public val tensorsCreated: Long = 0,
52+
53+
/**
54+
* Peak memory usage
55+
*/
56+
public val peakMemoryUsage: Long = 0
57+
)
58+
59+

skainet-lang/skainet-lang-core/src/commonMain/kotlin/sk/ainet/lang/graph/DefaultExecutionContext.kt

Lines changed: 0 additions & 148 deletions
This file was deleted.

skainet-lang/skainet-lang-core/src/commonMain/kotlin/sk/ainet/lang/graph/GraphExecutionContext.kt

Lines changed: 0 additions & 37 deletions
This file was deleted.

skainet-lang/skainet-lang-core/src/commonMain/kotlin/sk/ainet/lang/graph/GraphExecutionDSL.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sk.ainet.lang.graph
22

33
import sk.ainet.context.ExecutionStats
4+
import sk.ainet.context.GraphExecutionContext
45
import sk.ainet.lang.tensor.data.TensorDataFactory
56
import sk.ainet.lang.tensor.data.DenseTensorDataFactory
67
import sk.ainet.lang.types.DType
@@ -36,7 +37,7 @@ public data class GraphExecutionResult<V>(
3637
public fun <V, R> exec(
3738
dataFactory: TensorDataFactory = DenseTensorDataFactory(),
3839
block: GraphExecutionContext<V>.() -> R
39-
): GraphExecutionResult<V> {
40+
) {//}: GraphExecutionResult<V> {
4041
/*
4142
4243
return try {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package sk.ainet.lang.tensor.dsl
2+
3+
import sk.ainet.lang.tensor.Tensor
4+
import sk.ainet.lang.tensor.dsl.tensor
5+
import sk.ainet.lang.types.FP32
6+
import sk.ainet.lang.types.Int32
7+
8+
/**
9+
* Image normalization utilities for BCHW tensors with integer pixel values.
10+
*/
11+
public object ImageNormalization {
12+
/** Default max pixel value for 8-bit images */
13+
public const val DEFAULT_MAX_PIXEL: Float = 255f
14+
}
15+
16+
/**
17+
* Normalize a BCHW tensor with integer pixel values to the [0, 1] range.
18+
*
19+
* Steps:
20+
* - Validates BCHW rank (4D)
21+
* - Builds a new FP32 tensor with the same shape where each element is value/maxValue
22+
*/
23+
public fun Tensor<Int32, Int>.normalizePixels(maxValue: Float = ImageNormalization.DEFAULT_MAX_PIXEL): Tensor<FP32, Float> {
24+
require(this.rank == 4) { "Expected BCHW tensor with rank 4 (BCHW), but got rank=${this.rank} and shape=${this.shape}" }
25+
26+
val src = this
27+
val shape = src.shape
28+
29+
// Build a new FP32 tensor by mapping each Int pixel to Float/Max
30+
return tensor<FP32, Float> {
31+
shape(shape) {
32+
init { indices ->
33+
val b = indices[0]
34+
val c = indices[1]
35+
val h = indices[2]
36+
val w = indices[3]
37+
(src.data[b, c, h, w].toFloat() / maxValue)
38+
}
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)