Incorrect result with `cblas_dgemv` vs reference netlib and other libraries
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 28/100
- Issue 類型
- 缺陷
- 描述清晰度
- 基本清楚
- 活躍度
- 停滯
- 技術堆疊
- cpp
- 領域
- performance
研究方向
首先,使用 issue 中的命令,透過 OpenBLAS 和 netlib CBLAS 編譯附帶的 C++ 重現程式,然後將 cblas_dgemv 和 cblas_ddot 的結果與樸素乘法進行比較檢查。使用 reproduction.txt 作為輸入,並比較所回報環境之間的行為。完成標準是找出最後一個元素產生差異的原因,並證明修正後的結果,同時不失去所述的數值精度。
由索引模型根據 Issue 內容生成。
描述
We recently switched to testing openBLAS on a project and are noticing some test case failures due to a matrix multiplication operation returning an incorrect result.
This issue has been observed on a variety of platforms (Ubuntu 22.04, RHEL7, RHEL9, MSYS2 mingw), a variety of compilers (clang-15, mingw-13, gcc-12, gcc-11, gcc-9), as well as a variety of openblas versions(0.3.3, 0.3.20, 0.3.21, 0.3.24), and a variety of CPUs:
- Intel(R) Xeon(R) CPU E5-4650 0 @ 2.70GHz
- Intel(R) Xeon(R) Gold 6226R CPU @ 2.90GHz
- Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz 2.59 GHz
Reproduction
I have attached a minimally reproducible example (in C++) showing the problem
Reproduction Code
#include <cblas.h>
#include <cassert>
#include <iostream>
#include <fstream>
constexpr static size_t size = 16;
void get_values(double* A, double* b, const char* binfile) {
std::fstream binaryReader;
binaryReader.open(binfile, std::ios::in | std::ios::binary);
assert(binaryReader.is_open());
for (size_t i = 0; i < (size * size); i++) {
binaryReader.read(reinterpret_cast<char *>(&A[i]), sizeof(double));
}
for (size_t i = 0; i < size; i++) {
binaryReader.read(reinterpret_cast<char *>(&b[i]), sizeof(double));
}
}
void matrixMultiply(const double* A, const double* b, double* c) {
const size_t N = size;
for (size_t i = 0; i < N; ++i) {
for (size_t j = 0; j < N; ++j){
c[i] += A[ (i * N) + j] * b[j];
}
}
}
void CBlasMatrixMultiply(const double* a, const double* b, double* c){
int32_t M = size;
int32_t N = size;
cblas_dgemv(CblasRowMajor,
CblasNoTrans,
M, N,
1.0, a, N,
b, 1,
1.0, c, 1);
}
int main(int argc, char* argv[]) {
auto* A = new double[256]();
auto* b = new double[16]();
const char* binfile = argc > 1 ? argv[1] : BIN_FILE;
get_values(A, b, binfile);
auto* c_blas = new double[16]();
CBlasMatrixMultiply(A, b, c_blas);
auto *c_mat = new double[16]();
matrixMultiply(A, b, c_mat);
printf(" BLAS MAT\n");
for (size_t i = 0; i < size; i++) {
printf("%0.18e\t%0.18e\n", c_blas[i], c_mat[i]);
}
// dot product?
auto c_ddot = cblas_ddot(size, A + (size * 15), 1, b, 1);
printf("ddot: %0.18e\n", c_ddot);
printf("dgemv: %0.18e\n", c_blas[15]);
printf("ddot == dgemv? %s\n", c_ddot == c_blas[15] ? "YES" : "NO");
printf("dgemv is 0.0? %s\n", c_blas[15] == 0.0 ? "YES" : "NO");
delete[] A;
delete[] b;
delete[] c_blas;
delete[] c_mat;
return 0;
}
Compile this code with:
g++ -o blas_test blas_test.cpp -DBIN_FILE=\"path/to/bin\" $(pkg-config --libs --cflags openblas)
And observe the following output:
OpenBLAS result
BLAS MAT
1.175201193643801378e+00 1.175201193643801822e+00
1.103638323514327002e+00 1.103638323514327224e+00
3.578143506473725477e-01 3.578143506473724922e-01
7.045563366848892062e-02 7.045563366848883735e-02
9.965128148869371871e-03 9.965128148869309421e-03
1.099586127207577424e-03 1.099586127207556390e-03
9.945433911373591229e-05 9.945433911360671605e-05
7.620541308983597162e-06 7.620541308896932986e-06
5.064719745540013918e-07 5.064719744437437483e-07
2.971814122565419325e-08 2.971814140421127963e-08
1.560886642160141946e-09 1.560886564391797127e-09
7.419920233786569952e-11 7.419902813631537848e-11
3.221201083647429186e-12 3.221406076314455686e-12
1.292299600663682213e-13 1.289813102624490508e-13
4.440892098500626162e-15 4.440892098500626162e-15
0.000000000000000000e+00 -4.440892098500626162e-16
ddot: 0.000000000000000000e+00
dgemv: 0.000000000000000000e+00
ddot == dgemv? YES
dgemv is 0.0? YES
It is worth noting that only the last value is different outside of acceptable numerical precision, and that every other value passes within 1e-16. Furthermore, a value of exactly 0.0 is, in itself, suspicious, as there's no real circumstance the value could be that.
Change the compile command to:
# cblas here is netlib
g++ -o blas_test blas_test.cpp -DBIN_FILE=\"path/to/bin\" $(pkg-config --libs --cflags cblas)
and observe this result:
netlib result
BLAS MAT
1.175201193643801822e+00 1.175201193643801822e+00
1.103638323514327224e+00 1.103638323514327224e+00
3.578143506473724922e-01 3.578143506473724922e-01
7.045563366848883735e-02 7.045563366848883735e-02
9.965128148869309421e-03 9.965128148869309421e-03
1.099586127207556390e-03 1.099586127207556390e-03
9.945433911360671605e-05 9.945433911360671605e-05
7.620541308896932986e-06 7.620541308896932986e-06
5.064719744437437483e-07 5.064719744437437483e-07
2.971814140421127963e-08 2.971814140421127963e-08
1.560886564391797127e-09 1.560886564391797127e-09
7.419902813631537848e-11 7.419902813631537848e-11
3.221406076314455686e-12 3.221406076314455686e-12
1.289813102624490508e-13 1.289813102624490508e-13
4.440892098500626162e-15 4.440892098500626162e-15
-4.440892098500626162e-16 -4.440892098500626162e-16
ddot: -4.440892098500626162e-16
dgemv: -4.440892098500626162e-16
ddot == dgemv? YES
dgemv is 0.0? NO
Here is the binary file that contains a 16x16 matrix and a 16x1 vector:
(NOTE: This is a binary data file, extension changed to make github happy)
reproduction.txt
Other Notes
We have done extensive testing in other BLAS-like environments to get a result close to the expected -4e-16 result, which passes our test. Both MATLAB (2023a) and numpy (1.26 w/ MKL) return a result very close to what we expect, and pass our test. And, obviously, our naive matrix multiplication in the reproduction code gives
The matrix in question is not overly ill-conditioned, it has a condition number of ~10.
- 主要語言
- C
- 星號
- 7.6k
- 分支
- 1.7k
- 平均合併
- 1 天 6 小時
- 30 天內合併 PR
- 46
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
OpenMathLib/OpenBLAS 的其他 Issue
-
難度 3/5 1-2 天 新手友好度 68/100
OpenMathLib/OpenBLAS#6069 ·
維護者通常 1 天內回覆
-
難度 4/5 3-5 天 新手友好度 52/100
OpenMathLib/OpenBLAS#6059 ·
維護者通常 1 天內回覆
-
難度 4/5 3-5 天 新手友好度 48/100
OpenMathLib/OpenBLAS#6029 · 21 則留言 ·
維護者通常 1 天內回覆
-
難度 3/5 1-2 天 新手友好度 68/100
OpenMathLib/OpenBLAS#6028 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 4/5 3-5 天 新手友好度 35/100
OpenMathLib/OpenBLAS#6005 · 21 則留言 · 2 個 reaction ·
維護者通常 1 天內回覆
查看 OpenMathLib/OpenBLAS 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 90/100
BasedHardware/omi#19711 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 85/100
microsoft/ebpf-for-windows#5604 ·
維護者通常 3 天內回覆
-
難度 2/5 1-3 小時 新手友好度 68/100
trezor/trezor-firmware#7985 ·
維護者通常 2 天內回覆
-
難度 2/5 1-3 小時 新手友好度 84/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 70/100
維護者通常 2 天內回覆