1/**
2 * Copyright (c) Glow Contributors. See CONTRIBUTORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef GLOW_TOOLS_LOADER_EXECUTOR_CORE_HELPER_FUNCTIONS_H
18#define GLOW_TOOLS_LOADER_EXECUTOR_CORE_HELPER_FUNCTIONS_H
19
20#include "Loader.h"
21#include "glow/Graph/Nodes.h"
22#include "glow/Support/Support.h"
23
24#include "llvm/ADT/StringMap.h"
25#include "llvm/Support/Timer.h"
26
27/// Processes special command line args for Image module.
28void processExecutorCoreCmdArgVars();
29/// Clear external storage for cmd args defined in Image.
30void initExecutorCoreCmdArgVars();
31
32extern llvm::cl::list<std::string> inputImageDirs;
33extern glow::VecVec<std::string> inputImageFilenames_;
34extern std::vector<std::string> inputImageListFileOpt;
35extern llvm::cl::list<std::string> inputImageFilenamesOpt;
36extern llvm::cl::opt<std::string> inputTensorListFile;
37extern std::vector<std::string> modelInputsOpt;
38extern llvm::cl::opt<unsigned> excludedFirstWarmupRuns;
39
40extern llvm::cl::opt<unsigned> warmup;
41extern llvm::cl::opt<std::string> tracePath;
42extern llvm::cl::opt<bool> convertInAndOutToFp16;
43extern llvm::cl::opt<unsigned> miniBatch;
44extern llvm::cl::opt<unsigned> miniBatchThreads;
45extern llvm::cl::opt<bool> preloadAllImages;
46extern llvm::cl::opt<unsigned> repeatSingleBatchCount;
47
48extern std::unique_ptr<glow::TraceContext> traceContext;
49
50extern llvm::cl::opt<std::string> modelOutputName;
51
52/// Read all images from \p inputImageDir in to \p inputImageFilenames.
53void parseInputDir(const std::string &inputImageDir,
54 std::vector<std::string> &inputImageFilenames);
55
56/// Read all images from \p imageListFile in to \p imageFilenames.
57void parseInputList(const std::string &imageListFile,
58 std::vector<std::string> &imageFilenames);
59
60/// Write a prompt to stdout asking for filenames for classification. Read in
61/// those filenames and add them to \p filenames. \p filenames is cleared before
62/// adding the new set of filenames from stdin. \returns false if the passed in
63/// line was empty.
64bool getNextStdinImageFilenames(glow::VecVec<std::string> &filenames);
65
66/// Generate in \p imageLists the list of filenames, per input, corresponding to
67/// the next mini-batch of size \p miniBatchSize extracted from \p
68/// totalImageLists at index \p minibatchIndex. /returns true if the index is
69/// valid, false otherwise. In case the function returns true, \p minibatchIndex
70/// is incremented by \p miniBatchSize. Stop upon reaching \p miniBatchLimit.
71bool getNextMiniBatch(glow::VecVec<std::string> &imageLists,
72 glow::VecVecRef<std::string> totalImageLists,
73 size_t &miniBatchIndex, size_t miniBatchSize,
74 size_t miniBatchLimit);
75
76/// Given \p loader, the \p bindings, and \p inputImageType, build the graph
77/// from the provided protobuf file found via \p loader. Then compiles and
78/// \returns a pair of pointers to the input Placeholder and output Nodes Map.
79std::pair<llvm::StringMap<glow::Placeholder *>,
80 llvm::StringMap<glow::Placeholder *>>
81buildAndCompileAndGetInAndOutPair(glow::Loader &loader,
82 glow::PlaceholderBindings &bindings,
83 llvm::ArrayRef<glow::TypeRef> inputImageType);
84
85/// Application like classification or segmentation need to have output selected
86/// to which they will apply their postprocessing. In case the model has
87/// multiple outputs, one has to be select using -output-name command line
88/// attribute. This function wraps the selection of output for postprocessing.
89glow::Placeholder *
90getOutputForPostProcessing(const llvm::StringMap<glow::Placeholder *> &);
91
92/// Setup the pool of contexts needed for a benchmark run.
93glow::UniquePtrVec<glow::ExecutionContext>
94setupContextPool(const std::vector<glow::Placeholder *> outputPHV,
95 glow::Placeholder *inputImagePH, glow::Tensor &inputImageData);
96
97/// Run the requested number of benchmark requests \p requestCount prepended by
98/// \p warmUp cycles
99/// through the HostManager from the \p loader using the provided context pool
100/// \p contexts and wait for all runs to complete.
101void runBenchmark(std::string name, glow::Loader &loader,
102 glow::UniquePtrVec<glow::ExecutionContext> contexts,
103 unsigned requestCount, unsigned warmUp,
104 llvm::Timer *restRunsTimer, llvm::Timer *firstRunsTimer,
105 double *bestRunTime);
106#endif // GLOW_TOOLS_LOADER_EXECUTOR_CORE_HELPER_FUNCTIONS_H
107