Rate this Page

Program Listing for File Device.h#

Return to documentation for file (c10/core/Device.h)

#pragma once#include<c10/core/DeviceType.h>#include<c10/macros/Export.h>#include<c10/util/Exception.h>#include<cstddef>#include<cstdint>#include<functional>#include<iosfwd>#include<string>namespacec10{usingDeviceIndex=int8_t;structC10_APIDevicefinal{usingType=DeviceType;/* implicit */Device(DeviceTypetype,DeviceIndexindex=-1):type_(type),index_(index){validate();}/* implicit */Device(conststd::string&device_string);booloperator==(constDevice&other)constnoexcept{returnthis->type_==other.type_&&this->index_==other.index_;}booloperator!=(constDevice&other)constnoexcept{return!(*this==other);}voidset_index(DeviceIndexindex){index_=index;}DeviceTypetype()constnoexcept{returntype_;}DeviceIndexindex()constnoexcept{returnindex_;}boolhas_index()constnoexcept{returnindex_!=-1;}boolis_cuda()constnoexcept{returntype_==DeviceType::CUDA;}boolis_privateuseone()constnoexcept{returntype_==DeviceType::PrivateUse1;}boolis_mps()constnoexcept{returntype_==DeviceType::MPS;}boolis_hip()constnoexcept{returntype_==DeviceType::HIP;}boolis_ve()constnoexcept{returntype_==DeviceType::VE;}boolis_xpu()constnoexcept{returntype_==DeviceType::XPU;}boolis_ipu()constnoexcept{returntype_==DeviceType::IPU;}boolis_xla()constnoexcept{returntype_==DeviceType::XLA;}boolis_mtia()constnoexcept{returntype_==DeviceType::MTIA;}boolis_hpu()constnoexcept{returntype_==DeviceType::HPU;}boolis_lazy()constnoexcept{returntype_==DeviceType::Lazy;}boolis_vulkan()constnoexcept{returntype_==DeviceType::Vulkan;}boolis_metal()constnoexcept{returntype_==DeviceType::Metal;}boolis_maia()constnoexcept{returntype_==DeviceType::MAIA;}boolis_meta()constnoexcept{returntype_==DeviceType::Meta;}boolis_cpu()constnoexcept{returntype_==DeviceType::CPU;}boolsupports_as_strided()constnoexcept{returntype_!=DeviceType::IPU&&type_!=DeviceType::XLA&&type_!=DeviceType::Lazy;}std::stringstr()const;private:DeviceTypetype_;DeviceIndexindex_=-1;voidvalidate(){// Removing these checks in release builds noticeably improves// performance in micro-benchmarks.// This is safe to do, because backends that use the DeviceIndex// have a later check when we actually try to switch to that device.TORCH_INTERNAL_ASSERT_DEBUG_ONLY(index_>=-1,"Device index must be -1 or non-negative, got ",static_cast<int>(index_));TORCH_INTERNAL_ASSERT_DEBUG_ONLY(!is_cpu()||index_<=0,"CPU device index must be -1 or zero, got ",static_cast<int>(index_));}};C10_APIstd::ostream&operator<<(std::ostream&stream,constDevice&device);}// namespace c10namespacestd{template<>structhash<c10::Device>{size_toperator()(c10::Deviced)constnoexcept{// Are you here because this static assert failed?  Make sure you ensure// that the bitmasking code below is updated accordingly!static_assert(sizeof(c10::DeviceType)==1,"DeviceType is not 8-bit");static_assert(sizeof(c10::DeviceIndex)==1,"DeviceIndex is not 8-bit");// Note [Hazard when concatenating signed integers]// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// We must first convert to a same-sized unsigned type, before promoting to// the result type, to prevent sign extension when any of the values is -1.// If sign extension occurs, you'll clobber all of the values in the MSB// half of the resulting integer.//// Technically, by C/C++ integer promotion rules, we only need one of the// uint32_t casts to the result type, but we put in both for explicitness's// sake.uint32_tbits=static_cast<uint32_t>(static_cast<uint8_t>(d.type()))<<16|static_cast<uint32_t>(static_cast<uint8_t>(d.index()));returnstd::hash<uint32_t>{}(bits);}};}// namespace std