junrushao commented on code in PR #15179: URL: https://github.com/apache/tvm/pull/15179#discussion_r1246117951
########## python/tvm/testing/rpc_run.py: ########## @@ -0,0 +1,160 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# pylint: disable=invalid-name, missing-function-docstring +"""A utility method to run a TVM module on a remote device.""" +from typing import TYPE_CHECKING, Callable, List, Literal, Optional, Tuple, Union + +if TYPE_CHECKING: + import numpy as np + + from tvm.meta_schedule.runner import EvaluatorConfig, RPCConfig + from tvm.runtime import Device, Module, NDArray + +# pylint: disable=import-outside-toplevel,protected-access + + +def _args_to_remote(args, device): + import numpy as np + + from tvm.runtime.ndarray import NDArray, empty + + uploaded_args = [] + for arg in args: + if isinstance(arg, (np.ndarray, NDArray)): + uploaded_args.append(empty(arg.shape, dtype=arg.dtype, device=device).copyfrom(arg)) + elif isinstance(arg, (int, float)): + uploaded_args.append(arg) + else: + raise ValueError(f"Unsupported input type: {type(arg)}") + return uploaded_args + + +def _args_to_local(args): + from tvm.runtime.ndarray import NDArray + + downloaded_args = [] + for arg in args: + if isinstance(arg, NDArray): + downloaded_args.append(arg.numpy()) + else: + downloaded_args.append(arg) + return downloaded_args + + +def _normalize_export_func(export_func, output_format) -> Tuple[Callable, str]: + from tvm.contrib import ndk, tar + + def export_with(func): + return lambda rt_mod, path: rt_mod.export_library(path, func) + + if export_func == "tar": + export_func = export_with(tar.tar) + output_format = "tar" + elif export_func == "ndk": + export_func = export_with(ndk.create_shared) + output_format = "so" + elif callable(export_func): + if output_format is None: + raise ValueError("output_format must be specified if `export_func` is callable") + else: + raise ValueError(f"Unsupported export_func: {export_func}") + return export_func, output_format + + +def rpc_run( # pylint: disable=too-many-arguments,too-many-locals + rt_mod: "Module", Review Comment: well i just realized it's ambiguous - "rt" here means runtime actually ########## python/tvm/testing/rpc_run.py: ########## @@ -0,0 +1,160 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# pylint: disable=invalid-name, missing-function-docstring +"""A utility method to run a TVM module on a remote device.""" +from typing import TYPE_CHECKING, Callable, List, Literal, Optional, Tuple, Union + +if TYPE_CHECKING: + import numpy as np + + from tvm.meta_schedule.runner import EvaluatorConfig, RPCConfig + from tvm.runtime import Device, Module, NDArray + +# pylint: disable=import-outside-toplevel,protected-access + + +def _args_to_remote(args, device): + import numpy as np + + from tvm.runtime.ndarray import NDArray, empty + + uploaded_args = [] + for arg in args: + if isinstance(arg, (np.ndarray, NDArray)): + uploaded_args.append(empty(arg.shape, dtype=arg.dtype, device=device).copyfrom(arg)) + elif isinstance(arg, (int, float)): + uploaded_args.append(arg) + else: + raise ValueError(f"Unsupported input type: {type(arg)}") + return uploaded_args + + +def _args_to_local(args): + from tvm.runtime.ndarray import NDArray + + downloaded_args = [] + for arg in args: + if isinstance(arg, NDArray): + downloaded_args.append(arg.numpy()) + else: + downloaded_args.append(arg) + return downloaded_args + + +def _normalize_export_func(export_func, output_format) -> Tuple[Callable, str]: + from tvm.contrib import ndk, tar + + def export_with(func): + return lambda rt_mod, path: rt_mod.export_library(path, func) + + if export_func == "tar": + export_func = export_with(tar.tar) + output_format = "tar" + elif export_func == "ndk": + export_func = export_with(ndk.create_shared) + output_format = "so" + elif callable(export_func): + if output_format is None: + raise ValueError("output_format must be specified if `export_func` is callable") + else: + raise ValueError(f"Unsupported export_func: {export_func}") + return export_func, output_format + + +def rpc_run( # pylint: disable=too-many-arguments,too-many-locals + rt_mod: "Module", Review Comment: any ideas? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
