LibTorch 穩定 ABI¶
本說明最終將包含更多關於如何使用 torch/csrc/stable 中 API 的詳細資訊。目前,它包含一個內部表示的表格。
自定義擴充套件中的型別:終端使用者自定義庫中使用的型別。
StableIValue 表示:將型別穩定轉換為 ABI 穩定方式下使用者模型與 libtorch.so 之間的聯絡。
libtorch 中的型別:在 libtorch.so(或任何與 libtorch 二進位制鎖定的程式碼)中使用的型別。
Schema 型別:模式描述的型別,我們將其視為 native_functions.yaml 中 ATen 運算元和透過 TORCH_LIBRARY 或 torch.library 註冊到排程器的使用者定義自定義運算元的事實來源。
自定義擴充套件中的型別 |
StableIValue 表示 |
libtorch 中的型別 |
Schema 型別 |
|---|---|---|---|
std::optional<S> |
*reinterpret_cast<(StableIValue*)*>,指向遞迴定義的 StableIValue 的指標 |
std::optional<T> |
Type? |
std::nullopt |
*reinterpret_cast<nullptr_t*> |
IValue() |
None |
RAIIATH |
AtenTensorHandle 的 *reinterpret_cast<uint64_t*> |
at::Tensor |
Tensor |
int32_t |
*reinterpret_cast<uint64_t*> |
at::ScalarType |
ScalarType |
int32_t |
*reinterpret_cast<uint64_t*> |
at::Layout |
Layout |
int32_t |
*reinterpret_cast<uint64_t*> |
at::MemoryFormat |
MemoryFormat |
bool |
*reinterpret_cast<uint64_t*> |
bool |
bool |
int64_t |
*reinterpret_cast<uint64_t*> |
int64_t |
int |
double |
*reinterpret_cast<uint64_t*> |
double |
float |
? |
? |
c10::Device |
Device |
? |
? |
c10::Stream |
Stream |
? |
? |
c10::complex |
complex |
? |
? |
at::Scalar |
Scalar |
? |
? |
std::string/const char*/ivalue::ConstantString |
str |
? |
? |
at::Storage |
Storage |
? |
? |
at::Generator |
Generator |
? |
? |
c10::List<T> |
Type[] |
? |
? |
ivalue::Tuple<T> |
(Type, …) |
? |
? |
c10::SymInt |
SymInt |
? |
? |
c10::SymFloat |
SymFloat |
? |
? |
c10::SymBool |
SymBool |
? |
? |
at::QScheme |
QScheme |
我們有信心支援的型別是表中已完成行的型別。對於有限的一組用例,我們還隱式支援任何可表示在 64 位內的字面型別作為 StableIValues,因為預設的 reinterpret_cast 將會成功。即使在自定義擴充套件中沒有標準定義的 device 表示,您也可以在自定義 kernel 中使用 StableIValue 抽象來處理 c10::Device 等型別。例如,自定義運算元可以接受一個 StableIValue device 作為引數,並直接透過 aoti_torch_call_dispatcher 將其傳遞給一個 aten 運算元。