-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathONNXModelRunner.cpp
57 lines (50 loc) · 1.7 KB
/
ONNXModelRunner.cpp
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
//===- ONNXModelRunner.cpp - ONNX Runner ------------------------*- C++ -*-===//
//
// Part of the MLCompilerBridge Project, under the Apache License v2.0 with LLVM
// Exceptions. See the LICENSE file for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file defines the ONNXModelRunner class to support ML model inference
/// via ONNX.
///
//===----------------------------------------------------------------------===//
#include "MLModelRunner/ONNXModelRunner/ONNXModelRunner.h"
#include "SerDes/baseSerDes.h"
using namespace llvm;
namespace MLBridge {
ONNXModelRunner::ONNXModelRunner(Environment *env,
std::map<std::string, Agent *> agents,
LLVMContext *Ctx)
: MLModelRunner(Kind::ONNX, Ctx), env(env), agents(agents) {}
void ONNXModelRunner::addAgent(Agent *agent, std::string name) {
if (agents.find(name) == agents.end()) {
agents[name] = agent;
} else {
// throw error
std::cerr << "ERROR: Agent with the name " << name
<< " already exists. Please give a different name!\n";
exit(1);
}
}
void ONNXModelRunner::computeAction(Observation &obs) {
while (true) {
Action action;
// current agent
auto current_agent = this->agents[this->env->getNextAgent()];
action = current_agent->computeAction(obs);
this->env->step(action);
if (this->env->checkDone()) {
std::cout << "Done🎉\n";
break;
}
}
}
void *ONNXModelRunner::evaluateUntyped() {
Observation &obs = env->reset();
computeAction(obs);
return new int(0);
}
} // namespace MLBridge