





import doctest
import logging
import os
import unittest
from pathlib import Path
from typing import List, Union

import transformers
from transformers.testing_utils import require_tf, require_torch, slow


_UpperCAmelCase             =							logging.getLogger()






@unittest.skip('Temporarily disable the doc tests.'						)
@require_torch
@require_tf
@slow
class        snake_case_    (		unittest.TestCase						):






  def 				UpperCAmelCase__						(							self			:					str       ,  _snake_case			:					Path       ,  _snake_case			:					Union[str, None] = None       ,  _snake_case			:					Union[List[str], None] = None       ,  _snake_case			:					Union[str, List[str], None] = None       ,  _snake_case			:					bool = True       ,  )->Any:






   '''simple docstring'''

   __lowerCAmelCase					:	Any      =       [file for file in os.listdir(_snake_case      ) if os.path.isfile(os.path.join(_snake_case       ,  _snake_case      )      )]

   if identifier is not None:
    __lowerCAmelCase					:	str      =       [file for file in files if identifier in file]

   if n_identifier is not None:
    if isinstance(_snake_case       ,  _snake_case      ):
     for n_ in n_identifier:
      __lowerCAmelCase					:	List[str]      =       [file for file in files if n_ not in file]
    else:
     __lowerCAmelCase					:	int      =       [file for file in files if n_identifier not in file]

   __lowerCAmelCase					:	List[str]      =       ignore_files or []
   ignore_files.append("""__init__.py"""      )
   __lowerCAmelCase					:	List[str]      =       [file for file in files if file not in ignore_files]

   for file in files:
    # Open all files
    print("""Testing"""       ,  _snake_case      )

    if only_modules:
     __lowerCAmelCase					:	Tuple      =       file.split("""."""      )[0]
     try:
      __lowerCAmelCase					:	List[str]      =       getattr(_snake_case       ,  _snake_case      )
      __lowerCAmelCase					:	Optional[Any]      =       doctest.DocTestSuite(_snake_case      )
      __lowerCAmelCase					:	Any      =       unittest.TextTestRunner().run(_snake_case      )
      self.assertIs(len(result.failures      )       ,  0      )
     except AttributeError:
      logger.info(F'''{module_identifier} is not a module.'''      )
    else:
     __lowerCAmelCase					:	Any      =       doctest.testfile(str("""..""" / directory / file      )       ,  optionflags=doctest.ELLIPSIS      )
     self.assertIs(result.failed       ,  0      )






  def 				UpperCAmelCase__						(							self			:					List[Any]      )->Optional[Any]:






   '''simple docstring'''

   __lowerCAmelCase					:	Any      =       Path("""src/transformers"""      )
   __lowerCAmelCase					:	List[str]      =       """modeling"""
   __lowerCAmelCase					:	Optional[Any]      =       [
       """modeling_ctrl.py""",
       """modeling_tf_ctrl.py""",
   ]
   self.analyze_directory(_snake_case       ,  identifier=_snake_case       ,  ignore_files=_snake_case      )






  def 				UpperCAmelCase__						(							self			:					Dict      )->Tuple:






   '''simple docstring'''

   __lowerCAmelCase					:	List[str]      =       Path("""src/transformers"""      )
   __lowerCAmelCase					:	int      =       """tokenization"""
   self.analyze_directory(_snake_case       ,  identifier=_snake_case      )






  def 				UpperCAmelCase__						(							self			:					Tuple      )->List[str]:






   '''simple docstring'''

   __lowerCAmelCase					:	int      =       Path("""src/transformers"""      )
   __lowerCAmelCase					:	Union[str, Any]      =       """configuration"""
   self.analyze_directory(_snake_case       ,  identifier=_snake_case      )






  def 				UpperCAmelCase__						(							self			:					Optional[int]      )->Optional[Any]:






   '''simple docstring'''

   __lowerCAmelCase					:	Tuple      =       Path("""src/transformers"""      )
   __lowerCAmelCase					:	List[str]      =       ["""configuration""", """modeling""", """tokenization"""]
   self.analyze_directory(_snake_case       ,  n_identifier=_snake_case      )






  def 				UpperCAmelCase__						(							self			:					Optional[Any]      )->Optional[int]:






   '''simple docstring'''

   __lowerCAmelCase					:	str      =       Path("""docs/source"""      )
   __lowerCAmelCase					:	int      =       ["""favicon.ico"""]
   self.analyze_directory(_snake_case       ,  ignore_files=_snake_case       ,  only_modules=_snake_case      )






def 	_SCREAMING_SNAKE_CASE		(      )      ->   list[list[int]]:
 return [list(range(1_000 - i   ,   -1_000 - i   ,   -1   )   ) for i in range(1_000   )]


_UpperCAmelCase             =							generate_large_matrix()
_UpperCAmelCase             =							(
    [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]],
    [[3, 2], [1, 0]],
    [[7, 7, 6]],
    [[7, 7, 6], [-1, -2, -3]],
    grid,
)






def 	_SCREAMING_SNAKE_CASE		(      SCREAMING_SNAKE_CASE		:list[list[int]]   )      ->   None:
 assert all(row == sorted(SCREAMING_SNAKE_CASE   ,   reverse=SCREAMING_SNAKE_CASE   ) for row in grid   )
 assert all(list(SCREAMING_SNAKE_CASE   ) == sorted(SCREAMING_SNAKE_CASE   ,   reverse=SCREAMING_SNAKE_CASE   ) for col in zip(*SCREAMING_SNAKE_CASE   )   )






def 	_SCREAMING_SNAKE_CASE		(      SCREAMING_SNAKE_CASE		:list[int]   )      ->   int:
 __lowerCAmelCase					:	Optional[int]      =       0
 __lowerCAmelCase					:	Tuple      =       len(SCREAMING_SNAKE_CASE   ) - 1

 # Edge cases such as no values or all numbers are negative.
 if not array or array[0] < 0:
  return 0

 while right + 1 > left:
  __lowerCAmelCase					:	Any      =       (left + right) // 2
  __lowerCAmelCase					:	Optional[int]      =       array[mid]

  # Num must be negative and the index must be greater than or equal to 0.
  if num < 0 and array[mid - 1] >= 0:
   return mid

  if num >= 0:
   __lowerCAmelCase					:	Dict      =       mid + 1
  else:
   __lowerCAmelCase					:	str      =       mid - 1
    # No negative numbers so return the last index of the array + 1 which is the length.
 return len(SCREAMING_SNAKE_CASE   )






def 	_SCREAMING_SNAKE_CASE		(      SCREAMING_SNAKE_CASE		:list[list[int]]   )      ->   int:
 __lowerCAmelCase					:	Any      =       0
 __lowerCAmelCase					:	Union[str, Any]      =       len(grid[0]   )

 for i in range(len(SCREAMING_SNAKE_CASE   )   ):
  __lowerCAmelCase					:	str      =       find_negative_index(grid[i][:bound]   )
  total += bound
 return (len(SCREAMING_SNAKE_CASE   ) * len(grid[0]   )) - total






def 	_SCREAMING_SNAKE_CASE		(      SCREAMING_SNAKE_CASE		:list[list[int]]   )      ->   int:
 return len([number for row in grid for number in row if number < 0]   )






def 	_SCREAMING_SNAKE_CASE		(      SCREAMING_SNAKE_CASE		:list[list[int]]   )      ->   int:
 __lowerCAmelCase					:	List[str]      =       0
 for row in grid:
  for i, number in enumerate(SCREAMING_SNAKE_CASE   ):
   if number < 0:
    total += len(SCREAMING_SNAKE_CASE   ) - i
    break
 return total






def 	_SCREAMING_SNAKE_CASE		(      )      ->   None:
 from timeit import timeit

 print("""Running benchmarks"""   )
 __lowerCAmelCase					:	Tuple      =       (
     """from __main__ import count_negatives_binary_search, """
     """count_negatives_brute_force, count_negatives_brute_force_with_break, grid"""
 )
 for func in (
     "count_negatives_binary_search",  # took 0.7727 seconds
     "count_negatives_brute_force_with_break",  # took 4.6505 seconds
     "count_negatives_brute_force",  # took 12.8160 seconds
 ):
  __lowerCAmelCase					:	Optional[int]      =       timeit(F'''{func}(grid=grid)'''   ,   setup=SCREAMING_SNAKE_CASE   ,   number=500   )
  print(F'''{func}() took {time:0.4f} seconds'''   )


if __name__ == "__main__":
      import doctest

      doctest.testmod()
      benchmark()