mlir.cpp 5.04 KB
Newer Older
1
#include <migraphx/gpu/mlir.hpp>
Paul's avatar
Paul committed
2
3
#include <migraphx/gpu/target.hpp>
#include <migraphx/ref/target.hpp>
4
#include <migraphx/module.hpp>
Paul's avatar
Paul committed
5
#include <migraphx/program.hpp>
6
7
8
#include <migraphx/make_op.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/stringutils.hpp>
Paul's avatar
Paul committed
9
10
#include <migraphx/generate.hpp>
#include <migraphx/verify_args.hpp>
11
12
13
14
#include <test.hpp>

using migraphx::trim;

Paul's avatar
Paul committed
15
16
// m test_gpu_mlir && ./bin/test_gpu_mlir

17
18
19
20
std::string encode(std::string s)
{
    std::stringstream ss;
    bool prespace = false;
Paul's avatar
Paul committed
21
    for(auto c : s)
22
    {
Paul's avatar
Paul committed
23
        if(std::isspace(c))
24
        {
Paul's avatar
Paul committed
25
            if(not prespace)
26
27
28
                ss << "  ";
            prespace = true;
        }
Paul's avatar
Paul committed
29
        else if(std::isprint(c))
30
31
32
33
34
35
36
37
        {
            ss << c;
            prespace = false;
        }
    }
    return migraphx::trim(ss.str());
}

Paul's avatar
Paul committed
38
39
40
migraphx::program create_program_from_mlir(const migraphx::module& mmlir)
{
    migraphx::program p;
Paul's avatar
Paul committed
41
    auto* mm   = p.get_main_module();
Paul's avatar
Paul committed
42
43
44
45
46
47
48
    auto names = mmlir.get_parameter_names();
    std::vector<migraphx::instruction_ref> inputs;
    std::transform(names.begin(), names.end(), std::back_inserter(inputs), [&](const auto& name) {
        return mm->add_parameter(name, mmlir.get_parameter_shape(name));
    });
    inputs.push_back(mm->add_parameter("output", mmlir.get_output_shapes().front()));
    migraphx::gpu::insert_mlir(*mm, mm->end(), mmlir, inputs);
Paul's avatar
Paul committed
49
    std::cout << p << std::endl;
Paul's avatar
Paul committed
50
51
52
53
54
55
56
57
58
    return p;
}

migraphx::parameter_map generate_params(const migraphx::program& p)
{
    migraphx::parameter_map m;
    std::size_t i = 0;
    for(auto&& x : p.get_parameter_shapes())
    {
Paul's avatar
Updates  
Paul committed
59
        // m[x.first] = migraphx::fill_argument(x.second, 1);
Paul's avatar
Paul committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
        m[x.first] = migraphx::generate_argument(x.second, i++);
    }
    return m;
}

migraphx::argument run_gpu(migraphx::program p, const migraphx::parameter_map& inputs)
{
    migraphx::gpu::target t;
    p.compile(t);
    migraphx::parameter_map m;
    for(auto&& input : inputs)
    {
        m[input.first] = t.copy_to(input.second);
    }
    for(auto&& x : p.get_parameter_shapes())
    {
        if(m.count(x.first) == 0)
        {
            m[x.first] = t.allocate(x.second);
        }
    }
    return t.copy_from(p.eval(m).front());
}

migraphx::argument run_ref(migraphx::program p, const migraphx::parameter_map& inputs)
{
    p.compile(migraphx::ref::target{});
    return p.eval(inputs).front();
}

bool verify_mlir(const migraphx::module& mmlir)
{
    migraphx::program ref;
    ref.get_main_module()->insert_module_instructions(ref.get_main_module()->end(), &mmlir);

    auto inputs = generate_params(ref);

    auto mlir = create_program_from_mlir(mmlir);
    return migraphx::verify_args("mlir", run_ref(ref, inputs), run_gpu(mlir, inputs));
}

101
102
103
104
105
TEST_CASE(conv)
{
    const std::string mlir_output = R"__migraphx__(
module  {
  func @main(%arg0: tensor<1x8x4x4xf32>, %arg1: tensor<2x8x3x3xf32>) -> tensor<1x2x2x2xf32> {
106
107
    %0 = migraphx.convolution(%arg0, %arg1) {dilation = [1, 1], group = 1 : i64, padding = [0, 0, 0, 0], padding_mode = 0 : i64, stride = [1, 1]} : (tensor<1x8x4x4xf32>, tensor<2x8x3x3xf32>) -> tensor<1x2x2x2xf32>
    return %0 : tensor<1x2x2x2xf32>
108
109
110
111
  }
}
)__migraphx__";
    migraphx::module m;
Paul's avatar
Format  
Paul committed
112
113
    auto x = m.add_parameter("x", {migraphx::shape::float_type, {1, 8, 4, 4}});
    auto w = m.add_parameter("w", {migraphx::shape::float_type, {2, 8, 3, 3}});
Paul's avatar
Paul committed
114
    auto conv = m.add_instruction(migraphx::make_op("convolution"), x, w);
Paul's avatar
Paul committed
115
    m.add_return({conv});
116
    auto s = migraphx::gpu::dump_mlir(m);
Paul's avatar
Paul committed
117
    // Skip test if MLIR is not enabled
Paul's avatar
Format  
Paul committed
118
    if(s.empty())
Paul's avatar
Paul committed
119
        return;
Paul's avatar
Paul committed
120
    std::cout << s << std::endl;
Paul's avatar
Paul committed
121
    CHECK(encode(s) == encode(mlir_output));
Paul's avatar
Paul committed
122
    EXPECT(verify_mlir(m));
Paul's avatar
Paul committed
123
124
125
126
127
128
129
}

TEST_CASE(conv_add_relu)
{
    const std::string mlir_output = R"__migraphx__(
module  {
  func @main(%arg0: tensor<1x8x4x4xf32>, %arg1: tensor<2x8x3x3xf32>, %arg2: tensor<1x2x2x2xf32>) -> tensor<1x2x2x2xf32> {
130
    %0 = migraphx.convolution(%arg0, %arg1) {dilation = [1, 1], group = 1 : i64, padding = [0, 0, 0, 0], padding_mode = 0 : i64, stride = [1, 1]} : (tensor<1x8x4x4xf32>, tensor<2x8x3x3xf32>) -> tensor<1x2x2x2xf32>
Paul's avatar
Paul committed
131
132
    %1 = migraphx.add(%0, %arg2) : (tensor<1x2x2x2xf32>, tensor<1x2x2x2xf32>) -> tensor<1x2x2x2xf32>
    %2 = migraphx.relu(%1) : (tensor<1x2x2x2xf32>) -> tensor<1x2x2x2xf32>
133
    return %2 : tensor<1x2x2x2xf32>
Paul's avatar
Paul committed
134
135
136
137
  }
}
)__migraphx__";
    migraphx::module m;
Paul's avatar
Format  
Paul committed
138
139
140
    auto x    = m.add_parameter("x", {migraphx::shape::float_type, {1, 8, 4, 4}});
    auto w    = m.add_parameter("w", {migraphx::shape::float_type, {2, 8, 3, 3}});
    auto b    = m.add_parameter("b", {migraphx::shape::float_type, {1, 2, 2, 2}});
Paul's avatar
Paul committed
141
    auto conv = m.add_instruction(migraphx::make_op("convolution"), x, w);
Paul's avatar
Format  
Paul committed
142
    auto add  = m.add_instruction(migraphx::make_op("add"), conv, b);
Paul's avatar
Paul committed
143
144
    auto relu = m.add_instruction(migraphx::make_op("relu"), add);
    m.add_return({relu});
Paul's avatar
Paul committed
145
146
    auto s = migraphx::gpu::dump_mlir(m);
    // Skip test if MLIR is not enabled
Paul's avatar
Format  
Paul committed
147
    if(s.empty())
Paul's avatar
Paul committed
148
        return;
Paul's avatar
Paul committed
149
    std::cout << s << std::endl;
Paul's avatar
Paul committed
150
    CHECK(encode(s) == encode(mlir_output));
Paul's avatar
Paul committed
151
    EXPECT(verify_mlir(m));
152
153
154
}

int main(int argc, const char* argv[]) { test::run(argc, argv); }