def make_node(op_type: str, inputs: Sequence[str], outputs: Sequence[str], **kwargs) -> INodeProto: node = NodeProto() node.op_type = op_type node.input.extend(inputs) node.output.extend(outputs) node.attribute.extend(make_attribute(key, value) for key, value in kwargs.items()) return node
def make_node(op_type, inputs, outputs, name=None, doc_string=None, **kwargs): """Construct a NodeProto. Arguments: op_type (string): The name of the operator to construct inputs (list of string): list of input names outputs (list of string): list of output names name (string, default None): optional unique identifier for NodeProto doc_string (string, default None): optional documentation string for NodeProto **kwargs (dict): the attributes of the node. The acceptable values are documented in :func:`make_attribute`. """ node = NodeProto() node.op_type = op_type node.input.extend(inputs) node.output.extend(outputs) if name: node.name = name if doc_string: node.doc_string = doc_string if kwargs: node.attribute.extend( make_attribute(key, value) for key, value in kwargs.items()) return node
def _create_reshape(cls, op, op_t): """ get a onnx node from singa Concat operator Args: op: a given operator Args: op_t: the tensor of the operator Returns: the onnx node """ # make the shape node # because the reshape in singa does not provide its shape as input tensor shape_node_name = op.name + "#shape" shape_node = NodeProto() shape_node.name = shape_node_name shape_node.op_type = cls._rename_operators.get("Dummy", "Dummy") shape_node.output.extend([shape_node_name]) shape_node.attribute.extend([ helper.make_attribute( 'value', helper.make_tensor( name=shape_node_name, data_type=TensorProto.FLOAT, dims=[len(op_t.shape)], vals=op_t.shape, )) ]) # make the reshape node node = cls._common_singa_tensor_to_onnx_node(op, op_t) node.input.extend([shape_node_name]) return [shape_node, node]
def make_onnx(self): ''' Generate ONNX model from current graph ''' self.clean_init() nodes = [] for node in self.node: n = NodeProto() n.input.extend(node.input) n.output.extend(node.output) n.name = node.name n.op_type = node.op_type n.attribute.extend( helper.make_attribute(key, value) for key, value in sorted(node.attrs.items())) nodes.append(n) inputs = [] initializer = [] for k, v in self.tensor_dict.items(): init = numpy_helper.from_array(v, name=k) initializer.append(init) value_info = helper.make_tensor_value_info( name=k, elem_type=mapping.NP_TYPE_TO_TENSOR_TYPE[v.dtype], shape=list(v.shape)) inputs.append(value_info) graph_ = helper.make_graph(nodes=nodes, name='dlpy_graph', inputs=inputs + self.uninitialized, outputs=self.output, initializer=initializer) model = helper.make_model(graph_) return model
def make_node( op_type, # type: Text inputs, # type: Sequence[Text] outputs, # type: Sequence[Text] name=None, # type: Optional[Text] doc_string=None, # type: Optional[Text] **kwargs # type: Any ): # type: (...) -> NodeProto """Construct a NodeProto. Arguments: op_type (string): The name of the operator to construct inputs (list of string): list of input names outputs (list of string): list of output names name (string, default None): optional unique identifier for NodeProto doc_string (string, default None): optional documentation string for NodeProto **kwargs (dict): the attributes of the node. The acceptable values are documented in :func:`make_attribute`. """ node = NodeProto() node.op_type = op_type node.input.extend(inputs) node.output.extend(outputs) if name: node.name = name if doc_string: node.doc_string = doc_string if kwargs: node.attribute.extend( make_attribute(key, value) for key, value in sorted(kwargs.items())) return node
def make_node(op_type, inputs, outputs, name=None, doc_string=None, **kwargs): node = NodeProto() node.op_type = op_type node.input.extend(inputs) node.output.extend(outputs) if name: node.name = name if doc_string: node.doc_string = doc_string if kwargs: node.attribute.extend( make_attribute(key, value) for key, value in kwargs.items()) return node
def _onnx_rewrite_operator_node(existing_names, node, sub_onx): """ Replaces a node by a subgraph. :param existing_names: existing results names :param node: onnx node to replace :param sub_onx: onnx sub_graph to use as a replacement :return: new_initializer, new_nodes """ if len(node.input) != len(sub_onx.graph.input): raise ValueError( # pragma: no cover "Mismatch with the number of inputs for operator type %r. " "%d != %d." % (node.op_type, len(node.input), len(sub_onx.graph.nput))) if len(node.output) != len(sub_onx.graph.output): raise ValueError( # pragma: no cover "Mismatch with the number of outputs for operator type %r. " "%d != %d." % (node.op_type, len(node.output), len(sub_onx.graph.output))) replaces = {} for inp, name in zip(sub_onx.graph.input, node.input): replaces[inp.name] = name for inp, name in zip(sub_onx.graph.output, node.output): replaces[inp.name] = name new_inits = [] for init in sub_onx.graph.initializer: name = _unique_name(existing_names, init.name) replaces[init.name] = name tensor = from_array(to_array(init), name=name) new_inits.append(tensor) new_nodes = [] for n in sub_onx.graph.node: new_node = NodeProto() new_node.op_type = n.op_type new_node.attribute.extend(n.attribute) # pylint: disable=E1101 new_node.input.extend( # pylint: disable=E1101 [replaces[i] for i in n.input]) # pylint: disable=E1101 new_node.domain = n.domain new_out = [] for o in n.output: if o in replaces: new_out.append(replaces[o]) else: n = _unique_name(existing_names, o) new_out.append(n) new_node.output.extend(new_out) # pylint: disable=E1101 new_nodes.append(new_node) return new_inits, new_nodes
def _common_caffe2_op_to_onnx_node(cls, op_def, shapes): node_def = NodeProto() node_def.name = op_def.name node_def.op_type = cls._renamed_operators.get(op_def.type, op_def.type) node_def.input.extend(op_def.input) node_def.output.extend(op_def.output) attrs = filter(None, [cls.caffe2_arg_to_onnx_attr(op_def, arg) for arg in op_def.arg]) node_def.attribute.extend(attrs) return node_def
def create_node_from_schema(schema): """ Convert an OpSchema to a NodeProto :param schema: the input OpSchema :return: the equivalent NodeProto """ node_proto = NodeProto() for attribute in schema.attributes: attr_value = schema.attributes[attribute] if attr_value.default_value.name == '': attr_value_new = onnx.helper.make_attribute(attr_value.name, '') attr_value_new.type = convert_attr_type_to_enum(attr_value) node_proto.attribute.append(attr_value_new) else: node_proto.attribute.append(attr_value.default_value) node_proto.op_type = schema.name node_proto.doc_string = schema.doc node_proto.name = schema.name for input_arr in schema.inputs: input_types = input_arr.types type_attr = onnx.helper.make_attribute(input_arr.name + '-types', [ str(data_type).replace('tensor(', '').replace(')', '') for data_type in input_types ]) node_proto.attribute.append(type_attr) if node_proto.input is None: node_proto.input = [] node_proto.input.append(input_arr.name) for output_arr in schema.outputs: if node_proto.output is None: node_proto.output = [] output_types = output_arr.types type_attr = onnx.helper.make_attribute( output_arr.name + '-types', [ str(data_type).replace('tensor(', '').replace(')', '') for data_type in output_types ]) node_proto.attribute.append(type_attr) node_proto.output.append(output_arr.name) return node_proto
def make_node( op_type, # type: Text inputs, # type: Sequence[Text] outputs, # type: Sequence[Text] name=None, # type: Optional[Text] doc_string=None, # type: Optional[Text] domain=None, # type: Optional[Text] _dtype=None, # type: [np.float32, np.float64] **kwargs # type: Any ): # type: (...) -> NodeProto """Construct a NodeProto. Arguments: op_type (string): The name of the operator to construct inputs (list of string): list of input names outputs (list of string): list of output names name (string, default None): optional unique identifier for NodeProto doc_string (string, default None): optional documentation string for NodeProto dtype: dtype for double used to infer domain (string, default None): optional domain for NodeProto. If it's None, we will just use default domain (which is empty) **kwargs (dict): the attributes of the node. The acceptable values are documented in :func:`make_attribute`. """ if _dtype is None: raise ValueError("dtype cannot be None") node = NodeProto() node.op_type = op_type node.input.extend(inputs) node.output.extend(outputs) if name: node.name = name if doc_string: node.doc_string = doc_string if domain is not None: node.domain = domain if kwargs: node.attribute.extend( make_attribute(key, value, dtype=_dtype, domain=domain) for key, value in sorted(kwargs.items())) return node
def _common_singa_tensor_to_onnx_node(cls, op, op_t): """ get a onnx node from a singa operator, prepare its type, inputs and outputs Args: op: a given operator Args: op: the tensor of the operator Returns: the onnx node """ node_def = NodeProto() node_def.name = op.name optype = cls._get_singa_op_type(op) node_def.op_type = cls._rename_operators.get(optype, optype) inputs, outputs = cls._get_singa_op_inputs_outputs(op) node_def.input.extend(inputs) node_def.output.extend(outputs) return node_def
def make_node( op_type: Text, inputs: Sequence[Text], outputs: Sequence[Text], name: Optional[Text] = None, doc_string: Optional[Text] = None, domain: Optional[Text] = None, **kwargs: Any ) -> NodeProto: """Construct a NodeProto. Arguments: op_type (string): The name of the operator to construct inputs (list of string): list of input names outputs (list of string): list of output names name (string, default None): optional unique identifier for NodeProto doc_string (string, default None): optional documentation string for NodeProto domain (string, default None): optional domain for NodeProto. If it's None, we will just use default domain (which is empty) **kwargs (dict): the attributes of the node. The acceptable values are documented in :func:`make_attribute`. Returns: NodeProto """ node = NodeProto() node.op_type = op_type node.input.extend(inputs) node.output.extend(outputs) if name: node.name = name if doc_string: node.doc_string = doc_string if domain is not None: node.domain = domain if kwargs: node.attribute.extend( make_attribute(key, value) for key, value in sorted(kwargs.items()) if value is not None) return node
def _make_node(op_type, inputs, outputs, name=None, doc_string=None, domain=None, attributes=None): """ Constructs a NodeProto. :param op_type: (string): The name of the operator to construct :param inputs: list of input names :param outputs: list of output names :param name: optional unique identifier for NodeProto :param doc_string: optional documentation string for NodeProto :param domain: optional domain for NodeProto. If it's None, we will just use default domain (which is empty) :param attributes: the attributes of the node. The acceptable values are documented in :func:`make_attribute`. :return: node """ node = NodeProto() node.op_type = op_type node.input.extend(inputs) node.output.extend(outputs) if name: node.name = name if doc_string: node.doc_string = doc_string if domain is not None: node.domain = domain if isinstance(attributes, dict): if len(attributes) > 0: node.attribute.extend( make_attribute(key, value) for key, value in sorted(attributes.items())) elif attributes: for att in attributes: node.attribute.extend([att]) return node