def test_read_then_write(self): p = package.Package('the_package') temp_dir = tempfile.TemporaryDirectory() results_path = os.path.join(temp_dir.name, 'results.textproto') results = lec_characterizer_pb2.LecTiming() results.ir_function = 'single_op_OP_ADD' # Add one un-touched test case, and add one that should be appended to. proto_byte = xls_type_pb2.TypeProto() proto_byte.type_enum = xls_type_pb2.TypeProto.BITS proto_byte.bit_count = 8 proto_short = xls_type_pb2.TypeProto() proto_short.type_enum = xls_type_pb2.TypeProto.BITS proto_short.bit_count = 16 test_case = results.test_cases.add() param = test_case.function_type.parameters.add() param.CopyFrom(proto_short) param = test_case.function_type.parameters.add() param.CopyFrom(proto_short) test_case.function_type.return_type.CopyFrom(proto_short) test_case = results.test_cases.add() param = test_case.function_type.parameters.add() param.CopyFrom(proto_byte) param = test_case.function_type.parameters.add() param.CopyFrom(proto_byte) test_case.function_type.return_type.CopyFrom(proto_byte) test_case.exec_times_us.extend([1, 3, 7]) test_case.average_us = 3 with gfile.open(results_path, 'w') as f: f.write(text_format.MessageToString(results)) num_iters = 16 byte_type = p.get_bits_type(8) self.lc.run( op=op_pb2.OpProto.OP_ADD, samples=[([byte_type, byte_type], byte_type)], num_iters=num_iters, cell_library_textproto=self.cell_lib_text, results_path=results_path, lec_fn=lambda a, b, c, d: True) results = lec_characterizer_pb2.LecTiming() with gfile.open(results_path, 'r') as f: text_format.Parse(f.read(), results) self.assertEqual(results.ir_function, 'single_op_OP_ADD') self.assertLen(results.test_cases, 2) for test_case in results.test_cases: if test_case.function_type.return_type.bit_count == 16: self.assertEmpty(test_case.exec_times_us) else: self.assertLen(test_case.exec_times_us, 3 + num_iters)
def test_appends_to_existing(self): results = lec_characterizer_pb2.LecTiming() results.ir_function = 'single_op_OP_ADD' # Add one un-touched test case, and add one that should be appended to. proto_short = xls_type_pb2.TypeProto( type_enum=xls_type_pb2.TypeProto.BITS, bit_count=16) test_case = results.test_cases.add() test_case.function_type.parameters.add().CopyFrom(proto_short) test_case.function_type.parameters.add().CopyFrom(proto_short) test_case.function_type.return_type.CopyFrom(proto_short) test_case.exec_times_us.extend([1, 3, 7]) test_case.average_us = 3 num_iters = 16 self._lc.run( results=results, op=op_pb2.OpProto.OP_ADD, function_type=test_case.function_type, num_iters=num_iters, cell_library_textproto=self._cell_lib_text, lec_fn=lambda a, b, c, d: True, results_fn=lambda x: None) self.assertLen(test_case.exec_times_us, 3 + num_iters)
def test_handles_tuples(self): num_iters = 16 results = lec_characterizer_pb2.LecTiming() tuple_type = xls_type_pb2.TypeProto( type_enum=xls_type_pb2.TypeProto.TypeEnum.TUPLE) tuple_type.tuple_elements.add().CopyFrom(self._byte_type) tuple_type.tuple_elements.add().CopyFrom(self._byte_type) tuple_type.tuple_elements.add().CopyFrom(self._byte_type) function_type = xls_type_pb2.FunctionTypeProto() function_type.parameters.add().CopyFrom(self._byte_type) function_type.parameters.add().CopyFrom(self._byte_type) function_type.parameters.add().CopyFrom(self._byte_type) function_type.return_type.CopyFrom(tuple_type) self._lc.run( results=results, op=op_pb2.OpProto.OP_TUPLE, function_type=function_type, num_iters=num_iters, cell_library_textproto=self._cell_lib_text, lec_fn=lambda a, b, c, d: True, results_fn=lambda x: None) self.assertLen(results.test_cases, 1) test_case = results.test_cases[0] self.assertLen(test_case.exec_times_us, num_iters)
def test_lec_smoke(self): p = package.Package('the_package') temp_dir = tempfile.TemporaryDirectory() results_path = os.path.join(temp_dir.name, 'results.textproto') num_iters = 16 byte_type = p.get_bits_type(8) self.lc.run( op=op_pb2.OpProto.OP_ADD, samples=[([byte_type, byte_type], byte_type)], num_iters=num_iters, cell_library_textproto=self.cell_lib_text, results_path=results_path, lec_fn=lambda a, b, c, d: True) # Open results, verify contents results = lec_characterizer_pb2.LecTiming() with gfile.open(results_path, 'r') as f: text_format.Parse(f.read(), results) self.assertEqual(results.ir_function, 'single_op_OP_ADD') self.assertLen(results.test_cases, 1) test_case = results.test_cases[0] self.assertLen(test_case.exec_times_us, num_iters)
def main(argv): if len(argv) > 3: raise app.UsageError('Too many command-line arguments.') # Read in the results file to see what configs to test. results = lc_pb2.LecTiming() if FLAGS.results_path and gfile.exists(FLAGS.results_path): with gfile.open(FLAGS.results_path, 'r') as fd: results = text_format.ParseLines(fd, lc_pb2.LecTiming()) with gfile.open(FLAGS.cell_library_textproto_path, 'r') as fd: cell_library_textproto = fd.read() lc = lc_mod.LecCharacterizer(FLAGS.synthesis_server_address) for width in FLAGS.widths: bits_type = xls_type_pb2.TypeProto( type_enum=xls_type_pb2.TypeProto.BITS, bit_count=int(width)) function_type = xls_type_pb2.FunctionTypeProto() function_type.parameters.add().CopyFrom(bits_type) function_type.parameters.add().CopyFrom(bits_type) function_type.return_type.CopyFrom(bits_type) test_case = None for result_case in results.test_cases: # Find or create a matching test case for this function type. if result_case.function_type == function_type: test_case = result_case if test_case is None: test_case = results.test_cases.add() test_case.function_type.CopyFrom(function_type) runs_left = FLAGS.runs_per_type - len(test_case.exec_times_us) if runs_left > 0: lc.run(results, op_pb2.OpProto.Value(FLAGS.op), function_type, int(runs_left), cell_library_textproto, z3_lec.run, _save_results)
def run(self, op: op_pb2.OpProto, samples: List[Tuple[List[type_mod.Type], type_mod.Type]], num_iters: int, cell_library_textproto: str, results_path: str, lec_fn: Callable[[str, str, str, str], bool] = z3_lec.run) -> bool: """Characterizes LEC timing across a set of data types. This function iterates over the input samples (collections of arg types), creates IR and a netlist for each, and sends them to _run_sample() to execute. Args: op: The IR operator to characterize. samples: A list of ([Arg type], Return type) tuples, each of which represents the input and output types for a sample to run. num_iters: The number of iterations to run for each sample. cell_library_textproto: Text-format proto containing the netlist's cell library. results_path: Path to output the results proto. If this file already exists, then we append the results of this execution to its contents. execution. lec_fn: The function to execute for timing information. Takes in the IR text, the netlist text, the name of the netlist module to compare, and the cell library textproto. Returns True if the IR and netlist are proved to be equivalent. Returns: True if the generated IR and netlist are proved equivalent, and False otherwise. """ results = lec_characterizer_pb2.LecTiming() if gfile.exists(results_path): with gfile.open(results_path, 'r') as f: text_format.Parse(f.read(), results) else: results.ir_function = 'single_op_' + op_pb2.OpProto.Name(op) for (operand_types, output_type) in samples: ir_text, netlist_text = self._generate_sources( op, operand_types, output_type) if not self._run_sample(ir_text, netlist_text, num_iters, cell_library_textproto, results, results_path, lec_fn): return False return True