adjust_allocation.cpp 4.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24
#include <migraphx/gpu/allocation_model.hpp>
25
#include <migraphx/gpu/context.hpp>
26
27
28
#include <migraphx/gpu/lowering.hpp>
#include <migraphx/gpu/target.hpp>
#include <migraphx/adjust_allocation.hpp>
29
#include <migraphx/auto_contiguous.hpp>
30
#include <migraphx/dead_code_elimination.hpp>
31
#include <migraphx/eliminate_contiguous.hpp>
32
#include <migraphx/replace_allocate.hpp>
33
#include <migraphx/instruction.hpp>
34
35
#include <migraphx/iterator_for.hpp>
#include <migraphx/op/add.hpp>
36
#include <migraphx/op/contiguous.hpp>
37
#include <migraphx/op/tanh.hpp>
38
39
#include <migraphx/op/transpose.hpp>
#include <migraphx/pass_manager.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
40
#include <migraphx/make_op.hpp>
41
42
43
#include <basic_ops.hpp>
#include <test.hpp>

Shucai Xiao's avatar
Shucai Xiao committed
44
void run_lowering(migraphx::program& p, bool offload_copy = false)
45
{
46
    auto ctx = migraphx::gpu::context{};
47
48
49
50
51
52
53
54
55
    migraphx::run_passes(
        *p.get_main_module(),
        {migraphx::auto_contiguous{},
         migraphx::gpu::lowering{&ctx, offload_copy},
         migraphx::dead_code_elimination{},
         migraphx::eliminate_contiguous{"gpu::contiguous"},
         migraphx::dead_code_elimination{},
         migraphx::replace_allocate{migraphx::gpu::gpu_allocation_model{}, offload_copy},
         migraphx::dead_code_elimination{}});
56
}
57

Shucai Xiao's avatar
Shucai Xiao committed
58
TEST_CASE(tanh_shape)
59
60
61
{
    auto create_program = [] {
        migraphx::program p;
62
        auto* mm = p.get_main_module();
63
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
64
65
66
67
68
        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);
69
70
71
72
73
74
75
76

        return p;
    };

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

77
78
    run_lowering(p1);
    run_lowering(p2);
79
80
81

    EXPECT(p1 == p2);

Shucai Xiao's avatar
Shucai Xiao committed
82
    for(auto ins : iterator_for(*p1.get_main_module()))
83
    {
84
85
        if(ins->name() == "hip::allocate")
        {
Shucai Xiao's avatar
Shucai Xiao committed
86
            migraphx::shape new_s{migraphx::shape::float_type, {3, 2}, {1, 3}};
Paul's avatar
Paul committed
87
            ins->replace(migraphx::gpu::hip_allocate{new_s});
88
        }
89
90
91
    }
    EXPECT(p1 != p2);

92
    migraphx::run_passes(*p2.get_main_module(),
93
94
                         {migraphx::adjust_allocation{migraphx::gpu::gpu_allocation_model{}},
                          migraphx::dead_code_elimination{}});
95
96
97
    EXPECT(p1 == p2);
}

Shucai Xiao's avatar
Shucai Xiao committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
TEST_CASE(no_copy_dead_param)
{
    auto create_program = [] {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
        auto x = mm->add_parameter("x", s);
        mm->add_parameter("y", s);
        auto sum = mm->add_instruction(migraphx::make_op("add"), x, x);
        mm->add_return({sum});

        return p;
    };

    auto create_gpu_program = [] {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
        auto x = mm->add_parameter("x", s);
        mm->add_parameter("y", s);
        auto xb = mm->add_instruction(migraphx::make_op("hip::allocate", {{"shape", to_value(s)}}));
        auto gx = mm->add_instruction(migraphx::make_op("hip::copy_to_gpu"), x, xb);
        auto ab = mm->add_instruction(migraphx::make_op("hip::allocate", {{"shape", to_value(s)}}));
        auto sum = mm->add_instruction(migraphx::make_op("gpu::add"), gx, gx, ab);
        auto r   = mm->add_instruction(migraphx::make_op("hip::copy_from_gpu"), sum);
        mm->add_return({r});

        return p;
    };

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

    run_lowering(p1, true);
    EXPECT(p1 == p2);
}

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