File size: 6,151 Bytes
079c32c |
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 |
import time
import torch
import torch.nn.functional as F
from hpc_rll.origin.ppo import ppo_error, ppo_data
from hpc_rll.rl_utils.ppo import PPO
from testbase import mean_relative_error, times
assert torch.cuda.is_available()
use_cuda = True
B = 128
N = 128
clip_ratio = 0.2
use_value_clip = True
dual_clip = None
def ppo_val():
ori_logits_new = torch.randn(B, N)
ori_logits_old = torch.randn(B, N)
ori_action = torch.randint(0, N, size=(B, ))
ori_value_new = torch.randn(B)
ori_value_old = torch.randn(B)
ori_adv = torch.randn(B)
ori_return = torch.randn(B)
ori_weight = torch.randn(B)
hpc_logits_new = ori_logits_new.clone().detach()
hpc_logits_old = ori_logits_old.clone().detach()
hpc_action = ori_action.clone().detach()
hpc_value_new = ori_value_new.clone().detach()
hpc_value_old = ori_value_old.clone().detach()
hpc_adv = ori_adv.clone().detach()
hpc_return = ori_return.clone().detach()
hpc_weight = ori_weight.clone().detach()
hpc_ppo = PPO(B, N)
if use_cuda:
ori_logits_new = ori_logits_new.cuda()
ori_logits_old = ori_logits_old.cuda()
ori_action = ori_action.cuda()
ori_value_new = ori_value_new.cuda()
ori_value_old = ori_value_old.cuda()
ori_adv = ori_adv.cuda()
ori_return = ori_return.cuda()
ori_weight = ori_weight.cuda()
hpc_logits_new = hpc_logits_new.cuda()
hpc_logits_old = hpc_logits_old.cuda()
hpc_action = hpc_action.cuda()
hpc_value_new = hpc_value_new.cuda()
hpc_value_old = hpc_value_old.cuda()
hpc_adv = hpc_adv.cuda()
hpc_return = hpc_return.cuda()
hpc_weight = hpc_weight.cuda()
hpc_ppo = hpc_ppo.cuda()
ori_logits_new.requires_grad_(True)
ori_value_new.requires_grad_(True)
ori_loss, ori_info = ppo_error(
ppo_data(
ori_logits_new, ori_logits_old, ori_action, ori_value_new, ori_value_old, ori_adv, ori_return, ori_weight
), clip_ratio, use_value_clip, dual_clip
)
ori_loss = sum(ori_loss)
ori_loss.backward()
hpc_logits_new.requires_grad_(True)
hpc_value_new.requires_grad_(True)
hpc_loss, hpc_info = hpc_ppo(
hpc_logits_new, hpc_logits_old, hpc_action, hpc_value_new, hpc_value_old, hpc_adv, hpc_return, hpc_weight,
clip_ratio, use_value_clip, dual_clip
)
hpc_loss = sum(hpc_loss)
hpc_loss.backward()
print("ori_info: " + str(ori_info))
print("hpc_info: " + str(hpc_info))
mre = mean_relative_error(
torch.flatten(ori_loss).cpu().detach().numpy(),
torch.flatten(hpc_loss).cpu().detach().numpy()
)
print("ppo fp loss mean_relative_error: " + str(mre))
mre = mean_relative_error(
torch.flatten(ori_logits_new.grad).cpu().detach().numpy(),
torch.flatten(hpc_logits_new.grad).cpu().detach().numpy()
)
print("ppo bp logits_new mean_relative_error: " + str(mre))
mre = mean_relative_error(
torch.flatten(ori_value_new.grad).cpu().detach().numpy(),
torch.flatten(hpc_value_new.grad).cpu().detach().numpy()
)
print("ppo bp value_new mean_relative_error: " + str(mre))
def ppo_perf():
ori_logits_new = torch.randn(B, N)
ori_logits_old = torch.randn(B, N)
ori_action = torch.randint(0, N, size=(B, ))
ori_value_new = torch.randn(B)
ori_value_old = torch.randn(B)
ori_adv = torch.randn(B)
ori_return = torch.randn(B)
ori_weight = torch.randn(B)
hpc_logits_new = ori_logits_new.clone().detach()
hpc_logits_old = ori_logits_old.clone().detach()
hpc_action = ori_action.clone().detach()
hpc_value_new = ori_value_new.clone().detach()
hpc_value_old = ori_value_old.clone().detach()
hpc_adv = ori_adv.clone().detach()
hpc_return = ori_return.clone().detach()
hpc_weight = ori_weight.clone().detach()
hpc_ppo = PPO(B, N)
if use_cuda:
ori_logits_new = ori_logits_new.cuda()
ori_logits_old = ori_logits_old.cuda()
ori_action = ori_action.cuda()
ori_value_new = ori_value_new.cuda()
ori_value_old = ori_value_old.cuda()
ori_adv = ori_adv.cuda()
ori_return = ori_return.cuda()
ori_weight = ori_weight.cuda()
hpc_logits_new = hpc_logits_new.cuda()
hpc_logits_old = hpc_logits_old.cuda()
hpc_action = hpc_action.cuda()
hpc_value_new = hpc_value_new.cuda()
hpc_value_old = hpc_value_old.cuda()
hpc_adv = hpc_adv.cuda()
hpc_return = hpc_return.cuda()
hpc_weight = hpc_weight.cuda()
hpc_ppo = hpc_ppo.cuda()
ori_logits_new.requires_grad_(True)
ori_value_new.requires_grad_(True)
for i in range(times):
t = time.time()
ori_loss, ori_info = ppo_error(
ppo_data(
ori_logits_new, ori_logits_old, ori_action, ori_value_new, ori_value_old, ori_adv, ori_return,
ori_weight
), clip_ratio, use_value_clip, dual_clip
)
ori_loss = sum(ori_loss)
ori_loss.backward()
if use_cuda:
torch.cuda.synchronize()
print('epoch: {}, origin ppo cost time: {}'.format(i, time.time() - t))
hpc_logits_new.requires_grad_(True)
hpc_value_new.requires_grad_(True)
for i in range(times):
t = time.time()
hpc_loss, hpc_info = hpc_ppo(
hpc_logits_new, hpc_logits_old, hpc_action, hpc_value_new, hpc_value_old, hpc_adv, hpc_return, hpc_weight,
clip_ratio, use_value_clip, dual_clip
)
hpc_loss = sum(hpc_loss)
hpc_loss.backward()
if use_cuda:
torch.cuda.synchronize()
print('epoch: {}, hpc ppo cost time: {}'.format(i, time.time() - t))
if __name__ == '__main__':
print(
"target problem: B = {}, N = {}, clip_ratio = {}, use_value_clip = {}, dual_clip = {}".format(
B, N, clip_ratio, use_value_clip, dual_clip
)
)
print("================run ppo validation test================")
ppo_val()
print("================run ppo performance test================")
ppo_perf()
|