张量不会在超出范围的索引上抛出异常

Tensor does not throw exception on out of range index

在这里,我创建了一个 1 乘 1 的张量。我尝试在 (1,0) 和 (0,1) 处访问并设置一个数字,这两个数字都超出了范围索引。我期待得到断言或异常错误。

Eigen::Tensor<int,2> data(1,1);
auto n1 = data(1,0);
data(0,1) = 1;

但它运行得很好。但是如果我尝试用相同形状的矩阵做同样的事情,我会得到预期的断言错误。

Eigen::Matrix<int,1,1> matrix;
auto n2 = matrix(1,0); // Signal: SIGABRT (Aborted)
matrix(0,1) = 1; // same error here

检查 Eigen headers,我发现张量实现会像这样检查索引(尽管我不知道它是如何工作的):

bool checkIndexRange(const array<Index, NumIndices>& indices) const
{

  // ...

  return
    // check whether the indices are all >= 0
    array_apply_and_reduce<logical_and_op, greater_equal_zero_op>(indices) &&
    // check whether the indices fit in the dimensions
    array_zip_and_reduce<logical_and_op, lesser_op>(indices, m_storage.dimensions());
// m_storage.dimensions() evaluates to {1,1}
}

矩阵实现使用以下代码检查索引:

eigen_assert(row >= 0 && row < rows()
          && col >= 0 && col < cols());

我查看了 Eigen 文档 here,但它没有说明任何关于超出范围的行为。我的问题是:当我访问张量中超出范围的数字时,为什么我没有得到超出范围的断言错误?

编辑:我的 Eigen 版本是 3.3.90

Tensor 模块在这里似乎只做 eigen_internal_assert,而不是像 Core 模块那样做 eigen_assert。要激活内部断言,您需要使用 -DEIGEN_INTERNAL_DEBUGGING 进行编译——但这可能会显着降低 Eigen 的速度(当然,取决于您在做什么)。