Skip to content

TensorData.copyToFloatArray() default implementation is rank-broken — flat index into vararg get() trips every impl's arity check #930

Description

@michalharakal

The default implementation on the base interface (skainet-lang-core/src/commonMain/kotlin/sk/ainet/lang/tensor/data/TensorData.kt) iterates a single flat index and calls get(idx) — one vararg argument — regardless of tensor rank. Every concrete implementation's get begins with require(indices.size == shape.dimensions.size), so for any rank ≥ 2 tensor the default throws on the first element.

It is masked today because the implementations that matter override copyToFloatArray() — which makes this a latent trap for the next implementor: write a new TensorData, forget the override, and dense-looking code fails at runtime with a confusing arity error (or worse, works only for rank-1 test fixtures and fails on real tensors).

Also worth a look while in there: the default does get(idx) as Number and .toFloat(), which for packed-quant implementations (TensorData<DType, Byte>) would silently widen raw quantized bytes to floats if any of them ever inherited the default — semantically wrong, not just slow.

Options, cheapest first:

  1. Fix the default to do proper multi-dimensional index iteration (row-major unravel of the flat index) — correct for any rank.
  2. Or drop the default body entirely and make it abstract, forcing each implementor to decide — turns the latent runtime trap into a compile error.
  3. Either way, a commonTest contract test that runs copyToFloatArray() against a rank-2 instance of each implementation would pin the behavior.

Happy to open a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions