
    sgE!                     r    d dl Z d dlZd dlmZ ddZ G d d      ZdefdZdefd	Zdefd
Z	d Z
d Zd Zy)    N)validate_map_locationc                 >   t        | t        t        j                  f      r\t        j                  j                  |       st        d|  d      t        j                  j                  |       rt        d|  d      t        |      }t        | t        t        j                  f      r>t        j                  j                  t        j                  |       |      }t        |      S t        j                  j                  | j                         |      }t        |      S )au  
    Load a :class:`LiteScriptModule` saved with :func:`torch.jit._save_for_lite_interpreter`.

    Args:
        f: a file-like object (has to implement read, readline, tell, and seek),
            or a string containing a file name
        map_location: a string or torch.device used to dynamically remap
            storages to an alternative set of devices.

    Returns:
        A :class:`LiteScriptModule` object.

    Example:

    .. testcode::

        import torch
        import io

        # Load LiteScriptModule from saved file path
        torch.jit._load_for_lite_interpreter('lite_script_module.pt')

        # Load LiteScriptModule from io.BytesIO object
        with open('lite_script_module.pt', 'rb') as f:
            buffer = io.BytesIO(f.read())

        # Load all tensors to the original device
        torch.jit.mobile._load_for_lite_interpreter(buffer)
    The provided filename  does not exist is a directory)
isinstancestrosPathLikepathexists
ValueErrorisdirr   torch_C_load_for_lite_interpreterfspath&_load_for_lite_interpreter_from_bufferreadLiteScriptModule)fmap_location
cpp_modules      L/var/www/html/venv/lib/python3.12/site-packages/torch/jit/mobile/__init__.pyr   r      s    < !c2;;'(ww~~a 5aSHII77==5aSHII(6L!c2;;'(XX881|T
 J''	 XXDDFFHl

 J''    c                   6     e Zd Z fdZd Zd Zd Zd Z xZS )r   c                 0    || _         t        | 	          y N)_csuper__init__)selfr   	__class__s     r   r!   zLiteScriptModule.__init__9   s    r   c                 8    | j                   j                  |      S r   r   forwardr"   inputs     r   __call__zLiteScriptModule.__call__=       wwu%%r   c                 8    | j                   j                  |      S r   )r   find_method)r"   method_names     r   r,   zLiteScriptModule.find_method@   s    ww"";//r   c                 8    | j                   j                  |      S r   r%   r'   s     r   r&   zLiteScriptModule.forwardC   r*   r   c                 :    | j                   j                  ||      S r   )r   
run_method)r"   r-   r(   s      r   r0   zLiteScriptModule.run_methodF   s    ww!!+u55r   )	__name__
__module____qualname__r!   r)   r,   r&   r0   __classcell__)r#   s   @r   r   r   8   s    &0&6r   r   modulec                 T    t         j                  j                  | j                        S )zkReturn a set of root operator names (with overload name) that are used by any method in this mobile module.)r   r   _export_operator_listr   )r5   s    r   r7   r7   J   s    88))&))44r   returnc                    t        | t        t        j                  f      r\t        j                  j                  |       st        d|  d      t        j                  j                  |       rt        d|  d      t        | t        t        j                  f      r2t        j                  j                  t        j                  |             S t        j                  j                  | j                               S )a%  Take a file-like object to return an integer.

    Args:
        f_input: a file-like object (has to implement read, readline, tell, and seek),
            or a string containing a file name

    Returns:
        version: An integer. If the integer is -1, the version is invalid. A warning
            will show in the log.

    Example:
    .. testcode::

        from torch.jit.mobile import _get_model_bytecode_version

        # Get bytecode version from a saved file path
        version = _get_model_bytecode_version("path/to/model.ptl")

    r   r   r   )r   r	   r
   r   r   r   r   r   r   r   _get_model_bytecode_versionr   '_get_model_bytecode_version_from_bufferr   f_inputs    r   r:   r:   O   s    ( 'C-.ww~~g&5gYoNOO77==!5gYoNOO'C-.xx33BIIg4FGGxx??OOr   c                    t        | t        t        j                  f      r\t        j                  j                  |       st        d|  d      t        j                  j                  |       rt        d|  d      t        | t        t        j                  f      r2t        j                  j                  t        j                  |             S t        j                  j                  | j                               S )a6  Take a file-like object and return a set of string, like ("int", "Optional").

    Args:
        f_input: a file-like object (has to implement read, readline, tell, and seek),
            or a string containing a file name

    Returns:
        type_list: A set of string, like ("int", "Optional"). These are types used in bytecode.

    Example:

    .. testcode::

        from torch.jit.mobile import _get_mobile_model_contained_types

        # Get type list from a saved file path
        type_list = _get_mobile_model_contained_types("path/to/model.ptl")

    r   r   r   )r   r	   r
   r   r   r   r   r   r   r   !_get_mobile_model_contained_typesr   -_get_mobile_model_contained_types_from_bufferr   r<   s    r   r?   r?   o   s    ( 'C-.ww~~g&5gYoNOO77==!5gYoNOO'C-.xx99"))G:LMMxxEEgllnUUr   c                 x   t        | t        t        j                  f      r\t        j                  j                  |       st        d|  d      t        j                  j                  |       rt        d|  d      t        | t        t        j                  f      rgt        |t        t        j                  f      rGt        j                  j                  t        j                  |       t        j                  |      |      S t        j                  j                  | j                         t        |      |      S )a  Take a input string containing a file name (file-like object) and a new destination to return a boolean.

    Args:
        f_input: a file-like object (has to implement read, readline, tell, and seek),
            or a string containing a file name
        f_output: path to new model destination
        to_version: the expected output model bytecode version
    Returns:
        success: A boolean. If backport success, return true, otherwise false
    r   r   r   )r   r	   r
   r   r   r   r   r   r   r   _backport_for_mobiler    _backport_for_mobile_from_bufferr   )r=   f_output
to_versions      r   rB   rB      s     'C-.ww~~g&5gYoNOO77==!5gYoNOO7S"++./8c2;;/0xx,,IIg		( 3Z
 	
 xx88LLNCM:
 	
r   c                    t        | t        t        j                  f      r\t        j                  j                  |       st        d|  d      t        j                  j                  |       rt        d|  d      t        | t        t        j                  f      r3t        j                  j                  t        j                  |       |      S t        j                  j                  | j                         |      S )zTake a string containing a file name (file-like object).

    Args:
        f_input: a file-like object (has to implement read, readline, tell, and seek),
            or a string containing a file name

    r   r   r   )r   r	   r
   r   r   r   r   r   r   r   _backport_for_mobile_to_bufferr   *_backport_for_mobile_from_buffer_to_bufferr   )r=   rE   s     r   rG   rG      s     'C-.ww~~g&5gYoNOO77==!5gYoNOO'C-.xx66ryy7I:VVxxBBLLNJ
 	
r   c                    t        | t        t        j                  f      r\t        j                  j                  |       st        d|  d      t        j                  j                  |       rt        d|  d      t        | t        t        j                  f      r2t        j                  j                  t        j                  |             S t        j                  j                  | j                               S )a{  Retrieve the root (top level) operators of a model and their corresponding compatibility info.

    These root operators can call other operators within them (traced ops), and
    a root op can call many different traced ops depending on internal code paths in the root op.
    These traced ops are not returned by this function. Those operators are abstracted into the
    runtime as an implementation detail (and the traced ops themselves can also call other operators)
    making retrieving them difficult and their value from this api negligible since they will differ
    between which runtime version the model is run on. Because of this, there is a false positive this
    api can't prevent in a compatibility usecase. All the root ops of a model are present in a
    target runtime, but not all the traced ops are which prevents a model from being able to run.
    Args:
        f_input: a file-like object (has to implement read, readline, tell, and seek),
            or a string containing a file name

    Returns:
        Operators and info: A Dictionary mapping strings (the qualified names of the root operators)
        of the model to their OperatorInfo structs.

    Example:

    .. testcode::

        from torch.jit.mobile import _get_model_ops_and_info

        # Get bytecode version from a saved file path
        ops_and_info = _get_model_ops_and_info("path/to/model.ptl")

    r   r   r   )r   r	   r
   r   r   r   r   r   r   r   _get_model_ops_and_infor   r   r<   s    r   rJ   rJ      s    : 'C-.ww~~g&5gYoNOO77==!5gYoNOO'C-.xx//		'0BCCxx//??r   r   )r
   r   torch.jit._serializationr   r   r   r7   intr:   r?   rB   rG   rJ    r   r   <module>rN      sY    	  :-(`6 6$5"2 5
PC P@V# V@
:
,&@r   