File size: 8,476 Bytes
fb177fd | 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 | """
transfer_agent.py โ Architecture-aware Transfer Learning for LoopUnrollEnv
- arch๋ณ๋ก x86 / arm64 ๋ฑ์ ์ ํ์ ์ผ๋ก ์ง์
- Backbone: ๊ธฐ์กด {arch}_base ๋ชจ๋ธ์ ์ผ๋ถ ๋ ์ด์ด๋ฅผ ๋ฐฑ๋ณธ์ผ๋ก ์ฌ์ฉ
- Adapter: ์ ํ๊ฒฝ(๋๋ ์ CPU)์ ๋ง๊ฒ ์ํ ๋ ์ด์ด๋ง ์ฌํ์ต
"""
import os
import glob
import sys
import argparse
import numpy as np
import torch
import torch.nn as nn
import gymnasium as gym
from stable_baselines3 import PPO
from stable_baselines3.common.env_util import make_vec_env
from compiler_env import LoopUnrollEnv
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ์ ํธ: ๊ฒฝ๋ก ๋ฐ ๊ธฐ๋ณธ ์ค์
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
PROJECT_ROOT = os.path.expanduser("~/projects/machineai")
MODELS_DIR = os.path.join(PROJECT_ROOT, "models")
BENCH_DIR = os.path.join(PROJECT_ROOT, "benchmarks")
def get_model_paths(arch: str):
"""
์ํคํ
์ฒ๋ณ ๊ธฐ๋ณธ ๋ชจ๋ธ/์ ์ด ๋ชจ๋ธ ๊ฒฝ๋ก ์์ฑ
- base: models/model_{arch}_base.zip
- transfer: models/model_{arch}_transfer.zip
"""
base = os.path.join(MODELS_DIR, f"model_{arch}_base.zip")
transfer = os.path.join(MODELS_DIR, f"model_{arch}_transfer.zip")
return base, transfer
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# Backbone ๊ฐ์ค์น ์ถ์ถ
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def extract_backbone_weights(model_path: str) -> dict:
"""
๊ธฐ์กด PPO ๋ชจ๋ธ์์ mlp_extractor์ ์ผ๋ถ ๋ ์ด์ด๋ฅผ ๋ฐฑ๋ณธ์ผ๋ก ์ถ์ถ
- ํ์ฌ๋ policy_net์ ์ฒซ ๋ ๋ ์ด์ด๋ฅผ ๋ฐฑ๋ณธ์ผ๋ก ์ฌ์ฉ
"""
print(f"[Backbone] ๋ก๋: {model_path}")
model = PPO.load(model_path)
state_dict = model.policy.state_dict()
backbone = {}
for k, v in state_dict.items():
if "mlp_extractor.policy_net.0" in k or "mlp_extractor.policy_net.2" in k:
backbone[k] = v.clone()
print(f"[Backbone] ์ถ์ถ ๋ ์ด์ด:")
for k in backbone.keys():
print(f" - {k}")
return backbone
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# Transfer PPO ๋น๋
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def build_transfer_model(env, backbone_weights: dict | None, freeze_backbone: bool = True):
"""
Backbone ๋๊ฒฐ + Adapter ๋ ์ด์ด ์ถ๊ฐํ PPO ๋ชจ๋ธ ๊ตฌ์ฑ
- backbone_weights๊ฐ None์ด๋ฉด ์์ ์ ๋ชจ๋ธ๋ก ์์
"""
print("[Model] Transfer PPO ์์ฑ ์ค...")
model = PPO(
policy="MlpPolicy",
env=env,
learning_rate=1e-4, # ์ ์ดํ์ต์ ๋ฎ์ lr
n_steps=256,
batch_size=64,
n_epochs=10,
gamma=0.99,
verbose=1,
policy_kwargs=dict(net_arch=[64, 64, 32]), # +32 adapter layer
)
# ๋ฐฑ๋ณธ ๊ฐ์ค์น ์ฃผ์
if backbone_weights is not None:
print("[Model] Backbone ๊ฐ์ค์น ์ฃผ์
...")
state_dict = model.policy.state_dict()
injected, skipped = 0, 0
for k, v in backbone_weights.items():
if k in state_dict and state_dict[k].shape == v.shape:
state_dict[k] = v
injected += 1
print(f" โ ์ฃผ์
: {k}")
else:
skipped += 1
print(f" โ ์คํต: {k} (shape mismatch or not found)")
model.policy.load_state_dict(state_dict)
print(f"[Model] ์ฃผ์
์๋ฃ: {injected}๊ฐ, ์คํต: {skipped}๊ฐ")
else:
print("[Model] Backbone ์์ด ์ ๋ชจ๋ธ๋ก ์์")
# ๋ฐฑ๋ณธ ๋๊ฒฐ
if freeze_backbone and backbone_weights is not None:
print("[Model] Backbone ํ๋ผ๋ฏธํฐ ๋๊ฒฐ...")
for name, param in model.policy.named_parameters():
if "mlp_extractor.policy_net.0" in name or "mlp_extractor.policy_net.2" in name:
param.requires_grad = False
print(f" ๐ ๋๊ฒฐ: {name}")
trainable = sum(p.numel() for p in model.policy.parameters() if p.requires_grad)
total = sum(p.numel() for p in model.policy.parameters())
print(f"\n[Model] ํ๋ผ๋ฏธํฐ: {trainable}/{total} ํ์ต๊ฐ๋ฅ ({trainable/total*100:.1f}%)")
return model
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ๋ฉ์ธ ์ ์ดํ์ต ์คํ
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def main():
parser = argparse.ArgumentParser(description="Architecture-aware transfer learning for LoopUnrollEnv")
parser.add_argument("--arch", type=str, default="x86", help="ํ๊ฒ ์ํคํ
์ฒ (์: x86, arm64)")
parser.add_argument("--timesteps", type=int, default=2000, help="์ ์ดํ์ต ์คํ
์")
parser.add_argument("--load-base", action="store_true", help="๊ธฐ์กด base ๋ชจ๋ธ์์ backbone์ ๋ก๋ํ ์ง ์ฌ๋ถ")
parser.add_argument("--base-path", type=str, default="", help="์ง์ base ๋ชจ๋ธ ๊ฒฝ๋ก ์ง์ (์ต์
)")
parser.add_argument("--out-path", type=str, default="", help="์ ์ด ๊ฒฐ๊ณผ ์ ์ฅ ๊ฒฝ๋ก ์ง์ ์ง์ (์ต์
)")
parser.add_argument("--repeat-runs", type=int, default=3, help="์คํ ์๊ฐ ์ธก์ ๋ฐ๋ณต ํ์")
parser.add_argument("--freeze-backbone", action="store_true", help="Backbone ๋ ์ด์ด๋ฅผ ๋๊ฒฐํ ์ง ์ฌ๋ถ")
parser.add_argument("--clang-bin", type=str, default="", help="์ฌ์ฉํ clang ๋ฐ์ด๋๋ฆฌ (๋น์ฐ๋ฉด ๊ธฐ๋ณธ๊ฐ)")
parser.add_argument("--opt-bin", type=str, default="", help="์ฌ์ฉํ opt ๋ฐ์ด๋๋ฆฌ (๋น์ฐ๋ฉด ๊ธฐ๋ณธ๊ฐ)")
parser.add_argument("--source-files", type=str, nargs="+", default=[], help="ํ์ต์ ์ฌ์ฉํ ์์ค ํ์ผ ๋ชฉ๋ก")
args = parser.parse_args()
arch = args.arch
print(f"[Config] arch={arch}")
# ๊ฒฝ๋ก ์ค์
os.makedirs(MODELS_DIR, exist_ok=True)
default_base, default_transfer = get_model_paths(arch)
base_model_path = args.base_path or default_base
transfer_model_path = args.out_path or default_transfer
print(f"[Config] base_model_path = {base_model_path}")
print(f"[Config] transfer_model_path= {transfer_model_path}")
# ํ์ต ๋์ ์์ค ํ์ผ
if args.source_files:
source_files = [os.path.abspath(f) for f in args.source_files]
else:
source_files = sorted(glob.glob(os.path.join(BENCH_DIR, "*.c")))
print(f"[Data] ํ์ต ๋์: {source_files}")
# Backbone ๋ก๋ (์ต์
)
backbone = None
if args.load_base:
if not os.path.exists(base_model_path):
raise FileNotFoundError(f"Base ๋ชจ๋ธ์ ์ฐพ์ ์ ์์ต๋๋ค: {base_model_path}")
backbone = extract_backbone_weights(base_model_path)
else:
print("[Backbone] base ๋ชจ๋ธ ๋ก๋ ์๋ต (์์ ์ ๋ชจ๋ธ๋ก ์์)")
# Env ์์ฑ ํจ์
def make_env():
return LoopUnrollEnv(
source_files=source_files,
repeat_runs=args.repeat_runs,
arch=arch,
clang_bin=args.clang_bin or None,
opt_bin=args.opt_bin or None,
)
vec_env = make_vec_env(make_env, n_envs=1)
# Transfer ๋ชจ๋ธ ๋น๋
print("\n=== Transfer ๋ชจ๋ธ ๋น๋ ===")
model = build_transfer_model(vec_env, backbone, freeze_backbone=args.freeze_backbone)
# ํ์ต
print(f"\n=== Adapter ํ์ต ({args.timesteps} ์คํ
) ===")
model.learn(total_timesteps=args.timesteps, progress_bar=True)
# ์ ์ฅ
model.save(transfer_model_path.replace(".zip", ""))
print(f"\n์ ์ฅ ์๋ฃ: {transfer_model_path}")
if __name__ == "__main__":
main()
|