"official/legacy/transformer/embedding_layer.py" did not exist on "ddaca60a2c5e13177ec0ff13eea719cae6cb6ba2"
profiler_operation_registry.hpp 2.07 KB
Newer Older
1
2
3
4
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2022, Advanced Micro Devices, Inc. All rights reserved.

#include <functional>
5
#include <iterator>
6
7
8
#include <map>
#include <optional>
#include <string_view>
9
#include <utility>
10
11
12
13
14
15
16
17
18

class ProfilerOperationRegistry final
{
    ProfilerOperationRegistry() = default;

    public:
    using Operation = std::function<int(int, char*[])>;

    private:
19
20
21
    struct Entry final
    {
        explicit Entry(std::string_view description, Operation operation) noexcept
22
            : description_(description), operation_(std::move(operation))
23
24
25
26
27
28
29
30
        {
        }

        std::string_view description_;
        Operation operation_;
    };

    std::map<std::string_view, Entry> entries_;
31
32
33
34
35
36
37
38
39
40

    public:
    static ProfilerOperationRegistry& GetInstance()
    {
        static ProfilerOperationRegistry registry;
        return registry;
    }

    std::optional<Operation> Get(std::string_view name) const
    {
41
42
        const auto found = entries_.find(name);
        if(found == end(entries_))
43
44
45
46
        {
            return std::nullopt;
        }

47
        return (found->second).operation_;
48
49
    }

50
    bool Add(std::string_view name, std::string_view description, Operation operation)
51
    {
52
53
54
55
56
        return entries_
            .emplace(std::piecewise_construct,
                     std::forward_as_tuple(name),
                     std::forward_as_tuple(description, std::move(operation)))
            .second;
57
58
    }

59
60
61
62
63
64
65
66
67
68
    friend std::ostream& operator<<(std::ostream& stream, const ProfilerOperationRegistry& registry)
    {
        stream << "{\n";
        for(auto& [name, entry] : registry.entries_)
        {
            stream << "\t" << name << ": " << entry.description_ << "\n";
        }
        stream << "}";

        return stream;
69
    }
70
71
};

Po-Yen, Chen's avatar
Po-Yen, Chen committed
72
73
74
75
76
#define PP_CONCAT(x, y) PP_CONCAT_IMPL(x, y)
#define PP_CONCAT_IMPL(x, y) x##y

#define REGISTER_PROFILER_OPERATION(name, description, operation)              \
    static const bool PP_CONCAT(operation_registration_result_, __COUNTER__) = \
77
        ::ProfilerOperationRegistry::GetInstance().Add(name, description, operation)