o
    ٷiP%                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlmZ d dl	Z	e 
eZG dd dejZ				dded	ed
edededefddZ	dded	efddZdS )    N)SAM2Base)compare_tensors_with_tolerancerandom_sam2_input_image)nnc                
       sR   e Zd Zdeddf fddZ	ddejdedeejejejf fd	d
Z	  Z
S )SAM2ImageEncoder	sam_modelreturnNc                    s$   t    || _|j| _|j| _d S )N)super__init__modelimage_encoderno_mem_embed)selfr   	__class__ f/home/ubuntu/.local/lib/python3.10/site-packages/onnxruntime/transformers/models/sam2/image_encoder.pyr
      s   
zSAM2ImageEncoder.__init__Fimageenable_nvtx_profilec                 C   sT  d}|rddl m} |ddg}|dur|d | |}|dur,|d |d | jj|d d |d d< | jj|d d |d d< |d | jj	 d }|d | jj	 d }d	d
 |D }dd
 |D }	|	d | j
 |	d< dd
 t|	ddd |ddd ddD ddd }
|dur|d |  |
d |
d |
d fS )a  
        Encodes images into features.

        Only supports H=W=1024. If you want to use different image sizes like 512x512,
        see https://github.com/facebookresearch/segment-anything-2/issues/138.

        Args:
            image (torch.Tensor): images of shape [B, 3, H, W], B is batch size, H and W are height and width.
            enable_nvtx_profile (bool): enable NVTX profiling.

        Returns:
            image_features_0: image features of shape [B, 32, H/4, W/4] - high resolution features of level 0
            image_features_1: image features of shape [B, 64, H/8, W/8] - high resolution features of level 1
            image_embeddings: image features of shape [B, 256, H/16, W/16] - 16 is the backbone_stride
        Nr   )
NvtxHelperr   post_processbackbone_fpn   vision_pos_encc                 S   s    g | ]}|j d  |j d fqS ))shape.0xr   r   r   
<listcomp>D        z,SAM2ImageEncoder.forward.<locals>.<listcomp>c                 S   s    g | ]}| d d ddqS )   r   r   )flattenpermuter   r   r   r   r    H   r!   r   c                 S   s.   g | ]\}}| d ddjd dg|R  qS )r   r"   r   r   )r$   reshape)r   feat	feat_sizer   r   r   r    L   s    F)strictr"   )nvtx_helperr   start_profiler   stop_profiler   sam_mask_decoderconv_s0conv_s1num_feature_levelsr   zipprint_latency)r   r   r   r)   r   backbone_outfeature_mapsvision_pos_embeds
feat_sizesvision_featsfeatsr   r   r   forward   s4   



 
zSAM2ImageEncoder.forwardF)__name__
__module____qualname__r   r
   torchTensorbooltupler8   __classcell__r   r   r   r   r      s    	r   F
sam2_modelonnx_model_pathdynamic_batch_axesverbosedynamoclear_dynamo_metadatac                 C   s@  t  }t|  }||\}}	}
td|j td|j td|	j td|
j d }|r?ddiddiddiddid}t  |sVtjdt	j
jd	 tjdtd	 |skt	jj|||d
dd
dgg d|d	 nd
t	jj_t	jj||fddt	jjjigd}t	jj|dddgg dd
d}|  |j|d dd dd l}ddlm} |j|d d
d}|rd|jjd jjjjd _tdD ]}d|jj | jjjjd _q||}|!  |r|"  dd l#}|j$%|r|&| |j$%|d r|&|d  |j'j(|d
d
d
d W d    n	1 sw   Y  t)d| d S )Nimage.shape: %simage_features_0.shape: %simage_features_1.shape: %simage_embeddings.shape: %sr   
batch_size)r   image_features_0image_features_1image_embeddingsignore)categoryT   r   )rM   rN   rO   )export_paramsopset_versiondo_constant_foldinginput_namesoutput_namesdynamic_axesF)argsr(   dynamic_shapesr   )rT   rV   rW   rF   z.dynamo.onnx)external_data)DynamoOnnxHelper)load_external_data   z.data)use_external_data_formatall_tensors_to_one_fileconvert_attributezencoder onnx model saved to)*r   r   cpuloggerinfor   warningscatch_warningsfilterwarningsr=   jitTracerWarningUserWarningonnxexport_dynamoconfigcapture_scalar_outputsDimAUTOoptimizesave+onnxruntime.transformers.dynamo_onnx_helperr\   
load_modelgraphinputtypetensor_typedim	dim_paramrangeoutput!convert_constants_to_initializersclear_metadataospathexistsremover   save_model_to_fileprint)rB   rC   rD   rE   rF   rG   r   sam2_encoderrM   rN   rO   rX   eponnx_programrk   r\   
onnx_modelionnx_model_helperr   r   r   r   export_image_encoder_onnxX   s   

	
@r   c                    s  t j|dgd}|   fddtt D }td| | fddttD }td| |r<dd	gndg}|D ]}t|}t	| 
 }	|	| \}
}}td
|j td|
j td|j td|j ||d| i}t|D ]\}}td||| j q|\}}}td|
t|ddrtd|t|ddrtd|t|ddrtd| d|  qAtd| d|  qAd S )NCPUExecutionProvider)	providersc                       g | ]} | j qS r   namer   r   )model_inputsr   r   r           z+test_image_encoder_onnx.<locals>.<listcomp>zinput_names: %sc                    r   r   r   r   )model_outputsr   r   r       r   zoutput_names: %sr   r"   rH   rI   rJ   rK   r   zoutput %s shape %srM   )mismatch_percentage_tolerancerN   rO   z,onnx model has been verified for batch_size=z: z.onnx model verification failed for batch_size=)onnxruntimeInferenceSession
get_inputsr|   lenrc   rd   get_outputsr   r   rb   cloner   runnumpy	enumerater   r=   tensorr   )rB   rC   rD   ort_sessionrV   rW   batch_sizesrL   r   r   rM   rN   rO   outputsr   output_nameort_image_features_0ort_image_features_1ort_image_embeddingsr   )r   r   r   test_image_encoder_onnx   sX   
r   )FFFFr9   )loggingre   r=   sam2.modeling.sam2_baser   
sam2_utilsr   r   r   r   	getLoggerr:   rc   Moduler   strr?   r   r   r   r   r   r   <module>   s@   
I
`