test_custom_op.cpp 5.94 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
25
#include <algorithm>
#include <cmath>
26
#include <exception>
27
28
#include <migraphx/migraphx.h>
#include <migraphx/migraphx.hpp>
29
#include <stdexcept>
30
31
#include "test.hpp"

32
struct sigmoid_custom_op final : migraphx::experimental_custom_op_base
33
{
34
35
36
37
38
39
40
41
42
43
44
45
    virtual std::string name() const override { return "sigmoid_custom_op"; }
    virtual migraphx::argument
    compute(migraphx::context, migraphx::shape, migraphx::arguments inputs) const override
    {
        auto* output_ptr = reinterpret_cast<float*>(inputs[1].data());
        auto input_vec   = inputs[0].as_vector<float>();
        std::transform(input_vec.begin(), input_vec.end(), output_ptr, [](auto x) {
            return 1.f / (1.f + std::exp(-x));
        });
        return inputs[1];
    }

46
47
    virtual bool runs_on_offload_target() const override { return true; }

48
49
    virtual migraphx::shape compute_shape(migraphx::shapes inputs) const override
    {
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
        if(inputs.size() != 2)
        {
            throw std::runtime_error("op must have two inputs");
        }
        if(inputs[0].lengths().size() != 1)
        {
            throw std::runtime_error("input arg must be a vector or scalar");
        }
        if(inputs[0].type() != migraphx_shape_float_type)
        {
            throw std::runtime_error("input arg must be of type float");
        }
        if(inputs[0] != inputs[1])
        {
            throw std::runtime_error("input arg and buffer allocation must be of same shape");
        }
66
        return inputs.back();
67
68
69
70
71
    }
};

TEST_CASE(register_custom_op)
{
72
73
74
75
76
    sigmoid_custom_op sigmoid_op;
    migraphx::register_experimental_custom_op(sigmoid_op);
    auto op = migraphx::operation("sigmoid_custom_op");
    EXPECT(op.name() == "sigmoid_custom_op");
}
77

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
TEST_CASE(run_sigmoid_custom_op)
{
    migraphx::program p;
    migraphx::shape s{migraphx_shape_float_type, {12}};
    migraphx::module m = p.get_main_module();
    auto x             = m.add_parameter("x", s);
    auto alloc         = m.add_allocation(s);
    auto custom_kernel = m.add_instruction(migraphx::operation("sigmoid_custom_op"), {x, alloc});
    p.compile(migraphx::target("ref"));
    // run program
    migraphx::program_parameters pp;
    auto param_shapes            = p.get_parameter_shapes();
    migraphx::argument input_arg = migraphx::argument::generate(param_shapes["x"]);
    pp.add("x", input_arg);
    auto results         = p.eval(pp);
    auto result          = results[0];
    auto expected_result = input_arg.as_vector<float>();
    std::transform(expected_result.begin(),
                   expected_result.end(),
                   expected_result.begin(),
                   [](auto y) { return 1.f / (1.f + std::exp(-y)); });
    EXPECT(bool{result == migraphx::argument(s, expected_result.data())});
100
101
}

102
103
104
105
106
107
108
109
110
111
112
113
114
115
extern "C" void migraphx_test_private_disable_exception_catch(bool b);

TEST_CASE(run_sigmoid_with_incorrect_shape)
{
    migraphx::program p;
    migraphx::shape s{migraphx_shape_float_type, {12}};
    migraphx::module m = p.get_main_module();
    auto x             = m.add_parameter("x", s);
    migraphx_test_private_disable_exception_catch(true);
    EXPECT(test::throws<std::exception>(
        [&] { m.add_instruction(migraphx::operation("sigmoid_custom_op"), {x}); },
        "Error in compute_shape of: sigmoid_custom_op: op must have two inputs"));
}

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
struct identity_custom_op final : migraphx::experimental_custom_op_base
{
    virtual std::string name() const override { return "identity_custom_op"; }
    virtual migraphx::argument
    compute(migraphx::context, migraphx::shape, migraphx::arguments inputs) const override
    {
        return inputs[0];
    }

    virtual bool runs_on_offload_target() const override { return true; }

    virtual migraphx::shape compute_shape(migraphx::shapes inputs) const override
    {
        if(inputs.size() != 1)
        {
            throw std::runtime_error("Identity op must have only one input");
        }
        return inputs.back();
    }

    virtual std::vector<size_t> output_alias(migraphx::shapes) const override { return {0, 1}; }
};

TEST_CASE(run_custom_op_with_invalid_output_alias)
{
    identity_custom_op i_op;
    migraphx::register_experimental_custom_op(i_op);
    auto op = migraphx::operation("identity_custom_op");
    EXPECT(op.name() == "identity_custom_op");

    migraphx::program p;
    migraphx::shape s{migraphx_shape_float_type, {12}};
    migraphx::module m = p.get_main_module();
    auto x             = m.add_parameter("x", s);
    auto i_ins         = m.add_instruction(migraphx::operation("identity_custom_op"), {x});
    migraphx_test_private_disable_exception_catch(true);
    EXPECT(test::throws<std::exception>(
        [&] { p.compile(migraphx::target("ref")); },
        "Currently, CustomOps in MIGraphX only supports one output_alias"));
}

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