Skip to content

feat(nn): mixed-precision dense/stage via PrecisionBoundary (SKEEP-008 phase A1) - #1469

Merged
michalharakal merged 4 commits into
developfrom
feature/1433-mixed-precision-dsl
Nov 5, 2026
Merged

michalharakal merged 4 commits into
developfrom
feature/1433-mixed-precision-dsl

Conversation

@michalharakal

Copy link
Copy Markdown
Contributor

Part of #1433, implementing rollout phase A1 of xref SKEEP-008 (docs/modules/skeep/pages/008-precision-and-encoding-conversion.adoc). Part B (repackTo/requantizeTo) is a separate follow-up, so the SKEEP status stays Draft.

What

  • PrecisionBoundary (new, sk.ainet.lang.nn): wraps a module built in another dtype; casts in/out with the existing TensorOps.convert. Free when dtypes match, recorded by the tracing wrappers, lowers to stablehlo.convert. Inference-only until phase A3 makes convert @Diff — stated in the KDoc.
  • DSL: new overloads taking the dtype as a value — dense(64, dtype = BF16) { } and stage("head", dtype = FP16) { } — implemented in both NeuralNetworkDslImpl and StageImpl. The dtype object gives the implementation both the instance (for convert) and the KClass witness (for tensor creation); the outer dtype is recovered with DType.fromWitness.
  • Deprecations: the generic dense<TLayer>()/stage<TStage>() overloads could never be implemented (an interface method's type parameter cannot be reified, so no KClass exists at runtime) — now @Deprecated(level = ERROR) with fail-loud error() bodies, removing all six TODO() stubs and the NetworkBuilder.kt line from config/stub-allowlist.txt. The never-implemented scaffold (MixedPrecisionModule, InternalMixedPrecisionModule, MixedPrecisionTensorOps) is deprecated for removal (phase A4).
  • Docs: new Antora explanation article explanation/mixed-precision.adoc — why per-layer precision matters (FP16 vs BF16 trade-offs, SKaiNET's wasm/mobile targets), what the boundary design guarantees, and what it deliberately does not do yet (training, AMP, integer layers; float-backed storage per SKEEP-003).

Tests

MixedPrecisionDslTest (backend-cpu commonTest): a BF16 dense and an FP16 stage inside an FP32 network run on the CPU backend, stay close to the all-FP32 reference and differ from it (proving the rounding actually happens), an FP32-in-FP32 boundary is bit-exact (cast elision), and the wrapped layer appears as a PrecisionBoundary module.

./gradlew apiDump apiCheck checkUnmarkedStubs + jvmTest on lang-core, backend-cpu, compile-core/json/dag, plus a wasmJs compile — all green. API change is additive + deprecations (skainet-lang-core.api regenerated).

…8 A1, #1433)

Implement rollout phase A1 of SKEEP-008: the DSL takes the layer dtype as a
value (dense(64, dtype = BF16) { }, stage(id, dtype = FP16) { }) and wraps the
layer or block in PrecisionBoundary, which casts in and out with the existing
TensorOps.convert — free when dtypes match, visible in traces and StableHLO
exports, inference-only until convert becomes @Diff (phase A3).

The old generic overloads (dense<TLayer>(), stage<TStage>()) could never be
implemented — an interface method's type parameter cannot be reified, so there
was no KClass to build tensors with. They are deprecated at level ERROR and
their six TODO() stubs replaced by error() bodies; the NetworkBuilder.kt entry
leaves config/stub-allowlist.txt. The never-implemented scaffold
(MixedPrecisionModule, InternalMixedPrecisionModule, MixedPrecisionTensorOps)
is deprecated for removal.

Docs: new Antora explanation article on the value and limits of mixed
precision, linked from the nav and from SKEEP-008.
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

📖 Documentation Preview

The documentation has been built successfully for this PR.

Generated Files:

  • Operator documentation: docs/modules/operators/_generated_/
  • JSON schema output: operators.json

Artifacts:

  • Download the documentation-preview-1469 artifact to view the complete documentation locally.

This comment will be updated automatically when the PR is updated.

…nsorDataFactory

The non-JVM CI lanes failed on MixedPrecisionDslTest because BF16 was missing
from the float-backed dtype branches: DenseTensorDataFactory.init/uniform/
randomInit/wrapFloatArray/adoptFloatArray rejected it, and every DefaultCpuOps
float dispatch (matmul and 33 siblings) plus the sqrt/pow/powScalar/
convTranspose2d/upsample2d guards listed only FP32/FP16. BF16 is float-backed
exactly like FP16, so it joins those branches.
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

📖 Documentation Preview

The documentation has been built successfully for this PR.

Generated Files:

  • Operator documentation: docs/modules/operators/_generated_/
  • JSON schema output: operators.json

Artifacts:

  • Download the documentation-preview-1469 artifact to view the complete documentation locally.

This comment will be updated automatically when the PR is updated.

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

📖 Documentation Preview

The documentation has been built successfully for this PR.

Generated Files:

  • Operator documentation: docs/modules/operators/_generated_/
  • JSON schema output: operators.json

Artifacts:

  • Download the documentation-preview-1469 artifact to view the complete documentation locally.

This comment will be updated automatically when the PR is updated.

Conflict in config/stub-allowlist.txt: both deleted lines stand — this
branch implemented the six NetworkBuilder.kt mixed-precision stubs, and
develop independently removed the TensorDSL.kt entry (#1417). The merged
code has zero unmarked stubs in both files; checkUnmarkedStubs,
lang-core apiCheck and the lang-core/backend-cpu JVM tests pass on the
merge.
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

📖 Documentation Preview

The documentation has been built successfully for this PR.

Generated Files:

  • Operator documentation: docs/modules/operators/_generated_/
  • JSON schema output: operators.json

Artifacts:

  • Download the documentation-preview-1469 artifact to view the complete documentation locally.

This comment will be updated automatically when the PR is updated.

@michalharakal
michalharakal merged commit e61263d into develop Nov 5, 2026
22 checks passed
@michalharakal
michalharakal deleted the feature/1433-mixed-precision-dsl branch November 5, 2026 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants