LibTorch 稳定 ABI ¶
本说明将最终包含有关如何使用 torch/csrc/stable 中 API 的更多详细信息。目前,它包含内部表示法的表格:
自定义扩展中的类型:在最终用户自定义库中使用的类型。
稳定的 IValue 表示:将类型以 ABI 稳定的方式转换为用户模型与 libtorch.so 之间的连接。
libtorch 中的类型:在 libtorch.so(或任何与 libtorch 锁定二进制代码)内部使用的类型。
架构类型:由架构描述的类型,我们将其视为 native_functions.yaml 中 ATen 操作和通过 TORCH_LIBRARY 或 torch.library 注册到调度器的用户定义自定义操作的真实来源。
自定义扩展中的类型 |
稳定的 IValue 表示 |
在 libtorch 中输入 |
架构类型 |
---|---|---|---|
std::optional<S> |
*reinterpret_cast<(StableIValue*)*>, 指向递归定义的 StableIValue 的指针 |
std::optional<T> |
类型? |
std::nullopt |
nullptr 类型指针的重新解释 |
IValue() |
无 |
RAIIATH |
AtenTensorHandle 的 uint64_t 类型指针的重新解释 |
at::Tensor |
张量 |
int32_t |
*reinterpret_cast<uint64_t*> |
at::ScalarType |
标量类型 |
int32_t |
*reinterpret_cast -> *uint64_t 指针 |
at::Layout -> at::布局 |
布局 |
int32_t -> int32_t |
*reinterpret_cast -> *uint64_t 指针 |
at::内存格式 |
内存格式 |
bool |
*reinterpret_cast> |
bool |
bool |
int64_t |
*reinterpret_cast> |
int64_t |
int |
double |
*reinterpret_cast<uint64_t*> |
double |
float |
? |
? |
c10::设备 |
设备 |
? |
? |
c10::流 |
流 |
? |
? |
c10::复杂 |
复杂 |
? |
? |
at::标量 |
标量 |
? |
? |
std::string/const char*/ivalue::常量字符串 |
str |
? |
? |
at::存储 |
存储 |
? |
? |
at::生成器 |
生成器 |
? |
? |
c10::列表 |
类型[] |
? |
? |
ivalue::元组 |
(类型,…) |
? |
? |
c10::符号整型 |
SymInt |
? |
? |
c10::符号浮点型 |
SymFloat |
? |
? |
c10::符号布尔型 |
符号布尔值 |
? |
? |
at::QScheme |
QScheme |
我们自信支持的类型是表中已完成的行中的类型。对于有限的用例,我们也隐式支持任何可以表示为 64 位 StableIValues 的文本类型,因为默认的 reinterpret_cast 将成功。您可以在自定义内核中使用 StableIValue 抽象来处理 c10::Device 等类型,即使自定义扩展中没有定义设备的标准表示。例如,自定义操作符可以将 StableIValue 设备作为参数,并通过 aoti_torch_call_dispatcher 直接传递给 aten 操作符。