aiauto-client 0.1.5__tar.gz → 0.1.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aiauto_client-0.1.7/MANIFEST.in +1 -0
- aiauto_client-0.1.7/PKG-INFO +472 -0
- aiauto_client-0.1.7/README.md +445 -0
- aiauto_client-0.1.7/examples/example_torch_multiple_objective.py +468 -0
- aiauto_client-0.1.7/examples/example_torch_single_objective.py +471 -0
- aiauto_client-0.1.7/examples/simple_example.py +78 -0
- {aiauto_client-0.1.5 → aiauto_client-0.1.7}/pyproject.toml +4 -4
- {aiauto_client-0.1.5 → aiauto_client-0.1.7}/src/aiauto/constants.py +5 -10
- {aiauto_client-0.1.5 → aiauto_client-0.1.7}/src/aiauto/core.py +20 -15
- {aiauto_client-0.1.5 → aiauto_client-0.1.7}/src/aiauto/serializer.py +20 -10
- aiauto_client-0.1.7/src/aiauto_client.egg-info/PKG-INFO +472 -0
- {aiauto_client-0.1.5 → aiauto_client-0.1.7}/src/aiauto_client.egg-info/SOURCES.txt +4 -0
- aiauto_client-0.1.5/PKG-INFO +0 -102
- aiauto_client-0.1.5/README.md +0 -75
- aiauto_client-0.1.5/src/aiauto_client.egg-info/PKG-INFO +0 -102
- {aiauto_client-0.1.5 → aiauto_client-0.1.7}/setup.cfg +0 -0
- {aiauto_client-0.1.5 → aiauto_client-0.1.7}/src/aiauto/__init__.py +0 -0
- {aiauto_client-0.1.5 → aiauto_client-0.1.7}/src/aiauto/_config.py +0 -0
- {aiauto_client-0.1.5 → aiauto_client-0.1.7}/src/aiauto/http_client.py +0 -0
- {aiauto_client-0.1.5 → aiauto_client-0.1.7}/src/aiauto_client.egg-info/dependency_links.txt +0 -0
- {aiauto_client-0.1.5 → aiauto_client-0.1.7}/src/aiauto_client.egg-info/requires.txt +0 -0
- {aiauto_client-0.1.5 → aiauto_client-0.1.7}/src/aiauto_client.egg-info/top_level.txt +0 -0
- {aiauto_client-0.1.5 → aiauto_client-0.1.7}/tests/test_local_storage.py +0 -0
@@ -0,0 +1 @@
|
|
1
|
+
recursive-include examples *.py
|
@@ -0,0 +1,472 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: aiauto-client
|
3
|
+
Version: 0.1.7
|
4
|
+
Summary: AI Auto HPO (Hyperparameter Optimization) Client Library
|
5
|
+
Author-email: AIAuto Team <ainode@zeroone.ai>
|
6
|
+
Project-URL: Homepage, https://dashboard.aiauto.pangyo.ainode.ai
|
7
|
+
Project-URL: Repository, https://dashboard.aiauto.pangyo.ainode.ai
|
8
|
+
Project-URL: Documentation, https://dashboard.aiauto.pangyo.ainode.ai
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
10
|
+
Classifier: Intended Audience :: Developers
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
21
|
+
Requires-Python: >=3.8
|
22
|
+
Description-Content-Type: text/markdown
|
23
|
+
Requires-Dist: optuna>=3.0.0
|
24
|
+
Requires-Dist: requests>=2.25.0
|
25
|
+
Requires-Dist: grpcio>=1.48.0
|
26
|
+
Requires-Dist: grpcio-status>=1.48.0
|
27
|
+
|
28
|
+
# AIAuto - Hyperparameter Optimization Client Library
|
29
|
+
|
30
|
+
AIAuto는 Kubernetes 기반의 분산 HPO(Hyperparameter Optimization) 시스템을 위한 클라이언트 라이브러리입니다.
|
31
|
+
사용자 python lib <-> Next.js 서버 사이 Connect RPC (HTTP/1.1) 통신 담당
|
32
|
+
|
33
|
+
## 설치
|
34
|
+
- `pip install aiauto-client optuna`
|
35
|
+
|
36
|
+
## API 레퍼런스
|
37
|
+
|
38
|
+
### create_study 파라미터
|
39
|
+
- `study_name` (str): Study 이름
|
40
|
+
- `direction` (str): 단일 목적 최적화 방향 ("minimize" 또는 "maximize")
|
41
|
+
- `directions` (List[str]): 다중 목적 최적화 방향 리스트 (direction과 상호 배타적)
|
42
|
+
- `sampler` (object/dict): Optuna sampler 객체 또는 dict (선택적)
|
43
|
+
- `pruner` (object/dict): Optuna pruner 객체 또는 dict (선택적)
|
44
|
+
|
45
|
+
**주의**: `direction`과 `directions`는 둘 중 하나만 지정해야 합니다.
|
46
|
+
|
47
|
+
### optimize 파라미터
|
48
|
+
- `objective` (Callable): Trial을 인자로 받는 목적 함수
|
49
|
+
- `n_trials` (int): 총 trial 수
|
50
|
+
- `parallelism` (int): 동시 실행 Pod 수 (기본값: 2)
|
51
|
+
- `requirements_file` (str): requirements.txt 파일 경로 (requirements_list와 상호 배타적)
|
52
|
+
- `requirements_list` (List[str]): 패키지 리스트 (requirements_file과 상호 배타적)
|
53
|
+
- `resources_requests` (Dict[str, str]): 리소스 요청 (기본값: {"cpu": "256m", "memory": "256Mi"})
|
54
|
+
- `resources_limits` (Dict[str, str]): 리소스 제한 (기본값: {"cpu": "256m", "memory": "256Mi"})
|
55
|
+
- `runtime_image` (str): 커스텀 런타임 이미지 (None이면 자동 선택)
|
56
|
+
- `use_gpu` (bool): GPU 사용 여부 (기본값: False)
|
57
|
+
|
58
|
+
**주의**: `requirements_file`과 `requirements_list`는 둘 중 하나만 지정해야 합니다.
|
59
|
+
|
60
|
+
## 지원 런타임 이미지 확인
|
61
|
+
```python
|
62
|
+
import aiauto
|
63
|
+
|
64
|
+
# 사용 가능한 이미지 확인
|
65
|
+
for image in aiauto.RUNTIME_IMAGES:
|
66
|
+
print(image)
|
67
|
+
```
|
68
|
+
|
69
|
+
## 실행 흐름
|
70
|
+
### token 발급 # TODO
|
71
|
+
- `https://dashboard.aiauto.pangyo.ainode.ai` 에 접속하여 ainode 에 로그인 한 후
|
72
|
+
- `https://dashboard.aiauto.pangyo.ainode.ai/token` 으로 이동하여 aiauto 의 token 을 발급
|
73
|
+
- 아래 코드 처럼 발급한 token 을 넣어 AIAutoController singleton 객체를 초기화, OptunaWorkspace 를 활성화 시킨다
|
74
|
+
```python
|
75
|
+
import aiauto
|
76
|
+
|
77
|
+
ac = aiauto.AIAutoController('<token>')
|
78
|
+
```
|
79
|
+
- `https://dashboard.aiauto.pangyo.ainode.ai/workspace` 에서 생성된 OptunaWorkspace 와 optuna-dashboard 링크를 확인할 수 있음
|
80
|
+
- 아래 코드 처럼 study 를 생성하면 `https://dashboard.aiauto.pangyo.ainode.ai/study` 에서 확인할 수 있고 optuna-dashboard 링크에서도 확인 가능
|
81
|
+
```python
|
82
|
+
study_wrapper = ac.create_study(
|
83
|
+
study_name='test',
|
84
|
+
direction='maximize', # or 'minimize'
|
85
|
+
)
|
86
|
+
```
|
87
|
+
- 아래 코드 처럼 생성한 study 애서 objective 함수를 작성하여 넘겨주면 optimize 를 호출하면 `https://dashboard.aiauto.pangyo.ainode.ai/trialbatch` 에서 확인할 수 있고 optuna-dashboard 링크에서도 확인 가능
|
88
|
+
```python
|
89
|
+
study_wrapper.optimize(
|
90
|
+
objective=func_with_parameter_trial,
|
91
|
+
n_trials=4,
|
92
|
+
parallelism=2,
|
93
|
+
use_gpu=False,
|
94
|
+
runtime_image=aiauto.RUNTIME_IMAGES[0],
|
95
|
+
)
|
96
|
+
```
|
97
|
+
|
98
|
+
## Jupyter Notebook 사용 시 주의사항
|
99
|
+
|
100
|
+
Jupyter Notebook이나 Python REPL에서 정의한 함수는 Serialize 할 수 없습니다
|
101
|
+
대신 `%%writefile` magic 울 사용하여 파일로 저장한 후 import 하세요.
|
102
|
+
|
103
|
+
### Jupyter에서 objective 함수 작성 방법
|
104
|
+
- objective 함수를 파일로 저장
|
105
|
+
```python
|
106
|
+
%%writefile my_objective.py
|
107
|
+
import aiauto
|
108
|
+
import optuna
|
109
|
+
|
110
|
+
def objective(trial: optuna.trial.Trial):
|
111
|
+
"""
|
112
|
+
이 함수는 외부 서버에서 실행됩니다.
|
113
|
+
모든 import는 함수 내부에 작성하세요.
|
114
|
+
"""
|
115
|
+
import torch # 함수 내부에서 import
|
116
|
+
|
117
|
+
x = trial.suggest_float('x', -10, 10)
|
118
|
+
y = trial.suggest_float('y', -10, 10)
|
119
|
+
return (x - 2) ** 2 + (y - 3) ** 2
|
120
|
+
```
|
121
|
+
- 저장한 함수를 import해서 사용
|
122
|
+
```python
|
123
|
+
import aiauto
|
124
|
+
from my_objective import objective
|
125
|
+
|
126
|
+
ac = aiauto.AIAutoController('<token>')
|
127
|
+
study = ac.create_study('test', 'minimize')
|
128
|
+
study.optimize(objective, n_trials=10, parallelism=2)
|
129
|
+
```
|
130
|
+
|
131
|
+
## 빠른 시작
|
132
|
+
|
133
|
+
### 1. 간단한 예제 (수학 함수 최적화)
|
134
|
+
|
135
|
+
```python
|
136
|
+
import optuna
|
137
|
+
import aiauto
|
138
|
+
|
139
|
+
|
140
|
+
# `https://dashboard.aiauto.pangyo.ainode.ai` 에 접속하여 ainode 에 로그인 한 후 aiauto 의 token 을 발급
|
141
|
+
# AIAutoController singleton 객체를 초기화 하여, OptunaWorkspace 를 활성화 시킨다 (토큰은 한 번만 설정)
|
142
|
+
ac = aiauto.AIAutoController('<token>')
|
143
|
+
# `https://dashboard.aiauto.pangyo.ainode.ai/workspace` 에서 생성된 OptunaWorkspace 와 optuna-dashboard 링크를 확인할 수 있음
|
144
|
+
|
145
|
+
# StudyWrapper 생성
|
146
|
+
study_wrapper = ac.create_study(
|
147
|
+
study_name="simple_optimization",
|
148
|
+
direction="minimize"
|
149
|
+
# sampler=optuna.samplers.TPESampler(), # optuna 에서 제공하는 sampler 그대로 사용 가능, 참고 https://optuna.readthedocs.io/en/stable/reference/samplers/index.html
|
150
|
+
)
|
151
|
+
# `https://dashboard.aiauto.pangyo.ainode.ai/study` 에서 생성된 study 확인 가능
|
152
|
+
|
153
|
+
# objective 함수 정의
|
154
|
+
def objective(trial: optuna.trial.Trial):
|
155
|
+
"""실제 실행은 사용자 로컬 컴퓨터가 아닌 서버에서 실행 될 함수"""
|
156
|
+
x = trial.suggest_float('x', -10, 10)
|
157
|
+
y = trial.suggest_float('y', -10, 10)
|
158
|
+
return (x - 2) ** 2 + (y - 3) ** 2
|
159
|
+
|
160
|
+
# 사용자 모델 학습 or 최적화 실행 (서버에서 병렬 실행)
|
161
|
+
study_wrapper.optimize(
|
162
|
+
objective,
|
163
|
+
n_trials=100,
|
164
|
+
parallelism=4 # 동시 실행 Pod 수
|
165
|
+
)
|
166
|
+
# `https://dashboard.aiauto.pangyo.ainode.ai/workspace` 에서 생성된 optuna-dashboard 링크에서 결과 확인 가능
|
167
|
+
```
|
168
|
+
|
169
|
+
### 2. PyTorch 모델 최적화 (Single Objective)
|
170
|
+
|
171
|
+
```python
|
172
|
+
import optuna
|
173
|
+
import aiauto
|
174
|
+
|
175
|
+
|
176
|
+
# `https://dashboard.aiauto.pangyo.ainode.ai` 에 접속하여 ainode 에 로그인 한 후 aiauto 의 token 을 발급
|
177
|
+
# AIAutoController singleton 객체를 초기화 하여, OptunaWorkspace 를 활성화 시킨다 (토큰은 한 번만 설정)
|
178
|
+
ac = aiauto.AIAutoController('<token>')
|
179
|
+
# `https://dashboard.aiauto.pangyo.ainode.ai/workspace` 에서 생성된 OptunaWorkspace 와 optuna-dashboard 링크를 확인할 수 있음
|
180
|
+
|
181
|
+
# StudyWrapper 생성
|
182
|
+
study_wrapper = ac.create_study(
|
183
|
+
study_name="pytorch_optimization",
|
184
|
+
direction="minimize",
|
185
|
+
# sampler=optuna.samplers.TPESampler(), # optuna 에서 제공하는 sampler 그대로 사용 가능, 참고 https://optuna.readthedocs.io/en/stable/reference/samplers/index.html
|
186
|
+
pruner=optuna.pruners.PatientPruner( # optuna 에서 제공하는 pruner 그대로 사용 가능, 참고 https://optuna.readthedocs.io/en/stable/reference/pruners.html
|
187
|
+
optuna.pruners.MedianPruner(),
|
188
|
+
patience=4,
|
189
|
+
),
|
190
|
+
)
|
191
|
+
# `https://dashboard.aiauto.pangyo.ainode.ai/study` 에서 생성된 study 확인 가능
|
192
|
+
|
193
|
+
# objective 함수 정의
|
194
|
+
# https://docs.pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html 참고
|
195
|
+
def objective(trial: optuna.trial.Trial):
|
196
|
+
"""
|
197
|
+
실제 실행은 사용자 로컬 컴퓨터가 아닌 서버에서 실행 될 함수
|
198
|
+
모든 import는 함수 내부에 존재해야 함
|
199
|
+
"""
|
200
|
+
import torch
|
201
|
+
from torch import nn, optim
|
202
|
+
from torch.utils.data import DataLoader, random_split, Subset
|
203
|
+
from torchvision import transforms, datasets
|
204
|
+
import torch.nn.functional as F
|
205
|
+
|
206
|
+
# 하이퍼파라미터 샘플링
|
207
|
+
lr = trial.suggest_float('learning_rate', 1e-5, 1e-1, log=True)
|
208
|
+
momentom = trial.suggest_float('momentom', 0.1, 0.99)
|
209
|
+
batch_size = trial.suggest_categorical('batch_size', [16, 32, 64, 128])
|
210
|
+
epochs = trial.suggest_int('epochs', 10, 100, step=10)
|
211
|
+
|
212
|
+
# 모델 정의
|
213
|
+
class Net(nn.Module):
|
214
|
+
def __init__(self):
|
215
|
+
super().__init__()
|
216
|
+
self.conv1 = nn.Conv2d(3, 6, 5)
|
217
|
+
self.pool = nn.MaxPool2d(2, 2)
|
218
|
+
self.conv2 = nn.Conv2d(6, 16, 5)
|
219
|
+
self.fc1 = nn.Linear(16 * 5 * 5, 120)
|
220
|
+
self.fc2 = nn.Linear(120, 84)
|
221
|
+
self.fc3 = nn.Linear(84, 10)
|
222
|
+
|
223
|
+
def forward(self, x):
|
224
|
+
x = self.pool(F.relu(self.conv1(x)))
|
225
|
+
x = self.pool(F.relu(self.conv2(x)))
|
226
|
+
x = torch.flatten(x, 1) # flatten all dimensions except batch
|
227
|
+
x = F.relu(self.fc1(x))
|
228
|
+
x = F.relu(self.fc2(x))
|
229
|
+
x = self.fc3(x)
|
230
|
+
return x
|
231
|
+
|
232
|
+
# 모델 정의 및 학습 (GPU 자동 사용)
|
233
|
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
234
|
+
model = Net().to(device)
|
235
|
+
criterion = nn.CrossEntropyLoss()
|
236
|
+
optimizer = optim.SGD(model.parameters(), lr=lr, momentum=momentom)
|
237
|
+
|
238
|
+
# 데이터 로드
|
239
|
+
train_set = datasets.CIFAR10(
|
240
|
+
root="/tmp/cifar10_data", # Pod의 임시 디렉토리 사용
|
241
|
+
train=True,
|
242
|
+
download=True,
|
243
|
+
transform=[
|
244
|
+
transforms.ToTensor(),
|
245
|
+
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
|
246
|
+
],
|
247
|
+
)
|
248
|
+
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=2)
|
249
|
+
|
250
|
+
test_set = datasets.CIFAR10(
|
251
|
+
root="/tmp/cifar10_data", # Pod의 임시 디렉토리 사용
|
252
|
+
train=False,
|
253
|
+
download=True,
|
254
|
+
transform=[
|
255
|
+
transforms.ToTensor(),
|
256
|
+
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
|
257
|
+
],
|
258
|
+
)
|
259
|
+
test_loader = DataLoader(test_set, batch_size=batch_size, shuffle=True, num_workers=2)
|
260
|
+
|
261
|
+
# 학습
|
262
|
+
min_epochs_for_pruning = max(50, epochs // 5) # 최소 50 epoch 또는 전체의 1/5 후부터 pruning
|
263
|
+
total_loss = 0.0
|
264
|
+
for epoch in range(epochs): # loop over the dataset multiple times
|
265
|
+
running_loss = 0.0
|
266
|
+
model.train()
|
267
|
+
for i, (inputs, targets) in enumerate(train_loader, 0):
|
268
|
+
inputs, targets = inputs.to(device), targets.to(device)
|
269
|
+
# zero the parameter gradients
|
270
|
+
optimizer.zero_grad()
|
271
|
+
# forward + backward + optimize
|
272
|
+
outputs = model(inputs)
|
273
|
+
loss = criterion(outputs, targets)
|
274
|
+
loss.backward()
|
275
|
+
optimizer.step()
|
276
|
+
|
277
|
+
# print statistics
|
278
|
+
running_loss += loss.item()
|
279
|
+
if i % 2000 == 1999: # print every 2000 mini-batches
|
280
|
+
print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.3f}')
|
281
|
+
|
282
|
+
# intermediate result 보고 및 초기 중단 검사 - 최소 epochs 후 부터만 pruning
|
283
|
+
trial.report(running_loss, epoch)
|
284
|
+
total_loss += running_loss
|
285
|
+
if epoch >= min_epochs_for_pruning and trial.should_prune():
|
286
|
+
raise optuna.TrialPruned()
|
287
|
+
|
288
|
+
return total_loss
|
289
|
+
|
290
|
+
# GPU Pod에서 실행
|
291
|
+
study_wrapper.optimize(
|
292
|
+
objective,
|
293
|
+
n_trials=100,
|
294
|
+
parallelism=4,
|
295
|
+
use_gpu=True, # GPU 사용
|
296
|
+
requirements_list=['torch', 'torchvision'] # Pod에서 자동 설치
|
297
|
+
)
|
298
|
+
```
|
299
|
+
|
300
|
+
### 3. Multi-Objective 최적화 (Accuracy + FLOPS)
|
301
|
+
|
302
|
+
```python
|
303
|
+
import optuna
|
304
|
+
import aiauto
|
305
|
+
|
306
|
+
|
307
|
+
# `https://dashboard.aiauto.pangyo.ainode.ai` 에 접속하여 ainode 에 로그인 한 후 aiauto 의 token 을 발급
|
308
|
+
# AIAutoController singleton 객체를 초기화 하여, OptunaWorkspace 를 활성화 시킨다 (토큰은 한 번만 설정)
|
309
|
+
ac = aiauto.AIAutoController('<token>')
|
310
|
+
# `https://dashboard.aiauto.pangyo.ainode.ai/workspace` 에서 생성된 OptunaWorkspace 와 optuna-dashboard 링크를 확인할 수 있음
|
311
|
+
|
312
|
+
# StudyWrapper 생성
|
313
|
+
study_wrapper = ac.create_study(
|
314
|
+
study_name="pytorch_multiple_optimization",
|
315
|
+
direction=["minimize", "minimize"], # loss minimize, FLOPS minimize
|
316
|
+
# sampler=optuna.samplers.TPESampler(), # optuna 에서 제공하는 sampler 그대로 사용 가능, 참고 https://optuna.readthedocs.io/en/stable/reference/samplers/index.html
|
317
|
+
)
|
318
|
+
# `https://dashboard.aiauto.pangyo.ainode.ai/study` 에서 생성된 study 확인 가능
|
319
|
+
|
320
|
+
# objective 함수 정의
|
321
|
+
# https://docs.pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html 참고
|
322
|
+
def objective(trial: optuna.trial.Trial):
|
323
|
+
"""
|
324
|
+
실제 실행은 사용자 로컬 컴퓨터가 아닌 서버에서 실행 될 함수
|
325
|
+
모든 import는 함수 내부에 존재해야 함
|
326
|
+
"""
|
327
|
+
import torch
|
328
|
+
from torch import nn, optim
|
329
|
+
from torch.utils.data import DataLoader, random_split, Subset
|
330
|
+
from torchvision import transforms, datasets
|
331
|
+
import torch.nn.functional as F
|
332
|
+
from fvcore.nn import FlopCountAnalysis
|
333
|
+
|
334
|
+
# 하이퍼파라미터 샘플링
|
335
|
+
lr = trial.suggest_float('learning_rate', 1e-5, 1e-1, log=True)
|
336
|
+
momentom = trial.suggest_float('momentom', 0.1, 0.99)
|
337
|
+
batch_size = trial.suggest_categorical('batch_size', [16, 32, 64, 128])
|
338
|
+
epochs = trial.suggest_int('epochs', 10, 100, step=10)
|
339
|
+
|
340
|
+
# 모델 정의
|
341
|
+
class Net(nn.Module):
|
342
|
+
def __init__(self):
|
343
|
+
super().__init__()
|
344
|
+
self.conv1 = nn.Conv2d(3, 6, 5)
|
345
|
+
self.pool = nn.MaxPool2d(2, 2)
|
346
|
+
self.conv2 = nn.Conv2d(6, 16, 5)
|
347
|
+
self.fc1 = nn.Linear(16 * 5 * 5, 120)
|
348
|
+
self.fc2 = nn.Linear(120, 84)
|
349
|
+
self.fc3 = nn.Linear(84, 10)
|
350
|
+
|
351
|
+
def forward(self, x):
|
352
|
+
x = self.pool(F.relu(self.conv1(x)))
|
353
|
+
x = self.pool(F.relu(self.conv2(x)))
|
354
|
+
x = torch.flatten(x, 1) # flatten all dimensions except batch
|
355
|
+
x = F.relu(self.fc1(x))
|
356
|
+
x = F.relu(self.fc2(x))
|
357
|
+
x = self.fc3(x)
|
358
|
+
return x
|
359
|
+
|
360
|
+
# 모델 정의 및 학습 (GPU 자동 사용)
|
361
|
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
362
|
+
model = Net().to(device)
|
363
|
+
criterion = nn.CrossEntropyLoss()
|
364
|
+
optimizer = optim.SGD(model.parameters(), lr=lr, momentum=momentom)
|
365
|
+
|
366
|
+
# 데이터 로드
|
367
|
+
train_set = datasets.CIFAR10(
|
368
|
+
root="/tmp/cifar10_data", # Pod의 임시 디렉토리 사용
|
369
|
+
train=True,
|
370
|
+
download=True,
|
371
|
+
transform=[
|
372
|
+
transforms.ToTensor(),
|
373
|
+
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
|
374
|
+
],
|
375
|
+
)
|
376
|
+
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=2)
|
377
|
+
|
378
|
+
test_set = datasets.CIFAR10(
|
379
|
+
root="/tmp/cifar10_data", # Pod의 임시 디렉토리 사용
|
380
|
+
train=False,
|
381
|
+
download=True,
|
382
|
+
transform=[
|
383
|
+
transforms.ToTensor(),
|
384
|
+
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
|
385
|
+
],
|
386
|
+
)
|
387
|
+
test_loader = DataLoader(test_set, batch_size=batch_size, shuffle=True, num_workers=2)
|
388
|
+
|
389
|
+
# 학습
|
390
|
+
total_loss = 0.0
|
391
|
+
# multiple objective 는 pruning 미지원
|
392
|
+
for epoch in range(epochs): # loop over the dataset multiple times
|
393
|
+
running_loss = 0.0
|
394
|
+
model.train()
|
395
|
+
for i, (inputs, targets) in enumerate(train_loader, 0):
|
396
|
+
inputs, targets = inputs.to(device), targets.to(device)
|
397
|
+
# zero the parameter gradients
|
398
|
+
optimizer.zero_grad()
|
399
|
+
# forward + backward + optimize
|
400
|
+
outputs = model(inputs)
|
401
|
+
loss = criterion(outputs, targets)
|
402
|
+
loss.backward()
|
403
|
+
optimizer.step()
|
404
|
+
|
405
|
+
# print statistics
|
406
|
+
running_loss += loss.item()
|
407
|
+
if i % 2000 == 1999: # print every 2000 mini-batches
|
408
|
+
print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.3f}')
|
409
|
+
|
410
|
+
# multiple objective 는 pruning 미지원
|
411
|
+
|
412
|
+
# FLOPS 계산
|
413
|
+
dummy_input = torch.randn(1, 3, 32, 32).to(device)
|
414
|
+
flops = FlopCountAnalysis(model, (dummy_input,)).total()
|
415
|
+
|
416
|
+
return total_loss, flops
|
417
|
+
|
418
|
+
# GPU Pod에서 실행
|
419
|
+
study_wrapper.optimize(
|
420
|
+
objective,
|
421
|
+
n_trials=100,
|
422
|
+
parallelism=4,
|
423
|
+
use_gpu=True, # GPU 사용
|
424
|
+
requirements_list=['torch', 'torchvision', 'fvcore'] # Pod에서 자동 설치
|
425
|
+
)
|
426
|
+
```
|
427
|
+
|
428
|
+
### 4. Ask/Tell 패턴 및 Optuna 자체의 Study
|
429
|
+
|
430
|
+
```python
|
431
|
+
import optuna
|
432
|
+
import aiauto
|
433
|
+
|
434
|
+
# `https://dashboard.aiauto.pangyo.ainode.ai` 에 접속하여 ainode 에 로그인 한 후 aiauto 의 token 을 발급
|
435
|
+
# AIAutoController singleton 객체를 초기화 하여, OptunaWorkspace 를 활성화 시킨다 (토큰은 한 번만 설정)
|
436
|
+
ac = aiauto.AIAutoController('<token>')
|
437
|
+
# `https://dashboard.aiauto.pangyo.ainode.ai/workspace` 에서 생성된 OptunaWorkspace 와 optuna-dashboard 링크를 확인할 수 있음
|
438
|
+
|
439
|
+
# Study 생성
|
440
|
+
study_wrapper = ac.create_study(
|
441
|
+
study_name="test",
|
442
|
+
direction='minimize',
|
443
|
+
# sampler=optuna.samplers.TPESampler(), # optuna 에서 제공하는 sampler 그대로 사용 가능, 참고 https://optuna.readthedocs.io/en/stable/reference/samplers/index.html
|
444
|
+
# pruner=optuna.pruners.PatientPruner( # optuna 에서 제공하는 pruner 그대로 사용 가능, 참고 https://optuna.readthedocs.io/en/stable/reference/pruners.html
|
445
|
+
# optuna.pruners.MedianPruner(),
|
446
|
+
# patience=4,
|
447
|
+
# )
|
448
|
+
)
|
449
|
+
# `https://dashboard.aiauto.pangyo.ainode.ai/study` 에서 생성된 study 확인 가능
|
450
|
+
|
451
|
+
# 실제 optuna.Study 객체 획득 (로컬에서 ask/tell 가능)
|
452
|
+
study = study_wrapper.get_study()
|
453
|
+
|
454
|
+
# Ask/Tell 패턴으로 최적화
|
455
|
+
trial = study.ask()
|
456
|
+
|
457
|
+
# 파라미터 최적화
|
458
|
+
x = trial.suggest_float('x', -10, 10)
|
459
|
+
y = trial.suggest_float('y', -10, 10)
|
460
|
+
|
461
|
+
# 사용자 모델 학습 or 최적화 실행 (서버에서 병렬 실행)
|
462
|
+
ret = (x - 2) ** 2 + (y - 3) ** 2
|
463
|
+
|
464
|
+
# 결과 보고
|
465
|
+
study.tell(trial, ret)
|
466
|
+
# `https://dashboard.aiauto.pangyo.ainode.ai/workspace` 에서 생성된 optuna-dashboard 링크에서 결과 확인 가능
|
467
|
+
```
|
468
|
+
|
469
|
+
# lib build
|
470
|
+
```bash
|
471
|
+
make build push
|
472
|
+
```
|