
    `ic'                       d Z ddlmZ ddlZddlZddlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZ dZ e
d	e
          Zd7d8dZ	 	 d9d:dZed;d            Zed<d=d             Zd<d>d!Zd?d#Zd@d%ZdAd'Zd7dBd*ZdCd/ZdDd3ZdEd5Zg d6ZdS )Fz.FFI registry to register function and objects.    )annotationsN)AnyCallableLiteralSequenceTypeVaroverload   )core)TypeInfoF_T)boundtype_key
str | NonereturnCallable[[_T], _T]c                     ddt           t                    r	d fd}|S dfd} |S t           t                    r |           S t          d
          )a  Register object type.

    Parameters
    ----------
    type_key
        The type key of the node. It requires ``type_key`` to be registered already
        on the C++ side. If not specified, the class name will be used.

    Examples
    --------
    The following code registers MyObject using type key "test.MyObject", if the
    type key is already registered on the C++ side.

    .. code-block:: python

      @tvm_ffi.register_object("test.MyObject")
      class MyObject(Object):
          pass

    clsr   object_namestrr   c                    t          j        |          }|t          r| S t          d|           t          j        ||           }t          | |           t          | d|           | S )z+Register the object type with the FFI core.Nz"Cannot find object type index for )type_cls	type_info__tvm_ffi_type_info__)r   _object_type_key_to_index_SKIP_UNKNOWN_OBJECTS
ValueError_register_object_by_index_add_class_attrssetattr)r   r   
type_indexinfos       d/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/tvm_ffi/registry.py	_registerz"register_object.<locals>._register9   sy    3K@@
$ 
O+OOPPP-j#>>#6666,d333
    c                     |           S N )r   r$   r   s    r#   _decorator_with_namez-register_object.<locals>._decorator_with_nameG   s    9S(+++r%   c                &     | | j                   S r'   )__name__)r   r$   s    r#   _decorator_defaultz+register_object.<locals>._decorator_defaultL   s    ycl+++r%   Nz(type_key must be a string, type, or None)r   r   r   r   r   r   )r   r   r   r   )
isinstancer   type	TypeError)r   r)   r,   r$   s   `  @r#   register_objectr0   #   s    ,
 
 
 
 (C   $	, 	, 	, 	, 	, 	, 	, $#, , , , , , !!(D!! ,!!(+++
>
?
??r%   	func_namestr | Callable[..., Any]fCallable[..., Any] | Noneoverrideboolr   c                     t                     r	 }|j         t           t                    st	          d          d fd}| ||          S |S )	a  Register global function.

    Parameters
    ----------
    func_name
        The function name

    f
        The function to be registered.

    override
        Whether override existing entry.

    Returns
    -------
    fregister
        Register function if f is not specified.

    Examples
    --------
    .. code-block:: python

        import tvm_ffi


        # we can use decorator to register a function
        @tvm_ffi.register_global_func("mytest.echo")
        def echo(x):
            return x


        # After registering, we can get the function by its name
        f = tvm_ffi.get_global_func("mytest.echo")
        assert f(1) == 1

        # we can also directly register a function
        tvm_ffi.register_global_func("mytest.add_one", lambda x: x + 1)
        f = tvm_ffi.get_global_func("mytest.add_one")
        assert f(1) == 2

    See Also
    --------
    :py:func:`tvm_ffi.get_global_func`
    :py:func:`tvm_ffi.remove_global_func`

    zexpect string function namemyfCallable[..., Any]r   r   c                0    t          j        |           S )z/Register the global function with the FFI core.)r   _register_global_func)r8   r1   r5   s    r#   registerz&register_global_func.<locals>.register   s    ))S(CCCr%   N)r8   r9   r   r   )callabler+   r-   r   r   )r1   r3   r5   r<   s   ` ` r#   register_global_funcr>   V   s    f 	 J	i%% 86777D D D D D D D 	}x{{Or%   namer   allow_missingLiteral[True]core.Function | Nonec                    d S r'   r(   r?   r@   s     r#   get_global_funcrE      s    VYVYr%   Literal[False]core.Functionc                    d S r'   r(   rD   s     r#   rE   rE      s    X[X[r%   c                ,    t          j        | |          S )am  Get a global function by name.

    Parameters
    ----------
    name
        The name of the global function

    allow_missing
        Whether allow missing function or raise an error.

    Returns
    -------
    func
        The function to be returned, ``None`` if function is missing.

    Examples
    --------
    .. code-block:: python

        import tvm_ffi


        @tvm_ffi.register_global_func("demo.echo")
        def echo(x):
            return x


        f = tvm_ffi.get_global_func("demo.echo")
        assert f(123) == 123

    See Also
    --------
    :py:func:`tvm_ffi.register_global_func`

    )r   _get_global_funcrD   s     r#   rE   rE      s    H  }555r%   	list[str]c                 ~     t          d                       d          } fdt          |           D             S )zxGet list of global functions registered.

    Returns
    -------
    names
       List of global functions names.

    z"ffi.FunctionListGlobalNamesFunctorc                &    g | ]} |          S r(   r(   ).0iname_functors     r#   
<listcomp>z*list_global_func_names.<locals>.<listcomp>   s!    666LLOO666r%   )rE   range)	num_namesrQ   s    @r#   list_global_func_namesrU      sM     I?#GHHJJLR  I6666U9%5%56666r%   Nonec                6     t          d          |            dS )aa  Remove a global function by name.

    Parameters
    ----------
    name
        The name of the global function.

    Examples
    --------
    .. code-block:: python

        import tvm_ffi


        @tvm_ffi.register_global_func("my.temp")
        def temp():
            return 42


        assert tvm_ffi.get_global_func("my.temp", allow_missing=True) is not None
        tvm_ffi.remove_global_func("my.temp")
        assert tvm_ffi.get_global_func("my.temp", allow_missing=True) is None

    See Also
    --------
    :py:func:`tvm_ffi.register_global_func`
    :py:func:`tvm_ffi.get_global_func`

    zffi.FunctionRemoveGlobalNrE   r?   s    r#   remove_global_funcrZ      s#    < 0O.//55555r%   dict[str, Any]c                Z    t          j         t          d          |           pd          S )a  Get metadata (including type schema) for a global function.

    Parameters
    ----------
    name
        The name of the global function.

    Returns
    -------
    metadata
        A dictionary containing function metadata. The ``type_schema`` field
        encodes the callable signature.

    Examples
    --------
    .. code-block:: python

        import tvm_ffi

        meta = tvm_ffi.get_global_func_metadata("testing.add_one")
        print(meta)

    See Also
    --------
    :py:func:`tvm_ffi.get_global_func`
        Retrieve a callable for an existing global function.
    :py:func:`tvm_ffi.register_global_func`
        Register a Python callable as a global FFI function.

    zffi.GetGlobalFuncMetadataz{})jsonloadsrE   rY   s    r#   get_global_func_metadatar_      s-    > :Bo&ABB4HHPDQQQr%   	namespacetarget_module_namec                   |r|n| }|                      d          r| dd         }n| }t          j        |         }t                      D ]}}|                     |          s|t	          |          dz   d         }|                    d          dk    rLt          |          }t          |d|           t          |||           ~dS )a  Initialize register ffi api  functions into a given module.

    Parameters
    ----------
    namespace
       The namespace of the source registry

    target_module_name
       The target module name if different from namespace

    Examples
    --------
    A typical usage pattern is to create a _ffi_api.py file to register
    the functions under a given module. The following
    code populates all registered global functions
    prefixed with ``mypackage.`` into the current module,
    then we can call the function through ``_ffi_api.func_name(*args)``
    which will call into the registered global function "mypackage.func_name".

    .. code-block:: python

        # _ffi_api.py
        import tvm_ffi

        tvm_ffi.init_ffi_api("mypackage", __name__)

    ztvm.   Nr
   .rM   r+   )
startswithsysmodulesrU   lenfindrE   r    )r`   ra   prefixtarget_moduler?   fnamer3   s          r#   init_ffi_apirm     s    8 0BP++yF## 122K 23M&(( 
) 
)v&& 	S[[1_&&'::c??b  D!!:u%%%ua((((
) 
)r%   r   r.   r   r   c                   |j         D ]=}|j        }t          | |          s$t          | ||                    |                      >d}|j        D ]G}|j        }|dk    rd}d}t          | |          s$t          | ||                    |                      Hd| j        vrR|r t          | dt          | d                     n0t          | t          j                  st          | dt                     | S )NF__ffi_init____c_ffi_init__T__init__)fieldsr?   hasattrr    as_propertymethodsas_callable__dict__getattr
issubclassr   PyNativeObject__init__invalid)r   r   fieldr?   
has_c_initmethods         r#   r   r   K  s   ! A Azx&& 	AHdE$5$5h$?$?@@@J# B B{>!!#DJx&& 	BHdF$6$6x$@$@AAA*** 	;Hj'(N*K*KLLLLHd&9:: 	;Hj/:::Or%   selfargskwargsc                     t          d          )Nz5The __init__ method of this class is not implemented.)RuntimeError)r   r   r   s      r#   r{   r{   `  s    
N
O
OOr%   Sequence[str]c                 0     t          d                      S )zGet the list of valid type keys registered to TVM-FFI.

    Returns
    -------
    type_keys
        List of valid type keys.

    zffi.GetRegisteredTypeKeysrX   r(   r%   r#   get_registered_type_keysr   d  s     8?677999r%   )rE   r_   r   rm   rU   r>   r0   rZ   r'   )r   r   r   r   )NF)r1   r2   r3   r4   r5   r6   r   r   )r?   r   r@   rA   r   rB   )F)r?   r   r@   rF   r   rG   )r?   r   r@   r6   r   rB   )r   rK   )r?   r   r   rV   )r?   r   r   r[   )r`   r   ra   r   r   rV   )r   r.   r   r   r   r.   )r   r   r   r   r   r   r   rV   )r   r   )__doc__
__future__r   r]   rf   typingr   r   r   r   r   r	    r   r   r   r.   r   r0   r>   rE   rU   rZ   r_   rm   r   r{   r   __all__r(   r%   r#   <module>r      s  " 5 4 " " " " " "  



 F F F F F F F F F F F F F F F F               WT0@ 0@ 0@ 0@ 0@j $(@ @ @ @ @F 
 Y Y Y 
 Y 
 [ [ [ [ 
 [$6 $6 $6 $6 $6N7 7 7 76 6 6 6BR R R RD/) /) /) /) /)d   *P P P P	: 	: 	: 	:	 	 	r%   