TEST(JIT, CPP_Multi_strided) { const int num = 1024; gforSet(true); array a = randu(num, 1, s32); array b = randu(1, num, s32); array x = a + b; array y = a - b; eval(x, y); gforSet(false); vector<int> ha(num); vector<int> hb(num); vector<int> hx(num * num); vector<int> hy(num * num); a.host(&ha[0]); b.host(&hb[0]); x.host(&hx[0]); y.host(&hy[0]); for (int j = 0; j < num; j++) { for (int i = 0; i < num; i++) { ASSERT_EQ((ha[i] + hb[j]), hx[j*num + i]); ASSERT_EQ((ha[i] - hb[j]), hy[j*num + i]); } } }
TEST(JIT, CPP_Multi_pre_eval) { const int num = 1 << 16; array a = randu(num, s32); array b = randu(num, s32); array x = a + b; array y = a - b; eval(x); // Should evaluate only y eval(x, y); // Should not evaluate anything // Should not error out eval(x, y); vector<int> ha(num); vector<int> hb(num); vector<int> hx(num); vector<int> hy(num); a.host(&ha[0]); b.host(&hb[0]); x.host(&hx[0]); y.host(&hy[0]); for (int i = 0; i < num; i++) { ASSERT_EQ((ha[i] + hb[i]), hx[i]); ASSERT_EQ((ha[i] - hb[i]), hy[i]); } }
TEST(MatrixMultiply, RhsBroadcastBatched) { const int M = 512; const int K = 512; const int N = 10; const int D2 = 2; const int D3 = 3; for (int d3 = 1; d3 <= D3; d3 *= D3) { for (int d2 = 1; d2 <= D2; d2 *= D2) { array a = randu(M, K, d2, d3); array b = randu(K, N); array c = matmul(a, b); for (int j = 0; j < d3; j++) { for (int i = 0; i < d2; i++) { array a_ij = a(span, span, i, j); array c_ij = c(span, span, i, j); array res = matmul(a_ij, b); EXPECT_LT(max<float>(abs(c_ij - res)), 1E-3) << " for d2 = " << d2 << " for d3 = " << d3; } } } } }
TEST(JIT, TransposeBuffers) { const int num = 10; array a = randu(1, num); array b = randu(1, num); array c = a + b; array d = a.T() + b.T(); vector<float> ha(a.elements()); a.host(ha.data()); vector<float> hb(b.elements()); b.host(hb.data()); vector<float> hc(c.elements()); c.host(hc.data()); vector<float> hd(d.elements()); d.host(hd.data()); for (int i = 0; i < num; i++) { ASSERT_FLOAT_EQ(ha[i] + hb[i], hc[i]); ASSERT_FLOAT_EQ(hc[i], hd[i]); } }
void selectTest(const dim4 &dims) { if (noDoubleTests<T>()) return; dtype ty = (dtype)dtype_traits<T>::af_type; array a = randu(dims, ty); array b = randu(dims, ty); if (a.isinteger()) { a = (a % (1 << 30)).as(ty); b = (b % (1 << 30)).as(ty); } array cond = randu(dims, ty) > a; array c = select(cond, a, b); int num = (int)a.elements(); vector<T> ha(num); vector<T> hb(num); vector<T> hc(num); vector<char> hcond(num); a.host(&ha[0]); b.host(&hb[0]); c.host(&hc[0]); cond.host(&hcond[0]); for (int i = 0; i < num; i++) { ASSERT_EQ(hc[i], hcond[i] ? ha[i] : hb[i]); } }
void selectScalarTest(const dim4 &dims) { if (noDoubleTests<T>()) return; dtype ty = (dtype)dtype_traits<T>::af_type; array a = randu(dims, ty); array cond = randu(dims, ty) > a; double b = 3; if (a.isinteger()) { a = (a % (1 << 30)).as(ty); } array c = is_right ? select(cond, a, b) : select(cond, b, a); int num = (int)a.elements(); vector<T> ha(num); vector<T> hc(num); vector<char> hcond(num); a.host(&ha[0]); c.host(&hc[0]); cond.host(&hcond[0]); if (is_right) { for (int i = 0; i < num; i++) { ASSERT_EQ(hc[i], hcond[i] ? ha[i] : T(b)); } } else { for (int i = 0; i < num; i++) { ASSERT_EQ(hc[i], hcond[i] ? T(b) : ha[i]); } } }
TEST(JIT, NonLinearBuffers2) { array a = randu(100, 310); array b = randu(10, 10); for (int i = 0; i < 300; i++) { b += a(seq(10), seq(i, i+9)) * randu(10, 10); } b.eval(); }
TEST(JoinMany0, CPP) { array a0 = randu(10, 5); array a1 = randu(20, 5); array a2 = randu(5, 5); array output = join(0, a0, a1, a2); array gold = join(0, a0, join(0, a1, a2)); ASSERT_EQ(sum<float>(output - gold), 0); }
TEST(MatrixManipulation, SNIPPET_matrix_manipulation_transpose) { //! [ex_matrix_manipulation_transpose] array x = randu(2, 2, f32); af_print(x.T()); // transpose (real) array c = randu(2, 2, c32); af_print(c.T()); // transpose (complex) af_print(c.H()); // Hermitian (conjugate) transpose //! [ex_matrix_manipulation_transpose] }
TEST(JIT, NonLinearBuffers1) { array a = randu(5, 5); array a0 = a; for (int i = 0; i < 1000; i++) { array b = randu(1, 5); a += tile(b, 5); } a.eval(); }
TEST(JoinMany1, CPP) { array a0 = randu(20, 200); array a1 = randu(20, 400); array a2 = randu(20, 10); array a3 = randu(20, 100); int dim = 1; array output = join(dim, a0, a1, a2, a3); array gold = join(dim, a0, join(dim, a1, join(dim, a2, a3))); ASSERT_EQ(sum<float>(output - gold), 0); }
TEST(MatrixMultiply, ISSUE_1882) { const int m = 2; const int n = 3; array A = randu(m, n); array BB = randu(n, m); array B = BB(0, span); array res1 = matmul(A.T(), B.T()); array res2 = matmulTT(A, B); ASSERT_ARRAYS_NEAR(res1, res2, 1E-5); }
TEST(Join, JoinLargeDim) { using af::constant; using af::deviceGC; using af::span; // const int nx = 32; const int nx = 1; const int ny = 4 * 1024 * 1024; const int nw = 4 * 1024 * 1024; deviceGC(); { array in = randu(nx, ny, u8); array joined = join(0, in, in); dim4 in_dims = in.dims(); dim4 joined_dims = joined.dims(); ASSERT_EQ(2 * in_dims[0], joined_dims[0]); ASSERT_EQ(0.f, sum<float>((joined(0, span) - joined(1, span)).as(f32))); array in2 = constant(1, (dim_t)nx, (dim_t)ny, (dim_t)2, (dim_t)nw, u8); joined = join(3, in, in); in_dims = in.dims(); joined_dims = joined.dims(); ASSERT_EQ(2 * in_dims[3], joined_dims[3]); } }
TEST(Susan, InvalidThreshold) { try { array a = randu(256); features out = susan(a, 3, -32, 10, 0.05f, 3); EXPECT_TRUE(false); } catch (exception &e) { EXPECT_TRUE(true); } }
TEST(Susan, InvalidFeatureRatio) { try { array a = randu(256); features out = susan(a, 3, 32, 10, 1.3f, 3); EXPECT_TRUE(false); } catch (exception &e) { EXPECT_TRUE(true); } }
TEST(Susan, InvalidEdge) { try { array a = randu(128, 128); features out = susan(a, 3, 32, 10, 1.3f, 129); EXPECT_TRUE(false); } catch (exception &e) { EXPECT_TRUE(true); } }
TEST(Susan, InvalidRadius) { try { array a = randu(256); features out = susan(a, 10); EXPECT_TRUE(false); } catch (exception &e) { EXPECT_TRUE(true); } }
TEST(AnisotropicDiffusion, GradientInvalidInputArray) { try { array out = anisotropicDiffusion(randu(100), 0.125f, 0.2f, 10, AF_FLUX_QUADRATIC); } catch (exception &exp) { ASSERT_EQ(AF_ERR_SIZE, exp.err()); } }
TEST(AnisotropicDiffusion, CurvatureInvalidInputArray) { try { array out = anisotropicDiffusion(randu(100), 0.125f, 0.2f, 10); } catch (exception &exp) { ASSERT_EQ(AF_ERR_SIZE, exp.err()); } }
TEST(Select, ISSUE_1249) { dim4 dims(2, 3, 4); array cond = randu(dims) > 0.5; array a = randu(dims); array b = select(cond, a - a * 0.9, a); array c = a - a * cond * 0.9; int num = (int)dims.elements(); vector<float> hb(num); vector<float> hc(num); b.host(&hb[0]); c.host(&hc[0]); for (int i = 0; i < num; i++) { EXPECT_NEAR(hc[i], hb[i], 1e-7) << "at " << i; } }
TEST(JIT, ISSUE_1894) { array a = randu(1); array b = tile(a, 2 * (1 << 20)); eval(b); float ha = -100; vector<float> hb(b.elements(), -200); a.host(&ha); b.host(hb.data()); for (size_t i = 0; i < hb.size(); i++) { ASSERT_EQ(ha, hb[i]); } }
TEST(JIT, CPP_Multi_linear) { const int num = 1 << 16; array a = randu(num, s32); array b = randu(num, s32); array x = a + b; array y = a - b; eval(x, y); vector<int> ha(num); vector<int> hb(num); vector<int> hx(num); vector<int> hy(num); a.host(&ha[0]); b.host(&hb[0]); x.host(&hx[0]); y.host(&hy[0]); for (int i = 0; i < num; i++) { ASSERT_EQ((ha[i] + hb[i]), hx[i]); ASSERT_EQ((ha[i] - hb[i]), hy[i]); } }
TEST(Select, NaN) { dim4 dims(1000, 1250); dtype ty = f32; array a = randu(dims, ty); a(seq(a.dims(0) / 2), span, span, span) = NaN; float b = 0; array c = select(isNaN(a), b, a); int num = (int)a.elements(); vector<float> ha(num); vector<float> hc(num); a.host(&ha[0]); c.host(&hc[0]); for (int i = 0; i < num; i++) { ASSERT_FLOAT_EQ(hc[i], std::isnan(ha[i]) ? b : ha[i]); } }
int num = (int)dims.elements(); vector<float> hb(num); vector<float> hc(num); b.host(&hb[0]); c.host(&hc[0]); for (int i = 0; i < num; i++) { EXPECT_NEAR(hc[i], hb[i], 1e-7) << "at " << i; } } TEST(Select, 4D) { dim4 dims(2, 3, 4, 2); array cond = randu(dims) > 0.5; array a = randu(dims); array b = select(cond, a - a * 0.9, a); array c = a - a * cond * 0.9; int num = (int)dims.elements(); vector<float> hb(num); vector<float> hc(num); b.host(&hb[0]); c.host(&hc[0]); for (int i = 0; i < num; i++) { EXPECT_NEAR(hc[i], hb[i], 1e-7) << "at " << i; } }
void fftconvolveTestLarge(int sDim, int fDim, int sBatch, int fBatch, bool expand) { if (noDoubleTests<T>()) return; using af::dim4; using af::seq; using af::array; int outDim = sDim + fDim - 1; int fftDim = (int)pow(2, ceil(log2(outDim))); int sd[4], fd[4]; for (int k = 0; k < 4; k++) { if (k < baseDim) { sd[k] = sDim; fd[k] = fDim; } else if (k == baseDim) { sd[k] = sBatch; fd[k] = fBatch; } else { sd[k] = 1; fd[k] = 1; } } const dim4 signalDims(sd[0], sd[1], sd[2], sd[3]); const dim4 filterDims(fd[0], fd[1], fd[2], fd[3]); array signal = randu(signalDims, (af_dtype) af::dtype_traits<T>::af_type); array filter = randu(filterDims, (af_dtype) af::dtype_traits<T>::af_type); array out = fftConvolve(signal, filter, expand ? AF_CONV_EXPAND : AF_CONV_DEFAULT); array gold; switch(baseDim) { case 1: gold = real(af::ifft(af::fft(signal, fftDim) * af::fft(filter, fftDim))); break; case 2: gold = real(af::ifft2(af::fft2(signal, fftDim, fftDim) * af::fft2(filter, fftDim, fftDim))); break; case 3: gold = real(af::ifft3(af::fft3(signal, fftDim, fftDim, fftDim) * af::fft3(filter, fftDim, fftDim, fftDim))); break; default: ASSERT_LT(baseDim, 4); } int cropMin = 0, cropMax = 0; if (expand) { cropMin = 0; cropMax = outDim - 1; } else { cropMin = fDim/2; cropMax = outDim - fDim/2 - 1; } switch(baseDim) { case 1: gold = gold(seq(cropMin, cropMax)); break; case 2: gold = gold(seq(cropMin, cropMax), seq(cropMin, cropMax)); break; case 3: gold = gold(seq(cropMin, cropMax), seq(cropMin, cropMax), seq(cropMin, cropMax)); break; } size_t outElems = out.elements(); size_t goldElems = gold.elements(); ASSERT_EQ(goldElems, outElems); T *goldData = new T[goldElems]; gold.host(goldData); T *outData = new T[outElems]; out.host(outData); for (size_t elIter=0; elIter<outElems; ++elIter) { ASSERT_NEAR(goldData[elIter], outData[elIter], 5e-2) << "at: " << elIter << std::endl; } delete[] goldData; delete[] outData; }
TEST(Where, ISSUE_1259) { array a = randu(10, 10, 10); array indices = where(a > 2); ASSERT_EQ(indices.elements(), 0); }
TEST(FFTConvolve, Docs_Unified_Wrapper) { // This unit test doesn't necessarily need to function // accuracy as af::convolve is merely a wrapper to // af::convolve[1|2|3] using af::array; using af::dim4; using af::randu; using af::constant; using af::convolve; //![ex_image_convolve_1d] array a = randu(10); //af_print(a); //a [10 1 1 1] = 0.0000 0.1315 0.7556 0.4587 0.5328 0.2190 0.0470 0.6789 0.6793 0.9347 array b = randu(4); //af_print(b); //b [4 1 1 1] = 0.3835 0.5194 0.8310 0.0346 array c = convolve(a, b); //af_print(c); //c [10 1 1 1] = 0.3581 0.6777 1.0750 0.7679 0.5903 0.4851 0.6598 1.2770 1.0734 0.8002 //![ex_image_convolve_1d] //![ex_image_convolve_2d] array d = constant(0.5, 5, 5); //af_print(d); //d [5 5 1 1] // 0.5000 0.5000 0.5000 0.5000 0.5000 // 0.5000 0.5000 0.5000 0.5000 0.5000 // 0.5000 0.5000 0.5000 0.5000 0.5000 // 0.5000 0.5000 0.5000 0.5000 0.5000 // 0.5000 0.5000 0.5000 0.5000 0.5000 array e = constant(1, 2, 2); //af_print(e); //e [2 2 1 1] // 1.0000 1.0000 // 1.0000 1.0000 array f = fftConvolve(d, e); //af_print(f); //f [5 5 1 1] // 2.0000 2.0000 2.0000 2.0000 1.0000 // 2.0000 2.0000 2.0000 2.0000 1.0000 // 2.0000 2.0000 2.0000 2.0000 1.0000 // 2.0000 2.0000 2.0000 2.0000 1.0000 // 1.0000 1.0000 1.0000 1.0000 0.5000 //![ex_image_convolve_2d] //![ex_image_convolve_3d] array g = constant(1, 4, 4, 4); //af_print(g); //g [4 4 4 1] // 1.0000 1.0000 1.0000 1.0000 // 1.0000 1.0000 1.0000 1.0000 // 1.0000 1.0000 1.0000 1.0000 // 1.0000 1.0000 1.0000 1.0000 // 1.0000 1.0000 1.0000 1.0000 // 1.0000 1.0000 1.0000 1.0000 // 1.0000 1.0000 1.0000 1.0000 // 1.0000 1.0000 1.0000 1.0000 // 1.0000 1.0000 1.0000 1.0000 // 1.0000 1.0000 1.0000 1.0000 // 1.0000 1.0000 1.0000 1.0000 // 1.0000 1.0000 1.0000 1.0000 // 1.0000 1.0000 1.0000 1.0000 // 1.0000 1.0000 1.0000 1.0000 // 1.0000 1.0000 1.0000 1.0000 // 1.0000 1.0000 1.0000 1.0000 array h = constant(0.5, 2, 2, 2); //af_print(h); //h [2 2 2 1] // 0.5000 0.5000 // 0.5000 0.5000 // 0.5000 0.5000 // 0.5000 0.5000 array i = fftConvolve(g, h); //af_print(i); //i [4 4 4 1] // 4.0000 4.0000 4.0000 2.0000 // 4.0000 4.0000 4.0000 2.0000 // 4.0000 4.0000 4.0000 2.0000 // 2.0000 2.0000 2.0000 1.0000 // 4.0000 4.0000 4.0000 2.0000 // 4.0000 4.0000 4.0000 2.0000 // 4.0000 4.0000 4.0000 2.0000 // 2.0000 2.0000 2.0000 1.0000 // 4.0000 4.0000 4.0000 2.0000 // 4.0000 4.0000 4.0000 2.0000 // 4.0000 4.0000 4.0000 2.0000 // 2.0000 2.0000 2.0000 1.0000 // 2.0000 2.0000 2.0000 1.0000 // 2.0000 2.0000 2.0000 1.0000 // 2.0000 2.0000 2.0000 1.0000 // 1.0000 1.0000 1.0000 0.5000 //![ex_image_convolve_3d] }
********************************************************/ #include <gtest/gtest.h> #include <arrayfire.h> #include <af/dim4.hpp> #include <string> #include <vector> #include <testHelpers.hpp> using std::vector; using af::array; using af::randu; TEST(rgb_gray, 32bit) { array rgb = randu(10, 10, 3); array gray = rgb2gray(rgb); vector<float> h_rgb(rgb.elements()); vector<float> h_gray(gray.elements()); rgb.host(&h_rgb[0]); gray.host(&h_gray[0]); int num = gray.elements(); int roff = 0; int goff = num; int boff = 2 * num; const float rPercent=0.2126f; const float gPercent=0.7152f;