




import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch




class    __lowerCAmelCase       (							lowerCAmelCase):






       @require_torch
       def 					SCREAMING_SNAKE_CASE    (   self:					Dict				):
         # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
         # `transformers` is loaded, and it's too late for inside pytest - so we are changing it
         # while running an external program

         # python one-liner segments

         # this must be loaded before socket.socket is monkey-patched
         lowercase    :Dict      	=						"\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        "

         lowercase    :List[Any]      	=						"\nmname = \"hf-internal-testing/tiny-random-bert\"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task=\"fill-mask\", model=mname)\nprint(\"success\")\n        "

         lowercase    :int      	=						"\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError(\"Offline mode is enabled, we shouldn't access internet\")\nsocket.socket = offline_socket\n        "

         # Force fetching the files so that we can use the cache
         lowercase    :Tuple      	=						"hf-internal-testing/tiny-random-bert"
         BertConfig.from_pretrained(_lowerCAmelCase				)
         BertModel.from_pretrained(_lowerCAmelCase				)
         BertTokenizer.from_pretrained(_lowerCAmelCase				)
         pipeline(task="fill-mask"       ,					model=_lowerCAmelCase				)

         # baseline - just load from_pretrained with normal network
         lowercase    :Tuple      	=						[sys.executable, "-c", "\n".join([load, run, mock]				)]

         # should succeed
         lowercase    :Union[str, Any]      	=						self.get_env()
         # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
         lowercase    :Dict      	=						"1"
         lowercase    :Optional[Any]      	=						subprocess.run(_lowerCAmelCase       ,					env=_lowerCAmelCase       ,					check=_lowerCAmelCase       ,					capture_output=_lowerCAmelCase				)
         self.assertEqual(result.returncode       ,					0       ,					result.stderr				)
         self.assertIn("success"       ,					result.stdout.decode()				)






       @require_torch
       def 					SCREAMING_SNAKE_CASE    (   self:					int				):
         # python one-liner segments
         # this must be loaded before socket.socket is monkey-patched
         lowercase    :Any      	=						"\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        "

         lowercase    :str      	=						"\nmname = \"hf-internal-testing/tiny-random-bert\"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task=\"fill-mask\", model=mname)\nprint(\"success\")\n        "

         lowercase    :Dict      	=						"\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error(\"Faking flaky internet\")\nsocket.socket = offline_socket\n        "

         # Force fetching the files so that we can use the cache
         lowercase    :str      	=						"hf-internal-testing/tiny-random-bert"
         BertConfig.from_pretrained(_lowerCAmelCase				)
         BertModel.from_pretrained(_lowerCAmelCase				)
         BertTokenizer.from_pretrained(_lowerCAmelCase				)
         pipeline(task="fill-mask"       ,					model=_lowerCAmelCase				)

         # baseline - just load from_pretrained with normal network
         lowercase    :Any      	=						[sys.executable, "-c", "\n".join([load, run, mock]				)]

         # should succeed
         lowercase    :Union[str, Any]      	=						self.get_env()
         lowercase    :str      	=						subprocess.run(_lowerCAmelCase       ,					env=_lowerCAmelCase       ,					check=_lowerCAmelCase       ,					capture_output=_lowerCAmelCase				)
         self.assertEqual(result.returncode       ,					0       ,					result.stderr				)
         self.assertIn("success"       ,					result.stdout.decode()				)






       @require_torch
       def 					SCREAMING_SNAKE_CASE    (   self:					int				):
         # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
         # `transformers` is loaded, and it's too late for inside pytest - so we are changing it
         # while running an external program

         # python one-liner segments

         # this must be loaded before socket.socket is monkey-patched
         lowercase    :Optional[Any]      	=						"\nfrom transformers import BertConfig, BertModel, BertTokenizer\n        "

         lowercase    :Optional[Any]      	=						"\nmname = \"hf-internal-testing/tiny-random-bert-sharded\"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint(\"success\")\n        "

         lowercase    :List[str]      	=						"\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError(\"Offline mode is enabled\")\nsocket.socket = offline_socket\n        "

         # baseline - just load from_pretrained with normal network
         lowercase    :List[Any]      	=						[sys.executable, "-c", "\n".join([load, run]				)]

         # should succeed
         lowercase    :Optional[Any]      	=						self.get_env()
         lowercase    :Any      	=						subprocess.run(_lowerCAmelCase       ,					env=_lowerCAmelCase       ,					check=_lowerCAmelCase       ,					capture_output=_lowerCAmelCase				)
         self.assertEqual(result.returncode       ,					0       ,					result.stderr				)
         self.assertIn("success"       ,					result.stdout.decode()				)

         # next emulate no network
         lowercase    :List[Any]      	=						[sys.executable, "-c", "\n".join([load, mock, run]				)]

         # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
         # env["TRANSFORMERS_OFFLINE"] = "0"
         # result = subprocess.run(cmd, env=env, check=False, capture_output=True)
         # self.assertEqual(result.returncode, 1, result.stderr)

         # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
         lowercase    :str      	=						"1"
         lowercase    :List[str]      	=						subprocess.run(_lowerCAmelCase       ,					env=_lowerCAmelCase       ,					check=_lowerCAmelCase       ,					capture_output=_lowerCAmelCase				)
         self.assertEqual(result.returncode       ,					0       ,					result.stderr				)
         self.assertIn("success"       ,					result.stdout.decode()				)






       @require_torch
       def 					SCREAMING_SNAKE_CASE    (   self:					Union[str, Any]				):
         lowercase    :Dict      	=						"\nfrom transformers import pipeline\n        "
         lowercase    :Dict      	=						"\nmname = \"hf-internal-testing/tiny-random-bert\"\npipe = pipeline(model=mname)\n        "

         lowercase    :Dict      	=						"\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error(\"Offline mode is enabled\")\nsocket.socket = offline_socket\n        "
         lowercase    :Optional[Any]      	=						self.get_env()
         lowercase    :Tuple      	=						"1"
         lowercase    :List[str]      	=						[sys.executable, "-c", "\n".join([load, mock, run]				)]
         lowercase    :Optional[Any]      	=						subprocess.run(_lowerCAmelCase       ,					env=_lowerCAmelCase       ,					check=_lowerCAmelCase       ,					capture_output=_lowerCAmelCase				)
         self.assertEqual(result.returncode       ,					1       ,					result.stderr				)
         self.assertIn(
             "You cannot infer task automatically within `pipeline` when using offline mode"       ,					result.stderr.decode().replace("\n"       ,					""				)       ,					)






       @require_torch
       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         lowercase    :List[Any]      	=						"\nfrom transformers import AutoModel\n        "
         lowercase    :Optional[Any]      	=						"\nmname = \"hf-internal-testing/test_dynamic_model\"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint(\"success\")\n        "

         # baseline - just load from_pretrained with normal network
         lowercase    :int      	=						[sys.executable, "-c", "\n".join([load, run]				)]

         # should succeed
         lowercase    :Dict      	=						self.get_env()
         lowercase    :int      	=						subprocess.run(_lowerCAmelCase       ,					env=_lowerCAmelCase       ,					check=_lowerCAmelCase       ,					capture_output=_lowerCAmelCase				)
         self.assertEqual(result.returncode       ,					0       ,					result.stderr				)
         self.assertIn("success"       ,					result.stdout.decode()				)

         # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
         lowercase    :Any      	=						"1"
         lowercase    :Dict      	=						subprocess.run(_lowerCAmelCase       ,					env=_lowerCAmelCase       ,					check=_lowerCAmelCase       ,					capture_output=_lowerCAmelCase				)
         self.assertEqual(result.returncode       ,					0       ,					result.stderr				)
         self.assertIn("success"       ,					result.stdout.decode()				)











import os
import shutil
from pathlib import Path
from typing import Optional, Union

import numpy as np
from huggingface_hub import hf_hub_download

from ..utils import ONNX_EXTERNAL_WEIGHTS_NAME, ONNX_WEIGHTS_NAME, is_onnx_available, logging


if is_onnx_available():
     import onnxruntime as ort


_UpperCAmelCase	:						Dict			=     logging.get_logger(__name__)

_UpperCAmelCase	:						List[Any]			=     {
    "tensor(bool)": np.bool_,
    "tensor(int8)": np.inta,
    "tensor(uint8)": np.uinta,
    "tensor(int16)": np.intaa,
    "tensor(uint16)": np.uintaa,
    "tensor(int32)": np.intaa,
    "tensor(uint32)": np.uintaa,
    "tensor(int64)": np.intaa,
    "tensor(uint64)": np.uintaa,
    "tensor(float16)": np.floataa,
    "tensor(float)": np.floataa,
    "tensor(double)": np.floataa,
}




class    __lowerCAmelCase       :






       def __init__(   self:					Dict       ,					_lowerCAmelCase:					Union[str, Any]=None       ,					**_lowerCAmelCase:					Any				):
         logger.info("`diffusers.OnnxRuntimeModel` is experimental and might change in the future."				)
         lowercase    :str      	=						model
         lowercase    :Any      	=						kwargs.get("model_save_dir"       ,					_lowerCAmelCase				)
         lowercase    :Dict      	=						kwargs.get("latest_model_name"       ,					_lowerCAmelCase				)






       def __call__(   self:					Optional[Any]       ,					**_lowerCAmelCase:					List[Any]				):
         lowercase    :List[str]      	=						{k: np.array(_lowerCAmelCase				) for k, v in kwargs.items()}
         return self.model.run(_lowerCAmelCase       ,					_lowerCAmelCase				)






       @staticmethod
       def 					SCREAMING_SNAKE_CASE    (   _lowerCAmelCase:					Union[str, Path]       ,					_lowerCAmelCase:					List[Any]=None       ,					_lowerCAmelCase:					List[Any]=None				):
         if provider is None:
           logger.info("No onnxruntime provider specified, using CPUExecutionProvider"				)
           lowercase    :int      	=						"CPUExecutionProvider"

         return ort.InferenceSession(_lowerCAmelCase       ,					providers=[provider]       ,					sess_options=_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]       ,					_lowerCAmelCase:					Union[str, Path]       ,					_lowerCAmelCase:					Optional[str] = None       ,					**_lowerCAmelCase:					Any				):
         lowercase    :Optional[int]      	=						file_name if file_name is not None else ONNX_WEIGHTS_NAME

         lowercase    :Tuple      	=						self.model_save_dir.joinpath(self.latest_model_name				)
         lowercase    :Optional[int]      	=						Path(_lowerCAmelCase				).joinpath(_lowerCAmelCase				)
         try:
           shutil.copyfile(_lowerCAmelCase       ,					_lowerCAmelCase				)
         except shutil.SameFileError:
           pass

         # copy external weights (for models >2GB)
         lowercase    :Tuple      	=						self.model_save_dir.joinpath(_lowerCAmelCase				)
         if src_path.exists():
           lowercase    :Dict      	=						Path(_lowerCAmelCase				).joinpath(_lowerCAmelCase				)
           try:
             shutil.copyfile(_lowerCAmelCase       ,					_lowerCAmelCase				)
           except shutil.SameFileError:
             pass






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]       ,					_lowerCAmelCase:					Union[str, os.PathLike]       ,					**_lowerCAmelCase:					int       ,					):
         if os.path.isfile(_lowerCAmelCase				):
           logger.error(F"Provided path ({save_directory}) should be a directory, not a file"				)
           return

         os.makedirs(_lowerCAmelCase       ,					exist_ok=_lowerCAmelCase				)

         # saving model weights/files
         self._save_pretrained(_lowerCAmelCase       ,					**_lowerCAmelCase				)






       @classmethod
       def 					SCREAMING_SNAKE_CASE    (   cls:					Dict       ,					_lowerCAmelCase:					Union[str, Path]       ,					_lowerCAmelCase:					Optional[Union[bool, str, None]] = None       ,					_lowerCAmelCase:					Optional[Union[str, None]] = None       ,					_lowerCAmelCase:					bool = False       ,					_lowerCAmelCase:					Optional[str] = None       ,					_lowerCAmelCase:					Optional[str] = None       ,					_lowerCAmelCase:					Optional[str] = None       ,					_lowerCAmelCase:					Optional["ort.SessionOptions"] = None       ,					**_lowerCAmelCase:					Optional[int]       ,					):
         lowercase    :List[str]      	=						file_name if file_name is not None else ONNX_WEIGHTS_NAME
         # load model from local directory
         if os.path.isdir(_lowerCAmelCase				):
           lowercase    :Union[str, Any]      	=						OnnxRuntimeModel.load_model(
               os.path.join(_lowerCAmelCase       ,					_lowerCAmelCase				)       ,					provider=_lowerCAmelCase       ,					sess_options=_lowerCAmelCase				)
           lowercase    :Optional[Any]      	=						Path(_lowerCAmelCase				)
         # load model from hub
         else:
           # download model
           lowercase    :str      	=						hf_hub_download(
               repo_id=_lowerCAmelCase       ,					filename=_lowerCAmelCase       ,					use_auth_token=_lowerCAmelCase       ,					revision=_lowerCAmelCase       ,					cache_dir=_lowerCAmelCase       ,					force_download=_lowerCAmelCase       ,					)
           lowercase    :Optional[int]      	=						Path(_lowerCAmelCase				).parent
           lowercase    :Tuple      	=						Path(_lowerCAmelCase				).name
           lowercase    :Tuple      	=						OnnxRuntimeModel.load_model(_lowerCAmelCase       ,					provider=_lowerCAmelCase       ,					sess_options=_lowerCAmelCase				)
         return cls(model=_lowerCAmelCase       ,					**_lowerCAmelCase				)






       @classmethod
       def 					SCREAMING_SNAKE_CASE    (   cls:					str       ,					_lowerCAmelCase:					Union[str, Path]       ,					_lowerCAmelCase:					bool = True       ,					_lowerCAmelCase:					Optional[str] = None       ,					_lowerCAmelCase:					Optional[str] = None       ,					**_lowerCAmelCase:					Any       ,					):
         lowercase    :List[str]      	=						None
         if len(str(_lowerCAmelCase				).split("@"				)				) == 2:
           lowercase	,    lowercase    :Tuple      	=						model_id.split("@"				)

         return cls._from_pretrained(
             model_id=_lowerCAmelCase       ,					revision=_lowerCAmelCase       ,					cache_dir=_lowerCAmelCase       ,					force_download=_lowerCAmelCase       ,					use_auth_token=_lowerCAmelCase       ,					**_lowerCAmelCase       ,					)



