check_context.hpp 3.27 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.
 */
Paul's avatar
Paul committed
24
25
#ifndef MIGRAPHX_GUARD_RTGLIB_CHECK_CONTEXT_HPP
#define MIGRAPHX_GUARD_RTGLIB_CHECK_CONTEXT_HPP
Paul's avatar
Paul committed
26

Paul's avatar
Paul committed
27
28
#include <migraphx/program.hpp>
#include <migraphx/config.hpp>
29
#include <migraphx/register_op.hpp>
30
31
#include <migraphx/stringutils.hpp>
#include <migraphx/ranges.hpp>
Paul's avatar
Paul committed
32

Paul's avatar
Paul committed
33
namespace migraphx {
Paul's avatar
Paul committed
34
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
35

Paul's avatar
Paul committed
36
template <class T>
Paul's avatar
Paul committed
37
38
struct check_context
{
39
    struct op : auto_register_op<op>
Paul's avatar
Paul committed
40
    {
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
        static std::string compute_op_name()
        {
            const auto& op_type_name                      = get_type_name<T>();
            const auto& split_name                        = split_string(op_type_name, ':');
            std::vector<std::string> name_without_version = {"check_context"};
            // op_type_name would contain internal namespace name with version_x_y_z
            // remove version and construct op_name such as check_context::migraphx::gpu::context
            std::copy_if(
                split_name.begin(),
                split_name.end(),
                std::back_inserter(name_without_version),
                [&](const auto& i) { return not i.empty() and not contains(i, "version"); });
            return join_strings(name_without_version, "::");
        }

        std::string name() const
        {
            static auto op_name = compute_op_name();
            return op_name;
        }

Paul's avatar
Paul committed
62
63
        shape compute_shape(const std::vector<shape>&) const { return {}; }
        argument compute(context& ctx, const shape&, const std::vector<argument>&) const
Paul's avatar
Paul committed
64
65
66
67
68
69
70
71
72
        {
            this->check(ctx);
            return {};
        }
        void finalize(context& ctx, const shape&, const std::vector<shape>&) const
        {
            this->check(ctx);
        }
        void check(context& ctx) const
Paul's avatar
Paul committed
73
74
75
        {
            T* x = any_cast<T>(&ctx);
            if(x == nullptr)
Paul's avatar
Paul committed
76
                MIGRAPHX_THROW(std::string("Unexpected context type: ") + ctx.type_id().name());
Paul's avatar
Paul committed
77
78
79
80
        }
    };

    std::string name() const { return "check_context"; }
81
    void apply(module& m) const { m.insert_instruction(m.begin(), op{}); }
Paul's avatar
Paul committed
82
83
};

Paul's avatar
Paul committed
84
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
85
} // namespace migraphx
Paul's avatar
Paul committed
86
87

#endif