Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ std::vector<std::size_t> ListToVector(List<values...>) {
return {static_cast<std::size_t>(values)...};
}

template <typename ValueType, auto... values>
bool ListContains(ValueType value, List<values...>) {
return ((value == static_cast<ValueType>(values)) || ...);
}

} // namespace infini::ops::detail

template <>
Expand Down Expand Up @@ -219,6 +224,10 @@ class Operator : public OperatorBase {

static std::vector<std::size_t> active_implementation_indices(
Device::Type dev_type) {
if (!detail::ListContains(dev_type, ActiveDevices<Key>{})) {
return {};
}

std::vector<std::size_t> result;
DispatchFunc<ActiveDevices<Key>>(
dev_type,
Expand Down
37 changes: 37 additions & 0 deletions src/pybind11_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,43 @@ inline Device::Type DeviceTypeFromString(const std::string& name) {
return Device::TypeFromString(name);
}

// Returns `nullopt` rather than aborting when the name does not resolve.
// Used by generated pybind bindings to query implementation indices for
// devices an op may not support, without crashing the process.
template <typename T = void>
inline std::optional<Device::Type> TryDeviceTypeFromString(
const std::string& name) {
static const auto kTorchNameToTypes{
detail::BuildTorchNameMap(ActiveDevices<T>{})};

auto it{kTorchNameToTypes.find(name)};

if (it != kTorchNameToTypes.cend()) {
return it->second;
}

static const std::unordered_map<std::string, Device::Type> kPlatformNames{
{"cpu", Device::Type::kCpu},
{"nvidia", Device::Type::kNvidia},
{"cambricon", Device::Type::kCambricon},
{"ascend", Device::Type::kAscend},
{"metax", Device::Type::kMetax},
{"moore", Device::Type::kMoore},
{"iluvatar", Device::Type::kIluvatar},
{"kunlun", Device::Type::kKunlun},
{"hygon", Device::Type::kHygon},
{"qy", Device::Type::kQy},
};

auto platform_it{kPlatformNames.find(name)};

if (platform_it != kPlatformNames.cend()) {
return platform_it->second;
}

return std::nullopt;
}

inline Tensor TensorFromPybind11Handle(py::handle obj) {
auto data{
reinterpret_cast<void*>(obj.attr("data_ptr")().cast<std::uintptr_t>())};
Expand Down
Loading