Skip to content

Commit c0bb730

Browse files
move movable_blocks and movable_blocks_per_type from placement context to BlkLocRegistry
1 parent 943f9db commit c0bb730

File tree

7 files changed

+70
-38
lines changed

7 files changed

+70
-38
lines changed

vpr/src/place/RL_agent_util.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include "RL_agent_util.h"
2+
23
#include "static_move_generator.h"
34
#include "manual_move_generator.h"
5+
#include "placer_state.h"
6+
47

58
std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create_move_generators(PlacerState& placer_state,
69
const t_placer_opts& placer_opts,
@@ -60,6 +63,13 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
6063
second_state_avail_moves.push_back(e_move_type::NOC_ATTRACTION_CENTROID);
6164
}
6265

66+
std::vector<int> num_movable_blocks_per_type;
67+
std::ranges::transform(placer_state.blk_loc_registry().movable_blocks_per_type(),
68+
std::back_inserter(num_movable_blocks_per_type),
69+
[](const auto& innerVec) noexcept {
70+
return innerVec.size();
71+
});
72+
6373
if (placer_opts.place_agent_algorithm == e_agent_algorithm::E_GREEDY) {
6474
std::unique_ptr<EpsilonGreedyAgent> karmed_bandit_agent1, karmed_bandit_agent2;
6575
//agent's 1st state
@@ -68,13 +78,15 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
6878
karmed_bandit_agent1 = std::make_unique<EpsilonGreedyAgent>(first_state_avail_moves,
6979
e_agent_space::MOVE_BLOCK_TYPE,
7080
placer_opts.place_agent_epsilon,
71-
rng);
81+
rng,
82+
num_movable_blocks_per_type);
7283
} else {
7384
VTR_LOG("Using simple RL 'Epsilon Greedy agent' for choosing move types\n");
7485
karmed_bandit_agent1 = std::make_unique<EpsilonGreedyAgent>(first_state_avail_moves,
7586
e_agent_space::MOVE_TYPE,
7687
placer_opts.place_agent_epsilon,
77-
rng);
88+
rng,
89+
num_movable_blocks_per_type);
7890
}
7991
karmed_bandit_agent1->set_step(placer_opts.place_agent_gamma, move_lim);
8092
move_generators.first = std::make_unique<SimpleRLMoveGenerator>(placer_state,
@@ -87,7 +99,8 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
8799
karmed_bandit_agent2 = std::make_unique<EpsilonGreedyAgent>(second_state_avail_moves,
88100
e_agent_space::MOVE_TYPE,
89101
placer_opts.place_agent_epsilon,
90-
rng);
102+
rng,
103+
num_movable_blocks_per_type);
91104
karmed_bandit_agent2->set_step(placer_opts.place_agent_gamma, move_lim);
92105
move_generators.second = std::make_unique<SimpleRLMoveGenerator>(placer_state,
93106
reward_fun,
@@ -102,12 +115,14 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
102115
VTR_LOG("Using simple RL 'Softmax agent' for choosing move and block types\n");
103116
karmed_bandit_agent1 = std::make_unique<SoftmaxAgent>(first_state_avail_moves,
104117
e_agent_space::MOVE_BLOCK_TYPE,
105-
rng);
118+
rng,
119+
num_movable_blocks_per_type);
106120
} else {
107121
VTR_LOG("Using simple RL 'Softmax agent' for choosing move types\n");
108122
karmed_bandit_agent1 = std::make_unique<SoftmaxAgent>(first_state_avail_moves,
109123
e_agent_space::MOVE_TYPE,
110-
rng);
124+
rng,
125+
num_movable_blocks_per_type);
111126
}
112127
karmed_bandit_agent1->set_step(placer_opts.place_agent_gamma, move_lim);
113128
move_generators.first = std::make_unique<SimpleRLMoveGenerator>(placer_state,
@@ -119,7 +134,8 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
119134
//agent's 2nd state
120135
karmed_bandit_agent2 = std::make_unique<SoftmaxAgent>(second_state_avail_moves,
121136
e_agent_space::MOVE_TYPE,
122-
rng);
137+
rng,
138+
num_movable_blocks_per_type);
123139
karmed_bandit_agent2->set_step(placer_opts.place_agent_gamma, move_lim);
124140
move_generators.second = std::make_unique<SimpleRLMoveGenerator>(placer_state,
125141
reward_fun,

vpr/src/place/move_generators/move_generator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void MoveGenerator::calculate_reward_and_process_outcome(const MoveOutcomeStats&
5151
}
5252
}
5353

54-
void MoveTypeStat::print_placement_move_types_stats() const {
54+
void MoveTypeStat::print_placement_move_types_stats(const std::vector<std::vector<ClusterBlockId>>& movable_blocks_per_type) const {
5555
VTR_LOG("\n\nPlacement perturbation distribution by block and move type: \n");
5656

5757
VTR_LOG(
@@ -71,9 +71,9 @@ void MoveTypeStat::print_placement_move_types_stats() const {
7171
int num_of_avail_moves = blk_type_moves.size() / device_ctx.logical_block_types.size();
7272

7373
//Print placement information for each block type
74-
for (const auto& itype : device_ctx.logical_block_types) {
74+
for (const t_logical_block_type& itype : device_ctx.logical_block_types) {
7575
//Skip non-existing block types in the netlist
76-
if (itype.index == 0 || movable_blocks_per_type(itype).empty()) {
76+
if (itype.index == 0 || movable_blocks_per_type[itype.index].empty()) {
7777
continue;
7878
}
7979

vpr/src/place/move_generators/move_generator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct MoveTypeStat {
3737
/**
3838
* @brief Prints placement perturbation distribution by block and move type.
3939
*/
40-
void print_placement_move_types_stats() const;
40+
void print_placement_move_types_stats(const std::vector<std::vector<ClusterBlockId>>& movable_blocks_per_type) const;
4141

4242
inline void incr_blk_type_moves(const t_propose_action& proposed_action) {
4343
if (proposed_action.logical_blk_type_index != -1) { //if the agent proposed the block type, then collect the block type stat

vpr/src/place/move_generators/simpleRL_move_generator.cpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ void SimpleRLMoveGenerator::process_outcome(double reward, e_reward_function rew
3737
* K-Armed bandit agent implementation *
3838
* *
3939
* */
40-
KArmedBanditAgent::KArmedBanditAgent(std::vector<e_move_type> available_moves, e_agent_space agent_space, vtr::RngContainer& rng)
40+
KArmedBanditAgent::KArmedBanditAgent(std::vector<e_move_type> available_moves,
41+
e_agent_space agent_space,
42+
vtr::RngContainer& rng,
43+
const std::vector<int>& num_movable_blocks_per_type)
4144
: available_moves_(std::move(available_moves))
4245
, propose_blk_type_(agent_space == e_agent_space::MOVE_BLOCK_TYPE)
4346
, rng_(rng) {
44-
std::vector<int> available_logical_block_types = get_available_logical_blk_types_();
47+
std::vector<int> available_logical_block_types = get_available_logical_blk_types_(num_movable_blocks_per_type);
4548
num_available_types_ = available_logical_block_types.size();
4649

4750
size_t num_available_moves = available_moves_.size();
@@ -89,7 +92,7 @@ int KArmedBanditAgent::action_to_blk_type_(const size_t action_idx) {
8992
}
9093
}
9194

92-
std::vector<int> KArmedBanditAgent::get_available_logical_blk_types_() {
95+
std::vector<int> KArmedBanditAgent::get_available_logical_blk_types_(const std::vector<int>& num_movable_blocks_per_type) {
9396
const auto& device_ctx = g_vpr_ctx.device();
9497

9598
std::vector<int> available_blk_types;
@@ -99,9 +102,9 @@ std::vector<int> KArmedBanditAgent::get_available_logical_blk_types_() {
99102
continue;
100103
}
101104

102-
const auto& blk_per_type = movable_blocks_per_type(logical_blk_type);
105+
int num_blk_per_type = num_movable_blocks_per_type[logical_blk_type.index];
103106

104-
if (!blk_per_type.empty()) {
107+
if (num_blk_per_type > 0) {
105108
available_blk_types.push_back(logical_blk_type.index);
106109
}
107110
}
@@ -192,8 +195,12 @@ int KArmedBanditAgent::agent_to_phy_blk_type(const int idx) {
192195
* E-greedy agent implementation *
193196
* *
194197
* */
195-
EpsilonGreedyAgent::EpsilonGreedyAgent(std::vector<e_move_type> available_moves, e_agent_space agent_space, float epsilon, vtr::RngContainer& rng)
196-
: KArmedBanditAgent(std::move(available_moves), agent_space, rng) {
198+
EpsilonGreedyAgent::EpsilonGreedyAgent(std::vector<e_move_type> available_moves,
199+
e_agent_space agent_space,
200+
float epsilon,
201+
vtr::RngContainer& rng,
202+
const std::vector<int>& num_movable_blocks_per_type)
203+
: KArmedBanditAgent(std::move(available_moves), agent_space, rng, num_movable_blocks_per_type) {
197204
set_epsilon(epsilon);
198205
init_q_scores_();
199206
}
@@ -267,16 +274,19 @@ void EpsilonGreedyAgent::set_epsilon_action_prob() {
267274
* Softmax agent implementation *
268275
* *
269276
* */
270-
SoftmaxAgent::SoftmaxAgent(std::vector<e_move_type> available_moves, e_agent_space agent_space, vtr::RngContainer& rng)
271-
: KArmedBanditAgent(std::move(available_moves), agent_space, rng) {
272-
init_q_scores_();
277+
SoftmaxAgent::SoftmaxAgent(std::vector<e_move_type> available_moves,
278+
e_agent_space agent_space,
279+
vtr::RngContainer& rng,
280+
const std::vector<int>& num_movable_blocks_per_type)
281+
: KArmedBanditAgent(std::move(available_moves), agent_space, rng, num_movable_blocks_per_type) {
282+
init_q_scores_(num_movable_blocks_per_type);
273283
}
274284

275285
SoftmaxAgent::~SoftmaxAgent() {
276286
if (agent_info_file_) vtr::fclose(agent_info_file_);
277287
}
278288

279-
void SoftmaxAgent::init_q_scores_() {
289+
void SoftmaxAgent::init_q_scores_(const std::vector<int>& num_movable_blocks_per_type) {
280290
q_ = std::vector<float>(num_available_actions_, 0.);
281291
exp_q_ = std::vector<float>(num_available_actions_, 0.);
282292
num_action_chosen_ = std::vector<size_t>(num_available_actions_, 0);
@@ -297,7 +307,7 @@ void SoftmaxAgent::init_q_scores_() {
297307
* it will use the block ratio to calculate action probability for each q_table entry.
298308
*/
299309
if (propose_blk_type_) {
300-
set_block_ratio_();
310+
set_block_ratio_(num_movable_blocks_per_type);
301311
}
302312
set_action_prob_();
303313
}
@@ -320,11 +330,8 @@ t_propose_action SoftmaxAgent::propose_action() {
320330
return proposed_action;
321331
}
322332

323-
void SoftmaxAgent::set_block_ratio_() {
324-
const auto& place_ctx = g_vpr_ctx.placement();
325-
size_t num_movable_total_blocks = place_ctx.movable_blocks.size();
326-
327-
num_movable_total_blocks = std::max<size_t>(num_movable_total_blocks, 1);
333+
void SoftmaxAgent::set_block_ratio_(const std::vector<int>& num_movable_blocks_per_type) {
334+
size_t num_movable_total_blocks = std::max(1, std::accumulate(num_movable_blocks_per_type.begin(), num_movable_blocks_per_type.end(), 0));
328335

329336
// allocate enough space for available block types in the netlist
330337
block_type_ratio_.resize(num_available_types_);
@@ -336,7 +343,7 @@ void SoftmaxAgent::set_block_ratio_() {
336343
for (size_t itype = 0; itype < num_available_types_; itype++) {
337344
t_logical_block_type blk_type;
338345
blk_type.index = agent_to_phy_blk_type(itype);
339-
auto num_blocks = movable_blocks_per_type(blk_type).size();
346+
int num_blocks = num_movable_blocks_per_type[blk_type.index];
340347
block_type_ratio_[itype] = (float)num_blocks / num_movable_total_blocks;
341348
block_type_ratio_[itype] /= available_moves_.size();
342349
}

vpr/src/place/move_generators/simpleRL_move_generator.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
*/
1515
class KArmedBanditAgent {
1616
public:
17-
KArmedBanditAgent(std::vector<e_move_type> available_moves, e_agent_space agent_space, vtr::RngContainer& rng);
17+
KArmedBanditAgent(std::vector<e_move_type> available_moves,
18+
e_agent_space agent_space,
19+
vtr::RngContainer& rng,
20+
const std::vector<int>& num_movable_blocks_per_type);
1821
virtual ~KArmedBanditAgent() = default;
1922

2023
/**
@@ -107,7 +110,7 @@ class KArmedBanditAgent {
107110
*
108111
* @return A vector containing all logical block type indices that exist in the netlist.
109112
*/
110-
static std::vector<int> get_available_logical_blk_types_();
113+
static std::vector<int> get_available_logical_blk_types_(const std::vector<int>& num_movable_blocks_per_type);
111114

112115
private:
113116
std::vector<int> action_logical_blk_type_;
@@ -122,7 +125,11 @@ class KArmedBanditAgent {
122125
*/
123126
class EpsilonGreedyAgent : public KArmedBanditAgent {
124127
public:
125-
EpsilonGreedyAgent(std::vector<e_move_type> available_moves, e_agent_space agent_space, float epsilon, vtr::RngContainer& rng);
128+
EpsilonGreedyAgent(std::vector<e_move_type> available_moves,
129+
e_agent_space agent_space,
130+
float epsilon,
131+
vtr::RngContainer& rng,
132+
const std::vector<int>& num_movable_blocks_per_type);
126133
~EpsilonGreedyAgent() override;
127134

128135
t_propose_action propose_action() override; //Returns the type of the next action as well as the block type the agent wishes to perform
@@ -161,7 +168,10 @@ class EpsilonGreedyAgent : public KArmedBanditAgent {
161168
*/
162169
class SoftmaxAgent : public KArmedBanditAgent {
163170
public:
164-
SoftmaxAgent(std::vector<e_move_type> available_moves, e_agent_space agent_space, vtr::RngContainer& rng);
171+
SoftmaxAgent(std::vector<e_move_type> available_moves,
172+
e_agent_space agent_space,
173+
vtr::RngContainer& rng,
174+
const std::vector<int>& num_movable_blocks_per_type);
165175
~SoftmaxAgent() override;
166176

167177
t_propose_action propose_action() override; //Returns the type of the next action as well as the block type the agent wishes to perform
@@ -170,12 +180,12 @@ class SoftmaxAgent : public KArmedBanditAgent {
170180
/**
171181
* @brief Initialize agent's Q-table and internal variable to zero (RL-agent learns everything throughout the placement run and has no prior knowledge)
172182
*/
173-
void init_q_scores_();
183+
void init_q_scores_(const std::vector<int>& num_movable_blocks_per_type);
174184

175185
/**
176186
* @brief Calculate the fraction of total netlist blocks for each agent block type and will be used by the "set_action_prob" function.
177187
*/
178-
void set_block_ratio_();
188+
void set_block_ratio_(const std::vector<int>& num_movable_blocks_per_type);
179189

180190
/**
181191
* @brief Set action probability for all available actions.

vpr/src/place/move_utils.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef VPR_MOVE_UTILS_H
2-
#define VPR_MOVE_UTILS_H
1+
2+
#pragma once
33

44
#include "vpr_types.h"
55
#include "move_transactions.h"
@@ -453,4 +453,3 @@ std::pair<t_bb, t_bb> union_2d_bb_incr(const std::vector<t_2D_bb>& num_edge_vec,
453453
void enable_placer_debug(const t_placer_opts& placer_opts,
454454
ClusterBlockId blk_id);
455455

456-
#endif

vpr/src/place/placement_log_printer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ void PlacementLogPrinter::print_post_placement_stats() const {
279279
print_resources_utilization();
280280
print_placement_swaps_stats();
281281

282-
move_type_stats.print_placement_move_types_stats();
282+
move_type_stats.print_placement_move_types_stats(placer_.placer_state_.blk_loc_registry().movable_blocks_per_type());
283283

284284
if (placer_.noc_opts_.noc) {
285285
write_noc_placement_file(placer_.noc_opts_.noc_placement_file_name,

0 commit comments

Comments
 (0)