Class: Torch::Native::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/torch/native/function.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function) ⇒ Function

Returns a new instance of Function.



6
7
8
# File 'lib/torch/native/function.rb', line 6

def initialize(function)
  @function = function
end

Instance Attribute Details

#functionObject (readonly)

Returns the value of attribute function.



4
5
6
# File 'lib/torch/native/function.rb', line 4

def function
  @function
end

Instance Method Details

#argsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/torch/native/function.rb', line 26

def args
  @args ||= begin
    args = []
    pos = true
    args_str = func.split("(", 2).last.split(") ->").first
    args_str.split(", ").each do |a|
      if a == "*"
        pos = false
        next
      end
      t, _, k = a.rpartition(" ")
      k, d = k.split("=")
      d = d.to_i if d.to_i.to_s == d
      d = true if d == "True"
      d = false if d == "False"
      d = nil if d == "None"
      args << {name: k, type: t, default: d, pos: pos}
    end
    args
  end
end

#base_nameObject



73
74
75
# File 'lib/torch/native/function.rb', line 73

def base_name
  @base_name ||= name.split(".").first
end

#cpp_nameObject



69
70
71
# File 'lib/torch/native/function.rb', line 69

def cpp_name
  @cpp_name ||= "_" + name.downcase.sub(".", "_")
end

#funcObject



10
11
12
# File 'lib/torch/native/function.rb', line 10

def func
  @func ||= @function["func"]
end

#nameObject



14
15
16
# File 'lib/torch/native/function.rb', line 14

def name
  @name ||= func.split("(", 2).first
end

#out?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/torch/native/function.rb', line 52

def out?
  out_size > 0 && base_name[-1] != "_"
end

#out_sizeObject



48
49
50
# File 'lib/torch/native/function.rb', line 48

def out_size
  @out_size ||= func.split("->").last.count("!")
end

#python_moduleObject



18
19
20
# File 'lib/torch/native/function.rb', line 18

def python_module
  @python_module ||= @function["python_module"]
end

#ruby_nameObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/torch/native/function.rb', line 56

def ruby_name
  @ruby_name ||= begin
    name = base_name
    if name.end_with?("_")
      "#{name[0..-2]}!"
    elsif name.start_with?("is_")
      "#{name[3..-1]}?"
    else
      name
    end
  end
end

#variantsObject



22
23
24
# File 'lib/torch/native/function.rb', line 22

def variants
  @variants ||= (@function["variants"] || "function").split(", ")
end