-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathfind_solution.hpp
423 lines (391 loc) · 16.8 KB
/
find_solution.hpp
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*******************************************************************************
*
* MIT License
*
* Copyright (c) 2019 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.
*
*******************************************************************************/
#ifndef MIOPEN_GUARD_MLOPEN_FIND_SOLUTION_HPP
#define MIOPEN_GUARD_MLOPEN_FIND_SOLUTION_HPP
#include <miopen/env.hpp>
#include <miopen/conv_solution.hpp>
#include <miopen/execution_context.hpp>
#include <miopen/find_controls.hpp>
#include <miopen/handle.hpp>
#include <miopen/search_options.hpp>
#include <miopen/solver_id.hpp>
#include <miopen/solver.hpp>
#include <limits>
#include <vector>
namespace miopen {
struct AnyInvokeParams;
namespace solver {
template <class Solver, class Context, class Problem, class Db>
auto FindSolutionImpl(rank<1>,
Solver s,
const Context& context,
const Problem& problem,
Db& db,
const AnyInvokeParams& invoke_ctx,
const std::string& perf_cfg,
const std::optional<FindOptions>& options)
-> decltype(s.GetSolution(context, problem, s.Search(context, problem, invoke_ctx)))
{
const FindEnforce enforce =
options && options->find_enforce ? *options->find_enforce : FindEnforce{};
if(context.disable_perfdb_access)
{
MIOPEN_LOG_I(s.SolverDbId() << " (db access disabled)");
return s.GetSolution(context, problem, s.GetDefaultPerformanceConfig(context, problem));
}
MIOPEN_LOG_I(s.SolverDbId());
if(enforce.IsDbClean(context))
{
if(db.Remove(problem, s.SolverDbId()))
MIOPEN_LOG_W("Perf Db: record removed: " << s.SolverDbId() << ", enforce: " << enforce);
}
else
{
if((context.do_search || enforce.IsSearch(context)) &&
(context.db_update || enforce.IsDbUpdate(context)))
{
MIOPEN_LOG_W("Perf Db: load skipped: " << s.SolverDbId() << ", enforce: " << enforce);
}
else
{
using PerformanceConfig = decltype(s.GetDefaultPerformanceConfig(context, problem));
PerformanceConfig config{};
// The passes in string needs to have priority over the entry in the database
if(!perf_cfg.empty())
{
config.Deserialize(perf_cfg);
if(s.IsValidPerformanceConfig(context, problem, config))
{
return s.GetSolution(context, problem, config);
}
MIOPEN_LOG_WE("Invalid config loaded from Perf Db: "
<< s.SolverDbId() << ": " << config << ". Performance may degrade.");
}
else if(db.Load(problem, s.SolverDbId(), config))
{
MIOPEN_LOG_I2("Perf Db: record loaded: " << s.SolverDbId());
if(s.IsValidPerformanceConfig(context, problem, config))
{
return s.GetSolution(context, problem, config);
}
MIOPEN_LOG_WE("Invalid config loaded from Perf Db: "
<< s.SolverDbId() << ": " << config << ". Performance may degrade.");
}
else if(!s.AltSolverDbId().empty() && db.Load(problem, s.AltSolverDbId(), config))
{
MIOPEN_LOG_I("Perf Db: alternate record loaded: " << s.AltSolverDbId());
if(s.IsValidPerformanceConfig(context, problem, config))
{
return s.GetSolution(context, problem, config);
}
MIOPEN_LOG_WE("Invalid alternate record loaded from Perf Db: "
<< s.AltSolverDbId() << ": " << config
<< ". Performance may degrade.");
}
else
{
MIOPEN_LOG_I("Perf Db: record not found for: " << s.SolverDbId());
}
}
if(context.do_search || enforce.IsSearch(context)) // TODO: Make it a customization point
{
MIOPEN_LOG_I("Starting search: " << s.SolverDbId() << ", enforce: " << enforce);
try
{
auto c = s.Search(context, problem, invoke_ctx);
db.Update(problem, s.SolverDbId(), c);
return s.GetSolution(context, problem, c);
}
catch(const miopen::Exception& ex)
{
MIOPEN_LOG_E("Search failed for: " << s.SolverDbId() << ": " << ex.what());
return ConvSolution(miopenStatusInternalError);
}
}
}
return s.GetSolution(context, problem, s.GetDefaultPerformanceConfig(context, problem));
}
template <class Solver, class Context, class Problem, class Db>
auto FindSolutionImpl(rank<0>,
Solver s,
const Context& context,
const Problem& problem,
Db&,
const AnyInvokeParams&,
const std::string&,
const std::optional<FindOptions>&)
-> decltype(s.GetSolution(context, problem))
{
MIOPEN_LOG_I(s.SolverDbId() << " (not searchable)");
return s.GetSolution(context, problem);
}
/// Finds optimized Solution. Generic method.
///
/// Given the specific problem config, finds (hopefully) optimal
/// solution-specific parameters and returns the Solution object.
/// Could take long if an exhaustive search is requested/performed.
/// May read/write perfDb.
template <class Solver, class Context, class Problem, class Db>
ConvSolution FindSolution(Solver s,
const Context& context,
const Problem& problem,
Db& db,
const AnyInvokeParams& invoke_ctx,
const std::string& perf_cfg = "",
const std::optional<FindOptions>& options = std::nullopt)
{
static_assert(sizeof(Solver) == sizeof(SolverBase), "Solver must be stateless");
static_assert(std::is_base_of<SolverBase, Solver>{}, "Not derived class of SolverBase");
// TODO: This assumes all solutions are ConvSolution
auto solution =
FindSolutionImpl(rank<1>{}, s, context, problem, db, invoke_ctx, perf_cfg, options);
solution.solver_id = s.SolverDbId();
return solution;
}
template <class... Solvers>
struct SolverContainer
{
template <class... SolversRight>
auto operator+(SolverContainer<SolversRight...>) const
{
return SolverContainer<Solvers..., SolversRight...>{};
}
///\todo: remove when AnySolver would be able to work with non-conv solvers
template <class Functor>
void FindById(solver::Id id, Functor&& receiver)
{
bool found = false;
miopen::each_args(
[&](auto solver) {
if(found || id != solver::Id{solver.SolverDbId()})
return;
found = true;
receiver(solver);
},
Solvers{}...);
}
// Search for all applicable solutions among many solvers
template <class Context, class Problem, class Db, class Solution = miopen::solver::ConvSolution>
std::vector<Solution>
SearchForAllSolutions(const Context& ctx,
const Problem& problem,
Db&& db,
const AnyInvokeParams& invoke_ctx,
std::size_t limit = std::numeric_limits<std::size_t>::max(),
const std::optional<FindOptions>& options = std::nullopt) const
{
std::vector<Solution> ss;
std::size_t count = 0;
const auto find_only = GetEnvFindOnlySolver();
miopen::each_args(
[&](auto solver) {
if(count >= limit)
return;
if(find_only &&
(std::find(find_only->begin(), find_only->end(), Id{solver.SolverDbId()}) ==
find_only->end()))
{ // Do nothing (and keep silence for the sake of Tuna), just skip.
}
// For better performance, check IsDynamic() first, because
// it is much faster than IsApplicable().
else if(ctx.use_dynamic_solutions_only && !solver.IsDynamic())
{
MIOPEN_LOG_I2(solver.SolverDbId() << ": Skipped (non-dynamic)");
}
else if(!solver.IsApplicable(ctx, problem))
{
MIOPEN_LOG_I2(solver.SolverDbId() << ": Not applicable");
}
else
{
const Solution s =
FindSolution(solver, ctx, problem, db, invoke_ctx, "", options);
if(s.Succeeded())
{
++count;
ss.push_back(s);
MIOPEN_LOG_I2(solver.SolverDbId() << ": Success.");
}
else
{
/// \todo If Solver is applicable it must provide an appropriate Solution.
/// This is not the case for some 20x5 convolutions (and possibly others).
/// Normally we should not get here and message level should be Error.
/// For now, let's use Info (not Warning) level to avoid
/// flooding the console.
MIOPEN_LOG_I(solver.SolverDbId()
<< ": [Warning] Applicable Solver not succeeded.");
}
}
},
Solvers{}...);
return ss;
}
// Search for all applicable solutions among many solvers
template <class Problem, class Solution = miopen::solver::ConvSolution>
std::vector<Solution>
SearchForSolutions(const ExecutionContext& ctx,
const Problem& problem,
std::size_t limit = std::numeric_limits<std::size_t>::max()) const
{
std::vector<Solution> ss;
std::size_t count = 0;
const auto find_only = GetEnvFindOnlySolver();
miopen::each_args(
[&](auto solver) {
if(count >= limit)
return;
if(find_only &&
(std::find(find_only->begin(), find_only->end(), Id{solver.SolverDbId()}) ==
find_only->end()))
{ // Do nothing (and keep silence for the sake of Tuna), just skip.
}
// For better performance, check IsDynamic() first, because
// it is much faster than IsApplicable().
// else if(problem.use_dynamic_solutions_only && !solver.IsDynamic())
// MIOPEN_LOG_I2(solver.SolverDbId() << ": Skipped (non-dynamic)");
else if(!solver.IsApplicable(ctx, problem))
{
MIOPEN_LOG_I2(solver.SolverDbId() << ": Not applicable");
}
else
{
auto s = solver.GetSolution(ctx, problem);
s.solver_id = solver.SolverDbId();
if(s.Succeeded())
{
++count;
ss.push_back(s);
MIOPEN_LOG_I2(solver.SolverDbId() << ": Success.");
}
else
{
MIOPEN_LOG_E(solver.SolverDbId() << ": Applicable Solver not succeeded.");
}
}
},
Solvers{}...);
return ss;
}
template <class Context, class Problem>
std::vector<std::pair<std::string, size_t>>
GetWorkspaceSizes(const Context& ctx,
const Problem& problem,
std::size_t limit = std::numeric_limits<std::size_t>::max()) const
{
std::vector<std::pair<std::string, size_t>> res;
const auto find_only = GetEnvFindOnlySolver();
std::size_t count = 0;
miopen::each_args(
[&](auto solver) {
if(count >= limit)
return;
if(find_only &&
(std::find(find_only->begin(), find_only->end(), Id{solver.SolverDbId()}) ==
find_only->end()))
{ // Do nothing (and keep silence for the sake of Tuna), just skip.
}
else if(!solver.MayNeedWorkspace())
{
MIOPEN_LOG_I2(solver.SolverDbId() << ": Skipped (no workspace required)");
}
// For better performance, check IsDynamic() first, because
// it is much faster than IsApplicable().
else if(ctx.use_dynamic_solutions_only && !solver.IsDynamic())
{
MIOPEN_LOG_I2(solver.SolverDbId() << ": Skipped (non-dynamic)");
}
else if(!solver.IsApplicable(ctx, problem))
{
MIOPEN_LOG_I2(solver.SolverDbId() << ": Not applicable");
}
else
{
++count;
auto sz = solver.GetWorkspaceSize(ctx, problem);
res.push_back(std::make_pair(solver.SolverDbId(), sz));
MIOPEN_LOG_I2(solver.SolverDbId() << ": " << sz);
}
},
Solvers{}...);
return res;
}
// Search for all applicable solutions among many solvers
template <class Context, class Problem>
bool IsAnySolverApplicable(const Context& ctx, const Problem& problem) const
{
const auto find_only = GetEnvFindOnlySolver();
auto found = false;
miopen::each_args(
[&](auto solver) {
if(found || (find_only && (std::find(find_only->begin(),
find_only->end(),
Id{solver.SolverDbId()}) == find_only->end())))
return;
// For better performance, check IsDynamic() first, because
// it is much faster than IsApplicable().
if(ctx.use_dynamic_solutions_only && !solver.IsDynamic())
{
MIOPEN_LOG_I2(solver.SolverDbId() << ": Skipped (non-dynamic)");
return;
}
if(solver.IsApplicable(ctx, problem))
{
found = true;
return;
}
MIOPEN_LOG_I2(solver.SolverDbId() << ": Not applicable");
},
Solvers{}...);
return found;
}
template <class Problem>
void ExecutePrimitive(Handle& handle,
const Problem& problem,
const AlgorithmName& algo,
const AnyInvokeParams& invoke_params) const
{
const auto network_config = problem.MakeNetworkConfig();
if(const auto existingInvoker = handle.GetInvoker(network_config, boost::none, algo))
{
(*existingInvoker)(handle, invoke_params);
return;
}
auto ctx = ExecutionContext{&handle};
const auto slns = SearchForSolutions(ctx, problem, 1);
if(slns.empty())
MIOPEN_THROW(miopenStatusNotImplemented, "No solver found.");
const auto& sln = slns.front();
if(!sln.invoker_factory)
MIOPEN_THROW(miopenStatusInternalError, "Invoker missing in solver " + sln.solver_id);
const auto invoker = handle.PrepareInvoker(*sln.invoker_factory, sln.construction_params);
handle.RegisterInvoker(invoker, network_config, sln.solver_id, algo);
invoker(handle, invoke_params);
}
};
} // namespace solver
} // namespace miopen
#endif