PyTorch
Loading...
Searching...
No Matches
DeviceGuard.h
Go to the documentation of this file.
1#pragma once
2
3#include <ATen/core/IListRef.h>
4#include <ATen/core/Tensor.h>
5#include <c10/core/DeviceGuard.h>
6#include <c10/core/ScalarType.h> // TensorList whyyyyy
7
8namespace at {
9
10// Are you here because you're wondering why DeviceGuard(tensor) no
11// longer works? For code organization reasons, we have temporarily(?)
12// removed this constructor from DeviceGuard. The new way to
13// spell it is:
14//
15// OptionalDeviceGuard guard(device_of(tensor));
16
19 if (t.defined()) {
20 return c10::make_optional(t.device());
21 } else {
22 return c10::nullopt;
23 }
24}
25
27 return t.has_value() ? device_of(t.value()) : nullopt;
28}
29
33inline c10::optional<Device> device_of(ITensorListRef t) {
34 if (!t.empty()) {
35 return device_of(t.front());
36 } else {
37 return c10::nullopt;
38 }
39}
40
41} // namespace at
bool defined() const
Definition: TensorBase.h:198
Device device() const
Returns a Tensor's device.
Definition: TensorBase.h:387
Definition: TensorBody.h:90
Definition: Optional.h:549
Definition: TensorBase.h:34
c10::optional< Device > device_of(const Tensor &t)
Return the Device of a Tensor, if the Tensor is defined.
Definition: DeviceGuard.h:18
at::Tensor t(const at::Tensor &self)
Definition: Functions.h:7681
constexpr nullopt_t nullopt
Definition: Optional.h:163
constexpr optional< typename std::decay< T >::type > make_optional(T &&v)
Definition: Optional.h:1223