quantization.cpp 4.99 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.
 */
Shucai Xiao's avatar
Shucai Xiao committed
24
25
#include <iostream>
#include <vector>
26
#include <migraphx/gpu/fuse_mlir.hpp>
27
#include <migraphx/make_op.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
28
#include <migraphx/instruction.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
29
#include <migraphx/quantization.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
30
#include <migraphx/generate.hpp>
31
#include <migraphx/register_target.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
32
33
34
35
36
#include <migraphx/verify.hpp>
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/propagate_constant.hpp>
#include <migraphx/pass_manager.hpp>
#include <migraphx/onnx.hpp>
37
#include <test.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
38
39
#include <migraphx/half.hpp>

Shucai Xiao's avatar
Shucai Xiao committed
40
TEST_CASE(gpu_target_copy)
Shucai Xiao's avatar
Shucai Xiao committed
41
{
42
    migraphx::target gpu_t = migraphx::make_target("gpu");
Shucai Xiao's avatar
Shucai Xiao committed
43
    migraphx::shape s{migraphx::shape::int8_type, {2, 3, 4, 5}};
Shucai Xiao's avatar
Shucai Xiao committed
44

45
46
47
    auto ref_arg_orig  = migraphx::generate_argument(s, 0x123456L);
    auto gpu_arg       = gpu_t.copy_to(ref_arg_orig);
    auto ref_arg_final = gpu_t.copy_from(gpu_arg);
Shucai Xiao's avatar
Shucai Xiao committed
48

Shucai Xiao's avatar
Shucai Xiao committed
49
    std::vector<int8_t> val_orig;
50
    ref_arg_orig.visit([&](auto v) { val_orig.assign(v.begin(), v.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
51
    std::vector<int8_t> val_final;
52
    ref_arg_final.visit([&](auto v) { val_final.assign(v.begin(), v.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
53

54
    EXPECT(migraphx::verify::verify_rms_range(val_orig, val_final));
Shucai Xiao's avatar
Shucai Xiao committed
55
56
}

Shucai Xiao's avatar
Shucai Xiao committed
57
58
59
60
TEST_CASE(int8_quantization)
{
    auto run_prog = [](migraphx::program p,
                       const migraphx::target& t,
61
                       migraphx::parameter_map& m_in,
Shucai Xiao's avatar
Shucai Xiao committed
62
                       std::vector<float>& res) {
63
        std::vector<migraphx::parameter_map> cali_data;
Shucai Xiao's avatar
Shucai Xiao committed
64
65
66
        cali_data.push_back(m_in);
        migraphx::quantize_int8(p, t, cali_data);
        p.compile(t);
67
        migraphx::parameter_map m;
Shucai Xiao's avatar
Shucai Xiao committed
68
69
70
71
72
73
74
75
76
77
78
79
        for(auto&& x : p.get_parameter_shapes())
        {
            if(m_in.count(x.first) > 0)
            {
                m[x.first] = t.copy_to(m_in[x.first]);
            }
            else
            {
                m[x.first] = t.allocate(x.second);
            }
        }

80
        auto result = t.copy_from(p.eval(m).back());
Shucai Xiao's avatar
Shucai Xiao committed
81
82
83
84
85
        result.visit([&](auto v) { res.assign(v.begin(), v.end()); });
    };

    auto create_program = [] {
        migraphx::program p;
86
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
87
        migraphx::shape sa{migraphx::shape::float_type, {5, 16}};
Shucai Xiao's avatar
Shucai Xiao committed
88
        migraphx::shape sb{migraphx::shape::float_type, {16, 8}};
Shucai Xiao's avatar
Shucai Xiao committed
89
        migraphx::shape sc{migraphx::shape::float_type, {5, 8}};
90
91
        auto pa = mm->add_parameter("a", sa);
        auto pb = mm->add_parameter("b", sb);
92
        mm->add_instruction(migraphx::make_op("dot"), pa, pb);
Shucai Xiao's avatar
Shucai Xiao committed
93
94
95
96
97
98

        return p;
    };

    {
        auto p = create_program();
99
        migraphx::parameter_map m;
Shucai Xiao's avatar
Shucai Xiao committed
100
        migraphx::shape sa{migraphx::shape::float_type, {5, 16}};
Shucai Xiao's avatar
Shucai Xiao committed
101
        migraphx::shape sb{migraphx::shape::float_type, {16, 8}};
Shucai Xiao's avatar
Shucai Xiao committed
102
        migraphx::shape sc{migraphx::shape::float_type, {5, 8}};
Shucai Xiao's avatar
Shucai Xiao committed
103
        m["a"] = migraphx::generate_argument(sa);
Shucai Xiao's avatar
Shucai Xiao committed
104
        m["b"] = migraphx::generate_argument(sb);
105
        std::vector<float> ref_result;
106
        migraphx::target ref_t = migraphx::make_target("ref");
107
        run_prog(p, ref_t, m, ref_result);
Shucai Xiao's avatar
Shucai Xiao committed
108
109

        std::vector<float> gpu_result;
110
        migraphx::target gpu_t = migraphx::make_target("gpu");
Shucai Xiao's avatar
Shucai Xiao committed
111
112
        run_prog(p, gpu_t, m, gpu_result);

113
114
115
116
117
118
119
        // Note: the tolerance for mlir_enabled result is temporarily bumped
        // higher because the lowering pipeline between mlir fallback and
        // regular non-mlir pipeline diverged. MLIR fallback uses the
        // rewrite_quantization at the very end of the pipeline, whereas
        // the regular pipeline uses the rewrite_quantization in the much
        // earlier stage.
        if(migraphx::gpu::mlir_enabled())
120
121
122
123
            EXPECT(migraphx::verify::verify_range_with_tolerance(
                gpu_result,
                migraphx::verify::expected{ref_result},
                migraphx::verify::tolerance{0.01}));
124
        else
125
            EXPECT(migraphx::verify::verify_rms_range(gpu_result, ref_result));
Shucai Xiao's avatar
Shucai Xiao committed
126
127
128
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
129
int main(int argc, const char* argv[]) { test::run(argc, argv); }