halide 19.0.0__cp310-cp310-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. halide/__init__.py +39 -0
  2. halide/_generator_helpers.py +835 -0
  3. halide/bin/adams2019_retrain_cost_model +0 -0
  4. halide/bin/adams2019_weightsdir_to_weightsfile +0 -0
  5. halide/bin/anderson2021_retrain_cost_model +0 -0
  6. halide/bin/anderson2021_weightsdir_to_weightsfile +0 -0
  7. halide/bin/featurization_to_sample +0 -0
  8. halide/bin/gengen +0 -0
  9. halide/bin/get_host_target +0 -0
  10. halide/halide_.cpython-310-darwin.so +0 -0
  11. halide/imageio.py +60 -0
  12. halide/include/Halide.h +35293 -0
  13. halide/include/HalideBuffer.h +2618 -0
  14. halide/include/HalidePyTorchCudaHelpers.h +64 -0
  15. halide/include/HalidePyTorchHelpers.h +120 -0
  16. halide/include/HalideRuntime.h +2221 -0
  17. halide/include/HalideRuntimeCuda.h +89 -0
  18. halide/include/HalideRuntimeD3D12Compute.h +91 -0
  19. halide/include/HalideRuntimeHexagonDma.h +104 -0
  20. halide/include/HalideRuntimeHexagonHost.h +157 -0
  21. halide/include/HalideRuntimeMetal.h +112 -0
  22. halide/include/HalideRuntimeOpenCL.h +119 -0
  23. halide/include/HalideRuntimeQurt.h +32 -0
  24. halide/include/HalideRuntimeVulkan.h +137 -0
  25. halide/include/HalideRuntimeWebGPU.h +44 -0
  26. halide/lib/cmake/Halide/FindHalide_LLVM.cmake +152 -0
  27. halide/lib/cmake/Halide/FindV8.cmake +33 -0
  28. halide/lib/cmake/Halide/Halide-shared-deps.cmake +0 -0
  29. halide/lib/cmake/Halide/Halide-shared-targets-release.cmake +29 -0
  30. halide/lib/cmake/Halide/Halide-shared-targets.cmake +154 -0
  31. halide/lib/cmake/Halide/HalideConfig.cmake +162 -0
  32. halide/lib/cmake/Halide/HalideConfigVersion.cmake +65 -0
  33. halide/lib/cmake/HalideHelpers/FindHalide_WebGPU.cmake +27 -0
  34. halide/lib/cmake/HalideHelpers/Halide-Interfaces-release.cmake +116 -0
  35. halide/lib/cmake/HalideHelpers/Halide-Interfaces.cmake +236 -0
  36. halide/lib/cmake/HalideHelpers/HalideGeneratorHelpers.cmake +1056 -0
  37. halide/lib/cmake/HalideHelpers/HalideHelpersConfig.cmake +28 -0
  38. halide/lib/cmake/HalideHelpers/HalideHelpersConfigVersion.cmake +54 -0
  39. halide/lib/cmake/HalideHelpers/HalideTargetHelpers.cmake +99 -0
  40. halide/lib/cmake/HalideHelpers/MutexCopy.ps1 +31 -0
  41. halide/lib/cmake/HalideHelpers/TargetExportScript.cmake +55 -0
  42. halide/lib/cmake/Halide_Python/Halide_Python-targets-release.cmake +30 -0
  43. halide/lib/cmake/Halide_Python/Halide_Python-targets.cmake +125 -0
  44. halide/lib/cmake/Halide_Python/Halide_PythonConfig.cmake +26 -0
  45. halide/lib/cmake/Halide_Python/Halide_PythonConfigVersion.cmake +65 -0
  46. halide/lib/libHalide.dylib +0 -0
  47. halide/lib/libHalidePyStubs.a +0 -0
  48. halide/lib/libHalide_GenGen.a +0 -0
  49. halide/lib/libautoschedule_adams2019.so +0 -0
  50. halide/lib/libautoschedule_anderson2021.so +0 -0
  51. halide/lib/libautoschedule_li2018.so +0 -0
  52. halide/lib/libautoschedule_mullapudi2016.so +0 -0
  53. halide/share/doc/Halide/LICENSE.txt +233 -0
  54. halide/share/doc/Halide/README.md +439 -0
  55. halide/share/doc/Halide/doc/BuildingHalideWithCMake.md +626 -0
  56. halide/share/doc/Halide/doc/CodeStyleCMake.md +393 -0
  57. halide/share/doc/Halide/doc/FuzzTesting.md +104 -0
  58. halide/share/doc/Halide/doc/HalideCMakePackage.md +812 -0
  59. halide/share/doc/Halide/doc/Hexagon.md +73 -0
  60. halide/share/doc/Halide/doc/Python.md +844 -0
  61. halide/share/doc/Halide/doc/RunGen.md +283 -0
  62. halide/share/doc/Halide/doc/Testing.md +125 -0
  63. halide/share/doc/Halide/doc/Vulkan.md +287 -0
  64. halide/share/doc/Halide/doc/WebAssembly.md +228 -0
  65. halide/share/doc/Halide/doc/WebGPU.md +128 -0
  66. halide/share/tools/RunGen.h +1470 -0
  67. halide/share/tools/RunGenMain.cpp +642 -0
  68. halide/share/tools/adams2019_autotune_loop.sh +227 -0
  69. halide/share/tools/anderson2021_autotune_loop.sh +591 -0
  70. halide/share/tools/halide_benchmark.h +240 -0
  71. halide/share/tools/halide_image.h +31 -0
  72. halide/share/tools/halide_image_info.h +318 -0
  73. halide/share/tools/halide_image_io.h +2794 -0
  74. halide/share/tools/halide_malloc_trace.h +102 -0
  75. halide/share/tools/halide_thread_pool.h +161 -0
  76. halide/share/tools/halide_trace_config.h +559 -0
  77. halide-19.0.0.data/data/share/cmake/Halide/HalideConfig.cmake +6 -0
  78. halide-19.0.0.data/data/share/cmake/Halide/HalideConfigVersion.cmake +65 -0
  79. halide-19.0.0.data/data/share/cmake/HalideHelpers/HalideHelpersConfig.cmake +6 -0
  80. halide-19.0.0.data/data/share/cmake/HalideHelpers/HalideHelpersConfigVersion.cmake +54 -0
  81. halide-19.0.0.dist-info/METADATA +301 -0
  82. halide-19.0.0.dist-info/RECORD +84 -0
  83. halide-19.0.0.dist-info/WHEEL +5 -0
  84. halide-19.0.0.dist-info/licenses/LICENSE.txt +233 -0
@@ -0,0 +1,642 @@
1
+ #include "RunGen.h"
2
+
3
+ using namespace Halide::RunGen;
4
+ using Halide::Tools::BenchmarkConfig;
5
+
6
+ namespace {
7
+
8
+ struct RegisteredFilter {
9
+ struct RegisteredFilter *next;
10
+ int (*filter_argv_call)(void **);
11
+ const struct halide_filter_metadata_t *filter_metadata;
12
+ };
13
+
14
+ RegisteredFilter *registered_filters = nullptr;
15
+
16
+ extern "C" void halide_register_argv_and_metadata(
17
+ int (*filter_argv_call)(void **),
18
+ const struct halide_filter_metadata_t *filter_metadata,
19
+ const char *const *extra_key_value_pairs) {
20
+
21
+ auto *rf = new RegisteredFilter();
22
+ rf->next = registered_filters;
23
+ rf->filter_argv_call = filter_argv_call;
24
+ rf->filter_metadata = filter_metadata;
25
+ // RunGen ignores extra_key_value_pairs
26
+ registered_filters = rf;
27
+ }
28
+
29
+ std::string replace_all(const std::string &str,
30
+ const std::string &find,
31
+ const std::string &replace) {
32
+ size_t pos = 0;
33
+ std::string result = str;
34
+ while ((pos = result.find(find, pos)) != std::string::npos) {
35
+ result.replace(pos, find.length(), replace);
36
+ pos += replace.length();
37
+ }
38
+ return result;
39
+ }
40
+
41
+ void usage(const char *argv0) {
42
+ const std::string usage = R"USAGE(
43
+ Usage: $NAME$ argument=value [argument=value... ] [flags]
44
+
45
+ Arguments:
46
+
47
+ Specify the Generator's input and output values by name, in any order.
48
+
49
+ Scalar inputs are specified in the obvious syntax, e.g.
50
+
51
+ some_int=42 some_float=3.1415
52
+
53
+ You can also use the text `default` or `estimate` to use the default or
54
+ estimate value of the given input, respectively. (You can join these by
55
+ commas to give default-then-estimate or estimate-then-default behaviors.)
56
+
57
+ Buffer inputs and outputs are specified by pathname:
58
+
59
+ some_input_buffer=/path/to/existing/file.png
60
+ some_output_buffer=/path/to/create/output/file.png
61
+
62
+ We currently support JPG, PGM, PNG, PPM format. If the type or dimensions
63
+ of the input or output file type can't support the data (e.g., your filter
64
+ uses float32 input and output, and you load/save to PNG), we'll use the most
65
+ robust approximation within the format and issue a warning to stdout.
66
+
67
+ (We anticipate adding other image formats in the future, in particular,
68
+ TIFF and TMP.)
69
+
70
+ For inputs, there are also "pseudo-file" specifiers you can use; currently
71
+ supported are
72
+
73
+ zero:[NUM,NUM,...]
74
+
75
+ This input should be an image with the given extents, and all elements
76
+ set to zero of the appropriate type. (This is useful for benchmarking
77
+ filters that don't have performance variances with different data.)
78
+
79
+ constant:VALUE:[NUM,NUM,...]
80
+
81
+ Like zero, but allows an arbitrary value of the input's type.
82
+
83
+ identity:[NUM,NUM,...]
84
+
85
+ This input should be an image with the given extents, where diagonal
86
+ elements are set to one of the appropriate type, and the rest are zero.
87
+ Diagonal elements are those whose first two coordinates are equal.
88
+
89
+ random:SEED:[NUM,NUM,...]
90
+
91
+ This input should be an image with the given extents, and all elements
92
+ set to a random value of the appropriate type. The random values will
93
+ be constructed using the mt19937_64 engine, using the given seed;
94
+ all floating point values will be in a uniform distribution between
95
+ 0.0 and 1.0, while integral values will be uniform across the entire
96
+ range of the type.
97
+
98
+ (We anticipate adding other pseudo-file inputs in the future, e.g.
99
+ various random distributions, gradients, rainbows, etc.)
100
+
101
+ In place of [NUM,NUM,...] for boundary, you may specify 'auto'; this
102
+ will run a bounds-query to choose a legal input size given the output
103
+ size constraints. (In general, this is useful only when also using
104
+ the --output_extents flag.)
105
+
106
+ In place of [NUM,NUM,...] for boundary, you may specify 'estimate';
107
+ this will use the estimated bounds specified in the code.
108
+
109
+ Flags:
110
+
111
+ --help:
112
+ print this message and exit.
113
+
114
+ --describe:
115
+ print names and types of all arguments to stdout and exit.
116
+
117
+ --output_extents=[NUM,NUM,...]
118
+ By default, we attempt to calculate a reasonable size for the output
119
+ buffers, based on the size of the input buffers and bounds query; if we
120
+ guess wrong, or you want to explicitly specify the desired output size,
121
+ you can specify the extent of each dimension with this flag:
122
+
123
+ --output_extents=[1000,100] # 2 dimensions: w=1000 h = 100
124
+ --output_extents=[100,200,3] # 3 dimensions: w=100 h=200 c=3
125
+
126
+ Note that if there are multiple outputs, all will be constrained
127
+ to this shape.
128
+
129
+ --verbose:
130
+ emit extra diagnostic output.
131
+
132
+ --quiet:
133
+ Don't log calls to halide_print() to stdout.
134
+
135
+ --benchmarks=all:
136
+ Run the filter with the given arguments many times to
137
+ produce an estimate of average execution time; this currently
138
+ runs "samples" sets of "iterations" each, and chooses the fastest
139
+ sample set.
140
+
141
+ --benchmark_min_time=DURATION_SECONDS [default = 0.1]:
142
+ Override the default minimum desired benchmarking time; ignored if
143
+ --benchmarks is not also specified.
144
+
145
+ --track_memory:
146
+ Override Halide memory allocator to track high-water mark of memory
147
+ allocation during run; note that this may slow down execution, so
148
+ benchmarks may be inaccurate if you combine --benchmark with this.
149
+
150
+ --default_input_buffers=VALUE:
151
+ Specify the value for all otherwise-unspecified buffer inputs, in the
152
+ same syntax in use above. If you omit =VALUE, "zero:auto" will be used.
153
+
154
+ --default_input_scalars=VALUE:
155
+ Specify the value for all otherwise-unspecified scalar inputs, in the
156
+ same syntax in use above. If you omit =VALUE, "estimate,default"
157
+ will be used.
158
+
159
+ --parsable_output:
160
+ Final output is emitted in an easy-to-parse output (one value per line),
161
+ rather than easy-for-humans.
162
+
163
+ --estimate_all:
164
+ Request that all inputs and outputs are based on estimate,
165
+ and fill buffers with random values. This is exactly equivalent to
166
+ specifying
167
+
168
+ --default_input_buffers=estimate_then_auto
169
+ --default_input_scalars=estimate
170
+ --output_extents=estimate
171
+
172
+ and is a convenience for automated benchmarking.
173
+
174
+ --success:
175
+ Print "Success!" to stdout if we exit with a result code of zero.
176
+ (This is mainly useful for use with Halide's testing infrastructure,
177
+ which relies on this for successful tests.)
178
+
179
+ --skip_bad_environement:
180
+ If the Generator is built for a runtime environment we don't have available
181
+ (e.g. Cuda on a system without Cuda installed), emit a [SKIP] warning to
182
+ stdout and return an exit code of zero; this allows for quiet "failures"
183
+ when used with CTest.
184
+
185
+ Known Issues:
186
+
187
+ * Filters running on GPU (vs CPU) have not been tested.
188
+ * Filters using buffer layouts other than planar (e.g. interleaved/chunky)
189
+ may be buggy.
190
+
191
+ )USAGE";
192
+
193
+ std::string basename = split_string(replace_all(argv0, "\\", "/"), "/").back();
194
+ std::cout << replace_all(usage, "$NAME$", basename);
195
+ }
196
+
197
+ // Utility class for installing memory-tracking machinery into the Halide runtime
198
+ // when --track_memory is specified.
199
+ class HalideMemoryTracker {
200
+ static HalideMemoryTracker *active;
201
+
202
+ std::mutex tracker_mutex;
203
+
204
+ // Total current CPU memory allocated via halide_malloc.
205
+ // Access controlled by tracker_mutex.
206
+ uint64_t memory_allocated{0};
207
+
208
+ // High-water mark of CPU memory allocated since program start
209
+ // (or last call to get_cpu_memory_highwater_reset).
210
+ // Access controlled by tracker_mutex.
211
+ uint64_t memory_highwater{0};
212
+
213
+ // Map of outstanding allocation sizes.
214
+ // Access controlled by tracker_mutex.
215
+ std::map<void *, size_t> memory_size_map;
216
+
217
+ void *tracker_malloc_impl(void *user_context, size_t x) {
218
+ std::lock_guard<std::mutex> lock(tracker_mutex);
219
+
220
+ void *ptr = halide_default_malloc(user_context, x);
221
+
222
+ memory_allocated += x;
223
+ if (memory_highwater < memory_allocated) {
224
+ memory_highwater = memory_allocated;
225
+ }
226
+ if (memory_size_map.find(ptr) != memory_size_map.end()) {
227
+ halide_error(user_context, "Tracking error in tracker_malloc");
228
+ }
229
+ memory_size_map[ptr] = x;
230
+
231
+ return ptr;
232
+ }
233
+
234
+ void tracker_free_impl(void *user_context, void *ptr) {
235
+ std::lock_guard<std::mutex> lock(tracker_mutex);
236
+ auto it = memory_size_map.find(ptr);
237
+ if (it == memory_size_map.end()) {
238
+ halide_error(user_context, "Tracking error in tracker_free");
239
+ }
240
+ size_t x = it->second;
241
+ memory_allocated -= x;
242
+ memory_size_map.erase(it);
243
+ halide_default_free(user_context, ptr);
244
+ }
245
+
246
+ static void *tracker_malloc(void *user_context, size_t x) {
247
+ return active->tracker_malloc_impl(user_context, x);
248
+ }
249
+
250
+ static void tracker_free(void *user_context, void *ptr) {
251
+ return active->tracker_free_impl(user_context, ptr);
252
+ }
253
+
254
+ public:
255
+ void install() {
256
+ assert(!active);
257
+ active = this;
258
+ halide_set_custom_malloc(tracker_malloc);
259
+ halide_set_custom_free(tracker_free);
260
+ }
261
+
262
+ uint64_t allocated() {
263
+ std::lock_guard<std::mutex> lock(tracker_mutex);
264
+ return memory_allocated;
265
+ }
266
+
267
+ uint64_t highwater() {
268
+ std::lock_guard<std::mutex> lock(tracker_mutex);
269
+ return memory_highwater;
270
+ }
271
+
272
+ void highwater_reset() {
273
+ std::lock_guard<std::mutex> lock(tracker_mutex);
274
+ memory_highwater = memory_allocated;
275
+ }
276
+ };
277
+
278
+ /* static */ HalideMemoryTracker *HalideMemoryTracker::active{nullptr};
279
+
280
+ bool log_info = false;
281
+ bool log_warn = true;
282
+
283
+ void do_log_cout(const std::string &s) {
284
+ std::cout << s;
285
+ }
286
+
287
+ void do_log_cerr(const std::string &s) {
288
+ std::cerr << s;
289
+ }
290
+
291
+ void do_log_info(const std::string &s) {
292
+ if (log_info) {
293
+ do_log_cerr(s);
294
+ }
295
+ }
296
+
297
+ void do_log_warn(const std::string &s) {
298
+ if (log_warn) {
299
+ do_log_cerr("Warning: " + s);
300
+ }
301
+ }
302
+
303
+ void do_log_fail(const std::string &s) {
304
+ do_log_cerr(s);
305
+ abort();
306
+ }
307
+
308
+ } // namespace
309
+
310
+ namespace Halide {
311
+ namespace RunGen {
312
+
313
+ Logger log() {
314
+ return {do_log_cout, do_log_info, do_log_warn, do_log_fail};
315
+ }
316
+
317
+ } // namespace RunGen
318
+ } // namespace Halide
319
+
320
+ int main(int argc, char **argv) {
321
+ if (argc <= 1) {
322
+ usage(argv[0]);
323
+ return 0;
324
+ }
325
+
326
+ if (registered_filters == nullptr) {
327
+ std::cerr << "No filters registered. Compile RunGenMain.cpp along with at least one 'registration' output from a generator.\n";
328
+ return 1;
329
+ }
330
+
331
+ // Look for --name
332
+ std::string filter_name;
333
+ for (int i = 1; i < argc; ++i) {
334
+ if (argv[i][0] == '-') {
335
+ const char *p = argv[i] + 1; // skip -
336
+ if (p[0] == '-') {
337
+ p++; // allow -- as well, because why not
338
+ }
339
+ std::vector<std::string> v = split_string(p, "=");
340
+ std::string flag_name = v[0];
341
+ std::string flag_value = v.size() > 1 ? v[1] : "";
342
+ // Check for the help flag early so that it takes
343
+ // precedence over other errors that occur before full
344
+ // argument parsing.
345
+ if (flag_name == "help") {
346
+ usage(argv[0]);
347
+ return 0;
348
+ }
349
+ if (v.size() > 2) {
350
+ fail() << "Invalid argument: " << argv[i];
351
+ }
352
+ if (flag_name != "name") {
353
+ continue;
354
+ }
355
+ if (!filter_name.empty()) {
356
+ fail() << "--name cannot be specified twice.";
357
+ }
358
+ filter_name = flag_value;
359
+ if (filter_name.empty()) {
360
+ fail() << "--name cannot be empty.";
361
+ }
362
+ }
363
+ }
364
+
365
+ auto *rf = registered_filters;
366
+ if (filter_name.empty()) {
367
+ // Just choose the first one.
368
+ if (rf->next != nullptr) {
369
+ std::ostringstream o;
370
+ o << "Must specify --name if multiple filters are registered; registered filters are:\n";
371
+ for (auto *rf = registered_filters; rf != nullptr; rf = rf->next) {
372
+ o << " " << rf->filter_metadata->name << "\n";
373
+ }
374
+ o << "\n";
375
+ fail() << o.str();
376
+ }
377
+ } else {
378
+ for (; rf != nullptr; rf = rf->next) {
379
+ if (filter_name == rf->filter_metadata->name) {
380
+ break;
381
+ }
382
+ }
383
+ if (rf == nullptr) {
384
+ std::ostringstream o;
385
+ o << "Filter " << filter_name << " not found; registered filters are:\n";
386
+ for (auto *rf = registered_filters; rf != nullptr; rf = rf->next) {
387
+ o << " " << rf->filter_metadata->name << "\n";
388
+ }
389
+ o << "\n";
390
+ fail() << o.str();
391
+ }
392
+ }
393
+
394
+ RunGen r(rf->filter_argv_call, rf->filter_metadata);
395
+
396
+ std::string user_specified_output_shape;
397
+ std::set<std::string> seen_args;
398
+ bool benchmark = false;
399
+ bool track_memory = false;
400
+ bool describe = false;
401
+ double benchmark_min_time = BenchmarkConfig().min_time;
402
+ std::string default_input_buffers;
403
+ std::string default_input_scalars;
404
+ std::string benchmarks_flag_value;
405
+ bool emit_success = false;
406
+ bool skip_bad_environment = false;
407
+ for (int i = 1; i < argc; ++i) {
408
+ if (argv[i][0] == '-') {
409
+ const char *p = argv[i] + 1; // skip -
410
+ if (p[0] == '-') {
411
+ p++; // allow -- as well, because why not
412
+ }
413
+ std::vector<std::string> v = split_string(p, "=");
414
+ std::string flag_name = v[0];
415
+ std::string flag_value = v.size() > 1 ? v[1] : "";
416
+ if (v.size() > 2) {
417
+ fail() << "Invalid argument: " << argv[i];
418
+ }
419
+ if (flag_name == "name") {
420
+ continue;
421
+ } else if (flag_name == "verbose") {
422
+ if (flag_value.empty()) {
423
+ flag_value = "true";
424
+ }
425
+ if (!parse_scalar(flag_value, &log_info)) {
426
+ fail() << "Invalid value for flag: " << flag_name;
427
+ }
428
+ } else if (flag_name == "quiet") {
429
+ if (flag_value.empty()) {
430
+ flag_value = "true";
431
+ }
432
+ bool quiet;
433
+ if (!parse_scalar(flag_value, &quiet)) {
434
+ fail() << "Invalid value for flag: " << flag_name;
435
+ }
436
+ r.set_quiet(quiet);
437
+ } else if (flag_name == "parsable_output") {
438
+ if (flag_value.empty()) {
439
+ flag_value = "true";
440
+ }
441
+ bool parsable_output;
442
+ if (!parse_scalar(flag_value, &parsable_output)) {
443
+ fail() << "Invalid value for flag: " << flag_name;
444
+ }
445
+ r.set_parsable_output(parsable_output);
446
+ } else if (flag_name == "describe") {
447
+ if (flag_value.empty()) {
448
+ flag_value = "true";
449
+ }
450
+ if (!parse_scalar(flag_value, &describe)) {
451
+ fail() << "Invalid value for flag: " << flag_name;
452
+ }
453
+ } else if (flag_name == "track_memory") {
454
+ if (flag_value.empty()) {
455
+ flag_value = "true";
456
+ }
457
+ if (!parse_scalar(flag_value, &track_memory)) {
458
+ fail() << "Invalid value for flag: " << flag_name;
459
+ }
460
+ } else if (flag_name == "benchmarks") {
461
+ benchmarks_flag_value = flag_value;
462
+ benchmark = true;
463
+ } else if (flag_name == "benchmark_min_time") {
464
+ if (!parse_scalar(flag_value, &benchmark_min_time)) {
465
+ fail() << "Invalid value for flag: " << flag_name;
466
+ }
467
+ } else if (flag_name == "default_input_buffers") {
468
+ default_input_buffers = flag_value;
469
+ if (default_input_buffers.empty()) {
470
+ default_input_buffers = "zero:auto";
471
+ }
472
+ } else if (flag_name == "default_input_scalars") {
473
+ default_input_scalars = flag_value;
474
+ if (default_input_scalars.empty()) {
475
+ default_input_scalars = "estimate,default";
476
+ }
477
+ } else if (flag_name == "output_extents") {
478
+ user_specified_output_shape = flag_value;
479
+ } else if (flag_name == "estimate_all") {
480
+ // Equivalent to:
481
+ // --default_input_buffers=random:0:estimate_then_auto
482
+ // --default_input_scalars=estimate
483
+ // --output_extents=estimate
484
+ default_input_buffers = "random:0:estimate_then_auto";
485
+ default_input_scalars = "estimate";
486
+ user_specified_output_shape = "estimate";
487
+ } else if (flag_name == "success") {
488
+ if (flag_value.empty()) {
489
+ flag_value = "true";
490
+ }
491
+ if (!parse_scalar(flag_value, &emit_success)) {
492
+ fail() << "Invalid value for flag: " << flag_name;
493
+ }
494
+ } else if (flag_name == "skip_bad_environment") {
495
+ if (flag_value.empty()) {
496
+ flag_value = "true";
497
+ }
498
+ if (!parse_scalar(flag_value, &skip_bad_environment)) {
499
+ fail() << "Invalid value for flag: " << flag_name;
500
+ }
501
+ } else {
502
+ usage(argv[0]);
503
+ fail() << "Unknown flag: " << flag_name;
504
+ }
505
+ } else {
506
+ // Assume it's a named Input or Output for the Generator,
507
+ // in the form name=value.
508
+ std::vector<std::string> v = split_string(argv[i], "=");
509
+ if (v.size() != 2 || v[0].empty() || v[1].empty()) {
510
+ fail() << "Invalid argument: " << argv[i];
511
+ }
512
+ r.parse_one(v[0], v[1], &seen_args);
513
+ }
514
+ }
515
+
516
+ if (skip_bad_environment) {
517
+ // Let's do some preflighting to see if we are trying to run on a system that
518
+ // definitely doesn't support our target. For now, we're just checking Cuda
519
+ // because it's the only thing needed for current Halide tests.
520
+ //
521
+ // TODO: add checking for other GPU (etc) backends.
522
+ std::set<std::string> tokens;
523
+ {
524
+ std::stringstream s(rf->filter_metadata->target);
525
+ std::string tok;
526
+ while (std::getline(s, tok, '-')) {
527
+ tokens.insert(tok);
528
+ }
529
+ }
530
+ if (tokens.count("cuda")) {
531
+ void *cuda_intf = halide_get_symbol("halide_cuda_device_interface");
532
+ if (cuda_intf == nullptr) {
533
+ std::cout << "[SKIP] This system requires Cuda, but the Halide Cuda runtime is not available.\n";
534
+ return 0;
535
+ }
536
+ typedef halide_device_interface_t *halide_device_interface_t_ptr;
537
+ typedef halide_device_interface_t_ptr (*GetDeviceInterfaceFn)();
538
+ GetDeviceInterfaceFn fn = reinterpret_cast<GetDeviceInterfaceFn>(cuda_intf);
539
+ halide_device_interface_t_ptr intf = fn();
540
+ int major, minor;
541
+ int err = intf->compute_capability(nullptr, &major, &minor);
542
+ if (err != 0) {
543
+ std::cout << "[SKIP] This system requires Cuda, which is not available.\n";
544
+ return 0;
545
+ }
546
+ const int version_available = major * 10 + minor;
547
+
548
+ int version_required = 20; // Minimum for any Halide-generated Cuda output
549
+ static const std::pair<const char *, int> capabilities[] = {
550
+ {"cuda_capability30", 30},
551
+ {"cuda_capability32", 32},
552
+ {"cuda_capability35", 35},
553
+ {"cuda_capability50", 50},
554
+ {"cuda_capability61", 61},
555
+ {"cuda_capability70", 70},
556
+ {"cuda_capability75", 75},
557
+ {"cuda_capability80", 80},
558
+ {"cuda_capability86", 86},
559
+ };
560
+ for (const auto &it : capabilities) {
561
+ if (tokens.count(it.first)) {
562
+ version_required = std::max(version_required, it.second);
563
+ }
564
+ }
565
+
566
+ if (version_available < version_required) {
567
+ std::cout << "[SKIP] This system supports only Cuda compute capability " << major << "." << minor << " but compute capability "
568
+ << (version_required / 10) << "." << (version_required % 10) << " is required.\n";
569
+ return 0;
570
+ }
571
+ }
572
+ }
573
+
574
+ if (describe) {
575
+ r.describe();
576
+ return 0;
577
+ }
578
+
579
+ // It's OK to omit output arguments when we are benchmarking or tracking memory.
580
+ bool ok_to_omit_outputs = (benchmark || track_memory);
581
+
582
+ if (benchmark && track_memory) {
583
+ warn() << "Using --track_memory with --benchmarks will produce inaccurate benchmark results.";
584
+ }
585
+
586
+ // Check to be sure that all required arguments are specified.
587
+ r.validate(seen_args, default_input_buffers, default_input_scalars, ok_to_omit_outputs);
588
+
589
+ // Parse all the input arguments, loading images as necessary.
590
+ // (Don't handle outputs yet.)
591
+ r.load_inputs(user_specified_output_shape);
592
+
593
+ // Run a bounds query: we need to figure out how to allocate the output buffers,
594
+ // and the input buffers might need reshaping to satisfy constraints (e.g. a chunky/interleaved layout).
595
+ std::vector<Shape> constrained_shapes = r.run_bounds_query();
596
+
597
+ r.adapt_input_buffers(constrained_shapes);
598
+ r.allocate_output_buffers(constrained_shapes);
599
+
600
+ // If we're tracking memory, install the memory tracker *after* doing a bounds query.
601
+ HalideMemoryTracker tracker;
602
+ if (track_memory) {
603
+ tracker.install();
604
+ }
605
+
606
+ // This is a single-purpose binary to benchmark this filter, so we
607
+ // shouldn't be eagerly returning device memory.
608
+ if (auto result = halide_reuse_device_allocations(nullptr, true); result != halide_error_code_success) {
609
+ std::cerr << "halide_reuse_device_allocations() returned an error: " << (int)result << "\n";
610
+ }
611
+
612
+ if (benchmark) {
613
+ if (benchmarks_flag_value.empty()) {
614
+ benchmarks_flag_value = "all";
615
+ }
616
+ if (benchmarks_flag_value != "all") {
617
+ fail() << "The only valid value for --benchmarks is 'all'";
618
+ }
619
+ r.run_for_benchmark(benchmark_min_time);
620
+ } else {
621
+ r.run_for_output();
622
+ }
623
+
624
+ if (track_memory) {
625
+ // Ensure that we copy any GPU-output buffers back to host before
626
+ // we report on memory usage.
627
+ if (auto result = r.copy_outputs_to_host(); result != halide_error_code_success) {
628
+ std::cerr << "Warning: copy_outputs_to_host() returned error " << (int)result << "\n";
629
+ }
630
+ std::cout << "Maximum Halide memory: " << tracker.highwater()
631
+ << " bytes for output of " << r.megapixels_out() << " mpix.\n";
632
+ }
633
+
634
+ // Save the output(s), if necessary.
635
+ r.save_outputs();
636
+
637
+ if (emit_success) {
638
+ std::cout << "Success!\n";
639
+ }
640
+
641
+ return 0;
642
+ }