-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patho5_utils.py
executable file
·643 lines (559 loc) · 22.4 KB
/
o5_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
import os
import random
import time
import datetime
import dateutil.tz
import numpy as np
import torch
import torch.multiprocessing as mp
from collections import namedtuple, deque
from os.path import join
import os.path as osp
from o0_logger import logger
"""
OpenROAD Synthesis Config
"""
# lef lib path
lef = '/datasets/ai4multiplier/openroad_deb/leflib/NangateOpenCellLibrary.lef'
lib = '/datasets/ai4multiplier/openroad_deb/leflib/NangateOpenCellLibrary_typical.lib'
# EasyMacPath
# EasyMacPath = '/datasets/ai4multiplier/rl-mul-code/rl-16mul-code/easymac_backup'
# v1 easymac no booth any
# EasyMacPath = '/datasets/ai4multiplier/rl-mul-code/easymac_test/easymac_backup/target/scala-2.12/easymac-assembly-0.0.1.jar'
# v2 easymac booth any
EasyMacPath = '/datasets/ai4multiplier/rl-mul-code/easymac_latest/easymac/target/scala-2.12/easymac-assembly-0.0.1.jar'
BenchmarkPath = '/datasets/ai4multiplier/rl-mul-code/easymac_test/easymac_backup/benchmarks/16x16/ppa.txt'
EasyMacTarPath = '/datasets/ai4multiplier/rl-mul-code/rl-16mul-code/easymac.tar.gz'
# OpenRoadFlowPath
OpenRoadFlowPath = '/datasets/ai4multiplier/openroad_deb/OpenROAD-flow-scripts'
lef = '/home/xiaxilin/MiraLab/nips2024/ai4-multiplier-master/data/NangateOpenCellLibrary.lef'
lib = '/home/xiaxilin/MiraLab/ai4mul-power/ai4-multiplier-master/utils/NangateOpenCellLibrary_typical.lib'
OpenRoadFlowPath = '/home/xiaxilin/MiraLab/OpenROAD-flow-scripts'
OpenRoadPath = '/home/xiaxilin/MiraLab/OpenROAD'
# abc constraint 是设计约束吗?
def abc_constr_gen(ys_path):
abc_constr_file = os.path.join(ys_path, 'abc_constr')
with open(abc_constr_file, 'w') as f:
f.write('set_driving_cell BUF_X1')
f.write('\n')
f.write('set_load 10.0 [all_outputs]')
f.write('\n')
# abc synthesis scripts gen
def abc_syn_script_gen(ys_path, syn_script):
syn_script_file = os.path.join(ys_path, 'syn_script')
with open(syn_script_file, 'w') as f:
f.write(f'{syn_script}')
f.write('\n')
# sta 的运行脚本,OpenSTA timing analysis
def sta_scripts_gen(ys_path):
sta_file = os.path.join(ys_path, 'openroad_sta.tcl')
with open(sta_file, 'w') as f:
f.write('read_lef ' + str(lef))
f.write('\n')
f.write('read_lib ' + str(lib))
f.write('\n')
f.write('read_verilog ./netlist.v')
f.write('\n')
f.write('link_design MUL')
# xilin-modify-begin
f.write('\n')
f.write("set period 5")
f.write('\n')
f.write("create_clock -period $period [get_ports clock]")
f.write('\n')
f.write("set clk_period_factor .2")
f.write('\n')
f.write("set clk [lindex [all_clocks] 0]")
f.write('\n')
f.write("set period [get_property $clk period]")
f.write('\n')
f.write("set delay [expr $period * $clk_period_factor]")
f.write('\n')
f.write("set_input_delay $delay -clock $clk [delete_from_list [all_inputs] [all_clocks]]")
f.write('\n')
f.write("set_output_delay $delay -clock $clk [delete_from_list [all_outputs] [all_clocks]]")
f.write('\n')
# xilin-modify-end
f.write('\n')
f.write('set_max_delay -from [all_inputs] 0')
f.write('\n')
f.write('set critical_path [lindex [find_timing_paths -sort_by_slack] 0]')
f.write('\n')
f.write('set path_delay [sta::format_time [[$critical_path path] arrival] 4]')
f.write('\n')
f.write('puts \"wns $path_delay\"')
#f.write('report_wns')
f.write('\n')
f.write('report_design_area')
f.write('\n')
# xilin-modify-begin
f.write('set_power_activity -input -activity 0.5\n')
f.write('report_power\n')
# xilin-modify-end
f.write('exit')
# OpenRoad 脚本,ys 文件是啥?yosys, synthesis with target delay
def ys_scripts_gen(target_delay, ys_path, synthesis_path):
ys_file = os.path.join(ys_path, "syn_with_target_delay.ys")
with open(ys_file, 'w') as f:
f.write('read -sv ' + f'{synthesis_path}/rtl/MUL.v')
f.write('\n')
f.write('synth -top MUL')
f.write('\n')
f.write('dfflibmap -liberty ' + str(lib))
f.write('\n')
f.write('abc -D ')
f.write(str(target_delay))
f.write(' -constr ./abc_constr -liberty ' + str(lib))
#f.write(' -liberty ' + str(lib))
f.write('\n')
f.write('write_verilog ./netlist.v')
def ys_scripts_gen_v5(target_delay, ys_path, synthesis_path):
ys_file = os.path.join(ys_path, "syn_with_target_delay.ys")
with open(ys_file, 'w') as f:
f.write('read -sv ' + f'{synthesis_path}/rtl/MUL.v')
f.write('\n')
f.write('synth -top MUL')
f.write('\n')
f.write('dfflibmap -liberty ' + str(lib))
f.write('\n')
f.write('abc -D ')
f.write(str(target_delay))
f.write(' -constr ./abc_constr -liberty ' + str(lib))
#f.write(' -liberty ' + str(lib))
f.write('\n')
f.write('write_verilog ./netlist.v')
# OpenRoad 脚本,ys 文件是啥?yosys, synthesis with target delay v2 copy from laiyao paper
def ys_scripts_v2_gen(target_delay, ys_path, synthesis_path):
ys_file = os.path.join(ys_path, "syn_with_target_delay.ys")
with open(ys_file, 'w') as f:
f.write('read -sv ' + f'{synthesis_path}/rtl/MUL.v')
f.write('\n')
f.write('synth -top MUL')
f.write('\n')
f.write('dfflibmap -liberty ' + str(lib))
f.write('\n')
f.write('flatten') # add flatten
f.write('\n')
f.write('opt') # add opt
f.write('\n')
f.write('abc -D ')
f.write(str(target_delay))
f.write(' -constr ./abc_constr -fast -liberty ' + str(lib)) # fast
#f.write(' -liberty ' + str(lib))
f.write('\n')
f.write('write_verilog ./netlist.v')
# OpenRoad 脚本,ys 文件是啥?yosys, synthesis with target delay v2 copy from laiyao paper
def ys_scripts_v3_gen(target_delay, ys_path, synthesis_path):
ys_file = os.path.join(ys_path, "syn_with_target_delay.ys")
with open(ys_file, 'w') as f:
f.write('read -sv ' + f'{synthesis_path}/rtl/MUL.v')
f.write('\n')
f.write('synth -top MUL')
f.write('\n')
f.write('dfflibmap -liberty ' + str(lib))
f.write('\n')
f.write('flatten') # add flatten
f.write('\n')
f.write('opt') # add opt
f.write('\n')
f.write('abc -D ')
f.write(str(target_delay))
f.write(' -constr ./abc_constr -liberty ' + str(lib)) # fast
#f.write(' -liberty ' + str(lib))
f.write('\n')
f.write('write_verilog ./netlist.v')
def ys_scripts_v5_gen(target_delay, ys_path, synthesis_path):
ys_file = os.path.join(ys_path, "syn_with_target_delay.ys")
with open(ys_file, 'w') as f:
f.write('read -sv ' + f'{synthesis_path}/rtl/MUL.v')
f.write('\n')
f.write('synth -top MUL')
f.write('\n')
f.write('dfflibmap -liberty ' + str(lib))
f.write('\n')
f.write('abc -D ')
f.write(str(target_delay))
f.write(' -constr ./abc_constr -fast -liberty ' + str(lib))
#f.write(' -liberty ' + str(lib))
f.write('\n')
f.write('write_verilog ./netlist.v')
def ys_scripts_v4_gen(target_delay, ys_path, synthesis_path):
ys_file = os.path.join(ys_path, "syn_with_target_delay.ys")
with open(ys_file, 'w') as f:
f.write('read -sv ' + f'{synthesis_path}/rtl/MUL.v')
f.write('\n')
f.write('synth -top MUL')
f.write('\n')
f.write('dfflibmap -liberty ' + str(lib))
f.write('\n')
f.write('flatten') # add flatten
f.write('\n')
f.write('opt') # add opt
f.write('\n')
f.write('abc -D ')
f.write(str(target_delay))
f.write(' -constr ./abc_constr -script ./syn_script -liberty ' + str(lib)) # fast
#f.write(' -liberty ' + str(lib))
f.write('\n')
f.write('write_verilog ./netlist.v')
# 读取 .v 文件,执行 openroad synthesis 脚本,然后再执行 openroad sta timing analysis
def get_ppa(ys_path):
synthesis_cmd = 'cd ' + ys_path + ' \n' + f'source {OpenRoadFlowPath}/env.sh\n' + 'yosys ./syn_with_target_delay.ys' # open road synthesis
sta_cmd = 'cd ' + ys_path + ' \n' + f'source {OpenRoadFlowPath}/env.sh\n' + 'openroad openroad_sta.tcl | tee ./log' # openroad sta
rm_log_cmd = 'rm -f ' + ys_path + '/log'
rm_netlist_cmd = 'rm -f ' + ys_path + '/netlist.v'
# execute synthesis cmd
os.system(synthesis_cmd)
os.system(sta_cmd)
# get ppa
with open(ys_path + '/log', 'r') as f:
rpt = f.read().splitlines()
for line in rpt:
if len(line.rstrip()) < 2:
continue
line = line.rstrip().split()
if line[0] == 'wns':
delay = line[-1]
#delay = delay[1:]
continue
if line[0] == 'Design':
area = line[2]
# xilin-modify-begin
continue
if line[0] == 'Total':
power = line[-2]
internal_power = line[1]
switching_power = line[2]
leakage_power = line[3]
break
ppa_dict = {
"area": float(area),
"delay": float(delay),
"power": float(power),
"internal_power": float(internal_power),
"switching_power": float(switching_power),
"leakage_power": float(leakage_power),
}
# xilin-modify-end
# remove log
os.system(rm_log_cmd)
os.system(rm_netlist_cmd)
return ppa_dict
"""
Replay Buffer
"""
# Transition = namedtuple('Transition',
# ('state', 'action', 'next_state', 'reward','mask','next_state_mask','state_ct32','state_ct22','next_state_ct32','next_state_ct22','rewards_dict'))
# Transition = namedtuple('Transition',
# ('state', 'action', 'next_state', 'reward','mask','next_state_mask','state_ct32','state_ct22','next_state_ct32','next_state_ct22'))
Transition = namedtuple('Transition',
('state', 'action', 'next_state', 'reward','mask','next_state_mask'))
class ReplayMemory(object):
def __init__(self, capacity=10000):
self.memory = deque([],maxlen=capacity)
def push(self, *args):
'''Save a transition'''
self.memory.append(Transition(*args))
def sample(self, batch_size):
return random.sample(self.memory, batch_size)
def __len__(self):
return len(self.memory)
class NStepReplayMemory(object):
def __init__(self, capacity=10000):
self.memory = deque([],maxlen=capacity)
self.tmp_memory = []
def push(self, is_last, *args):
'''Save a transition'''
self.tmp_memory.append(Transition(*args))
if is_last:
self.memory.append(self.tmp_memory) # save list of transitions; list of tuple;
self.tmp_memory = [] # clear tmp memory
def sample(self, batch_size):
return random.sample(self.memory, batch_size)
def __len__(self):
return len(self.memory)
MBRLTransition = namedtuple('Transition',
('state', 'action', 'next_state', 'reward','mask','next_state_mask','normalize_area','normalize_delay'))
class MBRLReplayMemory(object):
def __init__(self, capacity=10000):
self.memory = deque([],maxlen=capacity)
def push(self, *args):
'''Save a transition'''
self.memory.append(MBRLTransition(*args))
def sample(self, batch_size):
return random.sample(self.memory, batch_size)
def __len__(self):
return len(self.memory)
MBRLMultiObjTransition = namedtuple('Transition',
('state', 'action', 'next_state', 'reward','mask','next_state_mask','normalize_area','normalize_delay', 'area_reward', 'delay_reward'))
class MBRLMultiObjReplayMemory(object):
def __init__(self, capacity=10000):
self.memory = deque([],maxlen=capacity)
def push(self, *args):
'''Save a transition'''
self.memory.append(MBRLMultiObjTransition(*args))
def sample(self, batch_size):
return random.sample(self.memory, batch_size)
def __len__(self):
return len(self.memory)
class MBRLTrajectoryReplayMemory(object):
def __init__(self, capacity=10000):
self.memory = deque([],maxlen=capacity)
self.tmp_memory = []
def push(self, is_last, *args):
'''Save a transition'''
self.tmp_memory.append(MBRLTransition(*args))
if is_last:
self.memory.append(self.tmp_memory) # save list of transitions; list of tuple;
self.tmp_memory = [] # clear tmp memory
def sample(self, batch_size):
return random.sample(self.memory, batch_size)
def __len__(self):
return len(self.memory)
MultiObjTransition = namedtuple('Transition',
('state', 'action', 'next_state', 'reward','mask','next_state_mask','area_reward', 'delay_reward'))
class MultiObjReplayMemory(object):
def __init__(self, capacity=10000):
self.memory = deque([],maxlen=capacity)
def push(self, *args):
'''Save a transition'''
self.memory.append(MultiObjTransition(*args))
def sample(self, batch_size):
return random.sample(self.memory, batch_size)
def __len__(self):
return len(self.memory)
PDMultiObjTransition = namedtuple('Transition',
('state', 'action', 'next_state', 'reward','mask','next_state_mask','area_reward', 'delay_reward', 'weight'))
class PDMultiObjReplayMemory(object):
def __init__(self, capacity=10000):
self.memory = deque([],maxlen=capacity)
def push(self, *args):
'''Save a transition'''
self.memory.append(PDMultiObjTransition(*args))
def sample(self, batch_size):
return random.sample(self.memory, batch_size)
def __len__(self):
return len(self.memory)
# SharedMultiObjTransition = namedtuple('Transition',
# ('state', 'action', 'next_state', 'reward','mask','next_state_mask','area_reward', 'delay_reward'))
class SharedMultiObjTransition(object):
# Define your transition class as needed
def __init__(self, state, action, next_state, reward, mask, next_state_mask, area_reward, delay_reward):
self.state = state
self.action = action
self.next_state = next_state
self.reward = reward
self.mask = mask
self.next_state_mask = next_state_mask
self.area_reward = area_reward
self.delay_reward = delay_reward
class SharedMultiObjReplayMemory(object):
def __init__(self, capacity=10000):
self.memory = mp.Manager().list(deque([], maxlen=capacity))
def push(self, *args):
'''Save a transition'''
# self.memory.append(SharedMultiObjTransition(*args))
try:
# Clone CUDA tensors to CPU tensors before storing
# args = tuple(x.cpu() if torch.is_tensor(x) and x.is_cuda else x for x in args)
self.memory.append(SharedMultiObjTransition(*args)) # numpy array
except Exception as e:
print(f"Error in push: {e}")
raise
def sample(self, batch_size):
memory_list = list(self.memory)
return random.sample(memory_list, batch_size)
def __len__(self):
return len(self.memory)
class SharedMultiObjVectorConditionTransition(object):
# Define your transition class as needed
def __init__(self, state, action, next_state, reward, mask, next_state_mask, area_reward, delay_reward, weight_vector, delay_condition):
self.state = state
self.action = action
self.next_state = next_state
self.reward = reward
self.mask = mask
self.next_state_mask = next_state_mask
self.area_reward = area_reward
self.delay_reward = delay_reward
self.weight_vector = weight_vector
self.delay_condition = delay_condition
class SharedMultiObjVectorConditionReplayMemory(object):
def __init__(self, capacity=10000):
self.memory = mp.Manager().list(deque([], maxlen=capacity))
def push(self, *args):
'''Save a transition'''
# self.memory.append(SharedMultiObjTransition(*args))
try:
# Clone CUDA tensors to CPU tensors before storing
# args = tuple(x.cpu() if torch.is_tensor(x) and x.is_cuda else x for x in args)
self.memory.append(SharedMultiObjVectorConditionTransition(*args)) # numpy array
except Exception as e:
print(f"Error in push: {e}")
raise
def sample(self, batch_size):
memory_list = list(self.memory)
return random.sample(memory_list, batch_size)
def __len__(self):
return len(self.memory)
"""
Set Seed
"""
############## set global seeds ##############
def set_global_seed(seed=None):
if seed is None:
seed = int(time.time()) % 4096
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed) # cpu
torch.cuda.manual_seed_all(seed) # 并行gpu
os.environ['PYTHONHASHSEED'] = str(seed)
# Set CuDNN to be deterministic. Notice that this may slow down the training.
os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':4096:8'
torch.use_deterministic_algorithms(True)
torch.backends.cudnn.enabled = True
torch.backends.cudnn.deterministic = True # cpu/gpu结果一致
torch.backends.cudnn.benchmark = True
return seed
"""
Set Logger
"""
def create_exp_name(exp_prefix, exp_id=0, seed=0):
"""
Create a semi-unique experiment name that has a timestamp
:param exp_prefix:
:param exp_id:
:return:
"""
now = datetime.datetime.now(dateutil.tz.tzlocal())
timestamp = now.strftime('%Y_%m_%d_%H_%M_%S')
return "%s_%s_%04d--s-%d" % (exp_prefix, timestamp, exp_id, seed)
def create_log_dir(
exp_prefix,
exp_id=0,
seed=0,
base_log_dir=None,
):
"""
Creates and returns a unique log directory.
:param exp_prefix: All experiments with this prefix will have log directories be under this directory.
:param exp_id: The number of the specific experiment run within this experiment.
:param base_log_dir: The directory where all log should be saved.
:return:
"""
exp_name = create_exp_name(exp_prefix, exp_id, seed)
if base_log_dir is None:
base_log_dir = _LOCAL_LOG_DIR
log_dir = join(base_log_dir, exp_prefix, exp_name)
if osp.exists(log_dir):
logger.log("WARNING: Log directory already exists {}".format(log_dir))
os.makedirs(log_dir, exist_ok=True)
return log_dir
def setup_logger(
exp_prefix="default",
variant=None,
text_log_file="debug.log",
variant_log_file="variant.json",
tabular_log_file="progress.csv",
snapshot_mode="last",
snapshot_gap=1,
log_tabular_only=False,
log_dir=None,
script_name=None,
**create_log_dir_kwargs
):
"""
Set up logger to have some reasonable default settings.
Will save log output to
base_log_dir/exp_prefix/exp_name.
exp_name will be auto-generated to be unique.
If log_dir is specified, then that directory is used as the output dir.
:param exp_prefix: The sub-directory for this specific experiment.
:param variant: 实验参数字典
:param text_log_file:
:param variant_log_file:
:param tabular_log_file:
:param snapshot_mode:
:param log_tabular_only:
:param snapshot_gap:
:param log_dir:
:param script_name: If set, save the script name to this.
:return:
"""
first_time = log_dir is None
if first_time:
log_dir = create_log_dir(exp_prefix, **create_log_dir_kwargs)
if variant is not None:
logger.log("Variant:")
# logger.log(json.dumps(dict_to_safe_json(variant), indent=2))
variant_log_path = join(log_dir, variant_log_file)
logger.log_variant(variant_log_path, variant)
tabular_log_path = join(log_dir, tabular_log_file)
text_log_path = join(log_dir, text_log_file)
logger.add_text_output(text_log_path)
if first_time:
logger.add_tabular_output(tabular_log_path)
else:
logger._add_output(tabular_log_path, logger._tabular_outputs, logger._tabular_fds, mode='a')
for tabular_fd in logger._tabular_fds:
logger._tabular_header_written.add(tabular_fd)
logger.set_snapshot_dir(log_dir)
logger.set_snapshot_mode(snapshot_mode)
logger.set_snapshot_gap(snapshot_gap)
logger.set_log_tabular_only(log_tabular_only)
exp_name = log_dir.split("/")[-1]
logger.push_prefix("[%s] " % exp_name)
if script_name is not None:
with open(join(log_dir, "script_name.txt"), "w") as f:
f.write(script_name)
return log_dir
"""
RunningMeanStd
"""
class RunningMeanStd(object):
# https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm
def __init__(self, epsilon=1e-4, shape=()):
self.mean = np.zeros(shape, 'float64')
self.var = np.ones(shape, 'float64')
self.count = epsilon
def update(self, x):
batch_mean = np.mean(x, axis=0)
batch_var = np.var(x, axis=0)
batch_count = x.shape[0]
self.update_from_moments(batch_mean, batch_var, batch_count)
def update_from_moments(self, batch_mean, batch_var, batch_count):
delta = batch_mean - self.mean
tot_count = self.count + batch_count
new_mean = self.mean + delta * batch_count / tot_count
m_a = self.var * (self.count)
m_b = batch_var * (batch_count)
M2 = m_a + m_b + np.square(delta) * self.count * batch_count / (self.count + batch_count)
new_var = M2 / (self.count + batch_count)
new_count = batch_count + self.count
self.mean = new_mean
self.var = new_var
self.count = new_count
if __name__ == "__main__":
replay_buffer = SharedMultiObjReplayMemory()
for _ in range(100):
replay_buffer.push(
np.array([[1,2]]),2,3,4,5,6,7,8
)
transitions = replay_buffer.sample(64)
batch = {
"state": [],
"action": [],
"next_state": [],
"reward": [],
"mask": [],
"next_state_mask": [],
"area_reward": [],
"delay_reward": []
}
for transition in transitions:
batch["state"].append(transition.state)
state_batch = np.concatenate(batch["state"])
state_batch = torch.tensor(state_batch)
print(state_batch.shape)
# batch = SharedMultiObjTransition(*zip(*transitions))
# print(batch)