adjust_allocation.cpp 5.02 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
#include <basic_ops.hpp>
#include <test.hpp>
43
44
45
46
#include "make_precompile_op.hpp"

// Treat some operators as compilable to enable lowering
MIGRAPHX_GPU_TEST_PRECOMPILE("add", "mul", "convert")
47

Shucai Xiao's avatar
Shucai Xiao committed
48
void run_lowering(migraphx::program& p, bool offload_copy = false)
49
{
50
    auto ctx = migraphx::gpu::context{};
51
52
53
54
55
56
57
58
59
    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{}});
60
}
61

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

        return p;
    };

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

81
82
    run_lowering(p1);
    run_lowering(p2);
83
84
85

    EXPECT(p1 == p2);

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

96
    migraphx::run_passes(*p2.get_main_module(),
97
98
                         {migraphx::adjust_allocation{migraphx::gpu::gpu_allocation_model{}},
                          migraphx::dead_code_elimination{}});
99
100
101
    EXPECT(p1 == p2);
}

Shucai Xiao's avatar
Shucai Xiao committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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)}}));
125
        auto sum = mm->add_instruction(make_precompile_op("add"), gx, gx, ab);
Shucai Xiao's avatar
Shucai Xiao committed
126
127
128
129
130
131
132
133
134
135
136
137
138
        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);
}

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