| 2085 | {} |
| 2086 | |
| 2087 | void perform_test() |
| 2088 | { |
| 2089 | const bool previous_setting = dnn_choose_algorithms_per_input_shape(); |
| 2090 | auto restore_setting = make_scope_exit([previous_setting] { |
| 2091 | set_dnn_choose_algorithms_per_input_shape(previous_setting); |
| 2092 | }); |
| 2093 | set_dnn_choose_algorithms_per_input_shape(true); |
| 2094 | |
| 2095 | // Vary each part of the shape while keeping stride, padding, and filter size fixed. |
| 2096 | // Columns are batch size, input channels, rows, columns, and output channels. |
| 2097 | const long shapes[][5] = { |
| 2098 | {1, 3, 11, 13, 4}, |
| 2099 | {2, 3, 11, 13, 4}, |
| 2100 | {1, 5, 11, 13, 4}, |
| 2101 | {1, 3, 17, 13, 4}, |
| 2102 | {1, 3, 11, 19, 4}, |
| 2103 | {1, 3, 11, 13, 7} |
| 2104 | }; |
| 2105 | cuda::tensor_conv gpu_conv; |
| 2106 | cpu::tensor_conv cpu_conv; |
| 2107 | dlib::rand rnd("convolution algorithms per input shape"); |
| 2108 | |
| 2109 | // Revisit the shapes to exercise reuse of their cached algorithms. Generate new |
| 2110 | // tensor values on each pass so that the expected outputs and gradients also change. |
| 2111 | for (int pass = 0; pass < 2; ++pass) |
| 2112 | { |
| 2113 | for (const auto& shape : shapes) |
| 2114 | { |
| 2115 | resizable_tensor data(shape[0], shape[1], shape[2], shape[3]); |
| 2116 | resizable_tensor filters(shape[4], shape[1], 3, 3); |
| 2117 | for (auto& value : data) |
| 2118 | value = rnd.get_random_float() - 0.5f; |
| 2119 | for (auto& value : filters) |
| 2120 | value = rnd.get_random_float() - 0.5f; |
| 2121 | |
| 2122 | gpu_conv.setup(data, filters, 1, 1, 1, 1); |
| 2123 | cpu_conv.setup(data, filters, 1, 1, 1, 1); |
| 2124 | resizable_tensor gpu_output, cpu_output; |
| 2125 | gpu_conv(false, gpu_output, data, filters); |
| 2126 | cpu_conv(false, cpu_output, data, filters); |
| 2127 | DLIB_TEST(have_same_dimensions(gpu_output, cpu_output)); |
| 2128 | DLIB_TEST(max(abs(mat(gpu_output)-mat(cpu_output))) < 1e-3); |
| 2129 | |
| 2130 | resizable_tensor gradient_input; |
| 2131 | gradient_input.copy_size(cpu_output); |
| 2132 | for (auto& value : gradient_input) |
| 2133 | value = rnd.get_random_float() - 0.5f; |
| 2134 | |
| 2135 | resizable_tensor gpu_data_gradient, cpu_data_gradient; |
| 2136 | resizable_tensor gpu_filter_gradient, cpu_filter_gradient; |
| 2137 | gpu_data_gradient.copy_size(data); |
| 2138 | cpu_data_gradient.copy_size(data); |
| 2139 | gpu_filter_gradient.copy_size(filters); |
| 2140 | cpu_filter_gradient.copy_size(filters); |
| 2141 | for (bool add_to : {false, true}) |
| 2142 | { |
| 2143 | gpu_conv.get_gradient_for_data(add_to, gradient_input, filters, gpu_data_gradient); |
| 2144 | cpu_conv.get_gradient_for_data(add_to, gradient_input, filters, cpu_data_gradient); |
nothing calls this directly
no test coverage detected