Skip to content

Commit

Permalink
Fuzzer: stop creating args/channel data/channel names files.
Browse files Browse the repository at this point in the history
These are now all covered by testvector.textproto.

Issues: #1645
PiperOrigin-RevId: 694679325
  • Loading branch information
hzeller authored and copybara-github committed Nov 9, 2024
1 parent 887923d commit 9552b36
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 147 deletions.
18 changes: 2 additions & 16 deletions xls/fuzzer/run_fuzz.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,6 @@ absl::Status RunSample(const Sample& smp, const std::filesystem::path& run_dir,
XLS_RETURN_IF_ERROR(SetTextProtoFile(testvector_path, testvector));
argv.push_back("--testvector_textproto=testvector.pbtxt");

std::filesystem::path args_file_name = run_dir / "args.txt";
XLS_RETURN_IF_ERROR(
SetFileContents(args_file_name, ArgsBatchToText(smp.args_batch())));

std::optional<std::filesystem::path> ir_channel_names_file_name =
std::nullopt;
if (!smp.ir_channel_names().empty()) {
ir_channel_names_file_name = run_dir / "ir_channel_names.txt";
XLS_RETURN_IF_ERROR(
SetFileContents(*ir_channel_names_file_name,
IrChannelNamesToText(smp.ir_channel_names())));
}

argv.push_back("\"$RUNDIR\"");

std::filesystem::path run_script_path = run_dir / "run.sh";
Expand All @@ -242,9 +229,8 @@ cd "$RUNDIR"
VLOG(1) << "Starting to run sample";
VLOG(2) << smp.input_text();
SampleRunner runner(run_dir);
XLS_RETURN_IF_ERROR(
runner.RunFromFiles(sample_file_name, options_file_name, args_file_name,
ir_channel_names_file_name, testvector_path));
XLS_RETURN_IF_ERROR(runner.RunFromFiles(sample_file_name, options_file_name,
testvector_path));

fuzzer::SampleTimingProto timing = runner.timing();

Expand Down
10 changes: 3 additions & 7 deletions xls/fuzzer/run_fuzz_multiprocess_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_two_samples(self):
sample1_contents = os.listdir(os.path.join(samples_path, 'worker0-sample0'))
self.assertIn('sample.x', sample1_contents)
self.assertIn('sample.x.results', sample1_contents)
self.assertIn('args.txt', sample1_contents)
self.assertIn('testvector.pbtxt', sample1_contents)
self.assertIn('sample.ir', sample1_contents)
self.assertIn('sample.ir.results', sample1_contents)
self.assertIn('sample.opt.ir', sample1_contents)
Expand All @@ -62,10 +62,6 @@ def test_two_samples(self):
self.assertNotIn('sample.v', sample1_contents)
self.assertNotIn('sample.sv', sample1_contents)

# Args file should have three lines in it.
with open(os.path.join(samples_path, 'worker0-sample0', 'args.txt')) as f:
self.assertEqual(len(f.read().strip().splitlines()), 3)

def test_multiple_workers(self):
crasher_path = self.create_tempdir().full_path
samples_path = self.create_tempdir().full_path
Expand Down Expand Up @@ -147,7 +143,7 @@ def test_codegen_and_simulate(self):
sample1_contents = os.listdir(os.path.join(samples_path, 'worker0-sample0'))
self.assertIn('sample.x', sample1_contents)
self.assertIn('sample.x.results', sample1_contents)
self.assertIn('args.txt', sample1_contents)
self.assertIn('testvector.pbtxt', sample1_contents)
self.assertIn('sample.ir', sample1_contents)
self.assertIn('sample.ir.results', sample1_contents)
self.assertIn('sample.opt.ir', sample1_contents)
Expand Down Expand Up @@ -196,7 +192,7 @@ def test_codegen_and_simulate_with_proc(self):
sample1_contents = os.listdir(os.path.join(samples_path, 'worker0-sample0'))
self.assertIn('sample.x', sample1_contents)
self.assertIn('sample.x.results', sample1_contents)
self.assertIn('args.txt', sample1_contents)
self.assertIn('testvector.pbtxt', sample1_contents)
self.assertIn('sample.ir', sample1_contents)
self.assertIn('sample.ir.results', sample1_contents)
self.assertIn('sample.opt.ir', sample1_contents)
Expand Down
2 changes: 2 additions & 0 deletions xls/fuzzer/sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ class Sample {
// file. Crashers may be checked in as tests in `xls/fuzzer/crashers/`.
std::string ToCrasher(std::string_view error_message) const;

// TODO(google/xls#1645) args batch and channel names should be sample inputs
// proto instead.
Sample(std::string input_text, SampleOptions options,
std::vector<std::vector<dslx::InterpValue>> args_batch,
std::vector<std::string> ir_channel_names = {})
Expand Down
103 changes: 23 additions & 80 deletions xls/fuzzer/sample_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -839,29 +839,13 @@ absl::Status SampleRunner::Run(const Sample& sample) {
XLS_RETURN_IF_ERROR(sample.FillSampleInputs(&testvector));
XLS_RETURN_IF_ERROR(SetTextProtoFile(testvector_path, testvector));

// TODO(hzeller): retire the following files and use testvector
std::filesystem::path args_path = run_dir_ / "args.txt";
XLS_RETURN_IF_ERROR(
SetFileContents(args_path, ArgsBatchToText(sample.args_batch())));

std::optional<std::filesystem::path> ir_channel_names_path = std::nullopt;
if (!sample.ir_channel_names().empty()) {
ir_channel_names_path = run_dir_ / "ir_channel_names.txt";
XLS_RETURN_IF_ERROR(
SetFileContents(*ir_channel_names_path,
IrChannelNamesToText(sample.ir_channel_names())));
}

return RunFromFiles(input_path, options_path, args_path,
ir_channel_names_path, testvector_path);
return RunFromFiles(input_path, options_path, testvector_path);
}

absl::Status SampleRunner::RunFromFiles(
const std::filesystem::path& input_path,
const std::filesystem::path& options_path,
const std::optional<std::filesystem::path>& args_path, // deprecated
const std::optional<std::filesystem::path>& ir_channel_names_path, // ditto
const std::optional<std::filesystem::path>& testvector_path) {
const std::filesystem::path& testvector_path) {
VLOG(1) << "Running sample in directory " << run_dir_;
VLOG(1) << "Reading sample files.";

Expand All @@ -875,11 +859,10 @@ absl::Status SampleRunner::RunFromFiles(
absl::Status status;
switch (options.sample_type()) {
case fuzzer::SampleType::SAMPLE_TYPE_FUNCTION:
status = RunFunction(input_path, options, std::nullopt, testvector_path);
status = RunFunction(input_path, options, testvector_path);
break;
case fuzzer::SampleType::SAMPLE_TYPE_PROC:
status = RunProc(input_path, options, std::nullopt, std::nullopt,
testvector_path);
status = RunProc(input_path, options, testvector_path);
break;
default:
status = absl::InvalidArgumentError(
Expand All @@ -903,23 +886,21 @@ absl::Status SampleRunner::RunFromFiles(

absl::Status SampleRunner::RunFunction(
const std::filesystem::path& input_path, const SampleOptions& options,
const std::optional<std::filesystem::path>& args_path,
const std::optional<std::filesystem::path>& testvector_path) {
const std::filesystem::path& testvector_path) {
XLS_ASSIGN_OR_RETURN(std::string input_text, GetFileContents(input_path));

std::optional<ArgsBatch> args_batch = std::nullopt;
if (args_path.has_value()) {
XLS_ASSIGN_OR_RETURN(std::string args_text, GetFileContents(*args_path));
XLS_ASSIGN_OR_RETURN(args_batch, dslx::ParseArgsBatch(args_text));
} else if (testvector_path.has_value()) {
{
XLS_RET_CHECK(!testvector_path.empty());
testvector::SampleInputsProto sample_inputs;
XLS_RETURN_IF_ERROR(ParseTextProtoFile(*testvector_path, &sample_inputs));
XLS_RETURN_IF_ERROR(ParseTextProtoFile(testvector_path, &sample_inputs));
ArgsBatch extracted;
XLS_RETURN_IF_ERROR(Sample::ExtractArgsBatch(/* is_proc_samples= */ false,
sample_inputs, extracted));
args_batch = std::move(extracted);
}

// Results from various ways of interpretation.
absl::flat_hash_map<std::string, std::vector<dslx::InterpValue>> results;

std::filesystem::path ir_path;
Expand Down Expand Up @@ -954,14 +935,14 @@ absl::Status SampleRunner::RunFunction(
// Unconditionally evaluate with the interpreter even if using the JIT. This
// exercises the interpreter and serves as a reference.
XLS_ASSIGN_OR_RETURN(results["evaluated unopt IR (interpreter)"],
EvaluateIrFunction(ir_path, *testvector_path, false,
EvaluateIrFunction(ir_path, testvector_path, false,
options, run_dir_, commands_));
timing_.set_unoptimized_interpret_ir_ns(
absl::ToInt64Nanoseconds(t.GetElapsedTime()));

if (options.use_jit()) {
XLS_ASSIGN_OR_RETURN(results["evaluated unopt IR (JIT)"],
EvaluateIrFunction(ir_path, *testvector_path, true,
EvaluateIrFunction(ir_path, testvector_path, true,
options, run_dir_, commands_));
timing_.set_unoptimized_jit_ns(
absl::ToInt64Nanoseconds(t.GetElapsedTime()));
Expand All @@ -979,15 +960,15 @@ absl::Status SampleRunner::RunFunction(
t.Reset();
XLS_ASSIGN_OR_RETURN(
results["evaluated opt IR (JIT)"],
EvaluateIrFunction(opt_ir_path, *testvector_path, true, options,
EvaluateIrFunction(opt_ir_path, testvector_path, true, options,
run_dir_, commands_));
timing_.set_optimized_jit_ns(
absl::ToInt64Nanoseconds(t.GetElapsedTime()));
}
t.Reset();
XLS_ASSIGN_OR_RETURN(
results["evaluated opt IR (interpreter)"],
EvaluateIrFunction(opt_ir_path, *testvector_path, false, options,
EvaluateIrFunction(opt_ir_path, testvector_path, false, options,
run_dir_, commands_));
timing_.set_optimized_interpret_ir_ns(
absl::ToInt64Nanoseconds(t.GetElapsedTime()));
Expand All @@ -1001,11 +982,12 @@ absl::Status SampleRunner::RunFunction(
timing_.set_codegen_ns(absl::ToInt64Nanoseconds(t.GetElapsedTime()));

if (options.simulate()) {
XLS_RET_CHECK(args_batch.has_value());
t.Reset();
XLS_ASSIGN_OR_RETURN(
results["simulated"],
SimulateFunction(verilog_path, "module_sig.textproto",
*testvector_path, options, run_dir_, commands_));
testvector_path, options, run_dir_, commands_));
timing_.set_simulate_ns(absl::ToInt64Nanoseconds(t.GetElapsedTime()));
}
}
Expand All @@ -1019,25 +1001,15 @@ absl::Status SampleRunner::RunFunction(

absl::Status SampleRunner::RunProc(
const std::filesystem::path& input_path, const SampleOptions& options,
const std::optional<std::filesystem::path>& args_path,
const std::optional<std::filesystem::path>& ir_channel_names_path,
const std::optional<std::filesystem::path>& testvector_path) {
const std::filesystem::path& testvector_path) {
XLS_ASSIGN_OR_RETURN(std::string input_text, GetFileContents(input_path));

std::optional<ArgsBatch> args_batch = std::nullopt;
std::optional<std::vector<std::string>> ir_channel_names = std::nullopt;

if (args_path.has_value()) {
XLS_ASSIGN_OR_RETURN(std::string args_text, GetFileContents(*args_path));
XLS_ASSIGN_OR_RETURN(args_batch, dslx::ParseArgsBatch(args_text));
if (ir_channel_names_path.has_value()) {
XLS_ASSIGN_OR_RETURN(std::string ir_channel_names_text,
GetFileContents(*ir_channel_names_path));
ir_channel_names = ParseIrChannelNames(ir_channel_names_text);
}
} else if (testvector_path.has_value()) {
{
testvector::SampleInputsProto sample_inputs;
XLS_RETURN_IF_ERROR(ParseTextProtoFile(*testvector_path, &sample_inputs));
XLS_RETURN_IF_ERROR(ParseTextProtoFile(testvector_path, &sample_inputs));
ArgsBatch extracted_args;
std::vector<std::string> extracted_channel_names;
XLS_RETURN_IF_ERROR(Sample::ExtractArgsBatch(/* is_proc_samples= */ true,
Expand All @@ -1047,35 +1019,6 @@ absl::Status SampleRunner::RunProc(
ir_channel_names = std::move(extracted_channel_names);
}

std::string ir_channel_values_file_content;
if (args_batch.has_value() && ir_channel_names.has_value()) {
absl::flat_hash_map<std::string, std::vector<dslx::InterpValue>>
all_channel_values;
for (const std::vector<dslx::InterpValue>& channel_values : *args_batch) {
if (channel_values.size() != ir_channel_names->size()) {
return absl::InvalidArgumentError(
absl::StrFormat("Invalid number of values in args_batch sample; "
"expected: %d, got: %d",
ir_channel_names->size(), channel_values.size()));
}
for (int i = 0; i < ir_channel_names->size(); ++i) {
all_channel_values[ir_channel_names->at(i)].push_back(
channel_values[i]);
}
}

absl::flat_hash_map<std::string, std::vector<Value>> ir_channel_values;
for (const auto& [key, values] : all_channel_values) {
XLS_ASSIGN_OR_RETURN(ir_channel_values[key],
dslx::InterpValue::ConvertValuesToIr(values));
}
ir_channel_values_file_content = ChannelValuesToString(ir_channel_values);
}
std::filesystem::path ir_channel_values_path =
run_dir_ / "channel_inputs.txt";
XLS_RETURN_IF_ERROR(
SetFileContents(ir_channel_values_path, ir_channel_values_file_content));

// Special case: When there no inputs for a proc, typically when there are no
// channels for a proc, tick_count results to 0. Set the tick_count to a
// non-zero value to execute in the eval proc main (bypasses a restriction on
Expand Down Expand Up @@ -1125,7 +1068,7 @@ absl::Status SampleRunner::RunProc(
// exercises the interpreter and serves as a reference.
Stopwatch t;
XLS_ASSIGN_OR_RETURN(results["evaluated unopt IR (interpreter)"],
EvaluateIrProc(ir_path, tick_count, *testvector_path,
EvaluateIrProc(ir_path, tick_count, testvector_path,
false, options, run_dir_, commands_));
if (!reference.has_value()) {
reference = results["evaluated unopt IR (interpreter)"];
Expand All @@ -1136,7 +1079,7 @@ absl::Status SampleRunner::RunProc(
if (options.use_jit()) {
t.Reset();
XLS_ASSIGN_OR_RETURN(results["evaluated unopt IR (JIT)"],
EvaluateIrProc(ir_path, tick_count, *testvector_path,
EvaluateIrProc(ir_path, tick_count, testvector_path,
true, options, run_dir_, commands_));
timing_.set_unoptimized_jit_ns(
absl::ToInt64Nanoseconds(t.GetElapsedTime()));
Expand All @@ -1155,7 +1098,7 @@ absl::Status SampleRunner::RunProc(
t.Reset();
XLS_ASSIGN_OR_RETURN(
results["evaluated opt IR (JIT)"],
EvaluateIrProc(*opt_ir_path, tick_count, *testvector_path, true,
EvaluateIrProc(*opt_ir_path, tick_count, testvector_path, true,
options, run_dir_, commands_));
timing_.set_optimized_jit_ns(
absl::ToInt64Nanoseconds(t.GetElapsedTime()));
Expand All @@ -1164,7 +1107,7 @@ absl::Status SampleRunner::RunProc(
t.Reset();
XLS_ASSIGN_OR_RETURN(
results["evaluated opt IR (interpreter)"],
EvaluateIrProc(*opt_ir_path, tick_count, *testvector_path, false,
EvaluateIrProc(*opt_ir_path, tick_count, testvector_path, false,
options, run_dir_, commands_));
timing_.set_optimized_interpret_ir_ns(
absl::ToInt64Nanoseconds(t.GetElapsedTime()));
Expand All @@ -1186,7 +1129,7 @@ absl::Status SampleRunner::RunProc(
XLS_ASSIGN_OR_RETURN(
results["simulated"],
SimulateProc(verilog_path, "module_sig.textproto",
*testvector_path, output_channel_counts_str, options,
testvector_path, output_channel_counts_str, options,
run_dir_, commands_));
timing_.set_simulate_ns(absl::ToInt64Nanoseconds(t.GetElapsedTime()));
}
Expand Down
27 changes: 10 additions & 17 deletions xls/fuzzer/sample_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,21 @@ class SampleRunner {

// Runs the provided files as a sample, writing out only outputs under the
// SampleRunner's `run_dir`.
absl::Status RunFromFiles(
const std::filesystem::path& input_path,
const std::filesystem::path& options_path,
const std::optional<std::filesystem::path>& args_path, // deprecated
const std::optional<std::filesystem::path>&
ir_channel_names_path, // same
const std::optional<std::filesystem::path>& testvector_path);
absl::Status RunFromFiles(const std::filesystem::path& input_path,
const std::filesystem::path& options_path,
const std::filesystem::path& testvector_path);

const fuzzer::SampleTimingProto& timing() const { return timing_; }

private:
// Runs a sample with a function as the top which is read from files.
absl::Status RunFunction(
const std::filesystem::path& input_path, const SampleOptions& options,
const std::optional<std::filesystem::path>& args_path,
const std::optional<std::filesystem::path>& testvector_path);

absl::Status RunProc(
const std::filesystem::path& input_path, const SampleOptions& options,
const std::optional<std::filesystem::path>& args_path,
const std::optional<std::filesystem::path>& ir_channel_names_path,
const std::optional<std::filesystem::path>& testvector_path);
absl::Status RunFunction(const std::filesystem::path& input_path,
const SampleOptions& options,
const std::filesystem::path& testvector_path);

absl::Status RunProc(const std::filesystem::path& input_path,
const SampleOptions& options,
const std::filesystem::path& testvector_path);

const std::filesystem::path run_dir_;
const Commands commands_;
Expand Down
Loading

0 comments on commit 9552b36

Please sign in to comment.