File: execute_primitive.cpp

package info (click to toggle)
miopen 6.4.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 66,788 kB
  • sloc: cpp: 300,511; lisp: 29,731; ansic: 2,683; sh: 471; python: 323; makefile: 155
file content (263 lines) | stat: -rw-r--r-- 8,856 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*******************************************************************************
 *
 * MIT License
 *
 * Copyright (c) 2023 Advanced Micro Devices, Inc.
 *
 * 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.
 *
 *******************************************************************************/

#include <miopen/filesystem.hpp>
#include <miopen/execution_context.hpp>
#include <miopen/find_solution.hpp>
#include <miopen/invoker.hpp>
#include <miopen/mlo_internal.hpp>
#include <miopen/names.hpp>
#include <miopen/problem_description_base.hpp>
#include <miopen/solver.hpp>
#include <miopen/temp_file.hpp>

namespace fs = miopen::fs;

#if MIOPEN_ENABLE_SQLITE && MIOPEN_USE_SQLITE_PERFDB
#include <miopen/sqlite_db.hpp>
#endif

#include "get_handle.hpp"

#include <gtest/gtest_common.hpp>
#include <gtest/gtest.h>

#include <cstdint>
#include <string>

struct TestResults
{
    bool regular_selected             = false;
    bool tunable_selected             = false;
    bool search_invoked               = false;
    bool get_default_perf_cfg_invoked = false;
    std::uint32_t total_get_solutions = 0;
    std::uint32_t perf_cfg_value      = 0;
};

struct TestProblemDescriptionTag
{
    // In real code this type should not have any data
    fs::path pdb_path;
    fs::path updb_path;
};

struct TestProblemDescription : miopen::ProblemDescriptionBase,
                                TestProblemDescriptionTag
#if MIOPEN_ENABLE_SQLITE && MIOPEN_USE_SQLITE_PERFDB
    ,
                                miopen::SQLiteSerializable<TestProblemDescription>
#endif
{
    std::uint32_t net_config;
    TestResults* test_results;

    auto MakeNetworkConfig() const -> miopen::NetworkConfig override
    {
        return miopen::NetworkConfig{std::to_string(net_config)};
    }

    void Serialize(std::ostream& stream) const { stream << net_config; }

#if MIOPEN_ENABLE_SQLITE && MIOPEN_USE_SQLITE_PERFDB
    static std::string table_name() { return "config"; }
#endif

    template <class TSelf>
    static void Visit(TSelf&& self, std::function<void(std::string, std::string)> visitor)
    {
    }

    template <class TSelf>
    static void Visit(TSelf&& self, std::function<void(std::int64_t, std::string)> visitor)
    {
        visitor(static_cast<std::int32_t>(self.net_config), "net_config");
    }

    template <class TSelf, class Visitor>
    static void VisitAll(TSelf&& self, Visitor visitor)
    {
        Visit(std::forward<TSelf>(self),
              [&](int64_t value, std::string name) { visitor(value, name); });
        Visit(std::forward<TSelf>(self),
              [&](std::string value, std::string name) { visitor(value, name); });
    }

    friend auto GetDb(const miopen::ExecutionContext&, const TestProblemDescriptionTag& problem)
        -> miopen::PerformanceDb
    {
        return {miopen::DbKinds::PerfDb, problem.pdb_path, problem.updb_path};
    }
};

struct TestPerfConfig : miopen::solver::PerfConfig
{
    static constexpr std::uint32_t default_value  = 1234;
    static constexpr std::uint32_t searched_value = 4321;

    std::uint32_t check_value;

    TestPerfConfig(std::uint32_t check_value_ = 0) : check_value(check_value_) {}

    void Serialize(std::ostream& stream) const override {}
    auto Deserialize(const std::string&) -> bool override { return true; }
};

static auto MakeNoopSolution() -> miopen::solver::ConvSolution
{
    miopen::solver::ConvSolution sol{};
    sol.invoker_factory = [](auto&&) { return [](auto&&, auto&&) {}; };
    return sol;
}

struct RegularTestSolver
    : miopen::solver::NonTunableSolverBase<miopen::ExecutionContext, TestProblemDescription>
{
    auto SolverDbId() const -> const std::string& override
    {
        return GetSolverDbId<RegularTestSolver>();
    }

    auto IsApplicable(const miopen::ExecutionContext&, const TestProblemDescription&) const
        -> bool override
    {
        return true;
    }

    auto GetSolution(const miopen::ExecutionContext&, const TestProblemDescription& problem) const
        -> miopen::solver::ConvSolution override
    {
        problem.test_results->regular_selected = true;
        problem.test_results->total_get_solutions++;
        return MakeNoopSolution();
    }
};

struct TunableTestSolver : miopen::solver::TunableSolverMixin<miopen::ExecutionContext,
                                                              TestProblemDescription,
                                                              TestPerfConfig>
{
    auto SolverDbId() const -> const std::string& override
    {
        return GetSolverDbId<TunableTestSolver>();
    }
    auto IsApplicable(const miopen::ExecutionContext&, const TestProblemDescription&) const
        -> bool override
    {
        return true;
    }

    auto GetDefaultPerformanceConfig(const miopen::ExecutionContext&,
                                     const TestProblemDescription& problem) const
        -> TestPerfConfig override
    {
        problem.test_results->get_default_perf_cfg_invoked = true;
        return {TestPerfConfig::default_value};
    }

    auto IsValidPerformanceConfig(const miopen::ExecutionContext&,
                                  const TestProblemDescription&,
                                  const TestPerfConfig&) const -> bool override
    {
        return true;
    }

    auto Search(const miopen::ExecutionContext&,
                const TestProblemDescription& problem,
                const miopen::AnyInvokeParams&) const -> TestPerfConfig override
    {
        problem.test_results->search_invoked = true;
        return {TestPerfConfig::searched_value};
    }

    auto GetSolution(const miopen::ExecutionContext&,
                     const TestProblemDescription& problem,
                     const TestPerfConfig& raw_perf_cfg) const
        -> miopen::solver::ConvSolution override
    {
        const auto& perf_cfg = dynamic_cast<const TestPerfConfig&>(raw_perf_cfg);

        problem.test_results->perf_cfg_value   = perf_cfg.check_value;
        problem.test_results->tunable_selected = true;
        problem.test_results->total_get_solutions++;
        return MakeNoopSolution();
    }
};

struct ExecutePrimitive : testing::Test
{
};

auto CallExecutePrimitive(const miopen::ExecutionContext& ctx) -> TestResults
{
    static std::uint32_t call_id = 0; // this is to distinguish between test cases

    miopen::TempFile pdb{"test_pdb"};
    miopen::TempFile updb{"test_fdb"};

    TestResults test_results{};
    TestProblemDescription problem{};
    problem.test_results = &test_results;
    problem.pdb_path     = pdb;
    problem.updb_path    = updb;
    problem.net_config   = call_id++;

    constexpr auto solvers =
        miopen::solver::SolverContainer<TunableTestSolver, RegularTestSolver>{};
    solvers.ExecutePrimitive(ctx, problem, miopen::AlgorithmName{"test::algo"}, {});

    return test_results;
}

TEST(CPU_ExecutePrimitive_NONE, NoParams)
{
    auto ctx = miopen::ExecutionContext{&get_handle()};

    auto results = CallExecutePrimitive(ctx);

    ASSERT_EQ(results.total_get_solutions, 1);
    ASSERT_FALSE(results.regular_selected);
    ASSERT_TRUE(results.tunable_selected);
    ASSERT_TRUE(results.get_default_perf_cfg_invoked);
    ASSERT_FALSE(results.search_invoked);
    ASSERT_EQ(results.perf_cfg_value, TestPerfConfig::default_value);
}

TEST(CPU_ExecutePrimitiveSearchUpdate_NONE, SearchUpdate)
{
    auto ctx      = miopen::ExecutionContext{&get_handle()};
    ctx.do_search = true;
    ctx.db_update = true;

    auto results = CallExecutePrimitive(ctx);

    ASSERT_EQ(results.total_get_solutions, 1);
    ASSERT_FALSE(results.regular_selected);
    ASSERT_TRUE(results.tunable_selected);
    ASSERT_FALSE(results.get_default_perf_cfg_invoked);
    ASSERT_TRUE(results.search_invoked);
    ASSERT_EQ(results.perf_cfg_value, TestPerfConfig::searched_value);
}