o
    di<                     @   s  d Z ddlmZmZmZmZ ddlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZ  ddl!m"Z" ddl#m$Z$ ddl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ ddl%m,Z- d	d
l.m/Z/m0Z0 e/ r-d	dl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z> d	dl?m@Z@ i dee4fddddee7fddddee5fddddee9fddddee8fddddee;fddddee3fddddee=fdddd ee;fd!ddd"ee:fd#ddd$ee:fd%ddd&ee:fd%ddd'ee<fd(d)dd*ee>fd+d)dd,ee2fd-d.dd/ee6fd0ddZAni ZA	1	1	1	1	1	1	1	2	1	3	1	1	1dLd4eBd5eeeCeBf  d6eBd7eeeBef  d8ed9ee fd:d;ZD	2	1	3	1	1dMd4eBd5eeeCeBf  d6eBd7eeeBef  d8ef
d<d=ZEeEeDd>ZF	1	1	1	1	1	?	1	@	1	1dNdAeBdBee dCeeeBef  dDeeeBe"f  dEeeeBe$f  dFeCd5eeeBeCf  dGeeB d6eeB dHeeC dIefdJdKZd1S )Oz%Pipelines running different backends.    )AnyDictOptionalUnion)AudioClassificationPipeline
AutoConfigAutoFeatureExtractorAutoImageProcessor"AutomaticSpeechRecognitionPipelineAutoTokenizerFeatureExtractionPipelineFillMaskPipelineImageClassificationPipelineImageSegmentationPipelineImageToImagePipelineImageToTextPipelinePipelinePreTrainedTokenizerPreTrainedTokenizerFastQuestionAnsweringPipelineSequenceFeatureExtractorSummarizationPipelineText2TextGenerationPipelineTextClassificationPipelineTextGenerationPipelineTokenClassificationPipelineTranslationPipelineZeroShotClassificationPipeline)pipeline)PreTrainedFeatureExtractor)BaseImageProcessor)FEATURE_EXTRACTOR_MAPPINGIMAGE_PROCESSOR_MAPPINGTOKENIZER_MAPPING
check_taskget_default_model_and_revisioninfer_framework_load_model)SUPPORTED_TASKS   )is_onnxruntime_availableis_transformers_version)ORTModelForAudioClassificationORTModelForCausalLMORTModelForFeatureExtractionORTModelForImageClassificationORTModelForImageToImageORTModelForMaskedLMORTModelForQuestionAnsweringORTModelForSemanticSegmentationORTModelForSeq2SeqLM!ORTModelForSequenceClassificationORTModelForSpeechSeq2SeqORTModelForTokenClassificationORTModelForVision2Seq)ORTModelzfeature-extractionzdistilbert-base-casedtext)implclassdefaulttypez	fill-maskzbert-base-casedzimage-classificationzgoogle/vit-base-patch16-224imagezimage-segmentationz)nvidia/segformer-b0-finetuned-ade-512-512zquestion-answeringz%distilbert-base-cased-distilled-squadztext-classificationz/distilbert-base-uncased-finetuned-sst-2-englishztext-generation
distilgpt2ztoken-classificationz0dbmdz/bert-large-cased-finetuned-conll03-englishzzero-shot-classificationzfacebook/bart-large-mnlisummarizationzt5-basetranslationzt5-smallztext2text-generationzautomatic-speech-recognitionzopenai/whisper-tiny.en
multimodalzimage-to-textz$nlpconnect/vit-gpt2-image-captioningzaudio-classificationzsuperb/hubert-base-superb-ksaudiozimage-to-imagez!caidas/swin2SR-classical-sr-x2-64N main	subfoldertokenrevisionmodel_kwargsconfig
hub_kwargsc                 K   s   ddl m} |d u rtddrddi}ni }t| tr| }nd }d|| d i}t| f||d|d||\}} |d	krAtd
|j| fi |} | ||||fS )Nr(   )BetterTransformerz>=z4.36.0attn_implementationeagerpt)model_classesrJ   	frameworktasktfzZBetterTransormer is PyTorch-specific. It will not work with the provided TensorFlow model.)bettertransformerrL   r*   
isinstancestrr&   NotImplementedError	transform)modeltargeted_taskload_tokenizer	tokenizerfeature_extractorload_feature_extractorimage_processorload_image_processorr'   rF   rG   rH   rI   rJ   rK   kwargsrL   model_idrP   rQ    rc   n/lsinfo/ai/hellotax_ai/llm_service/venv_embed/lib/python3.10/site-packages/optimum/pipelines/pipelines_base.pyload_bettertransformer   s6   




re   c                 K   s"  |d u ri }t | tr!| }|| d d j| f||	|
d|} nit | tr|d u rE|rE| jD ]}t |ttfr<|} nq/|d u rEtd|d u rb|rb| jD ]}t |trY|} nqN|d u rbtd|d u r|r| jD ]}t |t	rv|} nqk|d u rtdd }ntd|  d| ||||fS )	Nr;   r   )rH   rF   rG   z^Could not automatically find a tokenizer for the ORTModel, you must pass a tokenizer explictlyznCould not automatically find a feature extractor for the ORTModel, you must pass a feature_extractor explictlyzlCould not automatically find a feature extractor for the ORTModel, you must pass a image_processor explictlyzModel z is not supported. Please provide a valid model either as string or ORTModel.
            You can also provide non model then a default one will be used)
rU   rV   from_pretrainedr8   preprocessorsr   r   
ValueErrorr   r    )rY   rZ   r[   r\   r]   r^   r_   r`   r'   rF   rG   rH   rI   rJ   ra   rb   preprocessorrc   rc   rd   load_ort_pipeline   s^   







rj   )ortrT   Trk   rR   rY   r\   r]   r_   use_fastacceleratortrust_remote_codereturnc
              	   O   s  |  drdn| }|dkr#|tt vr#td| dtt  |tvr/td| d|dkr5tnt}|d u rY|dkrSt| \}}}t|d|\}}|pQ|}n|| d }|||	d d	}|	d
d }|d u rt
|trtj|fd| i||}|j|d< t }t }t }| D ]D\}}|d dkr|| || q|d dv r|| q|d dv r|| || q|d dvrtd| d|d  q|p|j}t|tv p|jd u}t|tv p|d u}t|tv p|d u}||v rd}||v rd}||v rd}|r|rd}t| ||||||||g|
R ||||d|\}}}}}|	|d}|d u rJ|rJtj|fd|i|}|d u r]|r]tj|fd|i|}|d u rn|rntj|fi |}t| f|||||d|S )NrA   rk   zTask zE is not supported for the ONNX Runtime pipeline. Supported tasks are zAccelerator zL is not supported. Supported accelerators are "ort" and "bettertransformer".rO   r<   )rH   rG   rn   _commit_hashrJ   _from_pipelinerp   r=   r9   >   r>   video>   rC   )rB   rC   rr   zSUPPORTED_TASK z contains invalid type F)r'   rJ   rK   rG   Truerl   )rY   r\   r]   r_   rl   )
startswithlistORT_SUPPORTED_TASKSkeysrh   MAPPING_LOADING_FUNCTRANSFORMERS_SUPPORTED_TASKSr$   r%   getrU   rV   r   rf   rp   setitemsaddrJ   r=   r#   tokenizer_classr!   r"   r   r   r	   transformers_pipeline)rR   rY   r\   r]   r_   rl   rG   rm   rH   rn   rI   ra   rZ   supported_tasks_target_tasktask_optionsdefault_revisionrK   rJ   no_feature_extractor_tasksno_tokenizer_tasksno_image_processor_tasks_taskvaluesmodel_configr[   r^   r`   rb   rc   rc   rd   r   2  s   







	r   )NNNNNNNrD   NrE   NNN)rD   NrE   NN)
NNNNNTNrk   NN)G__doc__typingr   r   r   r   transformersr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   %transformers.feature_extraction_utilsr   #transformers.image_processing_utilsr    transformers.pipelinesr!   r"   r#   r$   r%   r&   r'   ry   utilsr)   r*   onnxruntimer+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   onnxruntime.modeling_ortr8   rv   rV   boolre   rj   rx   rc   rc   rc   rd   <module>   s  h <%+17=CIOU[c

@

B	
