U
    ‰d¹  ã                   @   sf   d dl Z d dlmZmZ ee j e jdœdd„Ze jee j ddœdd„Ze jee ed	œd
d„ZdS )é    N)ÚIterableÚOptional)Ú
parametersÚreturnc                 C   s6   d}g }| D ]}t ||ƒ}| | d¡¡ qt |¡S )zâConvert parameters to one vector

    Args:
        parameters (Iterable[Tensor]): an iterator of Tensors that are the
            parameters of a model.

    Returns:
        The parameters represented by a single vector
    Néÿÿÿÿ)Ú_check_param_deviceÚappendÚviewÚtorchÚcat)r   Úparam_deviceÚvecÚparam© r   úE/tmp/pip-unpacked-wheel-ua33x9lu/torch/nn/utils/convert_parameters.pyÚparameters_to_vector   s    
r   )r   r   r   c                 C   sj   t | tjƒs td t | ¡¡ƒ‚d}d}|D ]8}t||ƒ}| ¡ }| ||| …  |¡j	|_	||7 }q,dS )zîConvert one vector to the parameters

    Args:
        vec (Tensor): a single vector represents the parameters of a model.
        parameters (Iterable[Tensor]): an iterator of Tensors that are the
            parameters of a model.
    z"expected torch.Tensor, but got: {}Nr   )
Ú
isinstancer
   ÚTensorÚ	TypeErrorÚformatÚtypenamer   ZnumelZview_asÚdata)r   r   r   Úpointerr   Z	num_paramr   r   r   Úvector_to_parameters   s    	ÿ
r   )r   Úold_param_devicer   c                 C   sL   |dkr| j r|  ¡ nd}n,d}| j r4|  ¡ |k}n|dk}|rHtdƒ‚|S )a'  This helper function is to check if the parameters are located
    in the same device. Currently, the conversion between model parameters
    and single vector form is not supported for multiple allocations,
    e.g. parameters in different GPUs, or mixture of CPU/GPU.

    Args:
        param ([Tensor]): a Tensor of a parameter of a model
        old_param_device (int): the device where the first parameter of a
                                model is allocated.

    Returns:
        old_param_device (int): report device for the first time
    Nr   FzKFound two parameters on different devices, this is currently not supported.)Zis_cudaZ
get_devicer   )r   r   Úwarnr   r   r   r   9   s    r   )	r
   Útypingr   r   r   r   r   Úintr   r   r   r   r   Ú<module>   s   