2222
2323#include " llvm/Support/Casting.h"
2424
25+ #include " llvm/Support/raw_ostream.h"
2526using namespace glow ;
2627
2728InterpreterFunction::InterpreterFunction (std::unique_ptr<IRFunction> F,
2829 const runtime::RuntimeBundle &bundle)
2930 : CompiledFunction(bundle), F_(std::move(F)) {}
3031
3132InterpreterFunction::~InterpreterFunction () {
32- // Delete the tensors that are owned by this backend.
33- for (const auto &p : tensors_) {
33+ for (const auto &p : constants_) {
3434 delete p.second ;
3535 }
36- tensors_ .clear ();
37- externalTensors_. clear ();
36+ constants_ .clear ();
37+
3838 alignedFree (runtimeBundle_.getConstants ());
3939 tearDownRuns ();
4040}
4141
4242void InterpreterFunction::collectConstants (IRFunction *F) {
4343 runtimeBundle_.collectConstants (F);
44- }
45-
46- void InterpreterFunction::setupRuns () {
47- if (!runsSetup_) {
44+ if (constants_.empty ()) {
4845 if (runtimeBundle_.getConstantWeightSize ()) {
4946 for (const auto &v : F_->getGraph ()->getParent ()->getConstants ()) {
5047 auto symbolInfo = runtimeBundle_.getSymbolInfo (v);
@@ -53,36 +50,27 @@ void InterpreterFunction::setupRuns() {
5350 constants_.emplace (std::string (v->getName ()), tensor);
5451 }
5552 }
56- runsSetup_ = true ;
57- }
58- }
59-
60- void InterpreterFunction::beforeRun (const Context &ctx) {
61- // Register the concrete tensors that back the placeholder tensors.
62- for (auto &ph : ctx.pairs ()) {
63- auto *w = F_->getWeightForNode (ph.first );
64- assert (!externalTensors_.count (w) && " The tensor is already registered" );
65- externalTensors_[w] = ph.second ;
6653 }
6754}
6855
69- void InterpreterFunction::afterRun (const Context &ctx) {
70- // Remove the concrete tensors that back the placeholder tensors.
71- for (auto &ph : ctx.pairs ()) {
72- auto *w = F_->getWeightForNode (ph.first );
73- externalTensors_.erase (w);
56+ void InterpreterFunction::execute (Context *ctx) {
57+ if (constants_.empty ()) {
58+ collectConstants (F_.get ());
7459 }
60+ BoundInterpreterFunction boundFunc (constants_);
61+ boundFunc.execute (F_.get (), ctx);
7562}
7663
77- void InterpreterFunction::tearDownRuns () {
78- for (const auto &p : constants_) {
64+ BoundInterpreterFunction::~BoundInterpreterFunction () {
65+ // Delete the tensors that are owned by this backend.
66+ for (const auto &p : tensors_) {
7967 delete p.second ;
8068 }
81- constants_ .clear ();
82- runsSetup_ = false ;
69+ tensors_ .clear ();
70+ externalTensors_. clear () ;
8371}
8472
85- Tensor *InterpreterFunction ::getTensor (const Value *v) const {
73+ Tensor *BoundInterpreterFunction ::getTensor (const Value *v) const {
8674 auto it = tensors_.find (v);
8775 if (it != tensors_.end ()) {
8876 return it->second ;
@@ -97,7 +85,7 @@ Tensor *InterpreterFunction::getTensor(const Value *v) const {
9785 return ie->second ;
9886}
9987
100- Tensor *InterpreterFunction ::getOrCreateTensor (const Value *v) {
88+ Tensor *BoundInterpreterFunction ::getOrCreateTensor (const Value *v) {
10189 auto ie = externalTensors_.find (v);
10290 if (ie != externalTensors_.end ()) {
10391 return ie->second ;
@@ -117,9 +105,8 @@ Tensor *InterpreterFunction::getOrCreateTensor(const Value *v) {
117105 return it->second ;
118106}
119107
120- Tensor *
121- InterpreterFunction::getOrCreateUnownedTensor (const Value *v, const Value *src,
122- llvm::ArrayRef<size_t > offsets) {
108+ Tensor *BoundInterpreterFunction::getOrCreateUnownedTensor (
109+ const Value *v, const Value *src, llvm::ArrayRef<size_t > offsets) {
123110 assert (llvm::isa<TensorViewInst>(v) && " Expected a tensor view" );
124111
125112 // Pick the tensor.
@@ -136,7 +123,7 @@ InterpreterFunction::getOrCreateUnownedTensor(const Value *v, const Value *src,
136123 return T;
137124}
138125
139- void InterpreterFunction ::deleteTensor (const Value *v) {
126+ void BoundInterpreterFunction ::deleteTensor (const Value *v) {
140127 auto it = tensors_.find (v);
141128 if (it == tensors_.end ()) {
142129 return ;
@@ -146,7 +133,14 @@ void InterpreterFunction::deleteTensor(const Value *v) {
146133 tensors_.erase (it);
147134}
148135
149- void InterpreterFunction::execute () {
136+ void BoundInterpreterFunction::execute (IRFunction *F, Context *ctx) {
137+ // Register the concrete tensors that back the placeholder tensors.
138+ for (auto &ph : ctx->pairs ()) {
139+ auto *w = F->getWeightForNode (ph.first );
140+ assert (!externalTensors_.count (w) && " The tensor is already registered" );
141+ externalTensors_[w] = ph.second ;
142+ }
143+
150144// Do the forward pass.
151145#define DEF_VALUE (CLASS, NAME )
152146#define DEF_INSTR (CLASS, NAME ) \
@@ -156,12 +150,18 @@ void InterpreterFunction::execute() {
156150 }
157151#define DEF_BACKEND_SPECIFIC_INSTR (CLASS, NAME )
158152 // Dispatch the interpreter on each instruction in the program:
159- for (const auto &I : F_ ->getInstrs ()) {
153+ for (const auto &I : F ->getInstrs ()) {
160154 switch (I.getKind ()) {
161155#include " glow/AutoGenInstr.def"
162156
163157 default :
164158 llvm_unreachable (" Invalid instruction." );
165159 }
166160 }
161+
162+ // Remove the concrete tensors that back the placeholder tensors.
163+ for (auto &ph : ctx->pairs ()) {
164+ auto *w = F->getWeightForNode (ph.first );
165+ externalTensors_.erase (w);
166+ }
167167}
0 commit comments