|
| 1 | +== Data sources and Hugging Face |
| 2 | + |
| 3 | +SKaiNET separates artifact resolution from dataset parsing and preprocessing. |
| 4 | +Use `skainet-data-source` when a dataset, tokenizer, model sidecar, or fixture |
| 5 | +can live either on disk or behind a remote URI. |
| 6 | + |
| 7 | +[cols="1,3",options="header"] |
| 8 | +|=== |
| 9 | +| URI form | Meaning |
| 10 | +| `file:///path/to/file` |
| 11 | +| Read a local file. |
| 12 | + |
| 13 | +| `https://host/path/file` |
| 14 | +| Download and cache a generic remote artifact. |
| 15 | + |
| 16 | +| `hf+https://huggingface.co/org/repo/resolve/main/file` |
| 17 | +| Treat a Hugging Face resolve URL as a Hugging Face artifact. |
| 18 | + |
| 19 | +| `hf://org/repo@main/path/file` |
| 20 | +| Expand to a Hugging Face model repository resolve URL. |
| 21 | + |
| 22 | +| `hf://datasets/org/repo@main/path/file` |
| 23 | +| Expand to a Hugging Face dataset repository resolve URL. |
| 24 | +|=== |
| 25 | + |
| 26 | +=== Add the modules |
| 27 | + |
| 28 | +For JVM consumers, add the source module beside the data loaders you use: |
| 29 | + |
| 30 | +[source,kotlin] |
| 31 | +---- |
| 32 | +dependencies { |
| 33 | + implementation(platform("sk.ainet:skainet-bom:0.32.4")) |
| 34 | +
|
| 35 | + implementation("sk.ainet.core:skainet-data-source-jvm") |
| 36 | + implementation("sk.ainet.core:skainet-data-simple-jvm") |
| 37 | +} |
| 38 | +---- |
| 39 | + |
| 40 | +=== Resolve one artifact |
| 41 | + |
| 42 | +`JvmDataSourceResolver` materializes remote artifacts into a cache and returns |
| 43 | +a `DataSourceArtifact` that opens a `kotlinx.io.Source`. Public Hugging Face |
| 44 | +files do not need credentials. Private files should pass an explicit |
| 45 | +`DataSourceAuthToken` on the request or resolver. Existing `Authorization` |
| 46 | +headers still take precedence. On JVM, the resolver can also read `HF_TOKEN` / |
| 47 | +`HUGGING_FACE_HUB_TOKEN` from the environment as an opt-in convenience fallback. |
| 48 | + |
| 49 | +[source,kotlin] |
| 50 | +---- |
| 51 | +import sk.ainet.data.source.DataSourceAuthToken |
| 52 | +import sk.ainet.data.source.DataSourceRequest |
| 53 | +import sk.ainet.data.source.JvmDataSourceResolver |
| 54 | +
|
| 55 | +val resolver = JvmDataSourceResolver( |
| 56 | + huggingFaceToken = DataSourceAuthToken.from("hf_...") |
| 57 | +) |
| 58 | +val artifact = resolver.resolve( |
| 59 | + DataSourceRequest( |
| 60 | + uri = "hf+https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct/resolve/main/tokenizer.json" |
| 61 | + ) |
| 62 | +) |
| 63 | +
|
| 64 | +println(artifact.filename) |
| 65 | +println(artifact.localPath) |
| 66 | +
|
| 67 | +val source = artifact.openSource() |
| 68 | +try { |
| 69 | + // Pass the source to a parser/loader for model-sized artifacts. |
| 70 | +} finally { |
| 71 | + source.close() |
| 72 | +} |
| 73 | +
|
| 74 | +// Convenience for small sidecars and tests. |
| 75 | +val bytes = artifact.readBytes() |
| 76 | +---- |
| 77 | + |
| 78 | +For per-request credentials, pass the token directly on `DataSourceRequest`. |
| 79 | +This is useful when one resolver works with more than one private repository: |
| 80 | + |
| 81 | +[source,kotlin] |
| 82 | +---- |
| 83 | +val privateArtifact = resolver.resolve( |
| 84 | + DataSourceRequest( |
| 85 | + uri = "hf://datasets/your-org/private-dataset@main/data/train.bin", |
| 86 | + huggingFaceToken = DataSourceAuthToken.from("hf_...") |
| 87 | + ) |
| 88 | +) |
| 89 | +---- |
| 90 | + |
| 91 | +To opt into JVM environment fallback: |
| 92 | + |
| 93 | +[source,kotlin] |
| 94 | +---- |
| 95 | +val resolver = JvmDataSourceResolver( |
| 96 | + useEnvironmentHuggingFaceToken = true |
| 97 | +) |
| 98 | +---- |
| 99 | + |
| 100 | +=== Use sources with built-in loaders |
| 101 | + |
| 102 | +MNIST and Fashion-MNIST expose per-file URI overrides. CIFAR-10 exposes an |
| 103 | +archive URI override. Defaults still point to the historical public dataset |
| 104 | +locations, so existing code keeps working. |
| 105 | + |
| 106 | +[source,kotlin] |
| 107 | +---- |
| 108 | +import sk.ainet.data.mnist.MNIST |
| 109 | +import sk.ainet.data.mnist.MNISTLoaderConfig |
| 110 | +
|
| 111 | +val token = "hf_..." |
| 112 | +val train = MNIST.loadTrain( |
| 113 | + MNISTLoaderConfig( |
| 114 | + trainImagesUri = "file:///datasets/mnist/train-images-idx3-ubyte", |
| 115 | + trainLabelsUri = "hf+https://huggingface.co/your-org/mnist-idx/resolve/main/train-labels-idx1-ubyte.gz", |
| 116 | + huggingFaceTokenProvider = { token } |
| 117 | + ) |
| 118 | +) |
| 119 | +
|
| 120 | +val batches = train.batchIterator<sk.ainet.lang.types.Int8, Byte>(batchSize = 64) |
| 121 | +---- |
| 122 | + |
| 123 | +=== Cache behavior |
| 124 | + |
| 125 | +Use `CachePolicy.Use` for normal operation, `Refresh` to re-download, |
| 126 | +`Offline` to require a cached copy, and `Bypass` to avoid writing the cache. |
| 127 | +Built-in JVM loaders map `useCache = true` to `Use` and `useCache = false` |
| 128 | +to `Refresh`. |
| 129 | + |
| 130 | +[source,kotlin] |
| 131 | +---- |
| 132 | +import sk.ainet.data.source.CachePolicy |
| 133 | +import sk.ainet.data.source.DataSourceRequest |
| 134 | +
|
| 135 | +val refreshed = resolver.resolve( |
| 136 | + DataSourceRequest( |
| 137 | + uri = "hf://datasets/your-org/your-dataset@main/data/train-00000.parquet", |
| 138 | + cachePolicy = CachePolicy.Refresh |
| 139 | + ) |
| 140 | +) |
| 141 | +---- |
| 142 | + |
| 143 | +=== Keep preprocessing separate |
| 144 | + |
| 145 | +After bytes are parsed into a dataset, continue using the existing transform |
| 146 | +DSL for image/tensor preprocessing: |
| 147 | + |
| 148 | +[source,kotlin] |
| 149 | +---- |
| 150 | +import sk.ainet.data.transform.mnistPreprocessing |
| 151 | +
|
| 152 | +val preprocessing = mnistPreprocessing(ctx) |
| 153 | +---- |
0 commit comments