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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
transfer_agent.py โ Architecture-aware Transfer Learning for LoopUnrollEnv
|
| 3 |
+
|
| 4 |
+
- arch๋ณ๋ก x86 / arm64 ๋ฑ์ ์ ํ์ ์ผ๋ก ์ง์
|
| 5 |
+
- Backbone: ๊ธฐ์กด {arch}_base ๋ชจ๋ธ์ ์ผ๋ถ ๋ ์ด์ด๋ฅผ ๋ฐฑ๋ณธ์ผ๋ก ์ฌ์ฉ
|
| 6 |
+
- Adapter: ์ ํ๊ฒฝ(๋๋ ์ CPU)์ ๋ง๊ฒ ์ํ ๋ ์ด์ด๋ง ์ฌํ์ต
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
import glob
|
| 11 |
+
import sys
|
| 12 |
+
import argparse
|
| 13 |
+
|
| 14 |
+
import numpy as np
|
| 15 |
+
import torch
|
| 16 |
+
import torch.nn as nn
|
| 17 |
+
import gymnasium as gym
|
| 18 |
+
from stable_baselines3 import PPO
|
| 19 |
+
from stable_baselines3.common.env_util import make_vec_env
|
| 20 |
+
|
| 21 |
+
from compiler_env import LoopUnrollEnv
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 25 |
+
# ์ ํธ: ๊ฒฝ๋ก ๋ฐ ๊ธฐ๋ณธ ์ค์
|
| 26 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 27 |
+
|
| 28 |
+
PROJECT_ROOT = os.path.expanduser("~/projects/machineai")
|
| 29 |
+
MODELS_DIR = os.path.join(PROJECT_ROOT, "models")
|
| 30 |
+
BENCH_DIR = os.path.join(PROJECT_ROOT, "benchmarks")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def get_model_paths(arch: str):
|
| 34 |
+
"""
|
| 35 |
+
์ํคํ
์ฒ๋ณ ๊ธฐ๋ณธ ๋ชจ๋ธ/์ ์ด ๋ชจ๋ธ ๊ฒฝ๋ก ์์ฑ
|
| 36 |
+
- base: models/model_{arch}_base.zip
|
| 37 |
+
- transfer: models/model_{arch}_transfer.zip
|
| 38 |
+
"""
|
| 39 |
+
base = os.path.join(MODELS_DIR, f"model_{arch}_base.zip")
|
| 40 |
+
transfer = os.path.join(MODELS_DIR, f"model_{arch}_transfer.zip")
|
| 41 |
+
return base, transfer
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 45 |
+
# Backbone ๊ฐ์ค์น ์ถ์ถ
|
| 46 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 47 |
+
|
| 48 |
+
def extract_backbone_weights(model_path: str) -> dict:
|
| 49 |
+
"""
|
| 50 |
+
๊ธฐ์กด PPO ๋ชจ๋ธ์์ mlp_extractor์ ์ผ๋ถ ๋ ์ด์ด๋ฅผ ๋ฐฑ๋ณธ์ผ๋ก ์ถ์ถ
|
| 51 |
+
- ํ์ฌ๋ policy_net์ ์ฒซ ๋ ๋ ์ด์ด๋ฅผ ๋ฐฑ๋ณธ์ผ๋ก ์ฌ์ฉ
|
| 52 |
+
"""
|
| 53 |
+
print(f"[Backbone] ๋ก๋: {model_path}")
|
| 54 |
+
model = PPO.load(model_path)
|
| 55 |
+
state_dict = model.policy.state_dict()
|
| 56 |
+
backbone = {}
|
| 57 |
+
|
| 58 |
+
for k, v in state_dict.items():
|
| 59 |
+
if "mlp_extractor.policy_net.0" in k or "mlp_extractor.policy_net.2" in k:
|
| 60 |
+
backbone[k] = v.clone()
|
| 61 |
+
|
| 62 |
+
print(f"[Backbone] ์ถ์ถ ๋ ์ด์ด:")
|
| 63 |
+
for k in backbone.keys():
|
| 64 |
+
print(f" - {k}")
|
| 65 |
+
return backbone
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 69 |
+
# Transfer PPO ๋น๋
|
| 70 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 71 |
+
|
| 72 |
+
def build_transfer_model(env, backbone_weights: dict | None, freeze_backbone: bool = True):
|
| 73 |
+
"""
|
| 74 |
+
Backbone ๋๊ฒฐ + Adapter ๋ ์ด์ด ์ถ๊ฐํ PPO ๋ชจ๋ธ ๊ตฌ์ฑ
|
| 75 |
+
- backbone_weights๊ฐ None์ด๋ฉด ์์ ์ ๋ชจ๋ธ๋ก ์์
|
| 76 |
+
"""
|
| 77 |
+
print("[Model] Transfer PPO ์์ฑ ์ค...")
|
| 78 |
+
model = PPO(
|
| 79 |
+
policy="MlpPolicy",
|
| 80 |
+
env=env,
|
| 81 |
+
learning_rate=1e-4, # ์ ์ดํ์ต์ ๋ฎ์ lr
|
| 82 |
+
n_steps=256,
|
| 83 |
+
batch_size=64,
|
| 84 |
+
n_epochs=10,
|
| 85 |
+
gamma=0.99,
|
| 86 |
+
verbose=1,
|
| 87 |
+
policy_kwargs=dict(net_arch=[64, 64, 32]), # +32 adapter layer
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# ๋ฐฑ๋ณธ ๊ฐ์ค์น ์ฃผ์
|
| 91 |
+
if backbone_weights is not None:
|
| 92 |
+
print("[Model] Backbone ๊ฐ์ค์น ์ฃผ์
...")
|
| 93 |
+
state_dict = model.policy.state_dict()
|
| 94 |
+
injected, skipped = 0, 0
|
| 95 |
+
for k, v in backbone_weights.items():
|
| 96 |
+
if k in state_dict and state_dict[k].shape == v.shape:
|
| 97 |
+
state_dict[k] = v
|
| 98 |
+
injected += 1
|
| 99 |
+
print(f" โ ์ฃผ์
: {k}")
|
| 100 |
+
else:
|
| 101 |
+
skipped += 1
|
| 102 |
+
print(f" โ ์คํต: {k} (shape mismatch or not found)")
|
| 103 |
+
model.policy.load_state_dict(state_dict)
|
| 104 |
+
print(f"[Model] ์ฃผ์
์๋ฃ: {injected}๊ฐ, ์คํต: {skipped}๊ฐ")
|
| 105 |
+
else:
|
| 106 |
+
print("[Model] Backbone ์์ด ์ ๋ชจ๋ธ๋ก ์์")
|
| 107 |
+
|
| 108 |
+
# ๋ฐฑ๋ณธ ๋๊ฒฐ
|
| 109 |
+
if freeze_backbone and backbone_weights is not None:
|
| 110 |
+
print("[Model] Backbone ํ๋ผ๋ฏธํฐ ๋๊ฒฐ...")
|
| 111 |
+
for name, param in model.policy.named_parameters():
|
| 112 |
+
if "mlp_extractor.policy_net.0" in name or "mlp_extractor.policy_net.2" in name:
|
| 113 |
+
param.requires_grad = False
|
| 114 |
+
print(f" ๐ ๋๊ฒฐ: {name}")
|
| 115 |
+
|
| 116 |
+
trainable = sum(p.numel() for p in model.policy.parameters() if p.requires_grad)
|
| 117 |
+
total = sum(p.numel() for p in model.policy.parameters())
|
| 118 |
+
print(f"\n[Model] ํ๋ผ๋ฏธํฐ: {trainable}/{total} ํ์ต๊ฐ๋ฅ ({trainable/total*100:.1f}%)")
|
| 119 |
+
return model
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
# โโโโโโโโโโโโโโโโโโ๏ฟฝ๏ฟฝ๏ฟฝโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 123 |
+
# ๋ฉ์ธ ์ ์ดํ์ต ์คํ
|
| 124 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 125 |
+
|
| 126 |
+
def main():
|
| 127 |
+
parser = argparse.ArgumentParser(description="Architecture-aware transfer learning for LoopUnrollEnv")
|
| 128 |
+
parser.add_argument("--arch", type=str, default="x86", help="ํ๊ฒ ์ํคํ
์ฒ (์: x86, arm64)")
|
| 129 |
+
parser.add_argument("--timesteps", type=int, default=2000, help="์ ์ดํ์ต ์คํ
์")
|
| 130 |
+
parser.add_argument("--load-base", action="store_true", help="๊ธฐ์กด base ๋ชจ๋ธ์์ backbone์ ๋ก๋ํ ์ง ์ฌ๋ถ")
|
| 131 |
+
parser.add_argument("--base-path", type=str, default="", help="์ง์ base ๋ชจ๋ธ ๊ฒฝ๋ก ์ง์ (์ต์
)")
|
| 132 |
+
parser.add_argument("--out-path", type=str, default="", help="์ ์ด ๊ฒฐ๊ณผ ์ ์ฅ ๊ฒฝ๋ก ์ง์ ์ง์ (์ต์
)")
|
| 133 |
+
parser.add_argument("--repeat-runs", type=int, default=3, help="์คํ ์๊ฐ ์ธก์ ๋ฐ๋ณต ํ์")
|
| 134 |
+
parser.add_argument("--freeze-backbone", action="store_true", help="Backbone ๋ ์ด์ด๋ฅผ ๋๊ฒฐํ ์ง ์ฌ๋ถ")
|
| 135 |
+
parser.add_argument("--clang-bin", type=str, default="", help="์ฌ์ฉํ clang ๋ฐ์ด๋๋ฆฌ (๋น์ฐ๋ฉด ๊ธฐ๋ณธ๊ฐ)")
|
| 136 |
+
parser.add_argument("--opt-bin", type=str, default="", help="์ฌ์ฉํ opt ๋ฐ์ด๋๋ฆฌ (๋น์ฐ๋ฉด ๊ธฐ๋ณธ๊ฐ)")
|
| 137 |
+
parser.add_argument("--source-files", type=str, nargs="+", default=[], help="ํ์ต์ ์ฌ์ฉํ ์์ค ํ์ผ ๋ชฉ๋ก")
|
| 138 |
+
args = parser.parse_args()
|
| 139 |
+
|
| 140 |
+
arch = args.arch
|
| 141 |
+
print(f"[Config] arch={arch}")
|
| 142 |
+
|
| 143 |
+
# ๊ฒฝ๋ก ์ค์
|
| 144 |
+
os.makedirs(MODELS_DIR, exist_ok=True)
|
| 145 |
+
default_base, default_transfer = get_model_paths(arch)
|
| 146 |
+
|
| 147 |
+
base_model_path = args.base_path or default_base
|
| 148 |
+
transfer_model_path = args.out_path or default_transfer
|
| 149 |
+
|
| 150 |
+
print(f"[Config] base_model_path = {base_model_path}")
|
| 151 |
+
print(f"[Config] transfer_model_path= {transfer_model_path}")
|
| 152 |
+
|
| 153 |
+
# ํ์ต ๋์ ์์ค ํ์ผ
|
| 154 |
+
if args.source_files:
|
| 155 |
+
source_files = [os.path.abspath(f) for f in args.source_files]
|
| 156 |
+
else:
|
| 157 |
+
source_files = sorted(glob.glob(os.path.join(BENCH_DIR, "*.c")))
|
| 158 |
+
print(f"[Data] ํ์ต ๋์: {source_files}")
|
| 159 |
+
|
| 160 |
+
# Backbone ๋ก๋ (์ต์
)
|
| 161 |
+
backbone = None
|
| 162 |
+
if args.load_base:
|
| 163 |
+
if not os.path.exists(base_model_path):
|
| 164 |
+
raise FileNotFoundError(f"Base ๋ชจ๋ธ์ ์ฐพ์ ์ ์์ต๋๋ค: {base_model_path}")
|
| 165 |
+
backbone = extract_backbone_weights(base_model_path)
|
| 166 |
+
else:
|
| 167 |
+
print("[Backbone] base ๋ชจ๋ธ ๋ก๋ ์๋ต (์์ ์ ๋ชจ๋ธ๋ก ์์)")
|
| 168 |
+
|
| 169 |
+
# Env ์์ฑ ํจ์
|
| 170 |
+
def make_env():
|
| 171 |
+
return LoopUnrollEnv(
|
| 172 |
+
source_files=source_files,
|
| 173 |
+
repeat_runs=args.repeat_runs,
|
| 174 |
+
arch=arch,
|
| 175 |
+
clang_bin=args.clang_bin or None,
|
| 176 |
+
opt_bin=args.opt_bin or None,
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
vec_env = make_vec_env(make_env, n_envs=1)
|
| 180 |
+
|
| 181 |
+
# Transfer ๋ชจ๋ธ ๋น๋
|
| 182 |
+
print("\n=== Transfer ๋ชจ๋ธ ๋น๋ ===")
|
| 183 |
+
model = build_transfer_model(vec_env, backbone, freeze_backbone=args.freeze_backbone)
|
| 184 |
+
|
| 185 |
+
# ํ์ต
|
| 186 |
+
print(f"\n=== Adapter ํ์ต ({args.timesteps} ์คํ
) ===")
|
| 187 |
+
model.learn(total_timesteps=args.timesteps, progress_bar=True)
|
| 188 |
+
|
| 189 |
+
# ์ ์ฅ
|
| 190 |
+
model.save(transfer_model_path.replace(".zip", ""))
|
| 191 |
+
print(f"\n์ ์ฅ ์๋ฃ: {transfer_model_path}")
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
if __name__ == "__main__":
|
| 195 |
+
main()
|