




"""simple docstring"""

import subprocess
import sys

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





class 				lowercase_       (     _lowerCamelCase							):



 '''simple docstring'''

 @require_torch
 def 			lowerCAmelCase_							( self       :       Any		):
       _A					   =   '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        '

       _A					   =   '\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        '

       _A					   =   '\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
       _A					   =   '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
       _A					   =   [sys.executable, '-c', '\n'.join([load, run, mock]		)]

       # should succeed
       _A					   =   self.get_env()
       # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
       _A					   =   '1'
       _A					   =   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 			lowerCAmelCase_							( self       :       Optional[Any]		):
       _A					   =   '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        '

       _A					   =   '\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        '

       _A					   =   '\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
       _A					   =   '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
       _A					   =   [sys.executable, '-c', '\n'.join([load, run, mock]		)]

       # should succeed
       _A					   =   self.get_env()
       _A					   =   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 			lowerCAmelCase_							( self       :       Optional[int]		):
       _A					   =   '\nfrom transformers import BertConfig, BertModel, BertTokenizer\n        '

       _A					   =   '\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n        '

       _A					   =   '\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
       _A					   =   [sys.executable, '-c', '\n'.join([load, run]		)]

       # should succeed
       _A					   =   self.get_env()
       _A					   =   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
       _A					   =   [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
       _A					   =   '1'
       _A					   =   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 			lowerCAmelCase_							( self       :       Dict		):
       _A					   =   '\nfrom transformers import pipeline\n        '
       _A					   =   '\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n        '

       _A					   =   '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n        '
       _A					   =   self.get_env()
       _A					   =   '1'
       _A					   =   [sys.executable, '-c', '\n'.join([load, mock, run]		)]
       _A					   =   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 			lowerCAmelCase_							( self       :       Tuple		):
       _A					   =   '\nfrom transformers import AutoModel\n        '
       _A					   =   '\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
       _A					   =   [sys.executable, '-c', '\n'.join([load, run]		)]

       # should succeed
       _A					   =   self.get_env()
       _A					   =   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
       _A					   =   '1'
       _A					   =   subprocess.run(lowerCAmelCase_    ,							env=lowerCAmelCase_    ,							check=lowerCAmelCase_    ,							capture_output=lowerCAmelCase_		)
       self.assertEqual(result.returncode    ,							0    ,							result.stderr		)
       self.assertIn('success'    ,							result.stdout.decode()		)



'''simple docstring'''







from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow


if is_tf_available():
					import numpy as np
					import tensorflow as tf

					from transformers import TFCamembertModel


@require_tf
@require_sentencepiece
@require_tokenizers
class 						__UpperCAmelCase      (					unittest.TestCase						):


							@slow
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		TFCamembertModel.from_pretrained('jplu/tf-camembert-base'  )

								_snake_case   						=		tf.convert_to_tensor(
								    [[5, 1_21, 11, 6_60, 16, 7_30, 2_55_43, 1_10, 83, 6]]    ,   dtype=tf.intaa    ,   )  # J'aime le camembert !"

								_snake_case   						=		model(lowerCAmelCase_  )['last_hidden_state']
								_snake_case   						=		tf.TensorShape((1, 10, 7_68)  )
								self.assertEqual(output.shape    ,   lowerCAmelCase_  )
								# compare the actual values for a slice.
								_snake_case   						=		tf.convert_to_tensor(
								    [[[-0.0254, 0.0235, 0.1027], [0.0606, -0.1811, -0.0418], [-0.1561, -0.1127, 0.2687]]]    ,   dtype=tf.floataa    ,   )
								# camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
								# camembert.eval()
								# expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach()

								self.assertTrue(np.allclose(output[:, :3, :3].numpy()    ,   expected_slice.numpy()    ,   atol=1E-4  )  )

