1 #include <c10/core/Device.h> 2 #include <c10/macros/Macros.h> 3 #include <c10/util/Exception.h> 15 DeviceType parse_type(
const std::string& device_string) {
16 static const std::array<std::pair<std::string, DeviceType>, 9> types = {{
17 {
"cpu", DeviceType::CPU},
18 {
"cuda", DeviceType::CUDA},
19 {
"mkldnn", DeviceType::MKLDNN},
20 {
"opengl", DeviceType::OPENGL},
21 {
"opencl", DeviceType::OPENCL},
22 {
"ideep", DeviceType::IDEEP},
23 {
"hip", DeviceType::HIP},
24 {
"msnpu", DeviceType::MSNPU},
25 {
"xla", DeviceType::XLA},
27 auto device = std::find_if(
30 [device_string](
const std::pair<std::string, DeviceType>& p) {
31 return p.first == device_string;
33 if (
device != types.end()) {
37 "Expected one of cpu, cuda, mkldnn, opengl, opencl, ideep, hip, msnpu device type at start of device string: ", device_string);
41 void Device::validate() {
42 AT_CHECK(index_ == -1 || index_ >= 0,
43 "Device index must be -1 or non-negative, got ", index_);
44 AT_CHECK(!
is_cpu() || index_ <= 0,
45 "CPU device index must be -1 or zero, got ", index_);
72 AT_CHECK(!device_string.empty(),
"Device string must not be empty");
73 int index = device_string.find(
":");
74 if (index == std::string::npos) {
75 type_ = parse_type(device_string);
78 s = device_string.substr(0, index);
79 AT_CHECK(!s.empty(),
"Device string must not be empty");
80 type_ = parse_type(s);
82 std::string
device_index = device_string.substr(index + 1);
84 index_ = c10::stoi(device_index);
85 }
catch (
const std::exception &) {
86 AT_ERROR(
"Could not parse device index '", device_index,
87 "' in device string '", device_string,
"'");
90 "Device index must be non-negative, got ", index_);
95 std::ostream& operator<<(std::ostream& stream,
const Device&
device) {
96 stream << device.
type();
98 stream <<
":" << device.
index();
bool has_index() const noexcept
Returns true if the device has a non-default index.
TensorOptions device_index(int16_t device_index)
Convenience function that returns a TensorOptions object with the device set to CUDA and the device_i...
TensorOptions device(Device device)
Convenience function that returns a TensorOptions object with the device set to the given one...
Represents a a compute device on which a tensor is located.
bool is_cpu() const noexcept
Return true if the device is of CPU type.
To register your own kernel for an operator, do in one (!) cpp file: C10_REGISTER_KERNEL(OperatorHand...
Device(DeviceType type, DeviceIndex index=-1)
Constructs a new Device from a DeviceType and an optional device index.
DeviceIndex index() const noexcept
Returns the optional index.
DeviceType type() const noexcept
Returns the type of device this is.