in python/singa/sonnx.py [0:0]
def run(self, x, **kwargs):
"""
run the forward of singa model
Args:
x (np.ndarray[]): a list of numpy ndarray as inputs
Returns:
a list of outputs
"""
if not self.has_initialized:
self.initialize()
if isinstance(x[0], tensor.Tensor):
self.dev = x[0].device
outputs_dict = OrderedDict([])
# last_layers means we run this model until the last #N layers
last_layers = kwargs.get('last_layers', len(self._layers) - 1)
last_layers = last_layers if last_layers >= 0 else (
last_layers + 1) % len(self._layers)
if last_layers != len(self._layers) - 1:
for outp in self._layers[last_layers].outputs:
outputs_dict[outp] = None
else:
for outp in self.outputs:
outputs_dict[outp.name] = None
aux_output = kwargs.get('aux_output', ())
for outp in aux_output:
outputs_dict[outp] = None
tensor_dict = self.to_input_tensor(x)
self.init_tensor_count()
# run the layer by the topo order
for node in self._layers[:last_layers + 1]:
op = self.__dict__[node.name]
self.handle_special_ops(node, op, tensor_dict)
# make input
inputs = []
for inp in node.inputs:
if inp not in node.weight_inputs and inp not in node.attr_inputs:
if inp in tensor_dict:
inputs.append(tensor_dict[inp])
elif inp in self.states:
# todo, scalar
val = np.atleast_1d(self.states[inp])
val = tensor.from_numpy(val)
val.to_device(self.dev)
inputs.append(val)
else:
raise KeyError(
"Not found the input {} for operation {}".format(
inp, node.name))
states = {}
if callable(getattr(op, "initialize",
None)) and not op._initialized:
# init the operator
op.initialize(*inputs)
op._initialized = True
for key, name in node.weight_inputs.items():
if key not in node.attr_inputs:
# find the weights and not in the inputs
states[name] = self.states[key]
# replace attrs by inputs
for key, name in node.attr_inputs.items():
if key in tensor_dict:
ts = tensor_dict[key]
elif key in self.states:
ts = self.states[key]
if isinstance(ts, tensor.Tensor):
ts = tensor.to_numpy(ts)
states[name] = ts
# set states
if states:
if callable(getattr(op, "set_states", None)):
# rename the layer's states
states = {
getattr(op, key).name: val
for (key, val) in states.items()
}
if self.is_graph and not self.has_initialized:
prev_state = self.dev.graph_enabled()
self.dev.EnableGraph(False)
op.set_states(states)
self.dev.EnableGraph(prev_state)
else:
op.set_states(states)
else:
for key, value in states.items():
setattr(op, key, value)
# run the node
outputs = _run_node(op, inputs)
# release the input tensor
for inp in node.inputs:
if inp in self.tensor_count:
self.tensor_count[inp] -= 1
if self.tensor_count[inp] == 0:
if inp in tensor_dict:
del tensor_dict[inp]
del self.tensor_count[inp]
# store the output
for (outp, val) in zip(node.outputs, outputs):
tensor_dict[outp] = val
if outp in outputs_dict:
outputs_dict[outp] = self.to_output_tensor(val, outp)
self.has_initialized = True
return list(outputs_dict.values())