| 91 | }; |
| 92 | |
| 93 | int main(int argc, const char* argv[]) |
| 94 | { |
| 95 | square_custom_op square_op; |
| 96 | migraphx::register_experimental_custom_op(square_op); |
| 97 | migraphx::program p; |
| 98 | migraphx::shape s{migraphx_shape_float_type, {32, 256}}; |
| 99 | migraphx::module m = p.get_main_module(); |
| 100 | auto x = m.add_parameter("x", s); |
| 101 | auto neg_ins = m.add_instruction(migraphx::operation("neg"), x); |
| 102 | // add allocation for the custom_kernel's output buffer |
| 103 | auto alloc = m.add_allocation(s); |
| 104 | auto custom_kernel = |
| 105 | m.add_instruction(migraphx::operation("square_custom_op"), {neg_ins, alloc}); |
| 106 | auto relu_ins = m.add_instruction(migraphx::operation("relu"), {custom_kernel}); |
| 107 | m.add_return({relu_ins}); |
| 108 | migraphx::compile_options options; |
| 109 | // set offload copy to true for GPUs |
| 110 | options.set_offload_copy(); |
| 111 | p.compile(migraphx::target("gpu"), options); |
| 112 | migraphx::program_parameters pp; |
| 113 | std::vector<float> x_data(s.elements()); |
| 114 | std::iota(x_data.begin(), x_data.end(), 0); |
| 115 | pp.add("x", migraphx::argument(s, x_data.data())); |
| 116 | auto results = p.eval(pp); |
| 117 | auto result = results[0]; |
| 118 | std::vector<float> expected_result = x_data; |
| 119 | std::transform(expected_result.begin(), |
| 120 | expected_result.end(), |
| 121 | expected_result.begin(), |
| 122 | [](auto i) { return std::pow(i, 2); }); |
| 123 | if(bool{result == migraphx::argument(s, expected_result.data())}) |
| 124 | { |
| 125 | std::cout << "Successfully executed custom HIP kernel example\n"; |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | std::cout << "Custom HIP kernel example failed\n"; |
| 130 | } |
| 131 | return 0; |
| 132 | } |
nothing calls this directly
no test coverage detected