


'''simple docstring'''


import operator as op


lowerCamelCase__					=   'scaler.pt'
lowerCamelCase__					=   'pytorch_model'
lowerCamelCase__					=   'random_states'
lowerCamelCase__					=   'optimizer'
lowerCamelCase__					=   'scheduler'
lowerCamelCase__					=   'pytorch_model.bin'
lowerCamelCase__					=   'pytorch_model.bin.index.json'
lowerCamelCase__					=   'model.safetensors'
lowerCamelCase__					=   'model.safetensors.index.json'
lowerCamelCase__					=   '1.10.2'
lowerCamelCase__					=   'py38'
lowerCamelCase__					=   '4.17.0'
lowerCamelCase__					=   ['ml.p3.16xlarge', 'ml.p3dn.24xlarge', 'ml.p4dn.24xlarge']
lowerCamelCase__					=   ['FULL_SHARD', 'SHARD_GRAD_OP', 'NO_SHARD', 'HYBRID_SHARD', 'HYBRID_SHARD_ZERO2']
lowerCamelCase__					=   ['TRANSFORMER_BASED_WRAP', 'SIZE_BASED_WRAP', 'NO_WRAP']
lowerCamelCase__					=   ['BACKWARD_PRE', 'BACKWARD_POST', 'NO_PREFETCH']
lowerCamelCase__					=   ['FULL_STATE_DICT', 'LOCAL_STATE_DICT', 'SHARDED_STATE_DICT']
lowerCamelCase__					=   '2.0.1'
lowerCamelCase__					=   ['pdsh', 'standard', 'openmpi', 'mvapich']
lowerCamelCase__					=   ['default', 'reduce-overhead', 'max-autotune']

lowerCamelCase__					=   {'>': op.gt, '>=': op.ge, '==': op.eq, '!=': op.ne, '<=': op.le, '<': op.lt}

# These are the args for `torch.distributed.launch` for pytorch < 1.9
lowerCamelCase__					=   [
    'nnodes',
    'nproc_per_node',
    'rdzv_backend',
    'rdzv_endpoint',
    'rdzv_id',
    'rdzv_conf',
    'standalone',
    'max_restarts',
    'monitor_interval',
    'start_method',
    'role',
    'module',
    'm',
    'no_python',
    'run_path',
    'log_dir',
    'r',
    'redirects',
    't',
    'tee',
    'node_rank',
    'master_addr',
    'master_port',
]

lowerCamelCase__					=   ['DEEPSPEED', 'MULTI_GPU', 'FSDP', 'MEGATRON_LM']
lowerCamelCase__					=   ['DEEPSPEED', 'MULTI_XPU', 'FSDP']







'''simple docstring'''


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


lowerCamelCase__					=   generate_large_matrix()
lowerCamelCase__					=   (
    [[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        __lowerCAmelCase       (__lowerCAmelCase      ):
		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        __lowerCAmelCase       (__lowerCAmelCase      ):
		_UpperCAmelCase						:				Any	    =					0
		_UpperCAmelCase						:				str	    =					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:
				_UpperCAmelCase						:				Union[str, Any]	    =					(left + right) // 2
				_UpperCAmelCase						:				List[str]	    =					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:
						_UpperCAmelCase						:				Tuple	    =					mid + 1
				else:
						_UpperCAmelCase						:				Optional[Any]	    =					mid - 1
    # No negative numbers so return the last index of the array + 1 which is the length.
		return len(__lowerCAmelCase      )






def        __lowerCAmelCase       (__lowerCAmelCase      ):
		_UpperCAmelCase						:				str	    =					0
		_UpperCAmelCase						:				int	    =					len(grid[0]      )

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






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






def        __lowerCAmelCase       (__lowerCAmelCase      ):
		_UpperCAmelCase						:				Tuple	    =					0
		for row in grid:
				for i, number in enumerate(__lowerCAmelCase      ):
						if number < 0:
								total += len(__lowerCAmelCase      ) - i
								break
		return total






def        __lowerCAmelCase       ():
		from timeit import timeit

		print("Running benchmarks"      )
		_UpperCAmelCase						:				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
		):
				_UpperCAmelCase						:				str	    =					timeit(F"""{func}(grid=grid)"""					,       setup=__lowerCAmelCase					,       number=500      )
				print(F"""{func}() took {time:0.4f} seconds"""      )


if __name__ == "__main__":
						import doctest

						doctest.testmod()
						benchmark()



