Skip to content

Commit

Permalink
[ONNX Editor] Use port indices instead of tensor names, new naming co…
Browse files Browse the repository at this point in the history
…nvention of inputs (#5668)
  • Loading branch information
Mateusz Bencer authored May 21, 2021
1 parent 9e46be7 commit 3dc1d7f
Show file tree
Hide file tree
Showing 17 changed files with 1,474 additions and 293 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ namespace ngraph
private:
std::vector<int> find_node_indexes(const std::string& node_name,
const std::string& output_name) const;
std::string get_node_input_name(int node_index, int input_index) const;
std::string get_node_output_name(int node_index, int output_index) const;

int get_node_input_idx(int node_index, const std::string& input_name) const;
int get_node_output_idx(int node_index, const std::string& output_name) const;

std::vector<std::vector<std::string>> m_node_inputs;
std::vector<std::vector<std::string>> m_node_outputs;
Expand Down
59 changes: 39 additions & 20 deletions ngraph/frontend/onnx_editor/include/onnx_editor/editor_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,59 @@ namespace ngraph
struct Edge
{
Edge() = delete;
Edge(const int node_idx, std::string tensor_name)
Edge(const int node_idx, const int port_idx, std::string new_input_name = "")
: m_node_idx{node_idx}
, m_tensor_name{std::move(tensor_name)}
, m_port_idx{port_idx}
, m_new_input_name{std::move(new_input_name)}
{
}

const int m_node_idx;
const std::string m_tensor_name;
const int m_port_idx;
const std::string m_new_input_name;
};
namespace onnx_editor
{
/// \brief Defines an edge connected to an input of any node in the graph.
/// It consists of a node index in the processed ONNX model and the input name.
/// The index should point to a node in the topological sort of the underlying graph
/// which means it has to be in range: 0 <= node_idx < graph.node_size()
/// It consists of a node index in the processed ONNX model and the port index.
/// The node index should point to a node in the topological sort of the underlying
/// graph which means it has to be in range: 0 <= node_idx < graph.node_size()
///
/// For a node number 5, with 3 inputs:
///
/// ----(in_A)----> +--------+
/// ----(in_B)----> | node 5 | ----(out)---->
/// ----(in_C)----> +--------+
/// ----(0)----> +--------+
/// ----(1)----> | node 5 | ----(0)---->
/// ----(2)----> +--------+
///
/// there are 3 possible valid instances of this struct:
/// InputEdge(5, "in_A")
/// InputEdge(5, "in_B")
/// InputEdge(5, "in_C")
/// InputEdge(5, 0)
/// InputEdge(5, 1)
/// InputEdge(5, 2)
///
/// If a new_input_name argument is provided, it is used as a new input name
/// in a place where a graph is cut (if creation of a new input is needed).
/// Otherwise, a new input name is set to:
/// - original name of an input tensor, if the tensor is consumed by only a one
/// node
/// - first output name of an input tensor consumer + "/placeholder_port_" +
/// port_index,
/// if the tensor is consumed by more than one node.
using InputEdge = Edge<EdgeType::INPUT>;

/// \brief Defines an edge connected to an output of any node in the graph.
/// It consists of a node index in the processed ONNX model and the output name.
/// It consists of a node index in the processed ONNX model and the port index.
///
/// For a node number 5, with 2 outputs:
///
/// +--------+ ----(out1)---->
/// ----(in_A)----> | node 5 |
/// +--------+ ----(out2)---->
/// +--------+ ----(0)---->
/// ----(0)----> | node 5 |
/// +--------+ ----(1)---->
///
/// there are 2 possible valid instances of this struct:
/// OutputEdge(5, "out1")
/// OutputEdge(5, "out2")
/// OutputEdge(5, 0)
/// OutputEdge(5, 1)
///
/// The optional argument "new_input_name" is ignored for OutputEdge case.
using OutputEdge = Edge<EdgeType::OUTPUT>;

/// \brief Specifies a single node input by the name or index.
Expand All @@ -69,19 +82,25 @@ namespace ngraph
/// ----(in_B)----> | test_node | ----(out)---->
/// ----(in_C)----> +-----------+
/// You can indicate in_B as EditorInput("in_B") or EditorInput(1)
///
/// The optional argument "new_input_name" can be used to set a custom input name
/// which can be created during cutting a graph.
struct EditorInput
{
EditorInput() = delete;
EditorInput(std::string input_name)
EditorInput(std::string input_name, std::string new_input_name = "")
: m_input_name{std::move(input_name)}
, m_new_input_name{std::move(new_input_name)}
{
}
EditorInput(const int input_index)
EditorInput(const int input_index, std::string new_input_name = "")
: m_input_index{input_index}
, m_new_input_name{std::move(new_input_name)}
{
}
const std::string m_input_name = "";
const int m_input_index = -1;
const std::string m_new_input_name = "";
};

/// \brief Specifies a single node output by the name or index.
Expand Down
Loading

0 comments on commit 3dc1d7f

Please sign in to comment.