a
    d&                     @   s   d Z ddlZddlZddlmZmZmZmZm	Z	 ddl
Z
e
je
je
je
je
je
jiZdd e D Ze
jeedddZddd	d	d	dd
e	ee
jee eedf f e
je	ee
jf ee ee eeeee
j e
jd
ddZdS )z1
This module contains tensor creation utilities.
    N)castListOptionalTupleUnionc                 C   s   i | ]\}}||qS  r   ).0kvr   r   `/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torch/testing/_creation.py
<dictcomp>   s   r   tlowhighc                 C   s@   || t | jjkr0| |d |d dS | ||S d S )N   )torchfinfodtypemaxZuniform_Zmul_r   r   r   r   _uniform_random   s    r   F)r   r   requires_gradnoncontiguousexclude_zeromemory_format.)
shaper   devicer   r   r   r   r   r   returnc              
   G   sf  dd }	t |dkr.t|d tjjr.|d }tttdf t|}t	j
t	jt	jt	jt	jg}
t	jt	jt	jt	jg}t	jt	jt	jg}|r| |vr| |vrtd| t	ju rt	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 rt	| jt	| jf}|	|||d |d d
d	| \}}t	j||||| d}n| |v rt	| jt	| jf}|	|||d |d d
d| \}}t	j||| d}t||| n| |v rRt|  }t	|jt	|jf}|	|||d |d d
d| \}}t	j||| d}t	 |}t||| nt!d|  d|rv|durvJ |r|" dkrt	j#|ddd}|ddddf }n|dur|j$|d}|rN| |
v s| t	ju rt	j%d|| d}nP| |v rt	j%t	| j&|| d}n,t|  }t	j%t	|j&||d}t	'||}|||dk< | || v rb||_(|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, ...]): Single integer or a sequence of integers defining the shape of the output tensor.
        dtype (:class:`torch.dtype`): The data type of the returned tensor.
        device (Union[str, torch.device]): The device 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``.
        memory_format (Optional[torch.memory_format]): The memory format of the returned tensor.  Incompatible
            with :attr:`noncontiguous`.

    Raises:
        ValueError: if ``requires_grad=True`` is passed for integral `dtype`
        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:
        >>> # xdoctest: +SKIP
        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
        >>> 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)
        >>> # xdoctest: +SKIP
        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 rt| 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)minr   )alhr   r   r   clampo   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!)

ValueErrorr   uint8int8int16int32int64mathfloorceil)r   r   ZlowestZhighestZdefault_lowZdefault_highr   r"   r   r   r   _modify_low_highj   s    z%make_tensor.<locals>._modify_low_high   r   .z;make_tensor: requires_grad must be False for integral dtyper   )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/issuesN)Zdim)r   ))len
isinstancecollectionsabcSequencer   r   inttupler   r$   r%   r&   r'   r(   float16Zbfloat16float32float64	complex32	complex64
complex128r#   boolrandintZiinfor   r   r   emptyr   'complex_to_corresponding_float_type_mapZview_as_real	TypeErrorZnumelZrepeat_interleavecloneZtensorZtinycomplexr   )r   r   r   r   r   r   r   r   r   r,   Z_integral_typesZ_floating_typesZ_complex_typesresultrangesZranges_floatsZm_lowZm_highZfloat_dtypeZresult_realZreplace_withZ	float_epsr   r   r   make_tensor   s~    K


 





rG   )__doc__collections.abcr3   r)   typingr   r   r   r   r   r   r;   r8   r<   r9   r=   r:   rA   itemsZ'float_to_corresponding_complex_type_mapZTensorfloatr   r6   Sizer   strr   r>   r   rG   r   r   r   r   <module>   s<   