1515# specific language governing permissions and limitations
1616# under the License.
1717
18+ from typing import List , Optional , Union , Dict
1819import tvm
19- from tvm .runtime import Object
20+ from tvm .runtime import Object , Device , Module , PackedFunc
2021from tvm ._ffi .base import _LIB , check_call
2122from . import _ffi_api
2223from ..rpc .base import RPC_SESS_MASK
2526@tvm ._ffi .register_object ("relax.Executable" )
2627class Executable (Object ):
2728 """The executable object emitted by the VM compiler or the ExecBuilder."""
29+
2830 def __init__ (self ):
2931 self .__init_handle_by_constructor__ (_ffi_api .Executable )
3032
31- def stats (self ):
33+ def stats (self ) -> str :
3234 """print the detailed statistics of the executable."""
3335 return _ffi_api .ExecutableStats (self )
3436
35- def save_to_file (self , file_name ) :
37+ def save_to_file (self , file_name : str ) -> None :
3638 """serialize and write the executable to a file."""
37- return _ffi_api .ExecutableSaveToFile (self , file_name )
39+ _ffi_api .ExecutableSaveToFile (self , file_name )
3840
39- def astext (self ):
41+ def astext (self ) -> str :
4042 """print the instructions as text format."""
4143 return _ffi_api .ExecutableAsText (self )
42-
43- def aspython (self ):
44+
45+ def aspython (self ) -> str :
4446 """print the instructions as python program."""
4547 return _ffi_api .ExecutableAsPython (self )
4648
47- def load_exec_from_file (file_name ):
49+
50+ def load_exec_from_file (file_name : str ) -> Executable :
4851 return _ffi_api .ExecutableLoadFromFile (file_name )
4952
53+
5054class VirtualMachine (object ):
5155 """Relax VM runtime."""
5256
5357 NAIVE_ALLOCATOR = 1
5458 POOLED_ALLOCATOR = 2
55-
56- def __init__ (self , exec , device , memory_cfg = None , mod = None ):
59+
60+ def __init__ (
61+ self ,
62+ exec : Executable ,
63+ device : Union [Device , List [Device ]],
64+ memory_cfg : Optional [Union [str , Dict [Device , str ]]] = None ,
65+ mod : Optional [Module ] = None ,
66+ ) -> None :
67+
5768 """
5869 Construct a VirtualMachine wrapper object.
5970
@@ -73,6 +84,9 @@ def __init__(self, exec, device, memory_cfg=None, mod=None):
7384 type specified in the dict, or pooled allocator if not specified in the
7485 dict.
7586
87+ mod : tvm.runtime.Module, optional
88+ Optional runtime module to load to the VM.
89+
7690 Returns
7791 -------
7892 vm: VirtualMachine
@@ -81,7 +95,7 @@ def __init__(self, exec, device, memory_cfg=None, mod=None):
8195 self .module = _ffi_api .VirtualMachine (exec , mod )
8296 self ._setup_device (device , memory_cfg )
8397
84- def _setup_device (self , dev , memory_cfg ) :
98+ def _setup_device (self , dev : Device , memory_cfg : Union [ str , Dict [ Device , str ]]) -> None :
8599 """init devices and allocators."""
86100 devs = dev
87101 if not isinstance (dev , (list , tuple )):
@@ -117,5 +131,5 @@ def _setup_device(self, dev, memory_cfg):
117131 init_args .append (alloc_type )
118132 _ffi_api .VirtualMachineInit (self .module , * init_args )
119133
120- def __getitem__ (self , key ) :
134+ def __getitem__ (self , key : str ) -> PackedFunc :
121135 return self .module [key ]
0 commit comments