Class: Executorch::Tensor

Inherits:
Object
  • Object
show all
Defined in:
lib/executorch.rb

Overview

Extend Tensor with Ruby-friendly methods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(data, shape: nil, dtype: :float) ⇒ Tensor

Create a new tensor from data

Examples:

Create from nested array (shape inferred)

Tensor.new([[1.0, 2.0], [3.0, 4.0]])  # shape: [2, 2]

Create from flat array with explicit shape

Tensor.new([1.0, 2.0, 3.0, 4.0], shape: [2, 2])

Parameters:

  • Array of numeric values (can be nested or flat)

  • (defaults to: nil)

    Shape of the tensor. If nil, inferred from data structure.

  • (defaults to: :float)

    Data type (:float, :double, :int, :long)

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/executorch.rb', line 31

def new(data, shape: nil, dtype: :float)
  if shape.nil?
    # Infer shape from nested array structure
    shape = infer_shape(data)
    flat_data = flatten_nested(data, shape)
  else
    # Use flat data directly (backward compatibility)
    flat_data = data
  end

  create(flat_data, shape, dtype)
end

Instance Method Details

#to_aArray

Convert tensor to a nested Ruby array matching the tensor’s shape

Examples:

tensor = Tensor.new([1, 2, 3, 4], shape: [2, 2])
tensor.to_a  # => [[1.0, 2.0], [3.0, 4.0]]

Returns:

  • Nested array with structure matching shape



125
126
127
128
# File 'lib/executorch.rb', line 125

def to_a
  flat = flat_to_a
  reshape_flat_to_nested(flat, shape)
end