
    shE!                     z    S SK r S SKrS SKJr  SS jr " S S5      rS\4S jrS\4S	 jrS\4S
 jr	S r
S rS rg)    N)validate_map_locationc                 T   [        U [        [        R                  45      (       af  [        R                  R                  U 5      (       d  [        SU  S35      e[        R                  R                  U 5      (       a  [        SU  S35      e[        U5      n[        U [        [        R                  45      (       a5  [        R                  R                  [        R                  " U 5      U5      nO.[        R                  R                  U R                  5       U5      n[        U5      $ )a%  
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      m/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/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
XXDDFFHl

 J''    c                   @   ^  \ rS rSrU 4S jrS rS rS rS rSr	U =r
$ )r   8   c                 .   > Xl         [        TU ]	  5         g N)_csuper__init__)selfr   	__class__s     r   r"   LiteScriptModule.__init__9   s    r   c                 8    U R                   R                  U5      $ r   r    forwardr#   inputs     r   __call__LiteScriptModule.__call__=       wwu%%r   c                 8    U R                   R                  U5      $ r   )r    find_method)r#   method_names     r   r/   LiteScriptModule.find_method@   s    ww"";//r   c                 8    U R                   R                  U5      $ r   r'   r)   s     r   r(   LiteScriptModule.forwardC   r-   r   c                 8    U R                   R                  X5      $ r   )r    
run_method)r#   r0   r*   s      r   r5   LiteScriptModule.run_methodF   s    ww!!+55r   )r    )__name__
__module____qualname____firstlineno__r"   r+   r/   r(   r5   __static_attributes____classcell__)r$   s   @r   r   r   8   s!    &0&6 6r   r   modulec                 T    [         R                  R                  U R                  5      $ )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    )r=   s    r   r?   r?   J   s    88))&))44r   returnc                 "   [        U [        [        R                  45      (       af  [        R                  R                  U 5      (       d  [        SU  S35      e[        R                  R                  U 5      (       a  [        SU  S35      e[        U [        [        R                  45      (       a3  [        R                  R                  [        R                  " U 5      5      $ [        R                  R                  U R                  5       5      $ )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   rB   rB   O   s    ( 'C-..ww~~g&&5gYoNOO77==!!5gYoNOO'C-..xx33BIIg4FGGxx??OOr   c                 "   [        U [        [        R                  45      (       af  [        R                  R                  U 5      (       d  [        SU  S35      e[        R                  R                  U 5      (       a  [        SU  S35      e[        U [        [        R                  45      (       a3  [        R                  R                  [        R                  " U 5      5      $ [        R                  R                  U R                  5       5      $ )a
  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   rD   s    r   rG   rG   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                    [        U [        [        R                  45      (       af  [        R                  R                  U 5      (       d  [        SU  S35      e[        R                  R                  U 5      (       a  [        SU  S35      e[        U [        [        R                  45      (       an  [        U[        [        R                  45      (       aI  [        R                  R                  [        R                  " U 5      [        R                  " U5      U5      $ [        R                  R                  U R                  5       [        U5      U5      $ )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   )rE   f_output
to_versions      r   rJ   rJ      s     'C-..ww~~g&&5gYoNOO77==!!5gYoNOO7S"++.//8c2;;/00xx,,IIg		( 3Z
 	
 xx88LLNCM:
 	
r   c                 &   [        U [        [        R                  45      (       af  [        R                  R                  U 5      (       d  [        SU  S35      e[        R                  R                  U 5      (       a  [        SU  S35      e[        U [        [        R                  45      (       a4  [        R                  R                  [        R                  " U 5      U5      $ [        R                  R                  U R                  5       U5      $ )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   )rE   rM   s     r   rO   rO      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                 "   [        U [        [        R                  45      (       af  [        R                  R                  U 5      (       d  [        SU  S35      e[        R                  R                  U 5      (       a  [        SU  S35      e[        U [        [        R                  45      (       a3  [        R                  R                  [        R                  " U 5      5      $ [        R                  R                  U R                  5       5      $ )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   rD   s    r   rR   rR      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   r?   intrB   rG   rJ   rO   rR    r   r   <module>rV      sY    	  :-(`6 6$5"2 5
PC P@V# V@
:
,&@r   