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



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

alias_method :_original_new, :new

._original_onesObject



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

alias_method :_original_ones, :ones

._original_randObject



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

alias_method :_original_rand, :rand

._original_randnObject



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

alias_method :_original_randn, :randn

._original_zerosObject



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

alias_method :_original_zeros, :zeros

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



63
64
65
# File 'lib/candle/tensor.rb', line 63

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

.ones(shape, device: nil) ⇒ Object



67
68
69
# File 'lib/candle/tensor.rb', line 67

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

.rand(shape, device: nil) ⇒ Object



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

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

.randn(shape, device: nil) ⇒ Object



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

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

.zeros(shape, device: nil) ⇒ Object



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

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

#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