dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
Driver.h
1
10#pragma once
11
12#include <functional>
13#include <istream>
14#include <list>
15#include <map>
16#include <optional>
17#include <string>
18#include <unordered_map>
19
20#include "dlinear/libs/libgmp.h"
21#include "dlinear/libs/libonnx.h"
22#include "dlinear/parser/Driver.h"
23#include "dlinear/parser/onnx/NodeOpType.h"
24#include "dlinear/parser/onnx/Tensor.h"
25#include "dlinear/symbolic/literal.h"
26#include "dlinear/symbolic/symbolic.h"
27#include "dlinear/util/concepts.h"
28
29namespace dlinear::onnx {
30
37class OnnxDriver : public Driver {
38 public:
43 explicit OnnxDriver(Context& context); // NOLINT(runtime/references): Reference context filled during parsing.
44
45 bool ParseStreamCore(std::istream& in) override;
46 bool ParseFile(const std::string& filename) override;
47
49 [[nodiscard]] const std::unordered_map<std::string, Tensor>& variables() const { return variables_; }
51 [[nodiscard]] const std::unordered_map<std::string, Tensor>& available_inputs() const { return available_inputs_; }
53 [[nodiscard]] const ::onnx::ModelProto& model() const { return model_; }
55 [[nodiscard]] const ::onnx::GraphProto& graph() const { return model_.graph(); }
56
57 private:
58 static const std::map<std::string, std::function<void(OnnxDriver&, const ::onnx::NodeProto&)>>
60
82 template <IsAnyOf<bool, float, std::int64_t, std::string, std::vector<float>, std::vector<std::int64_t>,
83 std::vector<std::string>, const ::onnx::TensorProto*>
84 T>
85 T GetAttribute(const ::onnx::NodeProto& node, const std::string& name,
86 const std::optional<T>& default_value = {}) const;
87
88 static void EnsureInput(const ::onnx::NodeProto& node, int lb, int ub);
89 static void EnsureInput(const ::onnx::NodeProto& node, int exact);
90
92 void ParseGraph();
101 void AddNodes();
111 bool AddNode(const ::onnx::NodeProto& node);
117 void AddValueInfo(const ::onnx::ValueInfoProto& value_info, bool is_input = false);
123 void AddValueInfoTensor(const ::onnx::ValueInfoProto& value_info, bool is_input = false);
128 void AddInitializer(const ::onnx::TensorProto& tensor);
133 void AddFormula(const std::string& output);
134
140 template <NodeOpType T>
141 void AddNode(const ::onnx::NodeProto& node);
142
151 const Variable& ToEqualVar(const Expression& expression);
152
153 ::onnx::ModelProto model_{};
154 std::unordered_map<std::string, Tensor> variables_;
155 std::unordered_map<std::string, Tensor> available_inputs_;
156 std::unordered_map<Expression, Variable> equal_vars_;
157};
158} // namespace dlinear::onnx
Context class that holds the set of constraints and provide Assert/Push/Pop/CheckSat functionalities.
Definition Context.h:31
The Driver is the base class for all the parsers.
Definition Driver.h:26
const Context & context() const
Get read-only access to the enabled of the Driver.
Definition Driver.h:143
Represents a symbolic form of an expression.
Represents a symbolic variable.
The OnnxDriver class reads the onnx file.
Definition Driver.h:37
const ::onnx::GraphProto & graph() const
Get read-only access to the graph of the Driver.
Definition Driver.h:55
void AddNodes()
Go through all the nodes in the graph and construct the final assertions.
Definition Driver.cpp:744
static const std::map< std::string, std::function< void(OnnxDriver &, const ::onnx::NodeProto &)> > node_handlers
Map between node op_type and the corresponding handler..
Definition Driver.h:59
bool ParseFile(const std::string &filename) override
Invoke the scanner and parser on a file.
Definition Driver.cpp:125
T GetAttribute(const ::onnx::NodeProto &node, const std::string &name, const std::optional< T > &default_value={}) const
Get the attribute name from the node.
Definition Driver.cpp:153
const Variable & ToEqualVar(const Expression &expression)
Associate to a linear expression a fresh variable.
Definition Driver.cpp:794
const std::unordered_map< std::string, Tensor > & variables() const
Get read-only access to the variables of the Driver.
Definition Driver.h:49
bool AddNode(const ::onnx::NodeProto &node)
Go through a specific node and add the corresponding assertions.
Definition Driver.cpp:718
std::unordered_map< Expression, Variable > equal_vars_
Variables created to summarize linear constraints.
Definition Driver.h:156
const std::unordered_map< std::string, Tensor > & available_inputs() const
Get read-only access to the available_inputs of the Driver.
Definition Driver.h:51
void AddNode(const ::onnx::NodeProto &node)
Add a node to the context.
void AddValueInfo(const ::onnx::ValueInfoProto &value_info, bool is_input=false)
Add the input and output to the Context.
Definition Driver.cpp:767
bool ParseStreamCore(std::istream &in) override
Parse the stream.
Definition Driver.cpp:115
void AddFormula(const std::string &output)
Add the formulas to the Context.
Definition Driver.cpp:174
std::unordered_map< std::string, Tensor > available_inputs_
Available inputs in the model.
Definition Driver.h:155
::onnx::ModelProto model_
The onnx model obtained from the file.
Definition Driver.h:153
OnnxDriver(Context &context)
Construct a new parser driver context.
Definition Driver.cpp:113
const ::onnx::ModelProto & model() const
Get read-only access to the model of the Driver.
Definition Driver.h:53
void AddInitializer(const ::onnx::TensorProto &tensor)
Add an initializer to the available_inputs_.
Definition Driver.cpp:167
std::unordered_map< std::string, Tensor > variables_
Variables in the model.
Definition Driver.h:154
void ParseGraph()
Parse the model_ 's graph.
Definition Driver.cpp:134
void AddValueInfoTensor(const ::onnx::ValueInfoProto &value_info, bool is_input=false)
Add the input and output to the Context.
Definition Driver.cpp:783
Namespace for the ONNX parser of the dlinear library.
Definition Driver.cpp:20