fast_math.cpp 2.72 KB
Newer Older
kahmed10's avatar
kahmed10 committed
1
2
3
#include <test.hpp>
#include <migraphx/quantization.hpp>
#include <migraphx/iterator_for.hpp>
4
5
6
7
8
9
10
#include <migraphx/op/add.hpp>
#include <migraphx/op/mul.hpp>
#include <migraphx/op/multibroadcast.hpp>
#include <migraphx/op/pow.hpp>
#include <migraphx/op/tanh.hpp>
#include <migraphx/gpu/target.hpp>
#include <migraphx/instruction.hpp>
kahmed10's avatar
kahmed10 committed
11
12
13
14

migraphx::program create_gelu()
{
    migraphx::program p;
15
    auto* mm                 = p.get_main_module();
kahmed10's avatar
kahmed10 committed
16
17
18
19
20
21
22
23
    std::vector<float> data0 = {0.044715};
    std::vector<float> data1 = {0.797885};
    std::vector<float> data2 = {3};
    std::vector<float> data3 = {0.5};
    migraphx::shape s0{migraphx::shape::float_type, {1}};

    std::vector<size_t> x_dims{1, 1, 5};

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    auto x         = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, x_dims});
    auto const_val = mm->add_literal(migraphx::literal{s0, data0});
    auto sqrt_2_pi = mm->add_literal(migraphx::literal{s0, data1});
    auto three_val = mm->add_literal(migraphx::literal{s0, data2});
    auto half_val  = mm->add_literal(migraphx::literal{s0, data3});

    auto mbcast_3         = mm->add_instruction(migraphx::op::multibroadcast{x_dims}, three_val);
    auto pow_op           = mm->add_instruction(migraphx::op::pow{}, x, mbcast_3);
    auto mbcast_const     = mm->add_instruction(migraphx::op::multibroadcast{x_dims}, const_val);
    auto mul_const        = mm->add_instruction(migraphx::op::mul{}, mbcast_const, pow_op);
    auto add_x            = mm->add_instruction(migraphx::op::add{}, x, mul_const);
    auto mbcast_sqrt_2_pi = mm->add_instruction(migraphx::op::multibroadcast{x_dims}, sqrt_2_pi);
    auto mul_add_x        = mm->add_instruction(migraphx::op::mul{}, mbcast_sqrt_2_pi, add_x);
    auto tanh_op          = mm->add_instruction(migraphx::op::tanh{}, mul_add_x);
    auto mbcast_half      = mm->add_instruction(migraphx::op::multibroadcast{x_dims}, half_val);
    auto mul_half         = mm->add_instruction(migraphx::op::mul{}, mbcast_half, tanh_op);
    auto add_mul_half     = mm->add_instruction(migraphx::op::add{}, mul_half, mbcast_half);
    auto mul_x            = mm->add_instruction(migraphx::op::mul{}, x, add_mul_half);
    mm->add_return({mul_x});
kahmed10's avatar
kahmed10 committed
43
44
45
46
47
48
49
50

    return p;
}

TEST_CASE(enable_fast_gelu)
{
    migraphx::program p = create_gelu();
    p.compile(migraphx::gpu::target{});
Shucai Xiao's avatar
Shucai Xiao committed
51
    CHECK(any_of(*p.get_main_module(), [&](auto&& i) { return i.name() == "gpu::gelu"; }));
kahmed10's avatar
kahmed10 committed
52
53
54
55
56
57
58
59
}

TEST_CASE(disable_fast_gelu)
{
    migraphx::program p = create_gelu();
    migraphx::compile_options options;
    options.fast_math = false;
    p.compile(migraphx::gpu::target{}, options);
Shucai Xiao's avatar
Shucai Xiao committed
60
    CHECK(any_of(*p.get_main_module(), [&](auto&& i) { return i.name() == "gpu::gelu_new"; }));
kahmed10's avatar
kahmed10 committed
61
62
63
}

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