模板规范但未使用模板参数?

发布于 2025-01-10 23:10:05 字数 1367 浏览 0 评论 0原文

我正在 Pytorch 存储库中查看此文件: https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/quantized/cpu/conv_packed_pa​​rams.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦境 2025-01-17 23:10:06

我不确定您给出的这个特定示例,但我能想到的一种用法是简单地根据模板参数重载某些函数,就像在这个小示例中一样:

#include <iostream>

template <int dimension = 1>
struct Space {};

template <int d>
void what(const Space<d>&) {
    std::cout << "Some space with dimension " << d << ".\n";
}

void what(const Space<1>&) { std::cout << "A line.\n"; }
void what(const Space<2>&) { std::cout << "A plane.\n"; }
void what(const Space<3>&) { std::cout << "A three-dimensional space.\n"; }

int main() {
    Space<> one_d;
    Space<2> two_d;
    Space<3> three_d;
    Space<4> four_d;

    what(one_d);
    what(two_d);
    what(three_d);
    what(four_d);
}

这将打印:

A line.
A plane.
A three-dimensional space.
Some space with dimension 4.

另请参阅 此处

这是有效的,因为基本上每个模板实例化都构成一个新类型,就好像您已经为每个(必要的)维度声明了单独的类型。因此,这将允许用户根据实例化类的维度来实现不同的行为。

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:

#include <iostream>

template <int dimension = 1>
struct Space {};

template <int d>
void what(const Space<d>&) {
    std::cout << "Some space with dimension " << d << ".\n";
}

void what(const Space<1>&) { std::cout << "A line.\n"; }
void what(const Space<2>&) { std::cout << "A plane.\n"; }
void what(const Space<3>&) { std::cout << "A three-dimensional space.\n"; }

int main() {
    Space<> one_d;
    Space<2> two_d;
    Space<3> three_d;
    Space<4> four_d;

    what(one_d);
    what(two_d);
    what(three_d);
    what(four_d);
}

This will print:

A line.
A plane.
A three-dimensional space.
Some space with dimension 4.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文