o
    ei                     @   s  d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlZdd	lmZ d
dlmZ g dZeddeee
f fddZdefddZdefddZdefddZdefddZdefddZdefddZdefddZdefddZd5d ejjddfd!d"ZG d#d$ d$ZG d%d& d&Ze Z e a!d5d ejjdefd'd(Z"G d)d* d*eZ#d+edefd,d+Z$de%fd-d.Z&d ejjddfd/d0Z'defd1d2Z(defd3d4Z)dS )6zj
This package implements abstractions found in ``torch.cuda``
to facilitate writing device-agnostic code.
    )Mapping)AbstractContextManager)	lru_cache)MappingProxyType)AnyN   device   )amp)is_availableis_initializedsynchronizecurrent_devicecurrent_streamstream
set_devicedevice_countStreamStreamContextEventget_capabilitiesreturnc                   C   s   t tjj S )a  
    Returns an immutable mapping of CPU capabilities detected at runtime.

    This function queries the CPU for supported instruction sets and features
    using cpuinfo. The result is cached after the first call for efficiency.

    The returned mapping contains architecture-specific capabilities:

    For x86/x86_64:
        - SSE family: sse, sse2, sse3, ssse3, sse4_1, sse4_2, sse4a
        - AVX family: avx, avx2, avx_vnni
        - AVX-512 family: avx512_f, avx512_cd, avx512_dq, avx512_bw, avx512_vl,
          avx512_ifma, avx512_vbmi, avx512_vbmi2, avx512_bitalg, avx512_vpopcntdq,
          avx512_vnni, avx512_bf16, avx512_fp16, avx512_vp2intersect,
          avx512_4vnniw, avx512_4fmaps
        - AVX10 family: avx10_1, avx10_2
        - AVX-VNNI-INT: avx_vnni_int8, avx_vnni_int16, avx_ne_convert
        - AMX: amx_bf16, amx_tile, amx_int8, amx_fp16
        - FMA: fma3, fma4
        - Other: f16c, bmi, bmi2, popcnt, lzcnt, aes, sha, clflush, clflushopt, clwb

    For ARM64:
        - SIMD: neon, fp16_arith, bf16, i8mm, dot
        - SVE: sve, sve2, sve_bf16, sve_max_length (when supported)
        - SME: sme, sme2, sme_max_length (when supported)
        - Other: atomics, fhm, rdm, crc32, aes, sha1, sha2, pmull

    Common to all architectures:
        - architecture: string identifying the CPU architecture

    Returns:
        MappingProxyType: An immutable mapping where keys are capability names
        (e.g., 'avx2', 'sve') and values are booleans indicating
        support, or integers for properties like vector lengths.

    Example:
        >>> caps = torch.cpu.get_capabilities()
        >>> if caps.get("avx2", False):
        ...     print("AVX2 is supported")
        >>> print(f"Architecture: {caps['architecture']}")
    )r   torch_C_cpu_get_cpu_capability r   r   \/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/torch/cpu/__init__.pyr   #   s   +r   c                   C      t  ddS )z/Returns a bool indicating if CPU supports AVX2.avx2Fr   getr   r   r   r   _is_avx2_supportedQ      r#   c                   C   r   )z1Returns a bool indicating if CPU supports AVX512.avx512_fFr!   r   r   r   r   _is_avx512_supportedV   r$   r&   c                   C   r   )z6Returns a bool indicating if CPU supports AVX512_BF16.avx512_bf16Fr!   r   r   r   r   _is_avx512_bf16_supported[   r$   r(   c                   C   r   )z/Returns a bool indicating if CPU supports VNNI.avx512_vnniFr!   r   r   r   r   _is_vnni_supported`   s   r*   c                   C   r   )z3Returns a bool indicating if CPU supports AMX_TILE.amx_tileFr!   r   r   r   r   _is_amx_tile_supportedf   r$   r,   c                   C   r   )z3Returns a bool indicating if CPU supports AMX FP16.amx_fp16Fr!   r   r   r   r   _is_amx_fp16_supportedk   r$   r.   c                   C   s   t jj S )zInitializes AMX instructions.)r   r   r   	_init_amxr   r   r   r   r/   p   s   r/   c                   C      dS )zReturns a bool indicating if CPU is currently available.

    N.B. This function only exists to facilitate device-agnostic code

    Tr   r   r   r   r   r   u   s   r   r	   c                 C   r0   )zWaits for all kernels in all streams on the CPU device to complete.

    Args:
        device (torch.device or int, optional): ignored, there's only one CPU device.

    N.B. This function only exists to facilitate device-agnostic code.
    Nr   r   r   r   r   r   ~       r   c                   @   sB   e Zd ZdZddeddfddZddd	Zdd
dZdddZdS )r   zH
    N.B. This class only exists to facilitate device-agnostic code
    priorityr   Nc                 C      d S Nr   )selfr3   r   r   r   __init__      zStream.__init__c                 C   r4   r5   r   r6   r   r   r   r   wait_stream   r8   zStream.wait_streamc                 C   r4   r5   r   r6   r   r   r   record_event   r8   zStream.record_eventc                 C   r4   r5   r   )r6   eventr   r   r   
wait_event   r8   zStream.wait_event)r2   r   N)	__name__
__module____qualname____doc__intr7   r:   r<   r>   r   r   r   r   r      s    

r   c                   @   s<   e Zd ZdefddZddddZdddZddd	d
ZdS )r   r   c                 C   r0   )NTr   r;   r   r   r   query   r8   zEvent.queryNc                 C   r4   r5   r   r9   r   r   r   record   r8   zEvent.recordc                 C   r4   r5   r   r;   r   r   r   r      r8   zEvent.synchronizec                 C   r4   r5   r   r9   r   r   r   wait   r8   z
Event.waitr5   r?   )r@   rA   rB   boolrE   rF   r   rG   r   r   r   r   r      s
    
r   c                 C   s   t S )zReturns the currently selected :class:`Stream` for a given device.

    Args:
        device (torch.device or int, optional): Ignored.

    N.B. This function only exists to facilitate device-agnostic code

    )_current_streamr   r   r   r   r      s   	r   c                   @   sH   e Zd ZU dZedB ed< dd Zdd Zded	ed
eddfddZ	dS )r   zvContext-manager that selects a given stream.

    N.B. This class only exists to facilitate device-agnostic code

    N
cur_streamc                 C   s   || _ t| _d S r5   )r   _default_cpu_streamprev_streamr9   r   r   r   r7      s   
zStreamContext.__init__c                 C   s    | j }|d u r	d S t| _|ad S r5   )r   rI   rL   )r6   rJ   r   r   r   	__enter__   s
   zStreamContext.__enter__typevalue	tracebackr   c                 C   s   | j }|d u r	d S | jad S r5   )r   rL   rI   )r6   rN   rO   rP   rJ   r   r   r   __exit__   s   
zStreamContext.__exit__)
r@   rA   rB   rC   r   __annotations__r7   rM   r   rQ   r   r   r   r   r      s   
 	r   r   c                 C   s   t | S )zWrapper around the Context-manager StreamContext that
    selects a given stream.

    N.B. This function only exists to facilitate device-agnostic code
    )r   )r   r   r   r   r      s   c                   C   r0   )zReturns number of CPU devices (not cores). Always 1.

    N.B. This function only exists to facilitate device-agnostic code
    r
   r   r   r   r   r   r         r   c                 C   r0   )zzSets the current device, in CPU we do nothing.

    N.B. This function only exists to facilitate device-agnostic code
    Nr   r   r   r   r   r      r1   r   c                   C   r0   )zyReturns current device for cpu. Always 'cpu'.

    N.B. This function only exists to facilitate device-agnostic code
    cpur   r   r   r   r   r      rS   r   c                   C   r0   )zReturns True if the CPU is initialized. Always True.

    N.B. This function only exists to facilitate device-agnostic code
    Tr   r   r   r   r   r      rS   r   r5   )*rC   collections.abcr   
contextlibr   	functoolsr   typesr   typingr   r    r	   _devicer   __all__strr   rH   r#   r&   r(   r*   r,   r.   r/   r   Devicer   r   r   rK   rI   r   r   r   rD   r   r   r   r   r   r   r   r   <module>   s@   -	
	