Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rheasukthanker committed May 28, 2024
1 parent a46a415 commit a72cd2e
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 98 deletions.
16 changes: 11 additions & 5 deletions lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def get_hw_predictor_surrogate(
with open(surrogate_path, "rb") as f:
predictor = pickle.load(f)
elif surrogate_type == "mlp":
predictor = Nethw(max_layers, False, 128,128).cuda()
predictor = Nethw(max_layers, False, 128, 128).cuda()
path = model_path + ".pth"
predictor.load_state_dict(torch.load(path))
return predictor
Expand All @@ -271,7 +271,7 @@ def predict_hw_surrogate(
return energy
energy = energy[0, quantile]
else:
energy = surrogate(torch.tensor(arch).cuda())#.item()
energy = surrogate(torch.tensor(arch).cuda()) # .item()
return energy


Expand All @@ -284,11 +284,17 @@ def get_ppl_predictor_surrogate(search_space):
max_layers = 36
ppl_predictor = Net(max_layers, 128).cuda()
if search_space == "s":
pred_path = "data_collection/gpt_datasets/predictor_ckpts/metric/perplexity_s.pt"
pred_path = (
"data_collection/gpt_datasets/predictor_ckpts/metric/perplexity_s.pt"
)
elif search_space == "m":
pred_path = "data_collection/gpt_datasets/predictor_ckpts/metric/perplexity_m.pt"
pred_path = (
"data_collection/gpt_datasets/predictor_ckpts/metric/perplexity_m.pt"
)
else:
pred_path = "data_collection/gpt_datasets/predictor_ckpts/metric/perplexity_l.pt"
pred_path = (
"data_collection/gpt_datasets/predictor_ckpts/metric/perplexity_l.pt"
)
ppl_predictor.load_state_dict(torch.load(pred_path))
return ppl_predictor

Expand Down
28 changes: 21 additions & 7 deletions plotting/eaf/eaf.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def _get_pf_set_list(costs: np.ndarray) -> list[np.ndarray]:
return pf_set_list


def _compute_emp_att_surf(X: np.ndarray, pf_set_list: list[np.ndarray], levels: np.ndarray) -> np.ndarray:
def _compute_emp_att_surf(
X: np.ndarray, pf_set_list: list[np.ndarray], levels: np.ndarray
) -> np.ndarray:
"""
Compute the empirical attainment surface of the given Pareto front sets.
Expand Down Expand Up @@ -146,28 +148,40 @@ def get_empirical_attainment_surface(

if len(costs.shape) != 3:
# costs.shape = (n_independent_runs, n_samples, n_obj)
raise ValueError(f"costs must have the shape of (n_independent_runs, n_samples, n_obj), but got {costs.shape}")
raise ValueError(
f"costs must have the shape of (n_independent_runs, n_samples, n_obj), but got {costs.shape}"
)

(n_independent_runs, _, n_obj) = costs.shape
if n_obj != 2:
raise NotImplementedError("Three or more objectives are not supported.")
if not all(1 <= level <= n_independent_runs for level in levels):
raise ValueError(f"All elements in levels must be in [1, n_independent_runs], but got {levels}")
raise ValueError(
f"All elements in levels must be in [1, n_independent_runs], but got {levels}"
)
if not np.all(np.maximum.accumulate(levels) == levels):
raise ValueError(f"levels must be an increasing sequence, but got {levels}")

_costs = costs.copy()
if larger_is_better_objectives is not None:
_costs = _change_directions(_costs, larger_is_better_objectives=larger_is_better_objectives)
_costs = _change_directions(
_costs, larger_is_better_objectives=larger_is_better_objectives
)

log_scale = log_scale if log_scale is not None else []
pf_set_list = _get_pf_set_list(_costs)
pf_sols = np.vstack(pf_set_list)
X = np.unique(np.hstack([LOGEPS if 0 in log_scale else -np.inf, pf_sols[:, 0], np.inf]))
X = np.unique(
np.hstack([LOGEPS if 0 in log_scale else -np.inf, pf_sols[:, 0], np.inf])
)

emp_att_surfs = _compute_emp_att_surf(X=X, pf_set_list=pf_set_list, levels=np.asarray(levels))
emp_att_surfs = _compute_emp_att_surf(
X=X, pf_set_list=pf_set_list, levels=np.asarray(levels)
)
if larger_is_better_objectives is not None:
emp_att_surfs = _change_directions(emp_att_surfs, larger_is_better_objectives=larger_is_better_objectives)
emp_att_surfs = _change_directions(
emp_att_surfs, larger_is_better_objectives=larger_is_better_objectives
)
if larger_is_better_objectives is not None and 0 in larger_is_better_objectives:
emp_att_surfs = np.flip(emp_att_surfs, axis=1)

Expand Down
98 changes: 73 additions & 25 deletions plotting/eaf/plot_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ def __init__(
):
self.step_dir = _step_direction(larger_is_better_objectives)
self.larger_is_better_objectives = (
larger_is_better_objectives if larger_is_better_objectives is not None else []
larger_is_better_objectives
if larger_is_better_objectives is not None
else []
)
self._ref_point = (
ref_point.copy().astype(np.float64) if ref_point is not None else None
)
self._ref_point = ref_point.copy().astype(np.float64) if ref_point is not None else None
self.log_scale = log_scale if log_scale is not None else []
self.x_is_log, self.y_is_log = 0 in self.log_scale, 1 in self.log_scale
self._plot_kwargs = dict(
Expand All @@ -81,8 +85,8 @@ def __init__(
)

if true_pareto_sols is not None:
self.x_min, self.x_max, self.y_min, self.y_max = _get_slighly_expanded_value_range(
true_pareto_sols, self.log_scale
self.x_min, self.x_max, self.y_min, self.y_max = (
_get_slighly_expanded_value_range(true_pareto_sols, self.log_scale)
)
self._true_pareto_sols = true_pareto_sols.copy()
else:
Expand All @@ -95,7 +99,9 @@ def __init__(

def _transform_surface_list(self, surfs_list: list[np.ndarray]) -> list[np.ndarray]:
for surf in surfs_list:
x_min, x_max, y_min, y_max = _get_slighly_expanded_value_range(surf, self.log_scale)
x_min, x_max, y_min, y_max = _get_slighly_expanded_value_range(
surf, self.log_scale
)
self.x_min, self.x_max = min(self.x_min, x_min), max(self.x_max, x_max)
self.y_min, self.y_max = min(self.y_min, y_min), max(self.y_max, y_max)

Expand Down Expand Up @@ -148,7 +154,9 @@ def plot_surface(
The plotted line object.
"""
if len(surf.shape) != 2 or surf.shape[1] != 2:
raise ValueError(f"The shape of surf must be (n_points, 2), but got {surf.shape}")
raise ValueError(
f"The shape of surf must be (n_points, 2), but got {surf.shape}"
)

_surf = surf.copy()
if transform:
Expand All @@ -159,7 +167,9 @@ def plot_surface(
kwargs.update(drawstyle=f"steps-{self.step_dir}")
_check_surface(_surf)
X, Y = _surf[:, 0], _surf[:, 1]
line = ax.plot(X, Y, color=color, label=label, linestyle=linestyle, marker=marker, **kwargs)[0]
line = ax.plot(
X, Y, color=color, label=label, linestyle=linestyle, marker=marker, **kwargs
)[0]
_change_scale(ax, self.log_scale)
return line

Expand Down Expand Up @@ -195,7 +205,9 @@ def plot_true_pareto_surface(
The plotted line object.
"""
if self._true_pareto_sols is None:
raise AttributeError("true_pareto_sols is not provided at the instantiation")
raise AttributeError(
"true_pareto_sols is not provided at the instantiation"
)

true_pareto_surf = pareto_front_to_surface(
self._true_pareto_sols.copy(),
Expand Down Expand Up @@ -258,7 +270,9 @@ def plot_multiple_surface(
n_surfs = len(_surfs)
linestyles = linestyles if linestyles is not None else [None] * n_surfs
markers = markers if markers is not None else [None] * n_surfs
for surf, color, label, linestyle, marker in zip(_surfs, colors, labels, linestyles, markers):
for surf, color, label, linestyle, marker in zip(
_surfs, colors, labels, linestyles, markers
):
kwargs.update(color=color, label=label, linestyle=linestyle, marker=marker)
line = self.plot_surface(ax, surf, transform=False, **kwargs)
lines.append(line)
Expand Down Expand Up @@ -308,7 +322,9 @@ def plot_surface_with_band(
The plotted line object.
"""
if surfs.shape[0] != 3:
raise ValueError(f"plot_surface_with_band requires three levels, but got only {surfs.shape[0]} levels")
raise ValueError(
f"plot_surface_with_band requires three levels, but got only {surfs.shape[0]} levels"
)

_surfs = deepcopy(surfs)
if transform:
Expand All @@ -324,12 +340,16 @@ def plot_surface_with_band(
marker_kwargs, kwargs = _extract_marker_kwargs(**kwargs)
kwargs.update(color=color)
alpha = kwargs.pop("alpha", None)
ax.fill_between(X, _surfs[0, :, 1], _surfs[2, :, 1], alpha=0.2, step=self.step_dir, **kwargs)
ax.fill_between(
X, _surfs[0, :, 1], _surfs[2, :, 1], alpha=0.2, step=self.step_dir, **kwargs
)
kwargs["alpha"] = alpha

# marker and linestyle are only for plot
kwargs.update(label=label, linestyle=linestyle, marker=marker, **marker_kwargs)
line = ax.plot(X, _surfs[1, :, 1], drawstyle=f"steps-{self.step_dir}", **kwargs)[0]
line = ax.plot(
X, _surfs[1, :, 1], drawstyle=f"steps-{self.step_dir}", **kwargs
)[0]
_change_scale(ax, self.log_scale)
return line

Expand Down Expand Up @@ -375,7 +395,9 @@ def plot_multiple_surface_with_band(
n_surfs = len(_surfs_list)
linestyles = linestyles if linestyles is not None else [None] * n_surfs
markers = markers if markers is not None else [None] * n_surfs
for surf, color, label, linestyle, marker in zip(_surfs_list, colors, labels, linestyles, markers):
for surf, color, label, linestyle, marker in zip(
_surfs_list, colors, labels, linestyles, markers
):
kwargs.update(color=color, label=label, linestyle=linestyle, marker=marker)
line = self.plot_surface_with_band(ax, surf, transform=False, **kwargs)
lines.append(line)
Expand All @@ -384,7 +406,9 @@ def plot_multiple_surface_with_band(
ax.set_ylim(self.y_min, self.y_max)
return lines

def _transform_ref_point_and_costs_array(self, costs_array: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
def _transform_ref_point_and_costs_array(
self, costs_array: np.ndarray
) -> tuple[np.ndarray, np.ndarray]:
ref_point = self._ref_point.copy()
_costs_array = costs_array.copy()
if self.x_is_log:
Expand All @@ -395,8 +419,12 @@ def _transform_ref_point_and_costs_array(self, costs_array: np.ndarray) -> tuple
ref_point[1] = np.log(ref_point[1])

if len(self.larger_is_better_objectives) > 0:
_costs_array = _change_directions(_costs_array, self.larger_is_better_objectives)
ref_point = _change_directions(ref_point[np.newaxis], self.larger_is_better_objectives)[0]
_costs_array = _change_directions(
_costs_array, self.larger_is_better_objectives
)
ref_point = _change_directions(
ref_point[np.newaxis], self.larger_is_better_objectives
)[0]

return ref_point, _costs_array

Expand Down Expand Up @@ -450,14 +478,18 @@ def plot_hypervolume2d_with_band(
f"The shape of costs_array must be (n_independent_runs, n_points, 2), but got {costs_array.shape}"
)
if self._ref_point is None:
raise AttributeError("ref_point must be provided for plot_hypervolume2d_with_band")
raise AttributeError(
"ref_point must be provided for plot_hypervolume2d_with_band"
)

ref_point, _costs_array = self._transform_ref_point_and_costs_array(costs_array)
(n_runs, n_observations, _) = _costs_array.shape
hvs = np.zeros((n_runs, n_observations))
for i in range(n_observations):
hvs[:, i] = _compute_hypervolume2d(costs_array=_costs_array[:, : i + 1], ref_point=ref_point)
#print(hvs)
hvs[:, i] = _compute_hypervolume2d(
costs_array=_costs_array[:, : i + 1], ref_point=ref_point
)
# print(hvs)
if normalize:
if self._true_pareto_sols is None:
raise AttributeError(
Expand Down Expand Up @@ -546,9 +578,19 @@ def plot_multiple_hypervolume2d_with_band(
n_lines = len(costs_array)
linestyles = linestyles if linestyles is not None else [None] * n_lines
markers = markers if markers is not None else [None] * n_lines
for _costs_array, color, label, linestyle, marker in zip(costs_array, colors, labels, linestyles, markers):
kwargs.update(color=color, label=label, linestyle=linestyle, marker=marker, normalize=normalize)
line = self.plot_hypervolume2d_with_band(ax, _costs_array, log=False, axis_label=False, **kwargs)
for _costs_array, color, label, linestyle, marker in zip(
costs_array, colors, labels, linestyles, markers
):
kwargs.update(
color=color,
label=label,
linestyle=linestyle,
marker=marker,
normalize=normalize,
)
line = self.plot_hypervolume2d_with_band(
ax, _costs_array, log=False, axis_label=False, **kwargs
)
lines.append(line)

if log:
Expand All @@ -562,12 +604,18 @@ def plot_multiple_hypervolume2d_with_band(

def _compute_true_pareto_surface_hypervolume2d(self) -> float:
if self._true_pareto_sols is None:
raise AttributeError("true_pareto_sols is not provided at the instantiation")
raise AttributeError(
"true_pareto_sols is not provided at the instantiation"
)

if self._ref_point is None:
raise AttributeError("ref_point must be provided for plot_hypervolume2d_with_band")
raise AttributeError(
"ref_point must be provided for plot_hypervolume2d_with_band"
)

ref_point, true_pf = self._transform_ref_point_and_costs_array(self._true_pareto_sols)
ref_point, true_pf = self._transform_ref_point_and_costs_array(
self._true_pareto_sols
)
hv = _compute_hypervolume2d(true_pf[np.newaxis], ref_point)[0]
return hv

Expand Down
Loading

0 comments on commit a72cd2e

Please sign in to comment.