Class: Candle::Tensor

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/candle/tensor.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._original_newObject



72
# File 'lib/candle/tensor.rb', line 72

alias_method :_original_new, :new

._original_onesObject



73
# File 'lib/candle/tensor.rb', line 73

alias_method :_original_ones, :ones

._original_randObject



75
# File 'lib/candle/tensor.rb', line 75

alias_method :_original_rand, :rand

._original_randnObject



76
# File 'lib/candle/tensor.rb', line 76

alias_method :_original_randn, :randn

._original_zerosObject



74
# File 'lib/candle/tensor.rb', line 74

alias_method :_original_zeros, :zeros

.new(data, dtype = nil, device: nil) ⇒ Object



78
79
80
# File 'lib/candle/tensor.rb', line 78

def new(data, dtype = nil, device: nil)
  _original_new(data, dtype, device)
end

.ones(shape, device: nil) ⇒ Object



82
83
84
# File 'lib/candle/tensor.rb', line 82

def ones(shape, device: nil)
  _original_ones(shape, device)
end

.rand(shape, device: nil) ⇒ Object



90
91
92
# File 'lib/candle/tensor.rb', line 90

def rand(shape, device: nil)
  _original_rand(shape, device)
end

.randn(shape, device: nil) ⇒ Object



94
95
96
# File 'lib/candle/tensor.rb', line 94

def randn(shape, device: nil)
  _original_randn(shape, device)
end

.zeros(shape, device: nil) ⇒ Object



86
87
88
# File 'lib/candle/tensor.rb', line 86

def zeros(shape, device: nil)
  _original_zeros(shape, device)
end

Instance Method Details

#eachObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/candle/tensor.rb', line 5

def each
  case self.rank
  when 0
    # Scalar tensor - yield the single value
    yield self.item
  when 1
    # 1D tensor - yield each value
    # Check if we can use f32 values to avoid conversion
    if dtype.to_s.downcase == "f32"
      begin
        values_f32.each { |value| yield value }
      rescue NoMethodError
        # :nocov:
        # If values_f32 isn't available yet (not recompiled), fall back
        if device.to_s != "cpu"
          # Move to CPU to avoid Metal F32->F64 conversion issue
          to_device(Candle::Device.cpu).values.each { |value| yield value }
        else
          values.each { |value| yield value }
        end
        # :nocov:
      end
    else
      # For non-F32 dtypes, use regular values
      values.each { |value| yield value }
    end
  else
    # Multi-dimensional tensor - yield each sub-tensor
    shape.first.times do |i|
      yield self[i]
    end
  end
end

#inspectObject

Improved inspect method showing shape, dtype, and device



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/candle/tensor.rb', line 55

def inspect
  shape_str = shape.join("x")
  
  parts = ["#<Candle::Tensor"]
  parts << "shape=#{shape_str}"
  parts << "dtype=#{dtype}"
  parts << "device=#{device}"
  
  # Add element count for clarity
  parts << "elements=#{elem_count}"
  
  parts.join(" ") + ">"
end

#to_fObject

Convert scalar tensor to float



40
41
42
43
44
45
46
47
# File 'lib/candle/tensor.rb', line 40

def to_f
  if rank == 0
    # Use item method which handles dtype conversion properly
    item
  else
    raise ArgumentError, "to_f can only be called on scalar tensors (rank 0), but this tensor has rank #{rank}"
  end
end

#to_iObject

Convert scalar tensor to integer



50
51
52
# File 'lib/candle/tensor.rb', line 50

def to_i
  to_f.to_i
end