Skip to content

Commit 81da98d

Browse files
committed
Move IO op to kotlinx-io library
1 parent b8e4f32 commit 81da98d

7 files changed

Lines changed: 33 additions & 20 deletions

File tree

gguf/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ kotlin {
2727
sourceSets {
2828
val commonMain by getting {
2929
dependencies {
30-
implementation(libs.okio)
30+
implementation(libs.kotlinx.io.core)
3131
}
3232
}
3333
val commonTest by getting {

gguf/src/commonMain/kotlin/sk/ai/net/gguf/GGUFReader.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package sk.ai.net.gguf
22

3-
import okio.BufferedSource
3+
import kotlinx.io.Buffer
4+
import kotlinx.io.Source
5+
import kotlinx.io.readByteArray
46
import sk.ai.net.gguf.utils.Endian
57
import sk.ai.net.gguf.utils.numberOfBytes
68
import sk.ai.net.gguf.utils.readDataByType
@@ -51,7 +53,7 @@ data class FieldParts(
5153
)
5254

5355
@OptIn(ExperimentalUnsignedTypes::class)
54-
class GGUFReader(bufferedSource: BufferedSource) {
56+
class GGUFReader(bufferedSource: Source) {
5557
// Properties
5658
var byteOrder: Char = 'I' // 'I' - same as host, 'S' - swapped
5759
var alignment: Int = GGUF_DEFAULT_ALIGNMENT

gguf/src/jvmTest/kotlin/sk/ai/net/gguf/GGUFReaderTest.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package sk.ai.net.gguf
22

3-
import okio.buffer
4-
import okio.source
3+
import kotlinx.io.asSource
4+
import kotlinx.io.buffered
55
import org.junit.Assert
66
import org.junit.Test
77
import sk.ai.net.gguf.utils.reshape
@@ -46,8 +46,9 @@ class GGUFReaderTest {
4646
javaClass.getResourceAsStream("/test_experiment.gguf").use { inputStream ->
4747
assertNotNull(inputStream, "Test resource file not found!")
4848

49+
// Convert it to a RawSource and then buffer it to get a Source:
4950

50-
val reader = GGUFReader(inputStream.source().buffer())
51+
val reader = GGUFReader(inputStream.asSource().buffered())
5152
// List all key-value pairs in a columned format
5253
println("Key-Value Pairs:")
5354
val maxKeyLength = reader.fields.keys.maxOf { it.length }

gradle/libs.versions.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ kotlinxSerializationJson = "1.8.0"
88
nexus-publish = "2.0.0"
99
jetbrainsKotlinJvm = "1.9.22"
1010
testng = "7.10.2"
11-
okio = "3.9.1"
12-
okioNodefilesystem = "3.9.0"
11+
kotlinxIo = "0.6.0"
1312
binaryCompatibility = "0.17.0"
1413
picnic = "0.7.0"
1514

@@ -20,8 +19,8 @@ kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serializa
2019
nexus-publish = { module = "io.github.gradle-nexus.publish-plugin:io.github.gradle-nexus.publish-plugin.gradle.plugin", version.ref = "nexus-publish" }
2120
picnic = { module = "com.jakewharton.picnic:picnic", version.ref = "picnic" }
2221
testng = { group = "org.testng", name = "testng", version.ref = "testng" }
23-
okio = { module = "com.squareup.okio:okio", version.ref = "okio" }
24-
okio-nodefilesystem = { module = "com.squareup.okio:okio-nodefilesystem", version.ref = "okioNodefilesystem" }
22+
kotlinx-io-core = { module = "org.jetbrains.kotlinx:kotlinx-io-core", version.ref = "kotlinxIo" }
23+
kotlinx-io-bytestring = { module = "org.jetbrains.kotlinx:kotlinx-io-bytestring", version.ref = "kotlinxIo" }
2524

2625
[plugins]
2726
androidLibrary = { id = "com.android.library", version.ref = "agp" }

io/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ kotlin {
3131
val commonMain by getting {
3232
dependencies {
3333
implementation(project(":core"))
34-
implementation(libs.okio)
34+
implementation(libs.kotlinx.io.core)
35+
3536
implementation(libs.kotlinx.serialization.json)
3637
}
3738
}

io/src/commonMain/kotlin/sk/ai/net/io/csv/CsvParameterLoader.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
package sk.ai.net.io.csv
22

3+
import kotlinx.io.Source
4+
import kotlinx.io.readByteArray
5+
import kotlinx.io.readString
36
import kotlinx.serialization.json.Json
4-
import okio.BufferedSource
5-
import okio.use
67
import sk.ai.net.Shape
78

89
import sk.ai.net.Tensor
910
import sk.ai.net.impl.DoublesTensor
1011
import sk.ai.net.io.ParametersLoader
1112

12-
class CsvParametersLoader(private val handleSource: () -> BufferedSource) :
13+
class CsvParametersLoader(private val handleSource: () -> Source) :
1314
ParametersLoader {
1415
override suspend fun load(onTensorLoaded: (String, Tensor) -> Unit) {
15-
handleSource().use { source ->
16+
17+
handleSource().use { source: Source ->
1618
// Initialize Json object
1719
val json = Json { ignoreUnknownKeys = true }
1820
// Deserialize JSON to Kotlin objects
19-
json.decodeFromString<List<Parameter>>(source.readUtf8()).also { values ->
21+
22+
json.decodeFromString<List<Parameter>>(source.readString()).also { values ->
2023
values.forEach { (name, array) ->
2124
val tensor = DoublesTensor(Shape(*array.shape.toIntArray()), array.values.toDoubleArray())
2225
onTensorLoaded(name, tensor)

io/src/jvmTest/kotlin/sk/ai/net/io/CsvParametersLoaderTest.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package sk.ai.net.io
22

33
import junit.framework.TestCase.assertTrue
44
import kotlinx.coroutines.runBlocking
5-
import okio.buffer
6-
import okio.source
7-
import org.junit.Test
5+
import kotlinx.io.asSource
6+
import kotlinx.io.buffered
7+
88
import sk.ai.net.Shape
99
import sk.ai.net.dsl.network
1010
import sk.ai.net.impl.DoublesTensor
@@ -16,9 +16,11 @@ import sk.ai.net.nn.reflection.flattenParams
1616
import sk.ai.net.nn.reflection.summary
1717
import sk.ai.net.nn.reflection.toVisualString
1818
import java.io.InputStream
19+
import kotlin.io.path.Path
1920
import kotlin.math.PI
2021
import kotlin.math.abs
2122
import kotlin.math.sin
23+
import kotlin.test.Test
2224
import kotlin.test.assertEquals
2325

2426
class CsvParametersLoaderTest {
@@ -47,7 +49,12 @@ class CsvParametersLoaderTest {
4749
print(sineModule.toVisualString())
4850

4951
javaClass.getResourceAsStream("/sinus-approximator.json")?.use { inputStream: InputStream ->
50-
val parametersLoader = CsvParametersLoader { inputStream.source().buffer() }
52+
53+
// Convert it to a RawSource and then buffer it to get a Source:
54+
val source = inputStream.asSource().buffered()
55+
56+
// Convert the InputStream to a kotlinx-io Input:
57+
val parametersLoader = CsvParametersLoader { source }
5158

5259
val mapper = NamesBasedValuesModelMapper()
5360

0 commit comments

Comments
 (0)