-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JBAI-4393 [core, ndarray] Refactored memory management and array hand…
…ling: streamlined array type handling and improved memory limit checks within create and reset methods; KIModel predict improved for NoAllocator case.
- Loading branch information
Showing
6 changed files
with
106 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 15 additions & 11 deletions
26
...ay/ndarray-api/src/commonMain/kotlin/io/kinference/ndarray/arrays/ArrayDispatcherUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
package io.kinference.ndarray.arrays | ||
|
||
enum class ArrayTypes(val index: Int, val size: Int) { | ||
ByteArray(0, Byte.SIZE_BYTES), | ||
UByteArray(1, UByte.SIZE_BYTES), | ||
ShortArray(2, Short.SIZE_BYTES), | ||
UShortArray(3, UShort.SIZE_BYTES), | ||
IntArray(4, Int.SIZE_BYTES), | ||
UIntArray(5, UInt.SIZE_BYTES), | ||
LongArray(6, Long.SIZE_BYTES), | ||
ULongArray(7, ULong.SIZE_BYTES), | ||
FloatArray(8, Float.SIZE_BYTES), | ||
DoubleArray(9, Double.SIZE_BYTES), | ||
BooleanArray(10, 1); | ||
ByteArrayType(0, Byte.SIZE_BYTES), | ||
UByteArrayType(1, UByte.SIZE_BYTES), | ||
ShortArrayType(2, Short.SIZE_BYTES), | ||
UShortArrayType(3, UShort.SIZE_BYTES), | ||
IntArrayType(4, Int.SIZE_BYTES), | ||
UIntArrayType(5, UInt.SIZE_BYTES), | ||
LongArrayType(6, Long.SIZE_BYTES), | ||
ULongArrayType(7, ULong.SIZE_BYTES), | ||
FloatArrayType(8, Float.SIZE_BYTES), | ||
DoubleArrayType(9, Double.SIZE_BYTES), | ||
BooleanArrayType(10, 1); | ||
|
||
companion object { | ||
fun sizeInBytes(index: Int, arraySize: Int): Long = entries[index].size * arraySize.toLong() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 14 additions & 26 deletions
40
ndarray/ndarray-core/src/jvmMain/kotlin/io/kinference/ndarray/arrays/memory/MemoryLimiter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,45 @@ | ||
package io.kinference.ndarray.arrays.memory | ||
|
||
import io.kinference.utils.PlatformUtils | ||
import kotlinx.atomicfu.AtomicLong | ||
import kotlinx.atomicfu.atomic | ||
import kotlinx.atomicfu.* | ||
|
||
interface MemoryLimiter { | ||
/** | ||
* Checks if the memory limit allows adding the specified amount of memory and performs the addition. | ||
* Checks if the memory limit allows adding the specified amount of memory and performs the addition | ||
* | ||
* @param added the memory in bytes to add | ||
* @return true if the memory was added successfully and false if adding the memory exceeds the memory limit | ||
*/ | ||
fun checkMemoryLimitAndAdd(added: Long): Boolean | ||
|
||
/** | ||
* Deducts the specified amount of memory from the memory limiter. | ||
* | ||
* @param deducted the memory in bytes to deduct from the memory limiter | ||
* Resets the used memory into 0L | ||
*/ | ||
fun deductMemory(deducted: Long) | ||
fun resetLimit() | ||
} | ||
|
||
class BaseMemoryLimiter(private val memoryLimit: Long) : MemoryLimiter { | ||
class BaseMemoryLimiter internal constructor(private val memoryLimit: Long) : MemoryLimiter { | ||
private var usedMemory: AtomicLong = atomic(0L) | ||
|
||
override fun checkMemoryLimitAndAdd(added: Long): Boolean { | ||
val currentMemory = usedMemory.addAndGet(added) | ||
return if (currentMemory > memoryLimit) { | ||
usedMemory.addAndGet(-added) | ||
false | ||
} else true | ||
// Attempt to add memory and check the limit | ||
val successful = usedMemory.getAndUpdate { current -> | ||
if (current + added > memoryLimit) current else current + added | ||
} != usedMemory.value // Check if the update was successful | ||
|
||
return successful | ||
} | ||
|
||
override fun deductMemory(deducted: Long) { | ||
usedMemory.addAndGet(-deducted) | ||
override fun resetLimit() { | ||
usedMemory.value = 0L | ||
} | ||
} | ||
|
||
object MemoryLimiters { | ||
val Default: MemoryLimiter = BaseMemoryLimiter((PlatformUtils.maxHeap * 0.3).toLong()) | ||
val NoAllocator: MemoryLimiter = NoAllocatorMemoryLimiter | ||
val NoAllocator: MemoryLimiter = BaseMemoryLimiter(0L) | ||
|
||
fun customLimiter(memoryLimit: Long): MemoryLimiter { | ||
return BaseMemoryLimiter(memoryLimit) | ||
} | ||
} | ||
|
||
internal object NoAllocatorMemoryLimiter : MemoryLimiter { | ||
override fun checkMemoryLimitAndAdd(added: Long): Boolean { | ||
return false | ||
} | ||
|
||
override fun deductMemory(deducted: Long) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters