run a single singa operator from a onnx node Args: node (NodeProto): a given onnx node inputs (ndarray[]): a list of numpy ndarray device (string): CPU or CUDA opset_version (int): the opset version Returns: list, t
(cls, node, inputs, device='CPU', opset_version=_opset_version)
| 1770 | |
| 1771 | @classmethod |
| 1772 | def run_node(cls, node, inputs, device='CPU', opset_version=_opset_version): |
| 1773 | """ |
| 1774 | run a single singa operator from a onnx node |
| 1775 | Args: |
| 1776 | node (NodeProto): a given onnx node |
| 1777 | inputs (ndarray[]): a list of numpy ndarray |
| 1778 | device (string): CPU or CUDA |
| 1779 | opset_version (int): the opset version |
| 1780 | Returns: |
| 1781 | list, the output |
| 1782 | """ |
| 1783 | node = OnnxNode(node) |
| 1784 | valid_inputs = [x for x in node.inputs if x != ""] |
| 1785 | assert len(valid_inputs) == len( |
| 1786 | inputs), "{}: expected {} inputs, but got {}. ".format( |
| 1787 | node.op_type, len(valid_inputs), len(inputs)) |
| 1788 | |
| 1789 | operator = cls._onnx_node_to_singa_op(node, opset_version) |
| 1790 | # separate weights with inputs, and init inputs as Tensor |
| 1791 | weights = {} |
| 1792 | _inputs = [] |
| 1793 | for (key, val) in zip(valid_inputs, inputs): |
| 1794 | val = val.astype(onnx_type_to_singa_type(val.dtype)) |
| 1795 | if key in node.weight_inputs: |
| 1796 | weights[key] = val |
| 1797 | else: |
| 1798 | x = tensor.from_numpy(val) |
| 1799 | if device != 'CPU': |
| 1800 | assert singa.USE_CUDA, "Your SINGA doesn't compile GPU module." |
| 1801 | dev = device.create_cuda_gpu() |
| 1802 | else: |
| 1803 | dev = device.get_default_device() |
| 1804 | x.to_device(dev) |
| 1805 | _inputs.append(x) |
| 1806 | inputs = _inputs |
| 1807 | # set params |
| 1808 | params = {} |
| 1809 | for key, name in node.weight_inputs.items(): |
| 1810 | params[name] = weights[key] |
| 1811 | operator.set_params(params) |
| 1812 | outputs = cls._run_node(operator, inputs) |
| 1813 | outputs_dict = OrderedDict() |
| 1814 | for (key, val) in zip(node.outputs, outputs): |
| 1815 | outputs_dict[key] = val |
| 1816 | return outputs_dict |
| 1817 | |
| 1818 | @classmethod |
| 1819 | def _run_node(cls, operator, inputs): |
nothing calls this directly
no test coverage detected