@@ -31,6 +31,56 @@ constexpr float kMinProbeTimeUs = 1.0f;
3131
3232} // namespace
3333
34+ // Benchmark graphs are immutable after input upload, so the operator nodes can
35+ // be recorded repeatedly. Staging uploads/downloads are encoded once:
36+ // repeating them would redo host-visible copies on every repetition and bias
37+ // per-invocation timings.
38+ RepeatedGraphExecutor::RepeatedGraphExecutor (
39+ ComputeGraph& graph,
40+ int repetitions,
41+ OpNodeRange op_nodes)
42+ : graph_(graph) {
43+ VK_CHECK_COND (repetitions > 0 );
44+ VK_CHECK_COND (op_nodes.begin <= op_nodes.end );
45+ VK_CHECK_COND (op_nodes.end <= graph_.execute_nodes ().size ());
46+
47+ api::Context* const context = graph_.context ();
48+ context->flush ();
49+ context->set_cmd (/* reusable=*/ true );
50+ context->cmd_reset_querypool ();
51+
52+ for (size_t i = 0 ; i < op_nodes.begin ; ++i) {
53+ graph_.execute_nodes ()[i]->encode (&graph_);
54+ }
55+ for (int i = 0 ; i < repetitions; ++i) {
56+ for (size_t j = op_nodes.begin ; j < op_nodes.end ; ++j) {
57+ graph_.execute_nodes ()[j]->encode (&graph_);
58+ }
59+ }
60+ for (size_t i = op_nodes.end ; i < graph_.execute_nodes ().size (); ++i) {
61+ graph_.execute_nodes ()[i]->encode (&graph_);
62+ }
63+
64+ command_ =
65+ std::make_unique<vkapi::CommandBuffer>(std::move (context->extract_cmd ()));
66+ }
67+
68+ void RepeatedGraphExecutor::execute () {
69+ api::Context* const context = graph_.context ();
70+ command_->end ();
71+
72+ // Intentionally bypasses Context::submit_cmd_to_gpu(): its submit-count
73+ // bookkeeping and threshold-split path only serve submit_compute_job
74+ // recording, which benchmarks don't use.
75+ vkapi::VulkanFence fence = context->fences ().get_fence ();
76+ context->adapter_ptr ()->submit_cmd (
77+ context->queue (),
78+ command_->get_submit_handle (/* final_use=*/ false ),
79+ fence.get_submit_handle ());
80+ fence.wait ();
81+ context->fences ().return_fence (fence);
82+ }
83+
3484int get_seed () {
3585 static int seed = 42 ;
3686 return seed++;
@@ -1433,17 +1483,23 @@ int64_t default_flop_calculator(const TestCase& test_case) {
14331483 return total_elements;
14341484}
14351485
1436- ComputeGraph setup_compute_graph (
1486+ BenchmarkGraph setup_compute_graph (
14371487 TestCase& test_case,
14381488 std::string op_name,
14391489 int op_invocations_per_execute) {
14401490 GraphConfig config;
14411491 config.enable_querypool = true ;
1492+ // Pool sizing takes max(execute, prepack) * factor, so scaling the factor
1493+ // also over-reserves the prepack side (encoded once). Accepted: precise
1494+ // execute-only sizing would need runtime changes.
1495+ config.descriptor_pool_safety_factor *=
1496+ std::max (1 , op_invocations_per_execute);
14421497 // Default-on (opt-out via TestCase::set_force_resize(false)): force every
1443- // DynamicDispatchNode to run its resize function on each execute(),
1444- // exercising the op's resize formula even when input shapes are unchanged.
1498+ // DynamicDispatchNode to run its resize function when execute_test_case
1499+ // runs propagate_resize() after prepack, exercising the op's resize formula
1500+ // even when input shapes are unchanged.
14451501 config.force_resize = test_case.get_force_resize ();
1446- ComputeGraph graph (config);
1502+ auto graph = std::make_unique<ComputeGraph> (config);
14471503
14481504 std::vector<ValueRef> input_values;
14491505
@@ -1452,17 +1508,17 @@ ComputeGraph setup_compute_graph(
14521508 const ValueSpec& input_spec = test_case.inputs ()[i];
14531509
14541510 if (input_spec.is_none ()) {
1455- input_values.push_back (graph. add_none ());
1511+ input_values.push_back (graph-> add_none ());
14561512 } else if (input_spec.is_float ()) {
14571513 ValueRef input_value =
1458- graph. add_scalar (static_cast <double >(input_spec.get_float_value ()));
1514+ graph-> add_scalar (static_cast <double >(input_spec.get_float_value ()));
14591515 input_values.push_back (input_value);
14601516 } else if (input_spec.is_int ()) {
14611517 ValueRef input_value =
1462- graph. add_scalar (static_cast <int64_t >(input_spec.get_int_value ()));
1518+ graph-> add_scalar (static_cast <int64_t >(input_spec.get_int_value ()));
14631519 input_values.push_back (input_value);
14641520 } else if (input_spec.is_bool ()) {
1465- ValueRef input_value = graph. add_scalar (input_spec.get_bool_value ());
1521+ ValueRef input_value = graph-> add_scalar (input_spec.get_bool_value ());
14661522 input_values.push_back (input_value);
14671523 } else if (input_spec.is_int_list ()) {
14681524 // Convert int32_t list to int64_t list for ComputeGraph
@@ -1472,20 +1528,20 @@ ComputeGraph setup_compute_graph(
14721528 for (int32_t val : int32_list) {
14731529 int64_list.push_back (static_cast <int64_t >(val));
14741530 }
1475- ValueRef input_value = graph. add_scalar_list (std::move (int64_list));
1531+ ValueRef input_value = graph-> add_scalar_list (std::move (int64_list));
14761532 input_values.push_back (input_value);
14771533 } else if (input_spec.is_string ()) {
14781534 std::string str_copy = input_spec.get_string_value ();
1479- ValueRef input_value = graph. add_string (std::move (str_copy));
1535+ ValueRef input_value = graph-> add_string (std::move (str_copy));
14801536 input_values.push_back (input_value);
14811537 } else if (input_spec.is_constant ()) {
1482- ValueRef input_value = graph. add_tensorref (
1538+ ValueRef input_value = graph-> add_tensorref (
14831539 input_spec.get_tensor_sizes (),
14841540 input_spec.dtype ,
14851541 input_spec.get_data_ptr ());
14861542 input_values.push_back (input_value);
14871543 } else {
1488- IOValueRef input_io = graph. add_input_tensor (
1544+ IOValueRef input_io = graph-> add_input_tensor (
14891545 input_spec.get_tensor_sizes (),
14901546 input_spec.dtype ,
14911547 input_spec.storage_type ,
@@ -1505,7 +1561,7 @@ ComputeGraph setup_compute_graph(
15051561 }
15061562
15071563 // Create output tensor
1508- ValueRef output_value = graph. add_tensor (
1564+ ValueRef output_value = graph-> add_tensor (
15091565 output_spec.get_tensor_sizes (),
15101566 output_spec.dtype ,
15111567 output_spec.storage_type ,
@@ -1521,17 +1577,16 @@ ComputeGraph setup_compute_graph(
15211577 std::vector<ValueRef> op_args = input_values;
15221578 op_args.insert (op_args.end (), output_values.begin (), output_values.end ());
15231579
1524- // Invoke the op op_invocations_per_execute times to stack dispatches per
1525- // graph.execute(). The output set_output_value() calls below still happen
1526- // exactly once.
1527- for (int i = 0 ; i < op_invocations_per_execute; ++i) {
1528- opFn (graph, op_args);
1529- }
1580+ // Nodes added before the op are staging uploads; nodes added after are
1581+ // staging downloads. Only the op's own nodes are repeated by benchmarks.
1582+ const size_t op_begin = graph->execute_nodes ().size ();
1583+ opFn (*graph, op_args);
1584+ const size_t op_end = graph->execute_nodes ().size ();
15301585
15311586 for (size_t i = 0 ; i < output_values.size (); ++i) {
1532- graph. set_output_value (output_values[i]);
1587+ graph-> set_output_value (output_values[i]);
15331588 }
1534- return graph;
1589+ return { std::move ( graph), {op_begin, op_end}} ;
15351590}
15361591
15371592// Test execution utilities
@@ -1549,16 +1604,20 @@ BenchmarkResult execute_test_case(
15491604 api::context ()->initialize_querypool ();
15501605 }
15511606
1552- // Build the measurement graph with the requested chained_dispatches factor.
1553- // The caller (typically execute_test_cases) decides what it should be —
1554- // this function is a pure "run at the given chained_dispatches" primitive.
1555- ComputeGraph graph = setup_compute_graph (
1607+ // Build the operator once. Benchmark repetition is encoded separately so
1608+ // persistent graph allocations are not duplicated.
1609+ BenchmarkGraph benchmark = setup_compute_graph (
15561610 test_case, test_case.operator_name (), chained_dispatches);
1611+ ComputeGraph& graph = *benchmark.graph ;
15571612
15581613 // Prepare the graph
15591614 graph.prepare ();
15601615 graph.prepack ();
15611616
1617+ // Run resize functions once so force_resize exercises resize formulas even
1618+ // though the record/replay path below never calls propagate_resize().
1619+ graph.propagate_resize ();
1620+
15621621 // Copy input data into the graph's staging buffers
15631622 size_t graph_input_idx = 0 ;
15641623 for (size_t i = 0 ; i < test_case.num_inputs (); ++i) {
@@ -1605,9 +1664,12 @@ BenchmarkResult execute_test_case(
16051664 ++graph_input_idx;
16061665 }
16071666
1667+ RepeatedGraphExecutor graph_executor (
1668+ graph, chained_dispatches, benchmark.op_nodes );
1669+
16081670 // Warmup runs
16091671 for (int run = 0 ; run < warmup_runs; ++run) {
1610- graph .execute ();
1672+ graph_executor .execute ();
16111673 }
16121674
16131675 // Benchmark runs - collect individual iteration timings
@@ -1619,7 +1681,7 @@ BenchmarkResult execute_test_case(
16191681 for (int run = 0 ; run < benchmark_runs; ++run) {
16201682 // Measure CPU time for each execute() call
16211683 auto cpu_start = std::chrono::high_resolution_clock::now ();
1622- graph .execute ();
1684+ graph_executor .execute ();
16231685 auto cpu_end = std::chrono::high_resolution_clock::now ();
16241686
16251687 auto cpu_duration = std::chrono::duration_cast<std::chrono::microseconds>(
0 commit comments