"""simple docstring"""






def   a_						(					):
				return [list(range(1_0_0_0 - i					,			-1_0_0_0 - i					,			-1       )       ) for i in range(1_0_0_0       )]


lowerCAmelCase__  :  Any						     =	generate_large_matrix()
lowerCAmelCase__  :  Any						     =	(
    [[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   a_						(					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   a_						(					lowerCamelCase       ):
				UpperCAmelCase__             =							0
				UpperCAmelCase__             =							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__             =							(left + right) // 2
								UpperCAmelCase__             =							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__             =							mid + 1
								else:
												UpperCAmelCase__             =							mid - 1
    # No negative numbers so return the last index of the array + 1 which is the length.
				return len(lowerCamelCase       )



def   a_						(					lowerCamelCase       ):
				UpperCAmelCase__             =							0
				UpperCAmelCase__             =							len(grid[0]       )

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



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



def   a_						(					lowerCamelCase       ):
				UpperCAmelCase__             =							0
				for row in grid:
								for i, number in enumerate(lowerCamelCase       ):
												if number < 0:
																total += len(lowerCamelCase       ) - i
																break
				return total



def   a_						(					):
				from timeit import timeit

				print('Running benchmarks'       )
				UpperCAmelCase__             =							(
				    '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__             =							timeit(f'''{func}(grid=grid)'''					,			setup=lowerCamelCase					,			number=5_0_0       )
								print(f'''{func}() took {time:0.4f} seconds'''       )


if __name__ == "__main__":
						import doctest

						doctest.testmod()
						benchmark()




"""simple docstring"""






import os

import numpy
import onnx



def   a_						(					lowerCamelCase					,			lowerCamelCase       ):
				UpperCAmelCase__             =							a.name
				UpperCAmelCase__             =							b.name

				UpperCAmelCase__             =							''
				UpperCAmelCase__             =							''

				UpperCAmelCase__             =							a == b

				UpperCAmelCase__             =							name_a
				UpperCAmelCase__             =							name_b

				return res



def   a_						(					lowerCamelCase					,			lowerCamelCase					,			lowerCamelCase       ):
				for i, input_name in enumerate(node_proto.input       ):
								if input_name == name:
												node_proto.input.insert(lowerCamelCase					,			lowerCamelCase       )
												node_proto.input.pop(i + 1       )

				if node_proto.op_type == "If":
								_graph_replace_input_with(node_proto.attribute[0].g					,			lowerCamelCase					,			lowerCamelCase       )
								_graph_replace_input_with(node_proto.attribute[1].g					,			lowerCamelCase					,			lowerCamelCase       )
				if node_proto.op_type == "Loop":
								_graph_replace_input_with(node_proto.attribute[0].g					,			lowerCamelCase					,			lowerCamelCase       )



def   a_						(					lowerCamelCase					,			lowerCamelCase					,			lowerCamelCase       ):
				for n in graph_proto.node:
								_node_replace_input_with(lowerCamelCase					,			lowerCamelCase					,			lowerCamelCase       )



def   a_						(					lowerCamelCase					,			lowerCamelCase					,			lowerCamelCase       ):
				UpperCAmelCase__             =							list(model.graph.initializer       )
				UpperCAmelCase__             =							list(model_without_ext.graph.initializer       )
				for i, ref_i in ind_to_replace:
								assert inits_with_data[i].name == inits[i].name
								assert inits_with_data[ref_i].name == inits[ref_i].name
								assert i > ref_i

								UpperCAmelCase__             =							inits[i].name
								UpperCAmelCase__             =							inits[ref_i].name

								model_without_ext.graph.initializer.remove(inits[i]       )

								# for n in model.graph.node:
								_graph_replace_input_with(model_without_ext.graph					,			lowerCamelCase					,			lowerCamelCase       )







def   a_						(					lowerCamelCase       ):
				UpperCAmelCase__             =							os.path.dirname(lowerCamelCase       )
				UpperCAmelCase__             =							os.path.basename(lowerCamelCase       )

				UpperCAmelCase__             =							onnx.load(os.path.join(lowerCamelCase					,			lowerCamelCase       )       )

				UpperCAmelCase__             =							list(model.graph.initializer       )

				UpperCAmelCase__             =							set()
				UpperCAmelCase__             =							{}
				UpperCAmelCase__             =							[]

				UpperCAmelCase__             =							0

				for i in range(len(lowerCamelCase       )       ):
								if i in dup_set:
												continue

								for j in range(i + 1					,			len(lowerCamelCase       )       ):
												if j in dup_set:
																continue
												if _is_equal_tensor_proto(inits[i]					,			inits[j]       ):
																dup_set.add(lowerCamelCase       )
																dup_set.add(lowerCamelCase       )

																UpperCAmelCase__             =							inits[j].data_type
																UpperCAmelCase__             =							numpy.prod(inits[j].dims       )
																if dtype == 1:
																				mem_size *= 4
																elif dtype == 6:
																				mem_size *= 4
																elif dtype == 7 or dtype == 1_1:
																				mem_size *= 8
																else:
																				print('unexpected data type: '					,			lowerCamelCase       )
																total_reduced_size += mem_size

																UpperCAmelCase__             =							inits[i].name
																UpperCAmelCase__             =							inits[j].name

																if name_i in dup_map:
																				dup_map[name_i].append(lowerCamelCase       )
																else:
																				UpperCAmelCase__             =							[name_j]
																ind_to_replace.append((j, i)       )

				print('total reduced size: '					,			total_reduced_size / 1_0_2_4 / 1_0_2_4 / 1_0_2_4					,			'GB'       )

				UpperCAmelCase__             =							sorted(lowerCamelCase       )
				_remove_dup_initializers_from_model(lowerCamelCase					,			lowerCamelCase					,			lowerCamelCase       )

				UpperCAmelCase__             =							'optimized_' + model_file_name
				UpperCAmelCase__             =							os.path.join(lowerCamelCase					,			lowerCamelCase       )
				onnx.save(lowerCamelCase					,			lowerCamelCase       )

				return new_model



