[BUG] SIFT: Inverted backward-substitution loop in `gaussianElimination`, corrupted `dys` Hessian stencil, stale layer pointers, and unguarded `1/sqrt(0)` `NaN` in `normalizeDesc` (CPU, CUDA, OpenCL)
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 45/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- cpp
調査の方向性
Read the SIFT implementations in src/backend/cpu/kernel/sift.hpp, src/backend/cuda/kernel/sift.hpp, and src/backend/opencl/kernel/sift_nonfree.cl, focusing on gaussianElimination, interpolateExtrema, and descriptor normalization. Start by comparing the three backend versions and use the reproducer in the issue to verify the reported numerical failures. Done means addressing all four reported bugs consistently across the backends and confirming the SIFT/GLOH results no longer show the described failures.
索引モデルが issue の本文から書いたものです。
説明
Four compounding numerical bugs affect SIFT/GLOH keypoint localization and descriptor normalization identically across all three backends (CPU, CUDA, and OpenCL).
Description
- Affected backends: All three backends — CPU (
src/backend/cpu/kernel/sift.hpp), CUDA (src/backend/cuda/kernel/sift.hpp), and OpenCL (src/backend/opencl/kernel/sift_nonfree.cl). - Pinned commit:
b11d7007980bee208f8e1c89dfbd9131790110f8(master). - Reproducibility: 100% deterministic.
1. Backward substitution in gaussianElimination runs forward and skips x[N - 1]
In src/backend/cuda/kernel/sift.hpp:144-156 (identical in src/backend/cpu/kernel/sift.hpp:150-159 and src/backend/opencl/kernel/sift_nonfree.cl:120-129):
for (int i = 0; i < N; i++) x[i] = 0;
// backward substitution
float sum = 0;
for (int i = 0; i <= N - 2; i++) {
sum = b[i];
for (int j = i + 1; j < N; j++) sum -= A[i * N + j] * x[j];
x[i] = sum / A[i * N + i];
}
The loop iterates forward (i = 0 .. N - 2) instead of backward (i = N - 1 down to 0):
x[N - 1](X[2], the scale-space offsetxl = -X[2]) is never computed and stays0.0ffor every SIFT keypoint.- When computing
x[0]andx[1], everyx[j](j > i) is still0.0f, so the inner loop subtracts zero and ignores all upper-triangular Hessian terms (A[0,1],A[0,2],A[1,2]). - On the symmetric positive-definite 3×3 system
H = [[4, 1, 1], [1, 4, 1], [1, 1, 4]],b = [6, 6, 6](exact solution[1.0, 1.0, 1.0]),gaussianElimination<3>returns[1.5, 1.2, 0.0].
2. Corrupted mixed-derivative finite-difference stencil for dys
In interpolateExtrema across all 3 backends (cpu/kernel/sift.hpp:281-283, cuda/kernel/sift.hpp:406-408, opencl/kernel/sift_nonfree.cl:404-406):
float dxs = (NPTR(x + 1, y) - NPTR(x - 1, y) - PPTR(x + 1, y) +
PPTR(x - 1, y)) * cross_deriv_scale;
float dys = (NPTR(x, y + 1) - NPTR(x - 1, y - 1) - PPTR(x, y - 1) +
PPTR(x - 1, y - 1)) * cross_deriv_scale;
The mixed derivative $\frac{\partial^2 D}{\partial y \partial s}$ should be (NPTR(x, y + 1) - NPTR(x, y - 1) - PPTR(x, y + 1) + PPTR(x, y - 1)) * cross_deriv_scale. Instead, the second term samples NPTR(x - 1, y - 1), the third term samples PPTR(x, y - 1) instead of PPTR(x, y + 1), and the fourth term samples PPTR(x - 1, y - 1).
3. Stale prev_ptr / center_ptr / next_ptr outside the MAX_INTERP_STEPS loop
In interpolateExtrema (cpu/kernel/sift.hpp:255-306, cuda/kernel/sift.hpp:384-429, opencl/kernel/sift_nonfree.cl:382-427), prev_ptr, center_ptr, and next_ptr are computed once before for (i = 0; i < MAX_INTERP_STEPS; i++). Inside the loop, layer += round(xl); updates layer, but the three pointers are never refreshed. This was previously hidden only because Bug 1 forced xl = -X[2] = 0.0f; once Bug 1 is fixed, subsequent iterations and the post-loop contrast check will read from the wrong DoG scale layer unless these pointers are recomputed after layer changes.
4. Unguarded 1.0f / sqrtf(len_sq) produces NaN descriptors on zero-gradient patches
In normalizeDesc and normalizeGLOHDesc across all 3 backends (cpu/kernel/sift.hpp:502-510, cuda/kernel/sift.hpp:183-188, 221-226, opencl/kernel/sift_nonfree.cl:173-178, 213-218):
float len_sq = accum[0];
float len_inv = 1.0f / sqrtf(len_sq);
for (int i = tid_x; i < histlen; i += bsz_x) {
desc[tid_y * histlen + i] *= len_inv;
}
When all histogram bins are 0.0f (flat/zero-gradient patch, or after clipping), len_sq == 0.0f, giving len_inv = +Inf and 0.0f * +Inf = NaN across all 128 SIFT (or 272 GLOH) descriptor elements. (In addition, sift_nonfree.cl:153-155 and 189-191 contain dead O(histlen) loops computing an unused float sum.)
Suggested Fix
Across src/backend/cpu/kernel/sift.hpp, src/backend/cuda/kernel/sift.hpp, and src/backend/opencl/kernel/sift_nonfree.cl:
- Fix backward substitution in
gaussianElimination:
- for (int i = 0; i <= N - 2; i++) {
+ for (int i = N - 1; i >= 0; i--) {
sum = b[i];
for (int j = i + 1; j < N; j++) sum -= A[i * N + j] * x[j];
x[i] = sum / A[i * N + i];
}
- Fix the
dysstencil ininterpolateExtrema:
- float dys = (NPTR(x, y + 1) - NPTR(x - 1, y - 1) - PPTR(x, y - 1) +
- PPTR(x - 1, y - 1)) * cross_deriv_scale;
+ float dys = (NPTR(x, y + 1) - NPTR(x, y - 1) - PPTR(x, y + 1) +
+ PPTR(x, y - 1)) * cross_deriv_scale;
- Recompute
prev_ptr,center_ptr,next_ptrinside theMAX_INTERP_STEPSloop (and once more after the loop iflayerchanged before the contrast calculation). - Guard
normalizeDesc/normalizeGLOHDescagainstlen_sq == 0.0f(e.g.,float len_inv = (len_sq > 0.0f) ? (1.0f / sqrtf(len_sq)) : 0.0f;), and remove the dead unusedsumloops insift_nonfree.cl.
Reproducible Code and/or Steps
Minimal standalone CUDA reproducer extracted directly from src/backend/cuda/kernel/sift.hpp (gaussianElimination<3> and normalizeDesc):
#include <cmath>
#include <cstdio>
#include <cuda_runtime.h>
template <int N, bool FIXED>
__device__ void gaussianElimination(float* A, float* b, float* x) {
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
float s = A[j * N + i] / A[i * N + i];
for (int k = i; k < N; k++) A[j * N + k] -= s * A[i * N + k];
b[j] -= s * b[i];
}
}
for (int i = 0; i < N; i++) x[i] = 0.0f;
if (!FIXED) {
for (int i = 0; i <= N - 2; i++) {
float sum = b[i];
for (int j = i + 1; j < N; j++) sum -= A[i * N + j] * x[j];
x[i] = sum / A[i * N + i];
}
} else {
for (int i = N - 1; i >= 0; i--) {
float sum = b[i];
for (int j = i + 1; j < N; j++) sum -= A[i * N + j] * x[j];
x[i] = sum / A[i * N + i];
}
}
}
__global__ void test_sift(float* x_buggy, float* x_fixed, float* desc_buggy, float* desc_fixed) {
if (threadIdx.x == 0) {
float A1[9] = {4,1,1, 1,4,1, 1,1,4}, b1[3] = {6,6,6};
float A2[9] = {4,1,1, 1,4,1, 1,1,4}, b2[3] = {6,6,6};
gaussianElimination<3, false>(A1, b1, x_buggy);
gaussianElimination<3, true>(A2, b2, x_fixed);
}
int i = threadIdx.x;
if (i < 128) {
float len_sq = 0.0f;
float inv_buggy = 1.0f / sqrtf(len_sq);
float inv_fixed = (len_sq > 0.0f) ? (1.0f / sqrtf(len_sq)) : 0.0f;
desc_buggy[i] = 0.0f * inv_buggy;
desc_fixed[i] = 0.0f * inv_fixed;
}
}
int main() {
float *dxb, *dxf, *ddb, *ddf, xb[3], xf[3], db[128], df[128];
cudaMalloc(&dxb, sizeof(xb)); cudaMalloc(&dxf, sizeof(xf));
cudaMalloc(&ddb, sizeof(db)); cudaMalloc(&ddf, sizeof(df));
test_sift<<<1, 128>>>(dxb, dxf, ddb, ddf);
cudaDeviceSynchronize();
cudaMemcpy(xb, dxb, sizeof(xb), cudaMemcpyDeviceToHost);
cudaMemcpy(xf, dxf, sizeof(xf), cudaMemcpyDeviceToHost);
cudaMemcpy(db, ddb, sizeof(db), cudaMemcpyDeviceToHost);
cudaMemcpy(df, ddf, sizeof(df), cudaMemcpyDeviceToHost);
int nb = 0, nf = 0;
for (int i = 0; i < 128; i++) { nb += std::isnan(db[i]); nf += std::isnan(df[i]); }
printf("gauss_buggy=[%.6f,%.6f,%.6f] gauss_fixed=[%.6f,%.6f,%.6f] exact=[1,1,1]\n",
xb[0], xb[1], xb[2], xf[0], xf[1], xf[2]);
printf("normalizeDesc(zero_patch): buggy_NaNs=%d/128 fixed_NaNs=%d/128\n", nb, nf);
return 0;
}
Compiled and run with nvcc -arch=sm_89 -O2 repro_sift.cu -o repro_sift && ./repro_sift:
gauss_buggy=[1.500000,1.200000,0.000000] gauss_fixed=[1.000000,1.000000,1.000000] exact=[1,1,1]
normalizeDesc(zero_patch): buggy_NaNs=128/128 fixed_NaNs=0/128
System Information
- ArrayFire version:
master(b11d7007980bee208f8e1c89dfbd9131790110f8) - OS / Device: Linux x86_64, NVIDIA L40 (
sm_89), CUDA 12.8 (nvcc 12.8) - Affected backends: CPU, CUDA, and OpenCL (source-identical across all three backends)
Checklist
- Using the latest available ArrayFire release
- GPU drivers are up to date
- 主要言語
- C++
- スター
- 4.9k
- フォーク
- 557
- 平均マージ
- 12時間 4分
- マージ済み PR(30日)
- 7
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
arrayfire/arrayfire のほかの issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
-
[BUG] Windows: getErrorMessage() reads uninitialized pointer when FormatMessage fails対応中かも @melonakos が 82 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
-
[BUG] Unified backend: af_get_last_error calls unresolved symbol without null check対応中かも @melonakos が 82 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
[BUG] af_device_info: CUDA backend writes 257 bytes into buffers documented as 64対応中かも @melonakos が 82 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
bug
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
arrayfire/arrayfire の issue をすべて見る
似ている issue
-
Invalid-MissingHash/Commit/NotAC
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
azerothcore/azerothcore-wotlk#28636 ·
メンテナーはふだん 1 日以内に返信
-
(S1 - Need confirmation)
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
CleverRaven/Cataclysm-DDA#89829 ·
メンテナーはふだん 1 日以内に返信
-
comp-documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
ClickHouse/ClickHouse#136277 ·
メンテナーはふだん 1 日以内に返信
-
Flow Needs: Triage :mag:
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
react/react-native#60447 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 1 日以内に返信