test_custom_op.cpp 4.41 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 migraphx::shape compute_shape(migraphx::shapes inputs) const override
    {
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
        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");
        }
64
        return inputs.back();
65
66
67
68
69
    }
};

TEST_CASE(register_custom_op)
{
70
71
72
73
74
    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");
}
75

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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())});
98
99
}

100
101
102
103
104
105
106
107
108
109
110
111
112
113
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"));
}

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