



"""simple docstring"""
from typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_vision_available():
						from PIL import Image

						from ..image_utils import load_image

if is_torch_available():
						import torch

						from transformers.modeling_outputs import BaseModelOutput

						from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

__a      =				logging.get_logger(__name__)
@add_end_docstrings(_lowerCAmelCase				)
class 				lowerCamelCase  (			_lowerCAmelCase				):






					'''simple docstring'''





					def __init__(       self:							Union[str, Any]						,     **snake_case:							int						)   ->	Any:
											super().__init__(**snake_case						)

											if self.framework == "tf":
																	raise ValueError(f"""The {self.__class__} is only available in PyTorch."""						)

											requires_backends(self						,     """vision"""						)
											self.check_model_type(snake_case						)






					def __call__(       self:							Union[str, Any]						,     snake_case:							Union[str, "Image.Image", List[Dict[str, Any]]]						,     snake_case:							Union[str, List[str]] = None						,     **snake_case:							Union[str, Any]						,     )   ->	Dict:
											if "text_queries" in kwargs:
																	snake_case_ :Optional[Any]						=			kwargs.pop("""text_queries"""						)

											if isinstance(snake_case						,     (str, Image.Image)						):
																	snake_case_ :Tuple						=			{"""image""": image, """candidate_labels""": candidate_labels}
											else:
																	snake_case_ :Dict						=			image
											snake_case_ :Optional[Any]						=			super().__call__(snake_case						,     **snake_case						)
											return results






					def    lowerCAmelCase_				(       self:							Any						,     **snake_case:							Dict						)   ->	Tuple:
											snake_case_ :int						=			{}
											if "threshold" in kwargs:
																	snake_case_ :Optional[Any]						=			kwargs["""threshold"""]
											if "top_k" in kwargs:
																	snake_case_ :Any						=			kwargs["""top_k"""]
											return {}, {}, postprocess_params






					def    lowerCAmelCase_				(       self:							Union[str, Any]						,     snake_case:							Any						)   ->	str:
											snake_case_ :List[str]						=			load_image(inputs["""image"""]						)
											snake_case_ :int						=			inputs["""candidate_labels"""]
											if isinstance(snake_case						,     snake_case						):
																	snake_case_ :Union[str, Any]						=			candidate_labels.split(""","""						)

											snake_case_ :List[str]						=			torch.tensor([[image.height, image.width]]						,     dtype=torch.intaa						)
											for i, candidate_label in enumerate(snake_case						):
																	snake_case_ :Dict						=			self.tokenizer(snake_case						,     return_tensors=self.framework						)
																	snake_case_ :Optional[int]						=			self.image_processor(snake_case						,     return_tensors=self.framework						)
																	yield {
																	    "is_last": i == len(snake_case						) - 1,
																	    "target_size": target_size,
																	    "candidate_label": candidate_label,
																	    **text_inputs,
																	    **image_features,
																	}






					def    lowerCAmelCase_				(       self:							Optional[Any]						,     snake_case:							Optional[int]						)   ->	Optional[Any]:
											snake_case_ :Union[str, Any]						=			model_inputs.pop("""target_size"""						)
											snake_case_ :Any						=			model_inputs.pop("""candidate_label"""						)
											snake_case_ :Optional[int]						=			model_inputs.pop("""is_last"""						)

											snake_case_ :Optional[Any]						=			self.model(**snake_case						)

											snake_case_ :Union[str, Any]						=			{"""target_size""": target_size, """candidate_label""": candidate_label, """is_last""": is_last, **outputs}
											return model_outputs






					def    lowerCAmelCase_				(       self:							List[Any]						,     snake_case:							List[str]						,     snake_case:							Optional[int]=0.1						,     snake_case:							Optional[Any]=None						)   ->	Tuple:
											snake_case_ :List[Any]						=			[]
											for model_output in model_outputs:
																	snake_case_ :List[str]						=			model_output["""candidate_label"""]
																	snake_case_ :Any						=			BaseModelOutput(snake_case						)
																	snake_case_ :int						=			self.image_processor.post_process_object_detection(
																	    outputs=snake_case						,     threshold=snake_case						,     target_sizes=model_output["""target_size"""]						)[0]

																	for index in outputs["scores"].nonzero():
																							snake_case_ :Union[str, Any]						=			outputs["""scores"""][index].item()
																							snake_case_ :Optional[int]						=			self._get_bounding_box(outputs["""boxes"""][index][0]						)

																							snake_case_ :Dict						=			{"""score""": score, """label""": label, """box""": box}
																							results.append(snake_case						)

											snake_case_ :List[str]						=			sorted(snake_case						,     key=lambda snake_case						: x["score"]						,     reverse=snake_case						)
											if top_k:
																	snake_case_ :Any						=			results[:top_k]

											return results






					def    lowerCAmelCase_				(       self:							Optional[int]						,     snake_case:							"torch.Tensor"						)   ->	Dict[str, int]:
											if self.framework != "pt":
																	raise ValueError("""The ZeroShotObjectDetectionPipeline is only available in PyTorch."""						)
											snake_case_,       snake_case_,       snake_case_,       snake_case_ :Dict						=			box.int().tolist()
											snake_case_ :Any						=			{
											    """xmin""": xmin,
											    """ymin""": ymin,
											    """xmax""": xmax,
											    """ymax""": ymax,
											}
											return bbox





"""simple docstring"""
import json
import os
import subprocess
import unittest
from ast import literal_eval

import pytest
from parameterized import parameterized, parameterized_class

from . import is_sagemaker_available


if is_sagemaker_available():
						from sagemaker import Session, TrainingJobAnalytics
						from sagemaker.huggingface import HuggingFace
@pytest.mark.skipif(
    literal_eval(os.getenv("""TEST_SAGEMAKER"""    ,						"""False"""				)				) is not True    ,						reason="""Skipping test because should only be run when releasing minor transformers version"""    ,						)
@pytest.mark.usefixtures("""sm_env"""				)
@parameterized_class(
    [
        {
            """framework""": """pytorch""",
            """script""": """run_glue_model_parallelism.py""",
            """model_name_or_path""": """roberta-large""",
            """instance_type""": """ml.p3dn.24xlarge""",
            """results""": {"""train_runtime""": 1_6_0_0, """eval_accuracy""": 0.3, """eval_loss""": 1.2},
        },
        {
            """framework""": """pytorch""",
            """script""": """run_glue.py""",
            """model_name_or_path""": """roberta-large""",
            """instance_type""": """ml.p3dn.24xlarge""",
            """results""": {"""train_runtime""": 1_6_0_0, """eval_accuracy""": 0.3, """eval_loss""": 1.2},
        },
    ]				)
class 				lowerCamelCase  (			unittest.TestCase				):






					'''simple docstring'''





					def    lowerCAmelCase_				(       self:							Any						)   ->	str:
											if self.framework == "pytorch":
																	subprocess.run(
																	    f"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split()						,     encoding="""utf-8"""						,     check=snake_case						,     )
											assert hasattr(self						,     """env"""						)






					def    lowerCAmelCase_				(       self:							int						,     snake_case:							Dict						)   ->	List[Any]:
											# configuration for running training on smdistributed Model Parallel
											snake_case_ :Tuple						=			{
											    """enabled""": True,
											    """processes_per_host""": 8,
											}
											snake_case_ :List[Any]						=			{
											    """enabled""": True,
											    """parameters""": {
											        """microbatches""": 4,
											        """placement_strategy""": """spread""",
											        """pipeline""": """interleaved""",
											        """optimize""": """speed""",
											        """partitions""": 4,
											        """ddp""": True,
											    },
											}

											snake_case_ :Tuple						=			{"""smdistributed""": {"""modelparallel""": smp_options}, """mpi""": mpi_options}

											snake_case_ :Any						=			"""trainer""" if self.script == """run_glue.py""" else """smtrainer"""
											# creates estimator
											return HuggingFace(
											    entry_point=self.script						,     source_dir=self.env.test_path						,     role=self.env.role						,     image_uri=self.env.image_uri						,     base_job_name=f"""{self.env.base_job_name}-{instance_count}-smp-{name_extension}"""						,     instance_count=snake_case						,     instance_type=self.instance_type						,     debugger_hook_config=snake_case						,     hyperparameters={
											        **self.env.hyperparameters,
											        """model_name_or_path""": self.model_name_or_path,
											        """max_steps""": 500,
											    }						,     metric_definitions=self.env.metric_definitions						,     distribution=snake_case						,     py_version="""py36"""						,     )






					def    lowerCAmelCase_				(       self:							Any						,     snake_case:							Tuple						)   ->	List[str]:
											TrainingJobAnalytics(snake_case						).export_csv(f"""{self.env.test_path}/{job_name}_metrics.csv"""						)






					@parameterized.expand([(1,)]						)
					def    lowerCAmelCase_				(       self:							Dict						,     snake_case:							Dict						)   ->	List[Any]:
											# create estimator
											snake_case_ :List[Any]						=			self.create_estimator(snake_case						)

											# run training
											estimator.fit()

											# result dataframe
											snake_case_ :Any						=			TrainingJobAnalytics(estimator.latest_training_job.name						).dataframe()

											# extract kpis
											snake_case_ :Tuple						=			list(result_metrics_df[result_metrics_df.metric_name == """eval_accuracy"""]["""value"""]						)
											snake_case_ :Dict						=			list(result_metrics_df[result_metrics_df.metric_name == """eval_loss"""]["""value"""]						)
											# get train time from SageMaker job, this includes starting, preprocessing, stopping
											snake_case_ :int						=			(
											    Session().describe_training_job(estimator.latest_training_job.name						).get("""TrainingTimeInSeconds"""						,     999_999						)
											)

											# assert kpis
											assert train_runtime <= self.results["train_runtime"]
											assert all(t >= self.results["""eval_accuracy"""] for t in eval_accuracy						)
											assert all(t <= self.results["""eval_loss"""] for t in eval_loss						)

											# dump tests result into json file to share in PR
											with open(f"""{estimator.latest_training_job.name}.json"""						,     """w"""						) as outfile:
																	json.dump({"""train_time""": train_runtime, """eval_accuracy""": eval_accuracy, """eval_loss""": eval_loss}						,     snake_case						)
