class Torchrb::Lua

Attributes

debug[RW]
enable_cuda[RW]

Public Class Methods

new(options={}) click to toggle source
# File lib/torchrb/lua.rb, line 6
def initialize options={}
  self.enable_cuda = options.delete(:enable_cuda) { false }
  self.debug = options.delete(:debug) { false }
  @additional_libraries = options.delete(:lua_libs){[]}
end

Public Instance Methods

eval(command, file=__FILE__, line=__LINE__, debug: false) click to toggle source
# File lib/torchrb/lua.rb, line 13
def eval(command, file=__FILE__, line=__LINE__, debug: false)
  load_libraries unless @libraries_loaded
  @last_command = command
  puts command if debug || @debug
  state.eval command, nil, file, line
end

Protected Instance Methods

state() click to toggle source
# File lib/torchrb/lua.rb, line 21
def state
  @state ||= Rufus::Lua::State.new
end

Private Instance Methods

load(lua_lib) click to toggle source
# File lib/torchrb/lua.rb, line 80
def load lua_lib
  eval "require '#{lua_lib}'", __FILE__, __LINE__
end
load_cuda(cudnn_lib) click to toggle source
# File lib/torchrb/lua.rb, line 47
    def load_cuda(cudnn_lib)
      begin
        eval(<<-EOF) #Load the libcudnn upfront so it is in the file cache and can be found later.
              local ffi = require 'ffi'
              ffi.cdef[[size_t cudnnGetVersion();]]
              local cudnn = ffi.load("#{cudnn_lib}")
              cuda_version = tonumber(cudnn.cudnnGetVersion())
        EOF
        load "cudnn"
        load "cunn"
        p "LOADED CUDNN VERSION: #{state["cuda_version"]}"
      rescue
        self.enable_cuda = false
        load "nn"
      end
    end
load_error_handler() click to toggle source
# File lib/torchrb/lua.rb, line 64
def load_error_handler
  @state.set_error_handler do |msg|
    puts msg
    level = 2
    loop do
      info = @state.eval "return debug.getinfo(#{level}, \"nSl\")"
      break if info.nil?
      line = info['currentline'].to_i
      file, ln = *info['source'].split(":")
      puts "\t#{file}:#{line + ln.to_i} (#{info['name']})"
      level += 1
    end
    puts @last_command
  end
end
load_libraries() click to toggle source
# File lib/torchrb/lua.rb, line 26
def load_libraries
  @libraries_loaded = true
  #load "torch"
  cudnn_lib = File.realpath("lib/packages/cuda/lib64/libcudnn.so")
  raise "Extract your CUDNN to #{cudnn_lib}" if enable_cuda && !File.exists?(cudnn_lib)
  if enable_cuda
    load_cuda(cudnn_lib)
  else
    load "nn"
  end
  @additional_libraries.each do |lib|
    load lib
  end

  #load "image"
  #load "optim"
  #load 'gnuplot'
  #eval "pp = require 'pl.pretty'", __FILE__, __LINE__
  load_error_handler
end