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

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

Shucai Xiao's avatar
Shucai Xiao committed
29
TEST_CASE(tanh_shape)
30
31
32
{
    auto create_program = [] {
        migraphx::program p;
33
        auto* mm = p.get_main_module();
34
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
35
36
37
38
39
        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);
40
41
42
43
44
45
46
47

        return p;
    };

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

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

    EXPECT(p1 == p2);

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

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

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