adjust_allocation.cpp 2.27 KB
Newer Older
1
#include <migraphx/gpu/allocation_model.hpp>
2
#include <migraphx/gpu/context.hpp>
3
4
5
#include <migraphx/gpu/lowering.hpp>
#include <migraphx/gpu/target.hpp>
#include <migraphx/adjust_allocation.hpp>
6
#include <migraphx/auto_contiguous.hpp>
7
#include <migraphx/dead_code_elimination.hpp>
8
#include <migraphx/eliminate_contiguous.hpp>
9
#include <migraphx/instruction.hpp>
10
11
#include <migraphx/iterator_for.hpp>
#include <migraphx/op/add.hpp>
12
#include <migraphx/op/contiguous.hpp>
13
#include <migraphx/op/tanh.hpp>
14
15
#include <migraphx/op/transpose.hpp>
#include <migraphx/pass_manager.hpp>
16
17
18
#include <basic_ops.hpp>
#include <test.hpp>

19
void run_lowering(migraphx::program& p)
20
{
21
    auto ctx = migraphx::gpu::context{};
22
    migraphx::run_passes(*p.get_main_module(),
23
24
25
26
27
28
                         {migraphx::auto_contiguous{},
                          migraphx::gpu::lowering{&ctx, false},
                          migraphx::dead_code_elimination{},
                          migraphx::eliminate_contiguous{},
                          migraphx::dead_code_elimination{}});
}
29

Shucai Xiao's avatar
Shucai Xiao committed
30
TEST_CASE(tanh_shape)
31
32
33
{
    auto create_program = [] {
        migraphx::program p;
34
        auto* mm = p.get_main_module();
35
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
36
37
38
39
40
        auto x   = mm->add_parameter("x", s);
        auto tx  = mm->add_instruction(migraphx::op::transpose{{1, 0}}, x);
        auto txh = mm->add_instruction(migraphx::op::tanh{}, tx);
        auto sum = mm->add_instruction(migraphx::op::add{}, txh, txh);
        mm->add_instruction(migraphx::op::contiguous{}, sum);
41
42
43
44
45
46
47
48

        return p;
    };

    auto p1 = create_program();
    auto p2 = create_program();
    EXPECT(p1 == p2);

49
50
    run_lowering(p1);
    run_lowering(p2);
51
52
53
54
55

    EXPECT(p1 == p2);

    for(auto ins : iterator_for(p1))
    {
56
57
        if(ins->name() == "hip::allocate")
        {
Shucai Xiao's avatar
Shucai Xiao committed
58
            migraphx::shape new_s{migraphx::shape::float_type, {3, 2}, {1, 3}};
Paul's avatar
Paul committed
59
            ins->replace(migraphx::gpu::hip_allocate{new_s});
60
        }
61
62
63
    }
    EXPECT(p1 != p2);

64
    migraphx::run_passes(*p2.get_main_module(),
65
66
                         {migraphx::adjust_allocation{migraphx::gpu::gpu_allocation_model{}},
                          migraphx::dead_code_elimination{}});
67
68
69
70
    EXPECT(p1 == p2);
}

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