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#ifndef GLOW_BACKENDS_INTERPRETER_INTERPRETER_H
17#define GLOW_BACKENDS_INTERPRETER_INTERPRETER_H
18
19#include "glow/Backends/Interpreter/InterpreterDeviceManager.h"
20#include "glow/Backends/Interpreter/InterpreterFunction.h"
21
22#include "glow/Backend/Backend.h"
23
24#include <numeric>
25#include <vector>
26
27namespace glow {
28
29/// This is the IR-interpreter. It owns the IR, and the heap, and is able to
30/// execute the instructions one at a time.
31class Interpreter final : public BackendUsingGlowIR,
32 public IRInstructionProcessingHandler {
33public:
34 /// Ctor.
35 Interpreter() = default;
36
37 /// @name Backend methods.
38 /// This is the implementation of the Backend interface.
39 ///@{
40 ~Interpreter() override = default;
41
42 std::string getBackendName() const override {
43 return Named::getName().empty() ? getName() : Named::getName().str();
44 }
45 static std::string getName() { return "Interpreter"; }
46 static unsigned numDevices() { return std::thread::hardware_concurrency(); }
47 static std::vector<unsigned> scanDeviceIDs() {
48 std::vector<unsigned> deviceIDs(Interpreter::numDevices());
49 std::iota(std::begin(deviceIDs), std::end(deviceIDs), 0);
50 return deviceIDs;
51 }
52
53 std::unique_ptr<CompiledFunction>
54 compileIR(std::unique_ptr<IRFunction> IR) const override;
55
56 std::unique_ptr<CompiledFunction>
57 compileIRWithoutConstants(std::unique_ptr<IRFunction> IR) const;
58
59 Expected<std::unique_ptr<CompiledFunction>>
60 compile(Function *F, const BackendOptions &opts) const override;
61
62 bool isOpSupported(const NodeInfo &NI) const override;
63
64 bool verify(const Function &F, bool verbose = true) const override;
65 bool verify(const IRFunction &IR) const override;
66
67 bool shouldLower(const Node *N) const override;
68
69 Expected<bool> transformPostLowering(
70 Function *F, CompilationContext &cctx,
71 const glow::runtime::DeviceInfo *devInfo = nullptr) const override;
72
73 /// \returns a unitless value to be used when comparing Nodes or
74 /// error if no estimate can be generated.
75 Expected<double> estimateNodeCost(const glow::Node *node) const override;
76
77 /// @}
78 //
79 /// \returns the size of metrics collected for a single TraceEvent.
80 static size_t getTraceEventDataSizeStatic() { return sizeof(uint64_t); }
81 size_t getTraceEventDataSize() const override {
82 return Interpreter::getTraceEventDataSizeStatic();
83 }
84
85 runtime::DeviceManager *
86 createDeviceManager(const runtime::DeviceConfig &deviceConfig) override {
87 return createInterpreterDeviceManager(deviceConfig);
88 }
89
90 void parseBackendSpecificOptions(const BackendOptions &opts) const;
91};
92
93} // namespace glow
94
95#endif // GLOW_BACKENDS_INTERPRETER_INTERPRETER_H
96