Skip to content

Commit 776cd83

Browse files
authored
vulkan: use roundEven for UnaryOp round (#7119)
GLSL round() leaves the direction of halfway cases to the implementation. MoltenVK rounds them away from zero, while every cpu implementation rounds half to even, so 0.5 / 2.5 / -0.5 came out as 1 / 3 / -1 on Apple gpus. Add exact halfway inputs to test_unaryop for op_type 18.
1 parent 2cbb264 commit 776cd83

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎src/layer/vulkan/shader/unaryop.comp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void main()
4646
if (op_type == 16) res = tanh(v);
4747
#endif
4848
if (op_type == 17) res = log(v) * afp(0.434294481903);
49-
if (op_type == 18) res = round(v);
49+
if (op_type == 18) res = roundEven(v);
5050
if (op_type == 19) res = trunc(v);
5151
if (op_type == 20) res = sign(v);
5252
if (op_type == 21) res = exp(v) - afp(1.f);

‎tests/test_unaryop.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ static int test_unaryop(const ncnn::Mat& _a, int flag = 0)
6666
a[i] = v;
6767
}
6868
}
69+
#else
70+
if (op_type == 18)
71+
{
72+
// exact halfway values round to even
73+
const float halfway[8] = {0.5f, 1.5f, 2.5f, 3.5f, -0.5f, -1.5f, -2.5f, -3.5f};
74+
for (int i = 0; i < 8 && i < (int)a.total(); i++)
75+
{
76+
a[i] = halfway[i];
77+
}
78+
}
6979
#endif // __powerpc__
7080

7181
ncnn::ParamDict pd;

0 commit comments

Comments
 (0)