Class: Torch::Native::Parser

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

Instance Method Summary collapse

Constructor Details

#initialize(functions) ⇒ Parser

Returns a new instance of Parser.



4
5
6
7
8
9
# File 'lib/torch/native/parser.rb', line 4

def initialize(functions)
  @functions = functions
  @name = @functions.first.ruby_name
  @min_args = @functions.map { |f| f.args.count { |a| a[:pos] && !a[:has_default] } }.min
  @max_args = @functions.map { |f| f.args.count { |a| a[:pos] } }.max
end

Instance Method Details

#parse(args, options) ⇒ Object



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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/torch/native/parser.rb', line 11

def parse(args, options)
  candidates = @functions.dup

  # remove nil
  while args.any? && args.last.nil?
    args.pop
  end

  # TODO account for args passed as options here
  if args.size < @min_args || args.size > @max_args
    expected = String.new(@min_args.to_s)
    expected += "..#{@max_args}" if @max_args != @min_args
    return {error: "wrong number of arguments (given #{args.size}, expected #{expected})"}
  end

  candidates.reject! { |f| args.size > f.args.size }

  # exclude functions missing required options
  candidates.reject! do |func|
    # TODO make more generic
    func.out? && !options[:out]
  end

  # handle out with multiple
  # there should only be one match, so safe to modify all
  out_func = candidates.find { |f| f.out? }
  if out_func && out_func.out_size > 1 && options[:out]
    out_args = out_func.args.last(2).map { |a| a[:name] }
    out_args.zip(options.delete(:out)).each do |k, v|
      options[k.to_sym] = v
    end
    candidates = [out_func]
  end

  # exclude functions where options don't match
  options.each do |k, v|
    candidates.select! do |func|
      func.args.any? { |a| a[:name] == k.to_s }
    end
    # TODO show all bad keywords at once like Ruby?
    return {error: "unknown keyword: #{k}"} if candidates.empty?
  end

  final_values = {}

  # check args
  candidates.select! do |func|
    good = true

    values = args.zip(func.args).map { |a, fa| [fa[:name], a] }.to_h
    values.merge!(options.map { |k, v| [k.to_s, v] }.to_h)
    func.args.each do |fa|
      values[fa[:name]] = fa[:default] if values[fa[:name]].nil?
    end

    arg_types = func.args.map { |a| [a[:name], a[:type]] }.to_h

    values.each_key do |k|
      v = values[k]
      t = arg_types[k].split("(").first

      good =
        case t
        when "Tensor"
          v.is_a?(Tensor)
        when "Tensor?"
          v.nil? || v.is_a?(Tensor)
        when "Tensor[]", "Tensor?[]"
          v.is_a?(Array) && v.all? { |v2| v2.is_a?(Tensor) }
        when "int"
          if k == "reduction"
            v.is_a?(String)
          else
            v.is_a?(Integer)
          end
        when "int?"
          v.is_a?(Integer) || v.nil?
        when "float?"
          v.is_a?(Numeric) || v.nil?
        when "bool?"
          v == true || v == false || v.nil?
        when "float"
          v.is_a?(Numeric)
        when /int\[.*\]/
          if v.is_a?(Integer)
            size = t[4..-2]
            raise Error, "Unknown size: #{size}. Please report a bug with #{@name}." unless size =~ /\A\d+\z/
            v = [v] * size.to_i
            values[k] = v
          end
          v.is_a?(Array) && v.all? { |v2| v2.is_a?(Integer) }
        when "Scalar"
          v.is_a?(Numeric)
        when "Scalar?"
          v.is_a?(Numeric) || v.nil?
        when "ScalarType"
          false # not supported yet
        when "ScalarType?"
          v.nil?
        when "bool"
          v == true || v == false
        when "str"
          v.is_a?(String)
        else
          raise Error, "Unknown argument type: #{arg_types[k]}. Please report a bug with #{@name}."
        end

      if !good
        if candidates.size == 1
          k = "input" if k == "self"
          return {error: "#{@name}(): argument '#{k}' must be #{t}"}
        end
        break
      end
    end

    if good
      final_values = values
    end

    good
  end

  if candidates.size != 1
    raise Error, "This should never happen. Please report a bug with #{@name}."
  end

  func = candidates.first
  args = func.args.map { |a| final_values[a[:name]] }
  args << TensorOptions.new.dtype(6) if func.tensor_options
  {
    name: func.cpp_name,
    args: args
  }
end