# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


UpperCAmelCase_								=							{'''configuration_mra''': ['''MRA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MraConfig''']}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      UpperCAmelCase_								=							[
          '''MRA_PRETRAINED_MODEL_ARCHIVE_LIST''',
          '''MraForMaskedLM''',
          '''MraForMultipleChoice''',
          '''MraForQuestionAnswering''',
          '''MraForSequenceClassification''',
          '''MraForTokenClassification''',
          '''MraLayer''',
          '''MraModel''',
          '''MraPreTrainedModel''',
      ]


if TYPE_CHECKING:
      from .configuration_mra import MRA_PRETRAINED_CONFIG_ARCHIVE_MAP, MraConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_mra import (
                MRA_PRETRAINED_MODEL_ARCHIVE_LIST,
                MraForMaskedLM,
                MraForMultipleChoice,
                MraForQuestionAnswering,
                MraForSequenceClassification,
                MraForTokenClassification,
                MraLayer,
                MraModel,
                MraPreTrainedModel,
            )
else:
      import sys

      UpperCAmelCase_								=							_LazyModule(__name__, globals()['''__file__'''], _import_structure)





def 							UpperCamelCase				(    )		->						list[list[int]]:




       '''simple docstring'''






       return [list(range(10_00 - i       ,					-10_00 - i       ,					-1   )   ) for i in range(10_00   )]


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 							UpperCamelCase				(    lowerCAmelCase_   )		->						None:




       '''simple docstring'''






       assert all(row == sorted(lowerCAmelCase_       ,					reverse=lowerCAmelCase_   ) for row in grid   )
       assert all(list(lowerCAmelCase_   ) == sorted(lowerCAmelCase_       ,					reverse=lowerCAmelCase_   ) for col in zip(*lowerCAmelCase_   )   )

def 							UpperCamelCase				(    lowerCAmelCase_   )		->						int:




       '''simple docstring'''






       _A=	0
       _A=	len(lowerCAmelCase_   ) - 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:
              _A=	(left + right) // 2
              _A=	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:
                     _A=	mid + 1
              else:
                     _A=	mid - 1
    # No negative numbers so return the last index of the array + 1 which is the length.
       return len(lowerCAmelCase_   )

def 							UpperCamelCase				(    lowerCAmelCase_   )		->						int:




       '''simple docstring'''






       _A=	0
       _A=	len(grid[0]   )

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

def 							UpperCamelCase				(    lowerCAmelCase_   )		->						int:




       '''simple docstring'''






       return len([number for row in grid for number in row if number < 0]   )

def 							UpperCamelCase				(    lowerCAmelCase_   )		->						int:




       '''simple docstring'''






       _A=	0
       for row in grid:
              for i, number in enumerate(lowerCAmelCase_   ):
                     if number < 0:
                            total += len(lowerCAmelCase_   ) - i
                            break
       return total

def 							UpperCamelCase				(    )		->						None:




       '''simple docstring'''






       from timeit import timeit

       print('Running benchmarks'   )
       _A=	(
           '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
       ):
              _A=	timeit(F"{func}(grid=grid)"       ,					setup=lowerCAmelCase_       ,					number=5_00   )
              print(F"{func}() took {time:0.4f} seconds"   )


if __name__ == "__main__":
      import doctest

      doctest.testmod()
      benchmark()