run the forward of singa model Args: x (np.ndarray[]): a list of numpy ndarray as inputs Returns: a list of outputs
(self, x, **kwargs)
| 2084 | op.bias_shape = shape |
| 2085 | |
| 2086 | def run(self, x, **kwargs): |
| 2087 | """ |
| 2088 | run the forward of singa model |
| 2089 | Args: |
| 2090 | x (np.ndarray[]): a list of numpy ndarray as inputs |
| 2091 | Returns: |
| 2092 | a list of outputs |
| 2093 | """ |
| 2094 | if not self.has_initialized: |
| 2095 | self.initialize() |
| 2096 | if isinstance(x[0], tensor.Tensor): |
| 2097 | self.dev = x[0].device |
| 2098 | |
| 2099 | outputs_dict = OrderedDict([]) |
| 2100 | |
| 2101 | # last_layers means we run this model until the last #N layers |
| 2102 | last_layers = kwargs.get('last_layers', len(self._layers) - 1) |
| 2103 | last_layers = last_layers if last_layers >= 0 else ( |
| 2104 | last_layers + 1) % len(self._layers) |
| 2105 | if last_layers != len(self._layers) - 1: |
| 2106 | for outp in self._layers[last_layers].outputs: |
| 2107 | outputs_dict[outp] = None |
| 2108 | else: |
| 2109 | for outp in self.outputs: |
| 2110 | outputs_dict[outp.name] = None |
| 2111 | |
| 2112 | aux_output = kwargs.get('aux_output', ()) |
| 2113 | for outp in aux_output: |
| 2114 | outputs_dict[outp] = None |
| 2115 | |
| 2116 | tensor_dict = self.to_input_tensor(x) |
| 2117 | self.init_tensor_count() |
| 2118 | |
| 2119 | # run the layer by the topo order |
| 2120 | for node in self._layers[:last_layers + 1]: |
| 2121 | op = self.__dict__[node.name] |
| 2122 | self.handle_special_ops(node, op, tensor_dict) |
| 2123 | # make input |
| 2124 | inputs = [] |
| 2125 | for inp in node.inputs: |
| 2126 | if inp not in node.weight_inputs and inp not in node.attr_inputs: |
| 2127 | if inp in tensor_dict: |
| 2128 | inputs.append(tensor_dict[inp]) |
| 2129 | elif inp in self.states: |
| 2130 | # todo, scalar |
| 2131 | val = np.atleast_1d(self.states[inp]) |
| 2132 | val = tensor.from_numpy(val) |
| 2133 | val.to_device(self.dev) |
| 2134 | inputs.append(val) |
| 2135 | else: |
| 2136 | raise KeyError( |
| 2137 | "Not found the input {} for operation {}".format( |
| 2138 | inp, node.name)) |
| 2139 | states = {} |
| 2140 | if callable(getattr(op, "initialize", |
| 2141 | None)) and not op._initialized: |
| 2142 | # init the operator |
| 2143 | op.initialize(*inputs) |
no test coverage detected