a
    Ž¥»a¥  ã                   @   s”   d Z ddlZddlmZmZmZmZmZ ddlZdgZ	ddddddœeej
ee eedf f eeejf ejee ee eeeejdœ	d	d„ZdS )
z1
This module contains tensor creation utilities.
é    N)ÚOptionalÚListÚTupleÚUnionÚcastÚmake_tensorF)ÚlowÚhighÚrequires_gradÚnoncontiguousÚexclude_zero.)	ÚshapeÚdeviceÚdtyper   r	   r
   r   r   Úreturnc             
   C   s4  dd„ }t jt jt jt jt jg}	t jt jt jt j	g}
t j
t jg}|t ju rbt jdd| ||d�}�nê|t ju rÊt  |¡jt  |¡jf}ttttf ||||d |d dd|ƒƒ\}}t j||| ||d�}�n‚||	v �r$t  |¡jt  |¡jf}||||d |d dd|ƒ\}}t j||| ||d�}�n(||
v �rŒt  |¡jt  |¡jf}||||d |d dd	|ƒ\}}t j| ||d�}|| |d|   }nÀ||v �r<|t j
u �r¨t jnt j}t  |¡jt  |¡jf}||||d |d dd	|ƒ\}}t j| ||d�}t j| ||d�}|| |d|   }|| |d|   }t  ||¡}ntd
|› d�ƒ‚|�r‚| ¡ dk�r‚t j|ddd�}|dddd…f }|�r||	v �sž|t ju �r°t jd||d�}n`||
v �rÔt jt  |¡j||d�}n<|t j
u �ræt jnt j}t jt  |¡j||d�}t  ||¡}|||dk< ||
| v �r0||_|S )a‘  Creates a tensor with the given :attr:`shape`, :attr:`device`, and :attr:`dtype`, and filled with
    values uniformly drawn from ``[low, high)``.

    If :attr:`low` or :attr:`high` are specified and are outside the range of the :attr:`dtype`'s representable
    finite values then they are clamped to the lowest or highest representable finite value, respectively.
    If ``None``, then the following table describes the default values for :attr:`low` and :attr:`high`,
    which depend on :attr:`dtype`.

    +---------------------------+------------+----------+
    | ``dtype``                 | ``low``    | ``high`` |
    +===========================+============+==========+
    | boolean type              | ``0``      | ``2``    |
    +---------------------------+------------+----------+
    | unsigned integral type    | ``0``      | ``10``   |
    +---------------------------+------------+----------+
    | signed integral types     | ``-9``     | ``10``   |
    +---------------------------+------------+----------+
    | floating types            | ``-9``     | ``9``    |
    +---------------------------+------------+----------+
    | complex types             | ``-9``     | ``9``    |
    +---------------------------+------------+----------+

    Args:
        shape (Tuple[int, ...]): A sequence of integers defining the shape of the output tensor.
        device (Union[str, torch.device]): The device of the returned tensor.
        dtype (:class:`torch.dtype`): The data type of the returned tensor.
        low (Optional[Number]): Sets the lower limit (inclusive) of the given range. If a number is provided it is
            clamped to the least representable finite value of the given dtype. When ``None`` (default),
            this value is determined based on the :attr:`dtype` (see the table above). Default: ``None``.
        high (Optional[Number]): Sets the upper limit (exclusive) of the given range. If a number is provided it is
            clamped to the greatest representable finite value of the given dtype. When ``None`` (default) this value
            is determined based on the :attr:`dtype` (see the table above). Default: ``None``.
        requires_grad (Optional[bool]): If autograd should record operations on the returned tensor. Default: ``False``.
        noncontiguous (Optional[bool]): If `True`, the returned tensor will be noncontiguous. This argument is
            ignored if the constructed tensor has fewer than two elements.
        exclude_zero (Optional[bool]): If ``True`` then zeros are replaced with the dtype's small positive value
            depending on the :attr:`dtype`. For bool and integer types zero is replaced with one. For floating
            point types it is replaced with the dtype's smallest positive normal number (the "tiny" value of the
            :attr:`dtype`'s :func:`~torch.finfo` object), and for complex types it is replaced with a complex number
            whose real and imaginary parts are both the smallest positive normal number representable by the complex
            type. Default ``False``.

    Raises:
        ValueError: If ``low > high``.
        ValueError: If either :attr:`low` or :attr:`high` is ``nan``.
        TypeError: If :attr:`dtype` isn't supported by this function.

    Examples:
        >>> from torch.testing import make_tensor
        >>> # Creates a float tensor with values in [-1, 1)
        >>> make_tensor((3,), device='cpu', dtype=torch.float32, low=-1, high=1)
        tensor([ 0.1205, 0.2282, -0.6380])
        >>> # Creates a bool tensor on CUDA
        >>> make_tensor((2, 2), device='cuda', dtype=torch.bool)
        tensor([[False, False],
                [False, True]], device='cuda:0')
    c                 S   s    dd„ }| dur| n|} |dur$|n|}| | ks8||kr@t dƒ‚| |krPt dƒ‚|| ||ƒ} ||||ƒ}|tjtjtjtjtjfv r˜t | ¡t 	|¡fS | |fS )zŽ
        Modifies (and raises ValueError when appropriate) low and high values given by the user (input_low, input_high) if required.
        c                 S   s   t t| |ƒ|ƒS )N)ÚminÚmax)ÚaÚlÚh© r   úQ/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/testing/_creation.pyÚclampU   s    z4make_tensor.<locals>._modify_low_high.<locals>.clampNz(make_tensor: one of low or high was NaN!z/make_tensor: low must be weakly less than high!)
Ú
ValueErrorÚtorchÚuint8Úint8Úint16Úint32Úint64ÚmathÚfloorÚceil)r   r	   ZlowestZhighestZdefault_lowZdefault_highr   r   r   r   r   Ú_modify_low_highQ   s    z%make_tensor.<locals>._modify_low_highr   é   )r   r   é   é
   i÷ÿÿÿé	   zThe requested dtype 'z‚' is not supported by torch.testing.make_tensor(). To request support, file an issue at: https://github.com/pytorch/pytorch/issueséÿÿÿÿ)Zdim.N)r   r   r   r   r   r   Úfloat16Zbfloat16Úfloat32Úfloat64ZcfloatZcdoubleÚboolÚrandintZiinfor   r   r   r   ÚintZfinfoZrandÚfloatÚdoubleÚcomplexÚ	TypeErrorZnumelZrepeat_interleaveZtensorZtinyr
   )r   r   r   r   r	   r
   r   r   r#   Z_integral_typesZ_floating_typesZ_complex_typesÚresultÚrangesZranges_floatsZrand_valZfloat_dtypeZreal_rand_valZimag_rand_valÚrealÚimagZreplace_withZ	float_epsr   r   r   r      sX    D

.
 
 
 
)Ú__doc__r   Útypingr   r   r   r   r   r    Ú__all__ÚSizer.   Ústrr   r   r/   r,   ZTensorr   r   r   r   r   Ú<module>   s*   ÿ	÷ö