模板规范但未使用模板参数?
我正在 Pytorch 存储库中查看此文件: https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/quantized/cpu/conv_packed_params.h
#pragma once
#include <ATen/ATen.h>
#include <ATen/core/ivalue.h>
template <int kSpatialDim = 2>
struct ConvPackedParamsBase : public torch::jit::CustomClassHolder {
virtual at::Tensor apply(
const at::Tensor& input,
double output_scale,
int64_t output_zero_point) = 0;
virtual at::Tensor apply_relu(
const at::Tensor& input,
double output_scale,
int64_t output_zero_point) = 0;
virtual at::Tensor apply_dynamic(
const at::Tensor& input,
bool reduce_range) = 0;
virtual std::tuple<at::Tensor, c10::optional<at::Tensor>> unpack() = 0;
virtual torch::List<int64_t> stride() const = 0;
virtual torch::List<int64_t> padding() const = 0;
virtual torch::List<int64_t> output_padding() const = 0;
virtual torch::List<int64_t> dilation() const = 0;
virtual int64_t groups() const = 0;
virtual bool transpose() const = 0;
};
它似乎是一个模板规范,但是 kSpatialDim
从未被使用过。我想知道这样的设计目的是什么?
I was looking at this file in the Pytorch repo: https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/quantized/cpu/conv_packed_params.h
#pragma once
#include <ATen/ATen.h>
#include <ATen/core/ivalue.h>
template <int kSpatialDim = 2>
struct ConvPackedParamsBase : public torch::jit::CustomClassHolder {
virtual at::Tensor apply(
const at::Tensor& input,
double output_scale,
int64_t output_zero_point) = 0;
virtual at::Tensor apply_relu(
const at::Tensor& input,
double output_scale,
int64_t output_zero_point) = 0;
virtual at::Tensor apply_dynamic(
const at::Tensor& input,
bool reduce_range) = 0;
virtual std::tuple<at::Tensor, c10::optional<at::Tensor>> unpack() = 0;
virtual torch::List<int64_t> stride() const = 0;
virtual torch::List<int64_t> padding() const = 0;
virtual torch::List<int64_t> output_padding() const = 0;
virtual torch::List<int64_t> dilation() const = 0;
virtual int64_t groups() const = 0;
virtual bool transpose() const = 0;
};
It appears to be a template specification but kSpatialDim
is never used. I was wondering what is the purpose of this kind of design?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您给出的这个特定示例,但我能想到的一种用法是简单地根据模板参数重载某些函数,就像在这个小示例中一样:
这将打印:
另请参阅 此处。
这是有效的,因为基本上每个模板实例化都构成一个新类型,就好像您已经为每个(必要的)维度声明了单独的类型。因此,这将允许用户根据实例化类的维度来实现不同的行为。
I am not sure about this particular example you've given, but one usage I could think of is simply overloading some function based on the template argument, like in this tiny example:
This will print:
See also here.
This works because basically every template instantiation constitutes a new type, as if you've had declared separate types for every (necessary) dimension. So this will allow users to implement different behavior depending on the dimension that the class is instantiated with.