a
    HSic
&                     @   s   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 dddZ
ddd	Zdd
dZdd Zdd ZdddZdd ZdS )zSerialization

This module contains functionality for serializing TorchScript modules, notably:
    * torch.jit.save
    * torch.jit.load

This is not intended to be imported directly; please use the exposed
functionalities in `torch.jit`.
    N)string_classes)wrap_cpp_module)validate_cuda_devicec                 C   sL   |du ri }t |ts"t |tjr2| j||d n| j|d}|| dS )aw  
    Save an offline version of this module for use in a separate process. The
    saved module serializes all of the methods, submodules, parameters, and
    attributes of this module. It can be loaded into the C++ API using
    ``torch::jit::load(filename)`` or into the Python API with
    :func:`torch.jit.load <torch.jit.load>`.

    To be able to save a module, it must not make any calls to native Python
    functions.  This means that all submodules must be subclasses of
    :class:`ScriptModule` as well.

    .. DANGER::
        All modules, no matter their device, are always loaded onto the CPU
        during loading.  This is different from :func:`torch.load`'s semantics
        and may change in the future.

    Args:
        m: A :class:`ScriptModule` to save.
        f: A file-like object (has to implement write and flush) or a string
           containing a file name.
        _extra_files: Map from filename to contents which will be stored as part of `f`.

    .. note::
        torch.jit.save attempts to preserve the behavior of some operators
        across versions. For example, dividing two integer tensors in
        PyTorch 1.5 performed floor division, and if the module
        containing that code is saved in PyTorch 1.5 and loaded in PyTorch 1.6
        its division behavior will be preserved. The same module saved in
        PyTorch 1.6 will fail to load in PyTorch 1.5, however, since the
        behavior of division changed in 1.6, and 1.5 does not know how to
        replicate the 1.6 behavior.

    Example:

    .. testcode::

        import torch
        import io

        class MyModule(torch.nn.Module):
            def forward(self, x):
                return x + 10

        m = torch.jit.script(MyModule())

        # Save to file
        torch.jit.save(m, 'scriptmodule.pt')
        # This line is equivalent to the previous
        m.save("scriptmodule.pt")

        # Save to io.BytesIO buffer
        buffer = io.BytesIO()
        torch.jit.save(m, buffer)

        # Save with extra files
        extra_files = {'foo.txt': b'bar'}
        torch.jit.save(m, 'scriptmodule.pt', _extra_files=extra_files)
    N)_extra_files)
isinstancestrpathlibPathsavesave_to_bufferwrite)mfr   ret r   T/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/jit/_serialization.pyr
      s    ;r
   c                 C   s   t | tr>tj| s$td| tj| r>td| t|}|du rRi }t	j
 }t | tsrt | tjrt	j
|t| ||}nt	j
||  ||}t|S )a  
    Load a :class:`ScriptModule` or :class:`ScriptFunction` previously
    saved with :func:`torch.jit.save <torch.jit.save>`

    All previously saved modules, no matter their device, are first loaded onto CPU,
    and then are moved to the devices they were saved from. If this fails (e.g.
    because the run time system doesn't have certain devices), an exception is
    raised.

    Args:
        f: a file-like object (has to implement read, readline, tell, and seek),
            or a string containing a file name
        map_location (string or torch.device): A simplified version of
            ``map_location`` in `torch.jit.save` used to dynamically remap
            storages to an alternative set of devices.
        _extra_files (dictionary of filename to content): The extra
            filenames given in the map would be loaded and their content
            would be stored in the provided map.

    Returns:
        A :class:`ScriptModule` object.

    Example:

    .. testcode::

        import torch
        import io

        torch.jit.load('scriptmodule.pt')

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

        # Load all tensors to the original device
        torch.jit.load(buffer)

        # Load all tensors onto CPU, using a device
        buffer.seek(0)
        torch.jit.load(buffer, map_location=torch.device('cpu'))

        # Load all tensors onto CPU, using a string
        buffer.seek(0)
        torch.jit.load(buffer, map_location='cpu')

        # Load with extra files.
        extra_files = {'foo.txt': ''}  # values will be replaced with data
        torch.jit.load('scriptmodule.pt', _extra_files=extra_files)
        print(extra_files['foo.txt'])

    .. testoutput::
        :hide:

        ...

    .. testcleanup::

        import os
        os.remove("scriptmodule.pt")
    'The provided filename {} does not exist'The provided filename {} is a directoryN)r   r   ospathexists
ValueErrorformatisdirvalidate_map_locationtorch_CCompilationUnitr   r   r	   import_ir_moduleimport_ir_module_from_bufferreadr   )r   map_locationr   cu
cpp_moduler   r   r   loadW   s    ?

r$   c                 C   sX   t | trt| } n(| d u s>t | tjs>tdtt|  t| drTt|  | S )NzJmap_location should be either None, string or torch.device, but got type: cuda)r   r   r   devicer   type
startswithr   )r!   r   r   r   r      s    

r   c                  C   s4   zdd l m}  | W S  ty.   td  Y n0 d S )Nr   z4Please include //caffe2:_C_flatbuffer as dependency.)Ztorch._C_flatbufferZ_C_flatbufferImportErrorprint)ffr   r   r   get_ff_module   s    r,   c                 C   s   t  }t| trDtj| s*td| tj| rDtd| t| t	sZt| t
jrpt	| } t|| S t||  S d S )Nr   r   )r,   r   r   r   r   r   r   r   r   r   r   r	   r   Z_load_jit_module_from_fileZ_load_jit_module_from_bytesr    )r   r+   r   r   r   jit_module_from_flatbuffer   s    
r-   c                 C   sb   |}|du ri }t  }t|ts,t|tjrFt|}|| j|| n|| j|}|| dS )a  
    Save an offline version of this module for use in a separate process. The
    saved module serializes all of the methods, submodules, parameters, and
    attributes of this module. It can be loaded into the C++ API using
    ``torch::jit::load_jit_module_from_file(filename)`` or into the Python API with
    :func:`torch.jit.jit_module_from_flatbuffer<torch.jit.jit_module_from_flatbuffer>`.

    To be able to save a module, it must not make any calls to native Python
    functions.  This means that all submodules must be subclasses of
    :class:`ScriptModule` as well.

    .. DANGER::
        All modules, no matter their device, are always loaded onto the CPU
        during loading.  This is different from :func:`torch.load`'s semantics
        and may change in the future.

    Args:
        m: A :class:`ScriptModule` to save.
        f: A string for file path


    Example:

    .. testcode::

        import torch
        import io

        class MyModule(torch.nn.Module):
            def forward(self, x):
                return x + 10

        m = torch.jit.script(MyModule())

        # Save to file
        torch.jit.save_jit_module_to_flatbuffer(m, 'scriptmodule.ff')
    N)	r,   r   r   r   r	   Z_save_jit_module_cZ_save_jit_module_to_bytesr   )r   r   r   extra_filesr+   sr   r   r   save_jit_module_to_flatbuffer   s    'r1   c                 C   sb   t  }t| tst| tjrPt| d}| }W d   qX1 sD0    Y  n|  }||S )a  Get some information regarding a model file in flatbuffer format.


    Args:
        path_or_file: Either str, Path or file like object (BytesIO OK).
            If it's str or Path, we will read the file referenced by that
            path as Bytes.

    Returns:
        A dict with metadata on what that file contains, currently looks like
        this:
        {
            'bytecode_version': 4,  # int
            'operator_version': 4,  # int
            'function_names': {
                '__torch__.___torch_mangle_0.Foo.forward'}, # set
            'type_names': set(),  # set
            'opname_to_num_args': {'aten::linear': 3} # Dict[str, int]
        }
    rbN)r,   r   r   r   r	   openr    Z _get_module_info_from_flatbuffer)Zpath_or_filer+   r   Z	all_bytesr   r   r   get_flatbuffer_module_info  s    (r4   )N)NN)N)N)__doc__r   r   r   
torch._sixr   torch.jit._recursiver   Ztorch.serializationr   r
   r$   r   r,   r-   r1   r4   r   r   r   r   <module>   s   	
D
U


4