Shortcuts

Program Listing for File DeviceGuard.h

Return to documentation for file (aten/src/ATen/DeviceGuard.h)

#pragma once

#include <ATen/core/IListRef.h>
#include <ATen/core/Tensor.h>
#include <c10/core/DeviceGuard.h>
#include <c10/core/ScalarType.h> // TensorList whyyyyy

namespace at {

// Are you here because you're wondering why DeviceGuard(tensor) no
// longer works?  For code organization reasons, we have temporarily(?)
// removed this constructor from DeviceGuard.  The new way to
// spell it is:
//
//    OptionalDeviceGuard guard(device_of(tensor));

inline c10::optional<Device> device_of(const Tensor& t) {
  if (t.defined()) {
    return c10::make_optional(t.device());
  } else {
    return c10::nullopt;
  }
}

inline c10::optional<Device> device_of(const c10::optional<Tensor>& t) {
  return t.has_value() ? device_of(t.value()) : c10::nullopt;
}

inline c10::optional<Device> device_of(ITensorListRef t) {
  if (!t.empty()) {
    return device_of(t.front());
  } else {
    return c10::nullopt;
  }
}

} // namespace at

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources