#include #include #include #include #include #include #include #include #include #include #include "test.hpp" #include "verify.hpp" auto& handlers() { static std::array, 2> x = {}; return x; } struct auto_print { migraph::program& p; int index; auto_print(migraph::program& pp, int i) : p(pp), index(i) { handlers()[index] = [this] { std::cout << p << std::endl; }; } ~auto_print() { handlers()[index] = [] {}; } }; template migraph::argument run_cpu() { V v; auto p = v.create_program(); auto_print pp{p, 0}; p.compile(migraph::cpu::cpu_target{}); migraph::program::parameter_map m; for(auto&& x : p.get_parameter_shapes()) { m[x.first] = migraph::generate_argument(x.second); } return p.eval(m); } template migraph::argument run_gpu() { V v; auto p = v.create_program(); auto_print pp{p, 1}; p.compile(migraph::gpu::target{}); migraph::program::parameter_map m; for(auto&& x : p.get_parameter_shapes()) { m[x.first] = migraph::gpu::to_gpu(migraph::generate_argument(x.second)); } return migraph::gpu::from_gpu(p.eval(m)); } template void verify_program() { std::set_terminate(+[] { try { std::rethrow_exception(std::current_exception()); } catch(const std::exception& e) { std::cout << "what(): " << e.what() << std::endl; } for(auto&& handle : handlers()) handle(); }); auto cpu_arg = run_cpu(); auto gpu_arg = run_gpu(); visit_all(cpu_arg, gpu_arg)([](auto cpu, auto gpu) { if(not test::verify_range(cpu, gpu)) { std::cout << "FAILED: " << migraph::get_type_name() << std::endl; } }); } struct test_literals { migraph::program create_program() const { migraph::program p; auto input = p.add_literal( generate_literal(migraph::shape{migraph::shape::float_type, {4, 3, 3, 3}})); auto weights = p.add_literal( generate_literal(migraph::shape{migraph::shape::float_type, {4, 3, 3, 3}})); auto conv = p.add_instruction(migraph::convolution{}, input, weights); p.add_instruction(migraph::activation{"relu"}, conv); return p; } }; struct test_add { migraph::program create_program() const { migraph::program p; migraph::shape s{migraph::shape::float_type, {3}}; auto x = p.add_parameter("x", s); auto y = p.add_parameter("y", s); p.add_instruction(migraph::add{}, x, y); return p; } }; struct test_add_broadcast { migraph::program create_program() const { migraph::program p; migraph::shape s{migraph::shape::float_type, {3}}; auto x = p.add_parameter("x", {migraph::shape::float_type, {2, 2, 3}}); auto y = p.add_parameter("y", {migraph::shape::float_type, {2, 2}}); auto by = p.add_instruction(migraph::broadcast{0}, x, y); p.add_instruction(migraph::add{}, x, by); return p; } }; struct test_conv_relu { migraph::program create_program() const { migraph::program p; auto input = p.add_parameter("x", migraph::shape{migraph::shape::float_type, {4, 3, 3, 3}}); auto weights = p.add_parameter("w", migraph::shape{migraph::shape::float_type, {4, 3, 3, 3}}); auto conv = p.add_instruction(migraph::convolution{}, input, weights); p.add_instruction(migraph::activation{"relu"}, conv); return p; } }; struct test_conv_pooling { migraph::program create_program() const { migraph::program p; auto input = p.add_parameter("x", migraph::shape{migraph::shape::float_type, {4, 3, 32, 32}}); auto weights = p.add_parameter("w", migraph::shape{migraph::shape::float_type, {4, 3, 3, 3}}); auto conv = p.add_instruction(migraph::convolution{}, input, weights); auto pooling = p.add_instruction(migraph::pooling{"max"}, conv); p.add_instruction(migraph::activation{"relu"}, pooling); return p; } }; struct test_gemm { migraph::program create_program() const { migraph::program p; auto a = p.add_parameter("a", migraph::shape{migraph::shape::float_type, {4, 5}}); auto b = p.add_parameter("b", migraph::shape{migraph::shape::float_type, {5, 3}}); p.add_instruction(migraph::gemm{}, a, b); return p; } }; struct test_gemm_ld { migraph::program create_program() const { migraph::program p; auto a = p.add_parameter("a", migraph::shape{migraph::shape::float_type, {4, 5}, {10, 1}}); auto b = p.add_parameter("b", migraph::shape{migraph::shape::float_type, {5, 3}, {20, 1}}); p.add_instruction(migraph::gemm{}, a, b); return p; } }; struct test_gemm_transposeb { migraph::program create_program() const { migraph::program p; auto a = p.add_parameter("a", migraph::shape{migraph::shape::float_type, {4, 5}}); auto b = p.add_parameter("b", migraph::shape{migraph::shape::float_type, {3, 5}}); auto bt = p.add_instruction(migraph::transpose{{1, 0}}, b); p.add_instruction(migraph::gemm{}, a, bt); return p; } }; struct test_gemm_transposea { migraph::program create_program() const { migraph::program p; auto a = p.add_parameter("a", migraph::shape{migraph::shape::float_type, {5, 4}}); auto b = p.add_parameter("b", migraph::shape{migraph::shape::float_type, {5, 3}}); auto at = p.add_instruction(migraph::transpose{{1, 0}}, a); p.add_instruction(migraph::gemm{}, at, b); return p; } }; struct test_gemm_transposeab { migraph::program create_program() const { migraph::program p; auto a = p.add_parameter("a", migraph::shape{migraph::shape::float_type, {5, 4}}); auto b = p.add_parameter("b", migraph::shape{migraph::shape::float_type, {3, 5}}); auto at = p.add_instruction(migraph::transpose{{1, 0}}, a); auto bt = p.add_instruction(migraph::transpose{{1, 0}}, b); p.add_instruction(migraph::gemm{}, at, bt); return p; } }; struct test_contiguous { migraph::program create_program() const { migraph::program p; migraph::shape s{migraph::shape::float_type, {4, 4, 4, 3}, {48, 4, 1, 16}}; auto x = p.add_parameter("x", s); p.add_instruction(migraph::contiguous{}, x); return p; } }; struct test_transpose { migraph::program create_program() const { migraph::program p; migraph::shape s{migraph::shape::float_type, {4, 3, 4, 4}}; auto x = p.add_parameter("x", s); std::vector perm = {0, 2, 3, 1}; auto l = p.add_instruction(migraph::transpose{perm}, x); p.add_instruction(migraph::contiguous{}, l); return p; } }; int main() { verify_program(); verify_program(); verify_program(); verify_program(); verify_program(); // verify_program(); verify_program(); verify_program(); verify_program(); verify_program(); verify_program(); }