def run_sample(smp: sample.Sample, run_dir: Text, summary_file: Optional[Text] = None, generate_sample_ns: Optional[int] = None): """Runs the given sample in the given directory. Args: smp: Sample to run. run_dir: Directory to run the sample in. The directory should exist and be empty. summary_file: The (optional) file to append sample summary. generate_sample_ns: The (optional) time in nanoseconds to generate the sample. Recorded in the summary file, if given. Raises: sample_runner.SampleError: on any non-zero status from the sample runner. """ start = time.time() _write_to_file(run_dir, 'sample.x', smp.input_text) _write_to_file(run_dir, 'options.json', smp.options.to_json()) if smp.args_batch: _write_to_file(run_dir, 'args.txt', sample.args_batch_to_text(smp.args_batch)) # Create a script named 'run.sh' for rerunning the sample. args = [ SAMPLE_RUNNER_MAIN_PATH, '--logtostderr', '--input_file=sample.x', '--options_file=options.json' ] if smp.args_batch: args.append('--args_file=args.txt') args.append(run_dir) _write_to_file(run_dir, 'run.sh', f'#!/bin/sh\n\n{subprocess.list2cmdline(args)}\n', executable=True) logging.vlog(1, 'Starting to run sample') logging.vlog(2, smp.input_text) runner = sample_runner.SampleRunner(run_dir) runner.run_from_files('sample.x', 'options.json', 'args.txt') timing = runner.timing timing.total_ns = int((time.time() - start) * 1e9) if generate_sample_ns: # The sample generation time, if given, is not part of the measured total # time, so add it in. timing.total_ns += generate_sample_ns timing.generate_sample_ns = generate_sample_ns logging.vlog(1, 'Completed running sample, elapsed: %0.2fs', time.time() - start) if summary_file: _write_ir_summaries(run_dir, timing, summary_file)
def run(self, smp: sample.Sample): """Runs the given sample. Args: smp: Sample to run. Raises: SampleError: If an error was encountered. """ if smp.options.input_is_dslx: input_filename = self._write_file('sample.x', smp.input_text) else: input_filename = self._write_file('sample.ir', smp.input_text) json_options_filename = self._write_file('options.json', smp.options.to_json()) args_filename = None if smp.args_batch: args_filename = self._write_file( 'args.txt', sample.args_batch_to_text(smp.args_batch)) self.run_from_files(input_filename, json_options_filename, args_filename)