Skip to content

Commit 57e467b

Browse files
committed
add GTests for Conv GPU bias, 1D and 3D paths
1 parent b7e5178 commit 57e467b

7 files changed

Lines changed: 133 additions & 0 deletions

File tree

core/test/TestCustomModelsFromONNXForAlpakaCuda.cxx

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@
164164
#include "ConvWithAsymmetricPadding_FromONNX_GPU_ALPAKA.hxx"
165165
#include "input_models/references/ConvWithAsymmetricPadding.ref.hxx"
166166

167+
#include "ConvWithBias_FromONNX_GPU_ALPAKA.hxx"
168+
#include "input_models/references/ConvWithBias.ref.hxx"
169+
170+
#include "Conv1d_FromONNX_GPU_ALPAKA.hxx"
171+
#include "input_models/references/Conv1d.ref.hxx"
172+
173+
#include "Conv3d_FromONNX_GPU_ALPAKA.hxx"
174+
#include "input_models/references/Conv3d.ref.hxx"
175+
167176
#include "BatchNorm_FromONNX_GPU_ALPAKA.hxx"
168177
#include "BatchNormRelu_FromONNX_GPU_ALPAKA.hxx"
169178

@@ -2442,6 +2451,121 @@ TEST_F(SofieAlpakaTest, ConvWithAsymmetricPadding)
24422451
}
24432452
}
24442453

2454+
// Exercises the Conv GPU bias path (BiasBroadcastKernel + GEMM beta=1). The existing
2455+
// ConvWith* models have no bias input, so the bias kernel was never run. Two output
2456+
// channels with distinct biases (10, 100) make a wrong-channel broadcast visible.
2457+
TEST_F(SofieAlpakaTest, ConvWithBias)
2458+
{
2459+
constexpr float TOLERANCE = DEFAULT_TOLERANCE;
2460+
2461+
// x[1,1,5,5] = iota 0..24
2462+
std::vector<float> input(25);
2463+
std::iota(input.begin(), input.end(), 0.0f);
2464+
2465+
auto input_h = alpaka::allocBuf<float, Idx>(host, Ext1D::all(Idx{input.size()}));
2466+
float* input_ptr = reinterpret_cast<float*>(alpaka::getPtrNative(input_h));
2467+
for (Idx i = 0; i < input.size(); ++i) input_ptr[i] = input[i];
2468+
2469+
auto input_d = alpaka::allocBuf<float, Idx>(device, Ext1D::all(Idx{input.size()}));
2470+
alpaka::memcpy(queue, input_d, input_h);
2471+
alpaka::wait(queue);
2472+
2473+
auto result_h = alpaka::allocBuf<float, Idx>(host, Ext1D::all(Idx{sizeof(ConvWithBias_ExpectedOutput::all_ones) / sizeof(float)}));
2474+
2475+
{
2476+
SOFIE_ConvWithBias::Session<alpaka::TagGpuCudaRt> session("ConvWithBias_FromONNX_GPU_ALPAKA.dat");
2477+
auto result = session.infer(input_d);
2478+
alpaka::wait(queue);
2479+
cudaDeviceSynchronize();
2480+
alpaka::memcpy(queue, result_h, result);
2481+
alpaka::wait(queue);
2482+
}
2483+
2484+
float* res_ptr = reinterpret_cast<float*>(alpaka::getPtrNative(result_h));
2485+
float *correct = ConvWithBias_ExpectedOutput::all_ones;
2486+
constexpr size_t nOut_bias = sizeof(ConvWithBias_ExpectedOutput::all_ones) / sizeof(float);
2487+
2488+
for (size_t i = 0; i < nOut_bias; ++i) {
2489+
EXPECT_LE(std::abs(res_ptr[i] - correct[i]), TOLERANCE) << "i=" << i;
2490+
}
2491+
}
2492+
2493+
// Exercises the Conv GPU 1D path (fDim==1 branches in weight-vec/im2col). All other
2494+
// alpaka Conv models are 2D. x[1,2,7], W[3,2,3] iota weights, bias [1,2,3], pads [1,1].
2495+
TEST_F(SofieAlpakaTest, Conv1d)
2496+
{
2497+
constexpr float TOLERANCE = DEFAULT_TOLERANCE;
2498+
2499+
// x[1,2,7] = iota 0..13
2500+
std::vector<float> input(14);
2501+
std::iota(input.begin(), input.end(), 0.0f);
2502+
2503+
auto input_h = alpaka::allocBuf<float, Idx>(host, Ext1D::all(Idx{input.size()}));
2504+
float* input_ptr = reinterpret_cast<float*>(alpaka::getPtrNative(input_h));
2505+
for (Idx i = 0; i < input.size(); ++i) input_ptr[i] = input[i];
2506+
2507+
auto input_d = alpaka::allocBuf<float, Idx>(device, Ext1D::all(Idx{input.size()}));
2508+
alpaka::memcpy(queue, input_d, input_h);
2509+
alpaka::wait(queue);
2510+
2511+
auto result_h = alpaka::allocBuf<float, Idx>(host, Ext1D::all(Idx{sizeof(Conv1d_ExpectedOutput::all_ones) / sizeof(float)}));
2512+
2513+
{
2514+
SOFIE_Conv1d::Session<alpaka::TagGpuCudaRt> session("Conv1d_FromONNX_GPU_ALPAKA.dat");
2515+
auto result = session.infer(input_d);
2516+
alpaka::wait(queue);
2517+
cudaDeviceSynchronize();
2518+
alpaka::memcpy(queue, result_h, result);
2519+
alpaka::wait(queue);
2520+
}
2521+
2522+
float* res_ptr = reinterpret_cast<float*>(alpaka::getPtrNative(result_h));
2523+
float *correct = Conv1d_ExpectedOutput::all_ones;
2524+
constexpr size_t nOut_conv1d = sizeof(Conv1d_ExpectedOutput::all_ones) / sizeof(float);
2525+
2526+
for (size_t i = 0; i < nOut_conv1d; ++i) {
2527+
EXPECT_LE(std::abs(res_ptr[i] - correct[i]), TOLERANCE) << "i=" << i;
2528+
}
2529+
}
2530+
2531+
// Exercises the Conv GPU 3D path (fDim>2 depth branches + depth handling in im2col).
2532+
// x[1,1,3,4,4], W[2,1,2,3,3] iota weights, bias [5,50], kernel (2,3,3), pads h/w by 1.
2533+
TEST_F(SofieAlpakaTest, Conv3d)
2534+
{
2535+
constexpr float TOLERANCE = DEFAULT_TOLERANCE;
2536+
2537+
// x[1,1,3,4,4] = iota 0..47
2538+
std::vector<float> input(48);
2539+
std::iota(input.begin(), input.end(), 0.0f);
2540+
2541+
auto input_h = alpaka::allocBuf<float, Idx>(host, Ext1D::all(Idx{input.size()}));
2542+
float* input_ptr = reinterpret_cast<float*>(alpaka::getPtrNative(input_h));
2543+
for (Idx i = 0; i < input.size(); ++i) input_ptr[i] = input[i];
2544+
2545+
auto input_d = alpaka::allocBuf<float, Idx>(device, Ext1D::all(Idx{input.size()}));
2546+
alpaka::memcpy(queue, input_d, input_h);
2547+
alpaka::wait(queue);
2548+
2549+
auto result_h = alpaka::allocBuf<float, Idx>(host, Ext1D::all(Idx{sizeof(Conv3d_ExpectedOutput::all_ones) / sizeof(float)}));
2550+
2551+
{
2552+
SOFIE_Conv3d::Session<alpaka::TagGpuCudaRt> session("Conv3d_FromONNX_GPU_ALPAKA.dat");
2553+
auto result = session.infer(input_d);
2554+
alpaka::wait(queue);
2555+
cudaDeviceSynchronize();
2556+
alpaka::memcpy(queue, result_h, result);
2557+
alpaka::wait(queue);
2558+
}
2559+
2560+
float* res_ptr = reinterpret_cast<float*>(alpaka::getPtrNative(result_h));
2561+
float *correct = Conv3d_ExpectedOutput::all_ones;
2562+
constexpr size_t nOut_conv3d = sizeof(Conv3d_ExpectedOutput::all_ones) / sizeof(float);
2563+
2564+
for (size_t i = 0; i < nOut_conv3d; ++i) {
2565+
EXPECT_LE(std::abs(res_ptr[i] - correct[i]), TOLERANCE) << "i=" << i;
2566+
}
2567+
}
2568+
24452569
TEST_F(SofieAlpakaTest, BatchNormalization)
24462570
{
24472571
constexpr float TOLERANCE = DEFAULT_TOLERANCE;

core/test/input_models/Conv1d.onnx

308 Bytes
Binary file not shown.

core/test/input_models/Conv3d.onnx

422 Bytes
Binary file not shown.
314 Bytes
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace Conv1d_ExpectedOutput {
2+
float all_ones[] = {71.0, 104.0, 119.0, 134.0, 149.0, 164.0, 95.0, 168.0, 267.0, 318.0, 369.0, 420.0, 471.0, 312.0, 265.0, 430.0, 517.0, 604.0, 691.0, 778.0, 529.0};
3+
} // namespace Conv1d_ExpectedOutput
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace Conv3d_ExpectedOutput {
2+
float all_ones[] = {1201.0, 1801.0, 1921.0, 1269.0, 1886.0, 2798.0, 2951.0, 1928.0, 2318.0, 3410.0, 3563.0, 2312.0, 1429.0, 2077.0, 2161.0, 1385.0, 2545.0, 3721.0, 3841.0, 2485.0, 3614.0, 5246.0, 5399.0, 3464.0, 4046.0, 5858.0, 6011.0, 3848.0, 2389.0, 3421.0, 3505.0, 2217.0, 2758.0, 4222.0, 4558.0, 3114.0, 4631.0, 7055.0, 7532.0, 5105.0, 5927.0, 8963.0, 9440.0, 6353.0, 4138.0, 6226.0, 6526.0, 4382.0, 6406.0, 9598.0, 9934.0, 6634.0, 9815.0, 14687.0, 15164.0, 10097.0, 11111.0, 16595.0, 17072.0, 11345.0, 7402.0, 11026.0, 11326.0, 7518.0};
3+
} // namespace Conv3d_ExpectedOutput
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace ConvWithBias_ExpectedOutput {
2+
float all_ones[] = {22.0, 31.0, 37.0, 43.0, 34.0, 43.0, 64.0, 73.0, 82.0, 61.0, 73.0, 109.0, 118.0, 127.0, 91.0, 103.0, 154.0, 163.0, 172.0, 121.0, 82.0, 121.0, 127.0, 133.0, 94.0, 112.0, 121.0, 127.0, 133.0, 124.0, 133.0, 154.0, 163.0, 172.0, 151.0, 163.0, 199.0, 208.0, 217.0, 181.0, 193.0, 244.0, 253.0, 262.0, 211.0, 172.0, 211.0, 217.0, 223.0, 184.0};
3+
} // namespace ConvWithBias_ExpectedOutput

0 commit comments

Comments
 (0)