Skip to content

Generic values factories and value processing #121

Closed

Description

@michalharakal

Reshaping Tensors in SKAInet (Similar to NumPy's reshape)

Based on the codebase exploration, the SKAInet tensor library doesn't currently have a reshape method implemented, even though it's referenced in the documentation. However, I can show you how reshaping can be achieved and how it should work when implemented.

Current Status

The reshape method is not yet implemented in the current codebase, despite being used in documentation examples like:

val inputReshaped = input.reshape(Shape(inputSize, 1))

How Reshape Should Work (When Implemented)

The reshape operation should work similar to NumPy's reshape, taking a tensor and changing its shape while preserving the total number of elements. Here's how it would be used:

// Create a tensor with shape [12] (1D array with 12 elements)
val x = CpuTensorFP32.fromArray(
    Shape(12), 
    floatArrayOf(1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f, 12f)
)

// Reshape to 3x4 matrix (similar to numpy's x.reshape(3, 4))
val X = x.reshape(Shape(3, 4))

Implementation Approach

Based on the existing codebase structure, the reshape method would likely be implemented as:

  1. As an extension function on the Tensor interface in TensorOps.kt:
public fun T.reshape(newShape: Shape): T
  1. Key constraints that would be enforced:

    • The total volume (number of elements) must remain the same
    • original.shape.volume == newShape.volume
  2. Implementation would involve:

    • Validating that the volumes match
    • Creating a new tensor view with the same underlying data but different shape and strides
    • Updating the Shape and strides properties

Workarounds Until Reshape is Implemented

1. Using Flatten + Manual Reconstruction

// For simple cases, you can use flatten and create a new tensor
val flattened = tensor.flatten()
val reshaped = CpuTensorFP32.fromArray(Shape(3, 4), /* extract data from flattened */)

2. Creating New Tensor with Desired Shape

// Extract data and create new tensor with desired shape
val data = FloatArray(tensor.shape.volume)
tensor.copyTo(data.toTypedArray())
val reshaped = CpuTensorFP32.fromArray(Shape(3, 4), data)

Expected Implementation Location

When implemented, the reshape method would likely be added to:

  1. API Level: TensorOps.kt interface
public fun T.reshape(newShape: Shape): T
  1. Implementation Level: Each tensor class (CpuTensorFP32, CpuTensorInt8, etc.) in CpuBackend.kt

Key Components Available for Implementation

The library already has the necessary building blocks:

  • Shape class: Handles dimensions and volume calculation
  • TensorData interface: Manages strides and memory layout
  • Volume validation: Shape.volume property for ensuring reshape validity

Example of Future Usage

// Original tensor: 12 elements in 1D
val original = CpuTensorFP32.fromArray(Shape(12), data)
println(original.shape) // Shape: Dimensions = [12], Size (Volume) = 12

// Reshape to 3x4 matrix
val reshaped = original.reshape(Shape(3, 4))
println(reshaped.shape) // Shape: Dimensions = [3 x 4], Size (Volume) = 12

// Reshape to 2x2x3 tensor
val tensor3d = original.reshape(Shape(2, 2, 3))
println(tensor3d.shape) // Shape: Dimensions = [2 x 2 x 3], Size (Volume) = 12

Recommendation

Since the reshape functionality is referenced in the documentation but not implemented, you should:

  1. File an issue or feature request for implementing reshape
  2. Use the workaround methods mentioned above for immediate needs
  3. Consider contributing the implementation if you're comfortable with the codebase

The reshape operation is fundamental for tensor operations and should be straightforward to implement given the existing infrastructure in the SKAInet library.

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