o
    h4K                     @   s(  d dl Z d dlmZmZ d dlmZmZ d dlmZm	Z	 ddgZ
daded	dfd
dZdd Zd$dedee	eee jf  d	efddZded	dfddZded	dfddZded	dfddZ	d$dedeee j  d	dfddZ				d%dededededeee j  d	dfd dZd!efd"d#ZdS )&    N)handle_torch_functionhas_torch_function_unary)_rename_privateuse1_backend_get_privateuse1_backend_name)OptionalUnionrename_privateuse1_backend(generate_methods_for_privateuse1_backendprivateuseonebackend_namereturnc                 C   s   t |  | adS )a	  
    Rename the privateuse1 backend device to make it more convenient to use as a device name within PyTorch APIs.

    The steps are:

    (1) (In C++) implement kernels for various torch operations, and register them
        to the PrivateUse1 dispatch key.
    (2) (In python) call torch.utils.rename_privateuse1_backend("foo")

    You can now use "foo" as an ordinary device string in python.

    Note: this API can only be called once per process. Attempting to change
    the external backend after it's already been set will result in an error.

    Note(AMP): If you want to support AMP on your device, you can register a custom backend module.
    The backend must register a custom backend module with ``torch._register_device_module("foo", BackendModule)``.
    BackendModule needs to have the following API's:

    (1) ``get_amp_supported_dtype() -> List[torch.dtype]``
        get the supported dtypes on your "foo" device in AMP, maybe the "foo" device supports one more dtype.

    Note(random): If you want to support to set seed for your device, BackendModule needs to have the following API's:

    (1) ``_is_in_bad_fork() -> bool``
        Return ``True`` if now it is in bad_fork, else return ``False``.

    (2) ``manual_seed_all(seed int) -> None``
        Sets the seed for generating random numbers for your devices.

    (3) ``device_count() -> int``
        Returns the number of "foo"s available.

    (4) ``get_rng_state(device: Union[int, str, torch.device] = 'foo') -> Tensor``
        Returns a list of ByteTensor representing the random number states of all devices.

    (5) ``set_rng_state(new_state: Tensor, device: Union[int, str, torch.device] = 'foo') -> None``
        Sets the random number generator state of the specified "foo" device.

    And there are some common funcs:

    (1) ``is_available() -> bool``
        Returns a bool indicating if "foo" is currently available.

    (2) ``current_device() -> int``
        Returns the index of a currently selected device.

    For more details, see https://pytorch.org/tutorials/advanced/extend_dispatcher.html#get-a-dispatch-key-for-your-backend
    For an existing example, see https://github.com/bdhirsh/pytorch_open_registration_example

    Example::

        >>> # xdoctest: +SKIP("failing")
        >>> torch.utils.rename_privateuse1_backend("foo")
        # This will work, assuming that you've implemented the right C++ kernels
        # to implement torch.ones.
        >>> a = torch.ones(2, device="foo")

    N)r   _privateuse1_backend_name)r    r   t/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/torch/utils/backend_registration.pyr      s   ;c                 C   s"   t | |rtd|  d| d S )NzThe custom device module of z" has already been registered with )hasattrRuntimeError)moduleattrr   r   r   _check_register_onceQ   s   
r   custom_backend_namedevicec                    sz    fdd}|d u r| S t |trt|}t |tjr9|j kr*td  d|jd u r4| }|S |j}|S |}|S )Nc                     s4   d} t t rt tt | rttt |  S dS )Ncurrent_devicer   )r   torchgetattr)_get_device_indexr   r   r   _get_current_device_indexW   s   
z8_normalization_device.<locals>._get_current_device_indexzInvalid device, must be z device)
isinstancestrr   r   typer   index)r   r   r   
device_idxr   r   r   _normalization_deviceV   s   	



r"   c                    s   t dtjdtf fddttjd   d  j_ttjd   d
dtjdtt	t
tjf  dtjf fdd	ttj   _ttj  d S )Nselfr   c                    s$   t | rtj| f| S | jj kS N)r   r   __get__r   r   r#   )r   wrap_tensor_backendr   r   r'   v   s   zM_generate_tensor_methods_for_privateuse1_backend.<locals>.wrap_tensor_backendis_Fr   c                    sT   t | rt| f| f|dd|S t |}| jdt  d| |d|S )a  Perform Tensor device conversion. Call the to operator implementation.

        .. note::
            If the ``self`` Tensor already
            has the correct :class:`torch.device`, then ``self`` is returned.
            Otherwise, the returned tensor is a copy of ``self`` with the desired :class:`torch.device`.

        Args:
            device (int, optional): if specified, all parameters will be copied to that device
            non_blocking (bool): If ``True`` and the source is in pinned memory,
                the copy will be asynchronous with respect to the host. Otherwise,
                the argument has no effect.
            **kwargs (dict): For compatibility, may contain the key ``memory_format`` argument.
        F)r   non_blocking:Nr   )r   r   r"   tor   r   )r#   r   r)   kwargsr!   )r   wrap_tensor_tor   r   r-      s   
&zH_generate_tensor_methods_for_privateuse1_backend.<locals>.wrap_tensor_toNF)propertyr   Tensorboolr   fget__name__setattrr   r   intr   r   r   )r   r'   r-   r   0_generate_tensor_methods_for_privateuse1_backendu   s   r6   c                    s   t tj std  d  d	 d	dtjjjjdtt	t
tjf  dtjjjjf fdd}ttjj  ttjj | d S )
NzCan not automatically generate zJ() method for torch.nn.Module.Because torch.Tensor doesn't has the method 7().For this error, you can try setting for_tensor=True.r#   r   r   c                    s   |   fddS )  Move all model parameters and buffers to the custom device.

        This also makes associated parameters and buffers different objects. So
        it should be called before constructing optimizer if the module will
        live on device while being optimized.

        .. note::
            This method modifies the module in-place.

        Args:
            device (int, optional): if specified, all parameters will be copied to that device
        c                    s   t |  S r$   )r   )t)r   r   r   r   <lambda>   s    zZ_generate_module_methods_for_privateuse1_backend.<locals>.wrap_module_to.<locals>.<lambda>)_apply)r#   r   r   r   r   wrap_module_to   s   zH_generate_module_methods_for_privateuse1_backend.<locals>.wrap_module_tor$   )r   r   r0   r   nnmodulesr   Tr   r   r5   r   r   Moduler4   )r   r=   r   r   r   0_generate_module_methods_for_privateuse1_backend   s   
rB   c              
      s   t tjd  rt tj s td  d  d  d  d	tdtjjjjdt	f fd	d
}t
tjjjjd   ttjjjjd  | dtjjjjdtjjjjf fdd}t
tjjjj  ttjjjj | d S )Nr(   z"Can not automatically generate is_z() or z_() method for torch.nn.utils.rnn.PackedSequence.Because torch.Tensor doesn't has the method is_z()or r7   r#   r   c                    s   | j jj kS r$   )datar   r   r&   r   r   r   r'      s   zV_generate_packed_sequence_methods_for_privateuse1_backend.<locals>.wrap_tensor_backendc                    s^   t jd| jj| jjdj|i |}|jj kr | j|i |S |d i | j|i |S )r8   r   )dtyper   r   )r   tensorrC   rD   r   r+   r   update)r#   argsr,   exr   r   r   r=      s
   $zQ_generate_packed_sequence_methods_for_privateuse1_backend.<locals>.wrap_module_to)r   r   r0   r   r/   r>   utilsrnnPackedSequencer1   r   r4   )r   r'   r=   r   r   r   9_generate_packed_sequence_methods_for_privateuse1_backend   s*   
 
rL   unsupported_dtypec                    s   t dtjjdtf fdd}ttjjd   ttjjd  | d fdd	}ttjj  ttjj | t dtjjdtf fd	d
}ttjd   ttjjd  | 	ddtjjdtjjf fdd}ttj  ttj | d S )Nr#   r   c                    s   | j j kS )z2Return the internal :class:`torch.UntypedStorage`.)r   r   r&   r   r   r   wrap_storage_backend   s   zO_generate_storage_methods_for_privateuse1_backend.<locals>.wrap_storage_backendr(   Fc                    sr   t  |}t| d  r|  |kr| S | jr td  dtj|  t  d| d}|	| | |S )a   Return a copy of this object in custom device memory.

        If this object is already in device memory and on the correct device, then
        no copy is performed and the original object is returned.

        Args:
            device (int): The destination device id. Defaults to the current device.
            non_blocking (bool): If ``True`` and the source is in pinned memory,
            the copy will be asynchronous with respect to the host. Otherwise,
            the argument has no effect.
        r(   z)Can not support a sparse storage move to z backendr*   r<   )
r"   r   
get_device	is_sparser   r   UntypedStoragesizer   copy_)r#   r   r)   r!   untyped_storager   r   r   wrap_storage_to   s   
zJ_generate_storage_methods_for_privateuse1_backend.<locals>.wrap_storage_toc                    s   t j  | jjj kS r$   )r   storage_warn_typed_storage_removal_untyped_storager   r   r&   r   r   r   wrap_typed_storage_backend  s   
zU_generate_storage_methods_for_privateuse1_backend.<locals>.wrap_typed_storage_backendc                    sT   t j  r| jv rtd  d| j dt| j ||fi |}| |S )NzCannot create z storage as z' dtype is not supported by this backend)r   rV   rW   rD   r   r   rX   _new_wrapped_storage)r#   r   r)   r,   custom_backend_storager   rM   r   r   wrap_typed_storage_to  s   



zP_generate_storage_methods_for_privateuse1_backend.<locals>.wrap_typed_storage_tor.   )r/   r   rV   _StorageBaser1   r   r4   TypedStorage)r   rM   rN   rU   rY   r]   r   r\   r   1_generate_storage_methods_for_privateuse1_backend   s"   
r`   TF
for_tensor
for_modulefor_packed_sequencefor_storagec                 C   s@   t  }| r	t| |rt| |rt|| |rt| dS dS )a  
    Automatically generate attributes and methods for the custom backend after rename privateuse1 backend.

    In the default scenario, storage-related methods will not be generated automatically.

    When you implement kernels for various torch operations, and register them to the PrivateUse1 dispatch key.
    And call the function torch.rename_privateuse1_backend("foo") to rename your backend name.
    At this point, you can easily register specific methods and attributes by calling this function.
    Just like torch.Tensor.foo(), torch.Tensor.is_foo, torch.Storage.foo(), torch.Storage.is_foo.

    Note: We recommend you use generic functions (check devices are equal or to(device=)).
    We provide these methods for convenience only and they will be "monkey patched" onto the objects
    and so will not be properly typed. For Storage methods generate, if you need to support sparse data storage,
    you need to extend the implementation yourself.

    Args:
        for_tensor (bool): whether register related methods for torch.Tensor class.
        for_module (bool): whether register related methods for torch.nn.Module class.
        for_storage (bool): whether register related methods for torch.Storage class.
        unsupported_dtype (List[torch.dtype]): takes effect only when the storage method needs to be generated,
            indicating that the storage does not support the torch.dtype type.

    Example::

        >>> # xdoctest: +SKIP("failing")
        >>> torch.utils.rename_privateuse1_backend("foo")
        >>> torch.utils.generate_methods_for_privateuse1_backend()
        # Then automatically generate backend-related attributes and methods.
        >>> a = torch.tensor(2).foo()
        >>> a.is_foo
        >>> hasattr(torch.nn.Module, 'foo')
    N)r   r6   rB   r`   rL   )ra   rb   rc   rd   rM   r   r   r   r   r	   '  s   $
	func_namec                 C   s   t | tsJ dt|  dt }tt|d}t|| d}|du s&|du rCd| d|  d}|d| d7 }|d	|  d
7 }t||S )aW  
    Return the func named `func_name` defined in custom device module. If not defined,
    return `None`. And the func is registered with `torch.utils.rename_privateuse1_backend('foo')`
    and `torch._register_device_module('foo', BackendModule)`.
    If the custom device module or the func is not defined, it will give warning or error message.
    Args:
        func_name (str): return the callable func named func_name defined in custom device module.
    Example::
        class DummyfooModule:
            @staticmethod
            def is_available():
                return True
            @staticmethod
            def func_name(*args, **kwargs):
                ....
        torch.utils.rename_privateuse1_backend("foo")
        torch._register_device_module("foo", DummyfooModule)
        foo_is_available_func = torch.utils.backend_registration._get_custom_mod_func("is_available")
        if foo_is_available_func:
            foo_is_available = foo_is_available_func()
        func_ = torch.utils.backend_registration._get_custom_mod_func("func_name")
        if func_:
            result = func_(*args, **kwargs)
    Attention: This function is not meant to be used directly by users, which is why
    it is marked as private. It is a convenience function for backend implementers to
    more easily call the hooks into their backend extensions.
    z"func_name must be `str`, but got `z`.NzTry to call torch..z-. The backend must register a custom backend z,module with `torch._register_device_module('z', BackendModule)`. And z3BackendModule needs to have the following API's:
 `z(*args, **kwargs)`. 
)r   r   r   r   r   r   r   )re   r   custom_device_modfunctionmessager   r   r   _get_custom_mod_funcY  s   rj   r$   )TTTFN)r   torch.overridesr   r   torch._Cr   r   typingr   r   __all__r   r   r   r   r5   r   r"   r6   rB   rL   listrD   r`   r1   r	   rj   r   r   r   r   <module>   s@   ?(&+
F
2