Skainet data dsl
Skill SKaiNET-developers/SKaiNET-coding-skills/skainet-consumer-skills/skills/skainet-data-dsl
Use when constructing tensors, slicing them, or building transform pipelines using the SKaiNET public DSL — applies in both consumer apps and inside the SKaiNET repo. Trigger tokens include `tensor(`, `tensor {`, `data<`, `pipeline<`, `sliceView`, `segment {`, `rescale`, `normalize`, `unsqueeze` (in pipeline context), `FP32::class`, `FP16::class`, `Int8::class`, `Int32::class`, `Ternary::class`, `randn(`, `uniform(`. Do NOT fire on tensor *assertions* (in-repo tests go to the contributor `skainet-testing` skill) or on neural-network builders (`sequential` / `dag` go to `skainet-nn-dsl`).From its SKILL.md
npx -y skills add SKaiNET-developers/SKaiNET-coding-skills --skill skainet-data-dslAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
7.4 KB, ~2.1k tokens by cl100k_base, as published. Nobody here has run it
skainet-data-dsl
Building blocks for tensor data: creation, initialisation, slicing, and transform pipelines for preprocessing. This skill is a cheatsheet — DSL skills teach usage rather than enforce constraints.
When to use
- Constructing a tensor literal with a known shape and fill (zeros, ones, fill, random, fromArray).
- Slicing or viewing an existing tensor.
- Building a preprocessing pipeline (rescale → normalize → unsqueeze → reshape).
- Choosing a dtype tag (
FP32,FP16,Int8,Int32,Int4,Ternary).
When NOT to use
- Asserting on tensor values in tests —
skainet-testing. - Defining neural network architecture with
sequential { }/dag { }—skainet-nn-dsl. - Exposing a tensor builder to Java —
skainet-java-interop. - Editing where a tensor file lives in source sets —
kmp.
Cheatsheet
Two equivalent tensor entry points
// (a) Direct entry — tensor(executionContext, dtypeKClass) { ... }
val t = tensor<FP32, Float>(ctx, FP32::class) {
tensor {
shape(2, 3) {
from(0f, 1f, 2f, 10f, 11f, 12f)
}
}
}
// from: SKaiNET/skainet-lang/skainet-lang-core/src/commonMain/kotlin/sk/ainet/lang/tensor/dsl/TensorDSL.kt:17-25
// (b) Phase-aware entry — data<T, V>(ctx) { tensor { ... } }
val t = data<FP32, Float>(ctx) {
tensor {
shape(2, 3) {
from(0f, 1f, 2f, 10f, 11f, 12f)
}
}
}
// from: SKaiNET/skainet-lang/skainet-lang-core/src/commonTest/kotlin/sk/ainet/readme/ReadmeSnippetsTest.kt:18-32
Use form (b) when you're already inside a phase-aware execution context (training vs eval) and want phase-tagged tensors. Use form (a) for plain inference / tests / examples.
Initialisation strategies inside shape(...) { ... }
shape(28, 28) { zeros() } // FloatArray of zeros
shape(28, 28) { ones() }
shape(28, 28) { full(0.5f) } // every element = 0.5
shape(2, 3) { from(1f, 2f, 3f, 4f, 5f, 6f) } // explicit values, length must equal shape volume
shape(2, 3) { fromArray(myFloatArray) }
shape(28, 28) { randn(mean = 0f, std = 0.02f) }
shape(28, 28) { uniform(min = -1f, max = 1f) }
shape(28, 28) { init { idx -> (idx[0] + idx[1]).toFloat() } }
shape(28, 28) { randomInit({ rng -> rng.nextFloat() }) }
// from: SKaiNET/skainet-lang/skainet-lang-core/src/commonMain/kotlin/sk/ainet/lang/tensor/dsl/TensorDSL.kt:50-108
Slice views — sliceView { segment { ... } }
val view = bigTensor.sliceView {
segment { range(0, 10) } // dim 0: indices 0..9 (exclusive end)
segment { at(5) } // dim 1: pick exactly index 5
segment { all() } // dim 2: keep everything
segment { step(0, 20, 2) } // dim 3: every 2nd index from 0 to 20
}
// from: SKaiNET/skainet-lang/skainet-lang-core/src/commonMain/kotlin/sk/ainet/lang/tensor/TensorSliceBuilder.kt:18-26
The number of segment { } blocks MUST equal the rank of the tensor — validate(tensorShape) throws otherwise.
Transform pipelines
val preprocess = pipeline<Tensor<FP32, Float>>()
.rescale(ctx, scale = 255f)
.normalize(ctx, mean = imagenetMean, std = imagenetStd, channelAxis = -1)
.unsqueeze(0) // add batch dim at position 0
val batch = preprocess(rawImageTensor)
// from: SKaiNET/skainet-data/skainet-data-transform/src/commonMain/kotlin/sk/ainet/data/transform/TensorTransformDsl.kt:18-50
Available transform extensions: rescale, normalize, scaleAndShift, clamp, reshape (more to follow — file is the source of truth).
Dtype tags
| Tag | Native value type V | Use |
|---|---|---|
FP32 | Float | default; training, inference, ground truth |
FP16 | Float (promoted) | half precision inference |
Int32 | Int | indices, labels |
Int8 | Byte | quantised inference |
Int4 | Byte (promoted) | aggressive quantisation |
Ternary | Byte | -1/0/+1 weights |
tensor<FP32, Float>(...) — the value-type parameter follows the table above. tensor<FP32, Int>(...) will not type-check.
Workflow
- Decide whether the tensor is part of a phase (training-aware) or a plain literal — pick
tensor(...) { tensor { } }ordata(...) { tensor { } }accordingly. - Pick the dtype + value-type pair from the table.
- Pick the initialisation: deterministic literals (
from,fromArray,full) for tests;randn/uniformfor parameter init;init/randomInitfor custom generators. - For preprocessing chains, compose transforms in
pipeline<...>()— every step takes theExecutionContextso the tensors land in the right backend. - Reach for
sliceView { segment { ... } }only when the operation isn't already covered by a tensor-op likenarrow,unsqueeze,squeeze,flatten(those are simpler and cheaper).
Related skills
- The
ExecutionContextitself comes fromDirectCpuExecutionContext.create()(CPU) orDefaultNeuralNetworkExecutionContext()for the phase-aware form — see../skainet-inference/SKILL.md. - Feeding the tensor into a network — see
../skainet-nn-dsl/SKILL.md. - Adding the SKaiNET dependency that exposes
tensor { }to your Gradle project — see../skainet-consumer-setup/SKILL.md. - Calling these from Java — see
../skainet-java-consumer/SKILL.md. - (In-repo only) Comparing computed tensors against expected values in tests — see the contributor
skainet-testingskill.
Anti-patterns
// WRONG — wrong value-type for the dtype
val t = tensor<FP32, Int>(ctx, FP32::class) { tensor { shape(2) { from(1, 2) } } }
// RIGHT — match the dtype/value-type table
val t = tensor<FP32, Float>(ctx, FP32::class) { tensor { shape(2) { from(1f, 2f) } } }
val ti = tensor<Int32, Int>(ctx, Int32::class) { tensor { shape(2) { from(1, 2) } } }
// WRONG — manually slicing with index loops in user code
val rows = (0 until 10).map { i -> bigTensor[i] }
// RIGHT — sliceView
val rows = bigTensor.sliceView { segment { range(0, 10) }; segment { all() } }
// WRONG — chained scalar ops to do preprocessing
val x1 = raw.ops.divScalar(raw, 255f)
val x2 = x1.ops.subScalar(x1, mean)
val x3 = x2.ops.divScalar(x2, std)
// RIGHT — a transform pipeline
val pre = pipeline<Tensor<FP32, Float>>()
.rescale(ctx, 255f)
.normalize(ctx, floatArrayOf(mean), floatArrayOf(std))
val out = pre(raw)
References
references/tensor-builders.md— every entry point onTensorCreationScopeandShapeBuilder, with signatures.references/transform-ops.md— every transform extension function inskainet-data-transform, with arguments and defaults.
What ships with it: 3 files
10.1 KB alongside SKILL.md
evals/
- evals.json2.4 KB
references/
- tensor-builders.md4.9 KB
- transform-ops.md2.8 KB