Tensor 基础¶
支持 PyTorch 的 ATen tensor 库是一个简单的 tensor 库,它直接在 C++17 中公开 Torch 中的 Tensor 操作。ATen 的 API 是从 PyTorch 使用的相同声明自动生成的,因此这两个 API 将随着时间的推移相互跟踪。
Tensor 类型是动态解析的,因此 API 是通用的,不包含模板。也就是说,只有一个 Tensor
类型。它可以容纳 CPU 或 CUDA Tensor,并且 tensor 可以有 Doubles、Float、Ints 等。这种设计使得编写通用代码而无需模板化所有内容变得容易。
有关提供的 API,请参阅 https://pytorch.ac.cn/cppdocs/api/namespace_at.html#functions。摘录
Tensor atan2(const Tensor & other) const;
Tensor & atan2_(const Tensor & other);
Tensor pow(Scalar exponent) const;
Tensor pow(const Tensor & exponent) const;
Tensor & pow_(Scalar exponent);
Tensor & pow_(const Tensor & exponent);
Tensor lerp(const Tensor & end, Scalar weight) const;
Tensor & lerp_(const Tensor & end, Scalar weight);
Tensor histc() const;
Tensor histc(int64_t bins) const;
Tensor histc(int64_t bins, Scalar min) const;
Tensor histc(int64_t bins, Scalar min, Scalar max) const;
还提供了就地操作,并且始终以 _ 为后缀,以指示它们将修改 Tensor。
高效访问 Tensor 元素¶
当使用 Tensor 范围的操作时,动态调度的相对成本非常小。但是,在某些情况下,尤其是在您自己的内核中,需要高效的元素级访问,并且元素级循环内部的动态调度成本非常高。ATen 提供了访问器,这些访问器通过对 Tensor 的类型和维度数量进行单次动态检查来创建。然后,访问器公开了一个 API,用于高效地访问 Tensor 元素。
访问器是 Tensor 的临时视图。它们仅在它们查看的 tensor 的生命周期内有效,因此应仅在函数本地使用,例如迭代器。
请注意,访问器与内核函数内部的 CUDA tensor 不兼容。相反,您必须使用打包访问器,它的行为方式相同,但复制 tensor 元数据而不是指向它。
因此,建议对 CPU tensor 使用访问器,对 CUDA tensor 使用打包访问器。
CPU 访问器¶
torch::Tensor foo = torch::rand({12, 12});
// assert foo is 2-dimensional and holds floats.
auto foo_a = foo.accessor<float,2>();
float trace = 0;
for(int i = 0; i < foo_a.size(0); i++) {
// use the accessor foo_a to get tensor data.
trace += foo_a[i][i];
}
CUDA 访问器¶
__global__ void packed_accessor_kernel(
torch::PackedTensorAccessor64<float, 2> foo,
float* trace) {
int i = threadIdx.x;
gpuAtomicAdd(trace, foo[i][i]);
}
torch::Tensor foo = torch::rand({12, 12});
// assert foo is 2-dimensional and holds floats.
auto foo_a = foo.packed_accessor64<float,2>();
float trace = 0;
packed_accessor_kernel<<<1, 12>>>(foo_a, &trace);
除了 PackedTensorAccessor64
和 packed_accessor64
之外,还有相应的 PackedTensorAccessor32
和 packed_accessor32
,它们使用 32 位整数进行索引。这在 CUDA 上可能会快很多,但可能会导致索引计算中的溢出。
请注意,模板可以容纳其他参数,例如指针限制和用于索引的整数类型。有关访问器和打包访问器的完整模板描述,请参阅文档。
使用外部创建的数据¶
如果您已在内存(CPU 或 CUDA)中分配了 tensor 数据,则可以将该内存视为 ATen 中的 Tensor
float data[] = { 1, 2, 3,
4, 5, 6 };
torch::Tensor f = torch::from_blob(data, {2, 3});
这些 tensor 无法调整大小,因为 ATen 不拥有内存,但其他行为与普通 tensor 相同。
标量和零维 tensor¶
除了 Tensor
对象之外,ATen 还包括 Scalar
,它表示单个数字。与 Tensor 类似,Scalars 是动态类型的,可以容纳 ATen 的任何一种数字类型。Scalars 可以从 C++ 数字类型隐式构造。需要 Scalars 是因为某些函数(如 addmm
)接受数字以及 Tensor,并期望这些数字与 tensor 具有相同的动态类型。它们也用于 API 中,以指示函数始终返回 Scalar 值的位置,例如 sum
。
namespace torch {
Tensor addmm(Scalar beta, const Tensor & self,
Scalar alpha, const Tensor & mat1,
const Tensor & mat2);
Scalar sum(const Tensor & self);
} // namespace torch
// Usage.
torch::Tensor a = ...
torch::Tensor b = ...
torch::Tensor c = ...
torch::Tensor r = torch::addmm(1.0, a, .5, b, c);
除了 Scalar
之外,ATen 还允许 Tensor
对象是零维的。这些 Tensor 容纳单个值,它们可以是较大 Tensor
中单个元素的引用。它们可以在任何期望 Tensor
的地方使用。它们通常由像 select 这样的运算符创建,这些运算符会减小 Tensor
的维度。
torch::Tensor two = torch::rand({10, 20});
two[1][2] = 4;
// ^^^^^^ <- zero-dimensional Tensor