Skip to content

Instantly share code, notes, and snippets.

View c-bata's full-sized avatar
🐟

c-bata c-bata

🐟
View GitHub Profile
@c-bata
c-bata / generator.py
Last active October 28, 2023 01:29
PyCon APAC 2023 - ハイパーパラメータ最適化フレームワーク Optunaの最新機能紹介
# Requirements:
# $ pip install optuna optuna-dashboard diffusers transformers accelerate scipy safetensors xformers botorch
#
# Process A: Launch a process that suggest new params and generate images.
# $ python generator.py
#
# Process B: Launch an Optuna Dashboard process.
# $ optuna-dashboard sqlite:///db.sqlite3 --artifact-dir ./artifact
import os
import time
from __future__ import annotations
import os
import shutil
import uuid
import tempfile
from typing import NoReturn
import optuna
import streamlit as st
import sys
import numpy as np
from kurobako import problem
from kurobako.problem import Problem
from typing import List
from typing import Optional
import argparse
import os
import subprocess
def run(args: argparse.Namespace) -> None:
kurobako_cmd = os.path.join(args.path_to_kurobako, "kurobako")
subprocess.run(f"{kurobako_cmd} --version", shell=True)
if not (os.path.exists(args.data_dir) and os.path.isdir(args.data_dir)):
import math
import time
import optuna
import numpy as np
def objective(trial: optuna.Trial, n_params: int) -> float:
return sum([
math.sin(trial.suggest_float('param-{}'.format(i), 0, math.pi * 2))
for i in range(n_params)
@c-bata
c-bata / main.py
Last active October 28, 2023 01:04
W&B 東京Meetup #3 - Optunaを使ったHuman-in-the-loop最適化の紹介
# Requirements:
# $ pip install optuna optuna-dashboard[preferential] diffusers transformers accelerate scipy safetensors xformers
#
# Process A: Launch a process that suggest new params and generate images.
# $ python main.py
#
# Process B: Launch an Optuna Dashboard process.
# $ optuna-dashboard sqlite:///db.sqlite3 --artifact-dir ./artifact
import time
# $ docker run -d --rm --platform linux/amd64 -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=root -e POSTGRES_DB=optuna --name optuna-postgres postgres:12.10
# $ docker run -it --rm --platform linux/amd64 --network host -v $(pwd):/usr/src python:3.10 bash
# # cd /usr/src
# # pip install -U setuptools pip psycopg2
# # pip install -e .
from __future__ import annotations
import math
# docker run -d --rm -p 3306:3306 -e MYSQL_USER=optuna -e MYSQL_DATABASE=optuna -e MYSQL_PASSWORD=password -e MYSQL_ALLOW_EMPTY_PASSWORD=yes --name optuna-mysql mysql:8.0
from __future__ import annotations
import math
import threading
import time
from sqlalchemy import event
from sqlalchemy.engine.base import Engine

Summary

Optuna uses CachedStorage, a wrapper class of BaseStorage interface, since the API calls of BaseStorage interface tends to be expensive. However, the implementation of CachedStorage is too complex and it's actually not a wrapper of BaseStorage since we've introduced some private storage APIs for CachedStorage like storage._check_and_set_param_distribution() and storage._get_trials().

So I implemented a prototype of a new simple caching mechanism to remove CachedStorage from Optuna. The change is only about 50 lines, but it is more efficient in many situations than CachedStorage. https://github.com/optuna/optuna/compare/master...c-bata:add-simple-inmemory-cache?expand=1

For the benchmark of the new caching mechanism, I prepared the same benchmark scenario with optuna/optuna#11

import time
import numpy
from bottle import Bottle
from optuna_dashboard import wsgi
from optuna.storages import RDBStorage
from python_tests.wsgi_client import send_request