def __call__(self, *input_values: NumericData) -> List[NumericData]: """Run computation on input values and return result.""" # Input validation if len(input_values) < len(self.parameters): raise UserInputError( "Expected %s params, received not enough %s values.", len(self.parameters), len(input_values)) param_names = [param.friendly_name for param in self.parameters] input_shapes = [get_shape(input_value) for input_value in input_values] if self.network_cache.get(str(input_shapes)) is None: function = self.function if self.function.is_dynamic(): function.reshape( dict( zip(param_names, [PartialShape(i) for i in input_shapes]))) self.network_cache[str(input_shapes)] = function else: function = self.network_cache[str(input_shapes)] executable_network = self.runtime.backend.compile_model( function, self.runtime.backend_name) for parameter, input in zip(self.parameters, input_values): parameter_shape = parameter.get_output_partial_shape(0) input_shape = PartialShape([]) if isinstance( input, (int, float)) else PartialShape(input.shape) if not parameter_shape.compatible(input_shape): raise UserInputError( "Provided tensor's shape: %s does not match the expected: %s.", input_shape, parameter_shape, ) is_bfloat16 = any( parameter.get_output_element_type(0) == Type.bf16 for parameter in self.parameters) if is_bfloat16: input_values = self.convert_to_tensors(input_values) request = executable_network.create_infer_request() result_buffers = request.infer(dict(zip(param_names, input_values))) # # Note: other methods to get result_buffers from request # # First call infer with no return value: # request.infer(dict(zip(param_names, input_values))) # # Now use any of following options: # result_buffers = [request.get_tensor(n).data for n in request.outputs] # result_buffers = [request.get_output_tensor(i).data for i in range(len(request.outputs))] # result_buffers = [t.data for t in request.output_tensors] # # Since OV overwrite result data type we have to convert results to the original one. original_dtypes = [ get_dtype(result.get_output_element_type(0)) for result in self.results ] converted_buffers = self.convert_buffers(result_buffers, original_dtypes) return converted_buffers
def _check_value(op_name, attr_key, value, val_type, cond=None): # type: (str, str, Any, Type, Optional[Callable[[Any], bool]]) -> bool """Check whether provided value satisfies specified criteria. :param op_name: The operator name which attributes are checked. :param attr_key: The attribute name. :param value: The value to check. :param val_type: Required value type. :param cond: The optional function running additional checks. :raises UserInputError: returns True if attribute satisfies all criterias. Otherwise False. """ if not np.issubdtype(type(value), val_type): raise UserInputError( '{} operator attribute "{}" value must by of type {}.'.format( op_name, attr_key, val_type ) ) if cond is not None and not cond(value): raise UserInputError( '{} operator attribute "{}" value does not satisfy provided condition.'.format( op_name, attr_key ) ) return True
def assert_list_of_ints(value_list: Iterable[int], message: str) -> None: """Verify that the provided value is an iterable of integers.""" try: for value in value_list: if not isinstance(value, int): raise TypeError except TypeError: log.warning(message) raise UserInputError(message, value_list)
def check_valid_attribute(op_name, attr_dict, attr_key, val_type, cond=None, required=False): # type: (str, dict, str, Type, Optional[Callable[[Any], bool]], Optional[bool]) -> bool """Check whether specified attribute satisfies given criteria. @param op_name: The operator name which attributes are checked. @param attr_dict: Dictionary containing key-value attributes to check. @param attr_key: Key value for validated attribute. @param val_type: Value type for validated attribute. @param cond: Any callable wich accept attribute value and returns True or False. @param required: Whether provided attribute key is not required. This mean it may be missing from provided dictionary. :raises UserInputError: @return True if attribute satisfies all criterias. Otherwise False. """ result = True if required and attr_key not in attr_dict: raise UserInputError( 'Provided dictionary is missing {} operator required attribute "{}"' .format(op_name, attr_key)) if attr_key not in attr_dict: return result attr_value = attr_dict[attr_key] if np.isscalar(attr_value): result = result and _check_value(op_name, attr_key, attr_value, val_type, cond) else: for v in attr_value: result = result and _check_value(op_name, attr_key, v, val_type, cond) return result
def i420_to_rgb( arg: NodeInput, arg_u: Optional[NodeInput] = None, arg_v: Optional[NodeInput] = None, name: Optional[str] = None, ) -> Node: """Return a node which performs I420toRGB operation. @param arg: The node providing single or Y plane data. @param arg_u: The node providing U plane data. Required for separate planes. @param arg_v: The node providing V plane data. Required for separate planes. @param name: The optional name for the created output node. @return The new node performing I420toRGB operation. """ if arg_u is None and arg_v is None: inputs = as_nodes(arg) elif arg_u is not None and arg_v is not None: inputs = as_nodes(arg, arg_u, arg_v) else: raise UserInputError( "Operation I420toRGB must have one (single plane) or three (separate planes) inputs provided." ) return _get_node_factory_opset8().create("I420toRGB", inputs)
def create( self, op_type_name: str, arguments: Optional[List[Union[Node, Output]]] = None, attributes: Optional[Dict[str, Any]] = None, ) -> Node: """Create node object from provided description. The user does not have to provide all node's attributes, but only required ones. @param op_type_name: The operator type name. @param arguments: The operator arguments. @param attributes: The operator attributes. @return Node object representing requested operator with attributes set. """ if arguments is None and attributes is None: node = self.factory.create(op_type_name) node._attr_cache = {} node._attr_cache_valid = False return node if arguments is None and attributes is not None: raise UserInputError( 'Error: cannot create "{}" op without arguments.'.format( op_type_name)) if attributes is None: attributes = {} assert arguments is not None arguments = self._arguments_as_outputs(arguments) node = self.factory.create(op_type_name, arguments, attributes) # Currently we don't support any attribute getters & setters for TensorIterator node. if node.get_type_name() == "TensorIterator": return node # Set getters and setters for each node's attribute. # node.get_attribute_name() # node.set_attribute_name() # For compound (with more than one level of nesting) attributes of form ie.: # node.class_member_name.some_metric.attr_name: # node.get_some_metric_attr_name() # node.set_some_metric_attr_name() # Please see test_dyn_attributes.py for more usage examples. all_attributes = node.get_attributes() for attr_name in all_attributes.keys(): setattr( node, self._normalize_attr_name_getter(attr_name), partial(NodeFactory._get_node_attr_value, node, attr_name), ) setattr( node, self._normalize_attr_name_setter(attr_name), partial(NodeFactory._set_node_attr_value, node, attr_name), ) # Setup helper members for caching attribute values. # The cache would be lazily populated at first access attempt. node._attr_cache = {} node._attr_cache_valid = False return node