def _get_trace_input_from_test_input(input, remainder_size=None, extra_config={}): """ Utility function used to properly put the inputs into a format understandable by torch. If `remainder_size` is provided, also return inputs for a remainder model (see below). """ remainder = None if isinstance(input, tuple): trace_input = [] for input_ in input: # Convert string arrays into int32. if input_.dtype.kind in constants.SUPPORTED_STRING_TYPES: assert constants.MAX_STRING_LENGTH in extra_config max_string_length = extra_config[constants.MAX_STRING_LENGTH] input_ = from_strings_to_ints(input_, max_string_length) trace_input.append(torch.from_numpy(input_)) trace_input = tuple(trace_input) if remainder_size is not None and remainder_size != 0: remainder = tuple( [inp[0:remainder_size, :] for inp in trace_input]) else: # Convert string arrays into int32. if input.dtype.kind in constants.SUPPORTED_STRING_TYPES: assert constants.MAX_STRING_LENGTH in extra_config max_string_length = extra_config[constants.MAX_STRING_LENGTH] input = from_strings_to_ints(input, max_string_length) trace_input = torch.from_numpy(input) if remainder_size is not None and remainder_size != 0: remainder = trace_input[0:remainder_size, :] return (trace_input, remainder)
def _torchscript_wrapper(device, function, *inputs, extra_config={}): """ This function contains the code to enable predictions over torchscript models. It is used to translates inputs in the proper torch format. """ inputs = [*inputs] with torch.no_grad(): if type(inputs) == DataFrame and DataFrame is not None: # Split the dataframe into column ndarrays inputs = inputs[0] input_names = list(inputs.columns) splits = [inputs[input_names[idx]] for idx in range(len(input_names))] splits = [df.to_numpy().reshape(-1, 1) for df in splits] inputs = tuple(splits) # Maps data inputs to the expected type and device. for i in range(len(inputs)): if type(inputs[i]) is list: inputs[i] = np.array(inputs[i]) if type(inputs[i]) is np.ndarray: # Convert string arrays into int32. if inputs[i].dtype.kind in constants.SUPPORTED_STRING_TYPES: assert constants.MAX_STRING_LENGTH in extra_config inputs[i] = from_strings_to_ints(inputs[i], extra_config[constants.MAX_STRING_LENGTH]) if inputs[i].dtype == np.float64: # We convert double precision arrays into single precision. Sklearn does the same. inputs[i] = inputs[i].astype("float32") inputs[i] = torch.from_numpy(inputs[i]) elif type(inputs[i]) is not torch.Tensor: raise RuntimeError("Inputer tensor {} of not supported type {}".format(i, type(inputs[i]))) if device.type != "cpu" and device is not None: inputs[i] = inputs[i].to(device) return function(*inputs)
def _get_named_inputs(self, inputs): """ Retrieve the inputs names from the session object. """ if len(inputs) < len(self._input_names): inputs = inputs[0] assert len(inputs) == len(self._input_names) named_inputs = {} for i in range(len(inputs)): input_ = np.array(inputs[i]) if input_.dtype.kind in constants.SUPPORTED_STRING_TYPES: assert constants.MAX_STRING_LENGTH in self._extra_config input_ = from_strings_to_ints( input_, self._extra_config[constants.MAX_STRING_LENGTH]) named_inputs[self._input_names[i]] = input_ return named_inputs
def forward(self, *inputs): with torch.no_grad(): assert len(self._input_names) == len(inputs) or ( type(inputs[0]) == DataFrame and DataFrame is not None and not self.check_dataframe_to_array and len(self._input_names) == len(inputs[0].columns) ), "number of inputs or number of columns in the dataframe do not match with the expected number of inputs {}".format( self._input_names) if type(inputs[0]) == DataFrame and DataFrame is not None: # Split the dataframe into column ndarrays. inputs = inputs[0] input_names = list(inputs.columns) splits = [ inputs[input_names[idx]] for idx in range(len(input_names)) ] splits = [df.to_numpy().reshape(-1, 1) for df in splits] inputs = tuple(splits) inputs = [*inputs] variable_map = {} device = get_device(self) # Maps data inputs to the expected variables. for i, input_name in enumerate(self._input_names): input_ = inputs[i] if type(input_) is list: input_ = np.array(input_) if type(input_) is np.ndarray: # Convert string arrays into int32. if input_.dtype.kind in constants.SUPPORTED_STRING_TYPES: assert self.max_string_length is not None input_ = from_strings_to_ints(input_, self.max_string_length) input_ = torch.from_numpy(input_) elif type(input_) is not torch.Tensor: raise RuntimeError( "Inputer tensor {} of not supported type {}".format( input_name, type(input_))) if input_.dtype == torch.float64: # We convert double precision arrays into single precision. Sklearn does the same. input_ = input_.float() if device is not None and device.type != "cpu": input_ = input_.to(device) variable_map[input_name] = input_ # Evaluate all the operators in the topology by properly wiring inputs \ outputs for operator in self._operators: outputs = operator(*(variable_map[input_name] for input_name in operator.inputs)) if len(operator.outputs) == 1: variable_map[operator.outputs[0]] = outputs else: for i, output_name in enumerate(operator.outputs): variable_map[output_name] = outputs[i] # Prepare and return the output. if len(self._output_names) == 1: return variable_map[self._output_names[0]] else: return tuple(variable_map[output_name] for output_name in self._output_names)