def __init__(self, name: str, values: Union[np.ndarray, LazyValues], data_location: int = None): """ Represents a Tensor whose value is known. Args: name (str): The name of the tensor. values (numpy.ndarray): The values in this tensor, in the form of a NumPy array. data_location (int): An enum value indicating the location where the tensor data is stored. Generally, this will come from onnx.TensorProto.DataLocation. """ self.name = name self.inputs = misc.SynchronizedList(self, field_name="outputs", initial=[]) self.outputs = misc.SynchronizedList(self, field_name="inputs", initial=[]) if not isinstance(values, np.ndarray) and not isinstance( values, LazyValues): G_LOGGER.critical( "Provided `values` argument is not a NumPy array or a LazyValues instance. " "Please provide a NumPy array or LazyValues instance to construct a Constant. " "Note: Provided `values` parameter was: {:}".format(values)) self._values = values self.data_location = data_location
def _get_node_id(self, node): try: return node.id except AttributeError: G_LOGGER.critical( "Encountered a node not in the graph:\n{:}.\n\nTo fix this, please append the node to this graph's `nodes` attribute." .format(node))
def add_to_tensor_map(tensor): if not tensor.is_empty(): if check_duplicates and tensor.name in tensor_map and not (tensor_map[tensor.name] is tensor): G_LOGGER.critical("Found distinct tensors that share the same name:\n[id: {:}] {:}\n[id: {:}] {:}" .format(id(tensor_map[tensor.name]), tensor_map[tensor.name], id(tensor), tensor)) tensor_map[tensor.name] = tensor
def export_value_info_proto(tensor: Variable, do_type_check: bool) -> onnx.ValueInfoProto: if do_type_check and tensor.dtype is None: G_LOGGER.critical("Graph input and output tensors must include dtype information. Please set the dtype attribute for: {:}".format(tensor)) if tensor.dtype is not None: onnx_tensor = onnx.helper.make_tensor_value_info(tensor.name, dtype_to_onnx(tensor.dtype), tensor.shape) else: onnx_tensor = onnx.helper.make_empty_tensor_value_info(tensor.name) return onnx_tensor
def process_io(io): new_io = [] for elem in io: if isinstance(elem, Tensor): new_io.append(elem) elif isinstance(elem, str): tensor = Variable(name=self._generate_name(elem)) new_io.append(tensor) elif isinstance(elem, np.ndarray): new_io.append(Constant(name=self._generate_name("onnx_graphsurgeon_constant"), values=elem)) else: G_LOGGER.critical("Unrecognized type passed to Graph.layer: {:}.\n\tHint: Did you forget to unpack a list with `*`?\n\tPlease use Tensors, strings, or NumPy arrays.".format(elem)) return new_io
def __init__(self, name: str, values: np.ndarray): """ Represents a Tensor whose value is known. Args: name (str): The name of the tensor. values (numpy.ndarray): The values in this tensor, in the form of a NumPy array. dtype (numpy.dtype): The data type of the tensor. shape (Sequence[Union[int, str]]): The shape of the tensor. """ self.name = name self.inputs = misc.SynchronizedList(self, field_name="outputs", initial=[]) self.outputs = misc.SynchronizedList(self, field_name="inputs", initial=[]) if not isinstance(values, np.ndarray): G_LOGGER.critical("Provided `values` argument is not a NumPy array (please provide a NumPy array to construct a Constant): {:}".format(values)) self.values = np.array(values)
def get_hierarchy_level(node): # Return all nodes that contribute to this node. def get_input_nodes(node): inputs = {} for tensor in node.inputs: for node in tensor.inputs: inputs[self._get_node_id(node)] = node return inputs.values() if self._get_node_id(node) in hierarchy_levels: return hierarchy_levels[self._get_node_id(node)].level # The level of a node is the level of it's highest input + 1. try: max_input_level = max([get_hierarchy_level(input_node) for input_node in get_input_nodes(node)] + [-1]) except RecursionError: G_LOGGER.critical("Cycle detected in graph! Are there tensors with duplicate names in the graph?") return max_input_level + 1