|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <cstdint> |
| 12 | +#include <functional> |
| 13 | +#include <string> |
| 14 | + |
| 15 | +#include <executorch/backends/aoti/slim/c10/core/DeviceType.h> |
| 16 | +#include <executorch/runtime/platform/assert.h> |
| 17 | + |
| 18 | +namespace executorch::backends::aoti::slim::c10 { |
| 19 | + |
| 20 | +/// An index representing a specific device; e.g., the 1 in GPU 1. |
| 21 | +/// A DeviceIndex is not independently meaningful without knowing |
| 22 | +/// the DeviceType it is associated; try to use Device rather than |
| 23 | +/// DeviceIndex directly. |
| 24 | +using DeviceIndex = int8_t; |
| 25 | + |
| 26 | +/// Represents a compute device on which a tensor is located. |
| 27 | +/// A device is uniquely identified by a type (e.g., CPU) and a device index. |
| 28 | +struct Device final { |
| 29 | + /// Constructs a new Device from a DeviceType and an optional device index. |
| 30 | + /// @param type The type of device. |
| 31 | + /// @param index The device index. For CPU, this should be -1 or 0. |
| 32 | + /* implicit */ |
| 33 | + explicit Device(DeviceType type, DeviceIndex index = -1) |
| 34 | + : type_(type), index_(index) { |
| 35 | + validate(); |
| 36 | + } |
| 37 | + |
| 38 | + /// Constructs a Device from a string description. |
| 39 | + /// The string must be "cpu" or "cpu:0". |
| 40 | + /* implicit */ Device(const std::string& device_string) |
| 41 | + : Device(DeviceType::CPU) { |
| 42 | + ET_CHECK_MSG(!device_string.empty(), "Device string must not be empty"); |
| 43 | + |
| 44 | + if (device_string == "cpu" || device_string == "CPU") { |
| 45 | + type_ = DeviceType::CPU; |
| 46 | + index_ = -1; |
| 47 | + } else if (device_string == "cpu:0" || device_string == "CPU:0") { |
| 48 | + type_ = DeviceType::CPU; |
| 49 | + index_ = static_cast<DeviceIndex>(device_string.back() - '0'); |
| 50 | + } else { |
| 51 | + ET_CHECK_MSG( |
| 52 | + false, |
| 53 | + "Invalid device string: %s. Currently only 'cpu' is supported.", |
| 54 | + device_string.c_str()); |
| 55 | + } |
| 56 | + validate(); |
| 57 | + } |
| 58 | + |
| 59 | + /// Returns true if the type and index of this Device matches that of other. |
| 60 | + bool operator==(const Device& other) const noexcept { |
| 61 | + return this->type_ == other.type_ && this->index_ == other.index_; |
| 62 | + } |
| 63 | + |
| 64 | + /// Returns true if the type or index of this Device differs from that of |
| 65 | + /// other. |
| 66 | + bool operator!=(const Device& other) const noexcept { |
| 67 | + return !(*this == other); |
| 68 | + } |
| 69 | + |
| 70 | + /// Sets the device index. |
| 71 | + void set_index(DeviceIndex index) { |
| 72 | + index_ = index; |
| 73 | + } |
| 74 | + |
| 75 | + /// Returns the type of device this is. |
| 76 | + DeviceType type() const noexcept { |
| 77 | + return type_; |
| 78 | + } |
| 79 | + |
| 80 | + /// Returns the device index. |
| 81 | + DeviceIndex index() const noexcept { |
| 82 | + return index_; |
| 83 | + } |
| 84 | + |
| 85 | + /// Returns true if the device has a non-default index. |
| 86 | + bool has_index() const noexcept { |
| 87 | + return index_ != -1; |
| 88 | + } |
| 89 | + |
| 90 | + /// Returns true if the device is of CPU type. |
| 91 | + bool is_cpu() const noexcept { |
| 92 | + return type_ == DeviceType::CPU; |
| 93 | + } |
| 94 | + |
| 95 | + /// Returns a string representation of the device (e.g., "cpu" or "cpu:0"). |
| 96 | + std::string str() const { |
| 97 | + std::string str = DeviceTypeName(type(), /* lower_case */ true); |
| 98 | + if (has_index()) { |
| 99 | + str.push_back(':'); |
| 100 | + str.append(std::to_string(index())); |
| 101 | + } |
| 102 | + return str; |
| 103 | + } |
| 104 | + |
| 105 | + private: |
| 106 | + DeviceType type_; |
| 107 | + DeviceIndex index_ = -1; |
| 108 | + |
| 109 | + void validate() { |
| 110 | + ET_DCHECK_MSG( |
| 111 | + index_ >= -1, |
| 112 | + "Device index must be -1 or non-negative, got %d", |
| 113 | + static_cast<int>(index_)); |
| 114 | + ET_DCHECK_MSG( |
| 115 | + !is_cpu() || index_ <= 0, |
| 116 | + "CPU device index must be -1 or zero, got %d", |
| 117 | + static_cast<int>(index_)); |
| 118 | + } |
| 119 | +}; |
| 120 | + |
| 121 | +inline std::ostream& operator<<(std::ostream& stream, const Device& device) { |
| 122 | + stream << device.str(); |
| 123 | + return stream; |
| 124 | +} |
| 125 | + |
| 126 | +} // namespace executorch::backends::aoti::slim::c10 |
| 127 | + |
| 128 | +namespace std { |
| 129 | +template <> |
| 130 | +struct hash<executorch::backends::aoti::slim::c10::Device> { |
| 131 | + size_t operator()( |
| 132 | + executorch::backends::aoti::slim::c10::Device d) const noexcept { |
| 133 | + static_assert( |
| 134 | + sizeof(executorch::backends::aoti::slim::c10::DeviceType) == 1, |
| 135 | + "DeviceType is not 8-bit"); |
| 136 | + static_assert( |
| 137 | + sizeof(executorch::backends::aoti::slim::c10::DeviceIndex) == 1, |
| 138 | + "DeviceIndex is not 8-bit"); |
| 139 | + uint32_t bits = static_cast<uint32_t>(static_cast<uint8_t>(d.type())) |
| 140 | + << 16 | |
| 141 | + static_cast<uint32_t>(static_cast<uint8_t>(d.index())); |
| 142 | + return std::hash<uint32_t>{}(bits); |
| 143 | + } |
| 144 | +}; |
| 145 | +} // namespace std |
0 commit comments