
    j
                    b    d dl mZ d dlmZ d dlZd dlZd dlmZ ddl	m
Z
  G d de
          ZdS )	    )annotations)PathN)LOGGER   )BaseBackendc                  "    e Zd ZdZddZdd
ZdS )DeepXBackendzDeepX NPU inference backend for DeepX hardware accelerators.

    Loads compiled DeepX models (.dxnn files) and runs inference using the DeepX DX-Runtime.
    weight
str | PathreturnNonec                   	 ddl m} n"# t          $ r}t          d          |d}~ww xY wt          j        d| d           t          |          }t          |                    d          d          }|t          d|            |t          |                    | _
        |j        d	z  }|                                r0dd
lm} |                     |                    |                     dS dS )ae  Load a DeepX model from a directory containing a .dxnn file.

        Args:
            weight (str | Path): Path to the DeepX model directory containing the .dxnn binary.

        Raises:
            ImportError: If the ``dx_engine`` Python package is not installed.
            FileNotFoundError: If no .dxnn file is found in the given directory.
        r   )InferenceEnginezDeepX inference requires the DeepX DX-Runtime and `dx_engine` Python package. See https://docs.ultralytics.com/integrations/deepx/#runtime-installation for installation instructions.NzLoading z for DeepX inference...z*.dxnnzNo .dxnn file found in: zmetadata.yaml)YAML)	dx_enginer   ImportErrorr   infor   nextrglobFileNotFoundErrorstrmodelparentexistsultralytics.utilsr   apply_metadataload)selfr
   r   ewfoundmetadata_filer   s           `/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/ultralytics/nn/backends/deepx.py
load_modelzDeepXBackend.load_model   s3   	1111111 	 	 	{  	 	>v>>>???LLQWWX&&--=#$Bq$B$BCCC$_SZZ00
 6!! 	:......		- 8 899999	: 	:s   	 
(#(imtorch.Tensornp.ndarray | list[np.ndarray]c           	        g }|                                                                 D ]}t          j        t          j        t          j        |d          dz  dd                              t          j                            }t          t          t          j
        | j                            |g                              D ]h\  }}|t          |          k    r|                    g            ||                             |j        r|j        d         dk    r|n|d                    i	d |D             }t          |          dk    r|d         n|S )a  Run inference on the DeepX NPU.

        Converts each image from BCHW float [0, 1] to HWC uint8 [0, 255] per the DeepX runtime contract,
        runs the engine per image, then stacks outputs along the batch dimension.

        Args:
            im (torch.Tensor): Input image tensor in BCHW format, normalized to [0, 1].

        Returns:
            (np.ndarray | list[np.ndarray]): Model predictions as a single array or list of arrays.
        )r      r      r   r   Nc                :    g | ]}t          j        |d           S )r   )axis)npconcatenate).0xs     r#   
<listcomp>z(DeepXBackend.forward.<locals>.<listcomp>J   s'    8881R^AA&&&888    )cpunumpyr-   ascontiguousarrayclip	transposeastypeuint8	enumeratemapasarrayr   runlenappendndimshape)r   r%   outputssampleioutys          r#   forwardzDeepXBackend.forward7   s>    ffhhnn&& 	X 	XF)"'",vy2Q2QTW2WYZ\_*`*`*g*ghjhp*q*qrrF#C
DJNNF84L4L$M$MNN X X3G$$NN2&&&
!!"Vcila6G6G##SQUYWWWWX 988881vv{{qtt)r2   N)r
   r   r   r   )r%   r&   r   r'   )__name__
__module____qualname____doc__r$   rG    r2   r#   r	   r	      sG         
 :  :  :  :D* * * * * *r2   r	   )
__future__r   pathlibr   r4   r-   torchr   r   baser   r	   rL   r2   r#   <module>rQ      s    # " " " " "            $ $ $ $ $ $      <* <* <* <* <*; <* <* <* <* <*r2   