Skip to content

Tensors #95

Closed
4 of 5 issues completed
Closed

Description

@michalharakal

Title: Minimal Implementation of Tensors in Kotlin Multiplatform

Description

We need a minimal implementation of Tensors in Kotlin Multiplatform that will serve as a foundation for future extensions. This implementation should focus on correctness, simplicity, and architectural flexibility while adhering to the following decisions and requirements.

Architectural Decisions

  • Memory layout: Row-major (C-style ordering).
  • Separation of concerns: Tensor memory definition and tensor operators must be kept separate.
  • Data types: Support custom/experimental data types (e.g., int4, ternary, etc.).
  • Hardware flexibility: Allow the possibility to implement tensor operations on various hardware backends (CPU, GPU, accelerators).
  • Slicing and views: Support creation of views/slices without memory copies (strides).

Requirements

  1. Core Tensor abstraction

    • Define a minimal Tensor interface/class to represent n-dimensional data.
    • Support arbitrary dimensionality (shape, stride).
    • Ensure row-major layout by default.
  2. Memory representation

    • Encapsulate storage (contiguous memory buffer).
    • Keep data storage independent of operator logic.
    • Abstract over data type, allowing for built-in (e.g., Float32, Int32) and custom (e.g., Int4) types.
  3. Operators layer (separate from storage)

    • Define an initial set of minimal operators (e.g., element access, reshape, transpose).
    • Operators should work via interfaces/extensions and not be tied to the underlying memory representation.
  4. Slicing and views

    • Implement slicing using stride manipulation, not memory copies.
    • Views should reflect changes in the underlying data.
  5. Extensibility & HW ops

    • Define hooks/abstractions for plugging in hardware-specific operator implementations later.
  6. Testing & simplicity

    • Minimal API surface that is easy to reason about and test.

    • Provide unit tests covering:

      • Creation of tensors.
      • Shape/stride correctness.
      • View/slice behavior.
      • Simple operator correctness.

Out of Scope (for this issue)

  • Full operator suite (e.g., matmul, convolutions).
  • Advanced broadcasting semantics.
  • Performance optimizations.
  • Hardware-specific kernels.

Acceptance Criteria

  • Can create tensors of arbitrary shape with row-major layout.
  • Can slice tensors and obtain views without copying data.
  • Operators are implemented outside the memory layer.
  • Implementaion of operators supports broadcasting
  • Custom data types can be registered/used in a basic way
  • Code is simple, minimal, and well-tested across JVM/Native/JS targets.
  • Factory for create a special types of tensors like zeros, ones or reandom values with different radn generators

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