






'''simple docstring'''
import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


UpperCamelCase__ =			'hf-internal-testing/tiny-random-bert'
UpperCamelCase__ =			os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
UpperCamelCase__ =			'9b8c223d42b2188cb49d29af482996f9d0f3e5a6'


class        _UpperCAmelCase						(		unittest.TestCase			):




    def 				lowerCAmelCase__				(			self  :       Optional[int]			):






       '''simple docstring'''






       lowercase_	:    str           =     cached_file(a  ,     a			)
       # Should have downloaded the file in here
       self.assertTrue(os.path.isdir(a			)			)
       # Cache should contain at least those three subfolders:
       for subfolder in ["blobs", "refs", "snapshots"]:
          self.assertTrue(os.path.isdir(os.path.join(a  ,     a			)			)			)
       with open(os.path.join(a  ,     "refs"  ,     "main"			)			) as f:
          lowercase_	:    List[Any]           =     f.read()
       self.assertEqual(a  ,     os.path.join(a  ,     "snapshots"  ,     a  ,     a			)			)
       self.assertTrue(os.path.isfile(a			)			)

       # File is cached at the same place the second time.
       lowercase_	:    List[str]           =     cached_file(a  ,     a			)
       self.assertEqual(a  ,     a			)

       # Using a specific revision to test the full commit hash.
       lowercase_	:    int           =     cached_file(a  ,     a  ,     revision="9b8c223"			)
       self.assertEqual(a  ,     os.path.join(a  ,     "snapshots"  ,     a  ,     a			)			)




    def 				lowerCAmelCase__				(			self  :       Optional[Any]			):






       '''simple docstring'''






       with self.assertRaisesRegex(a  ,     "is not a valid model identifier"			):
          lowercase_	:    Any           =     cached_file("tiny-random-bert"  ,     a			)

       with self.assertRaisesRegex(a  ,     "is not a valid git identifier"			):
          lowercase_	:    Tuple           =     cached_file(a  ,     a  ,     revision="aaaa"			)

       with self.assertRaisesRegex(a  ,     "does not appear to have a file named"			):
          lowercase_	:    List[Any]           =     cached_file(a  ,     "conf"			)




    def 				lowerCAmelCase__				(			self  :       str			):






       '''simple docstring'''






       with self.assertRaisesRegex(a  ,     "does not appear to have a file named"			):
          lowercase_	:    Optional[Any]           =     cached_file(a  ,     "conf"			)

       with open(os.path.join(a  ,     "refs"  ,     "main"			)			) as f:
          lowercase_	:    Optional[int]           =     f.read()
       self.assertTrue(os.path.isfile(os.path.join(a  ,     ".no_exist"  ,     a  ,     "conf"			)			)			)

       lowercase_	:    int           =     cached_file(a  ,     "conf"  ,     _raise_exceptions_for_missing_entries=a			)
       self.assertIsNone(a			)

       lowercase_	:    List[Any]           =     cached_file(a  ,     "conf"  ,     local_files_only=a  ,     _raise_exceptions_for_missing_entries=a			)
       self.assertIsNone(a			)

       lowercase_	:    int           =     mock.Mock()
       lowercase_	:    Tuple           =     5_0_0
       lowercase_	:    Tuple           =     {}
       lowercase_	:    List[Any]           =     HTTPError
       lowercase_	:    Optional[int]           =     {}

       # Under the mock environment we get a 500 error when trying to reach the tokenizer.
       with mock.patch("requests.Session.request"  ,     return_value=a			) as mock_head:
          lowercase_	:    Union[str, Any]           =     cached_file(a  ,     "conf"  ,     _raise_exceptions_for_connection_errors=a			)
          self.assertIsNone(a			)
          # This check we did call the fake head request
          mock_head.assert_called()




    def 				lowerCAmelCase__				(			self  :       Tuple			):






       '''simple docstring'''






       self.assertTrue(has_file("hf-internal-testing/tiny-bert-pt-only"  ,     a			)			)
       self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only"  ,     a			)			)
       self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only"  ,     a			)			)




    def 				lowerCAmelCase__				(			self  :       str			):






       '''simple docstring'''






       self.assertIsNone(get_file_from_repo("bert-base-cased"  ,     "ahah.txt"			)			)

       # The function raises if the repository does not exist.
       with self.assertRaisesRegex(a  ,     "is not a valid model identifier"			):
          get_file_from_repo("bert-base-case"  ,     a			)

       # The function raises if the revision does not exist.
       with self.assertRaisesRegex(a  ,     "is not a valid git identifier"			):
          get_file_from_repo("bert-base-cased"  ,     a  ,     revision="ahaha"			)

       lowercase_	:    Optional[int]           =     get_file_from_repo("bert-base-cased"  ,     a			)
       # The name is the cached name which is not very easy to test, so instead we load the content.
       lowercase_	:    Dict           =     json.loads(open(a  ,     "r"			).read()			)
       self.assertEqual(config["hidden_size"]  ,     7_6_8			)





    def 				lowerCAmelCase__				(			self  :       Union[str, Any]			):






       '''simple docstring'''






       with tempfile.TemporaryDirectory() as tmp_dir:
          lowercase_	:    Dict           =     Path(a			) / "a.txt"
          filename.touch()
          self.assertEqual(get_file_from_repo(a  ,     "a.txt"			)  ,     str(a			)			)

          self.assertIsNone(get_file_from_repo(a  ,     "b.txt"			)			)












'''simple docstring'''
def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							):


   """simple docstring"""




   def update_area_of_max_square(_UpperCamelCase						,		_UpperCamelCase							) -> int:
      # BASE CASE
      if row >= rows or col >= cols:
         return 0

      lowercase_	:    List[str]           =     update_area_of_max_square(_UpperCamelCase						,		col + 1							)
      lowercase_	:    List[Any]           =     update_area_of_max_square(row + 1						,		col + 1							)
      lowercase_	:    Tuple           =     update_area_of_max_square(row + 1						,		_UpperCamelCase							)

      if mat[row][col]:
         lowercase_	:    Optional[int]           =     1 + min([right, diagonal, down]							)
         lowercase_	:    Any           =     max(largest_square_area[0]						,		_UpperCamelCase							)
         return sub_problem_sol
      else:
         return 0

   lowercase_	:    int           =     [0]
   update_area_of_max_square(0						,		0							)
   return largest_square_area[0]




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							):


   """simple docstring"""




   def update_area_of_max_square_using_dp_array(
       _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							) -> int:
      if row >= rows or col >= cols:
         return 0
      if dp_array[row][col] != -1:
         return dp_array[row][col]

      lowercase_	:    Dict           =     update_area_of_max_square_using_dp_array(_UpperCamelCase						,		col + 1						,		_UpperCamelCase							)
      lowercase_	:    str           =     update_area_of_max_square_using_dp_array(row + 1						,		col + 1						,		_UpperCamelCase							)
      lowercase_	:    Optional[Any]           =     update_area_of_max_square_using_dp_array(row + 1						,		_UpperCamelCase						,		_UpperCamelCase							)

      if mat[row][col]:
         lowercase_	:    Tuple           =     1 + min([right, diagonal, down]							)
         lowercase_	:    int           =     max(largest_square_area[0]						,		_UpperCamelCase							)
         lowercase_	:    Dict           =     sub_problem_sol
         return sub_problem_sol
      else:
         return 0

   lowercase_	:    Any           =     [0]
   lowercase_	:    Optional[int]           =     [[-1] * cols for _ in range(_UpperCamelCase							)]
   update_area_of_max_square_using_dp_array(0						,		0						,		_UpperCamelCase							)

   return largest_square_area[0]




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							):


   """simple docstring"""




   lowercase_	:    str           =     [[0] * (cols + 1) for _ in range(rows + 1							)]
   lowercase_	:    List[Any]           =     0
   for row in range(rows - 1						,		-1						,		-1							):
      for col in range(cols - 1						,		-1						,		-1							):
         lowercase_	:    Tuple           =     dp_array[row][col + 1]
         lowercase_	:    List[str]           =     dp_array[row + 1][col + 1]
         lowercase_	:    List[Any]           =     dp_array[row + 1][col]

         if mat[row][col] == 1:
            lowercase_	:    Any           =     1 + min(_UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							)
            lowercase_	:    Any           =     max(dp_array[row][col]						,		_UpperCamelCase							)
         else:
            lowercase_	:    int           =     0

   return largest_square_area




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							):


   """simple docstring"""




   lowercase_	:    Optional[int]           =     [0] * (cols + 1)
   lowercase_	:    Union[str, Any]           =     [0] * (cols + 1)
   lowercase_	:    int           =     0
   for row in range(rows - 1						,		-1						,		-1							):
      for col in range(cols - 1						,		-1						,		-1							):
         lowercase_	:    Dict           =     current_row[col + 1]
         lowercase_	:    List[Any]           =     next_row[col + 1]
         lowercase_	:    Tuple           =     next_row[col]

         if mat[row][col] == 1:
            lowercase_	:    Dict           =     1 + min(_UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							)
            lowercase_	:    int           =     max(current_row[col]						,		_UpperCamelCase							)
         else:
            lowercase_	:    Tuple           =     0
      lowercase_	:    Optional[Any]           =     current_row

   return largest_square_area


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))




