Module: Torch::DDP::MonkeyPatch

Defined in:
lib/torch/ddp/monkey_patch.rb

Constant Summary collapse

WARNING_PREFIX =
"[torch-ddp]".freeze

Class Method Summary collapse

Class Method Details

.apply_if_neededObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/torch/ddp/monkey_patch.rb', line 7

def apply_if_needed
  return if defined?(@applied) && @applied

  missing = missing_features
  return if missing.empty?

  warn("#{WARNING_PREFIX} Applying torch compatibility patch for: #{missing.join(', ')}. Please upgrade the torch gem for native support.")
  patch_cuda_set_device if missing.include?(:cuda_set_device)
  patch_cuda_empty_cache if missing.include?(:cuda_empty_cache)
  patch_device_helpers if missing.include?(:device_helpers)
  patch_load if missing.include?(:load_keywords)
  patch_tensor_item if missing.include?(:tensor_item_scalar)
  @applied = true
end

.load_with_device(filename, device) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/torch/ddp/monkey_patch.rb', line 333

def load_with_device(filename, device)
  fallback_load =
    if Torch.respond_to?(:_torch_ddp_original_load)
      Torch.method(:_torch_ddp_original_load)
    else
      Torch.method(:load)
    end

  return fallback_load.call(filename) unless Torch.respond_to?(:_load_with_device)

  device_str = device.respond_to?(:_str) ? device._str : device.to_s
  Torch.send(:to_ruby, Torch._load_with_device(filename, device_str))
rescue StandardError
  fallback_load.call(filename)
end