From 065ee12b0ec387c7587d10f8dce6df80aed84cd3 Mon Sep 17 00:00:00 2001 From: yanghaiqie Date: Sat, 9 Oct 2021 01:05:30 +0800 Subject: [PATCH] delete useless code --- autogl/module/nas/estimator/__init__.py | 8 +-- autogl/module/nas/estimator/one_shot.py | 46 +++++++------- .../nas/estimator/one_shot_hardware_aware.py | 63 ------------------- autogl/module/nas/estimator/train_scratch.py | 18 ++++-- .../estimator/train_scratch_hardware_aware.py | 53 ---------------- 5 files changed, 37 insertions(+), 151 deletions(-) delete mode 100644 autogl/module/nas/estimator/one_shot_hardware_aware.py delete mode 100644 autogl/module/nas/estimator/train_scratch_hardware_aware.py diff --git a/autogl/module/nas/estimator/__init__.py b/autogl/module/nas/estimator/__init__.py index 2d28328..e95af8b 100644 --- a/autogl/module/nas/estimator/__init__.py +++ b/autogl/module/nas/estimator/__init__.py @@ -1,5 +1,3 @@ -import importlib -import os from .base import BaseEstimator NAS_ESTIMATOR_DICT = {} @@ -21,10 +19,8 @@ def register_nas_estimator(name): return register_nas_estimator_cls -from .one_shot import OneShotEstimator -from .train_scratch import TrainEstimator -from .one_shot_hardware_aware import OneShotEstimator_HardwareAware -from .train_scratch_hardware_aware import TrainEstimator_HardwareAware +from .one_shot import OneShotEstimator, OneShotEstimator_HardwareAware +from .train_scratch import TrainEstimator, TrainEstimator_HardwareAware def build_nas_estimator_from_name(name: str) -> BaseEstimator: """ diff --git a/autogl/module/nas/estimator/one_shot.py b/autogl/module/nas/estimator/one_shot.py index fd25d04..3ee1ddb 100644 --- a/autogl/module/nas/estimator/one_shot.py +++ b/autogl/module/nas/estimator/one_shot.py @@ -1,31 +1,11 @@ -import torch.nn as nn import torch.nn.functional as F from . import register_nas_estimator from ..space import BaseSpace from .base import BaseEstimator from ..backend import * -from ...train.evaluation import Evaluation, Acc - -# @register_nas_estimator("oneshot") -# class OneShotEstimator(BaseEstimator): -# """ -# One shot estimator. - -# Use model directly to get estimations. -# """ - -# def infer(self, model: BaseSpace, dataset, mask="train"): -# device = next(model.parameters()).device -# dset = dataset[0].to(device) -# pred = model(dset)[getattr(dset, f"{mask}_mask")] -# y = dset.y[getattr(dset, f"{mask}_mask")] -# loss = getattr(F, self.loss_f)(pred, y) -# # acc=sum(pred.max(1)[1]==y).item()/y.size(0) -# probs = F.softmax(pred, dim=1).detach().cpu().numpy() -# y = y.cpu() -# metrics = [eva.evaluate(probs, y) for eva in self.evaluation] -# return metrics, loss +from ...train.evaluation import Acc + @register_nas_estimator("oneshot") class OneShotEstimator(BaseEstimator): @@ -48,8 +28,28 @@ class OneShotEstimator(BaseEstimator): y = label[mask] loss = getattr(F, self.loss_f)(pred, y) - # acc=sum(pred.max(1)[1]==y).item()/y.size(0) probs = F.softmax(pred, dim=1).detach().cpu().numpy() y = y.cpu() metrics = [eva.evaluate(probs, y) for eva in self.evaluation] return metrics, loss + + +@register_nas_estimator("oneshot_hardware") +class OneShotEstimator_HardwareAware(OneShotEstimator): + """ + One shot hardware-aware estimator. + + Use model directly to get estimations. + """ + def __init__(self, loss_f="nll_loss", evaluation=[Acc()], hardware_evaluation="parameter"): + super().__init__(loss_f, evaluation) + self.hardware_evaluation = hardware_evaluation + + def infer(self, model: BaseSpace, dataset, mask="train"): + metrics, loss = super().infer(model, dataset, mask) + if isinstance(self.hardware_evaluation, str): + model_info = model.get_model_info() + metrics.append(model_info[self.hardware_evaluation]()) + else: + metrics.append(self.hardware_evaluation(model)) + return metrics, loss diff --git a/autogl/module/nas/estimator/one_shot_hardware_aware.py b/autogl/module/nas/estimator/one_shot_hardware_aware.py deleted file mode 100644 index a4c574e..0000000 --- a/autogl/module/nas/estimator/one_shot_hardware_aware.py +++ /dev/null @@ -1,63 +0,0 @@ -import torch.nn as nn -import torch.nn.functional as F - -from . import register_nas_estimator -from ..space import BaseSpace -from .base import BaseEstimator -from ..backend import * -from ...train.evaluation import Evaluation, Acc - -# @register_nas_estimator("oneshot") -# class OneShotEstimator(BaseEstimator): -# """ -# One shot estimator. - -# Use model directly to get estimations. -# """ - -# def infer(self, model: BaseSpace, dataset, mask="train"): -# device = next(model.parameters()).device -# dset = dataset[0].to(device) -# pred = model(dset)[getattr(dset, f"{mask}_mask")] -# y = dset.y[getattr(dset, f"{mask}_mask")] -# loss = getattr(F, self.loss_f)(pred, y) -# # acc=sum(pred.max(1)[1]==y).item()/y.size(0) -# probs = F.softmax(pred, dim=1).detach().cpu().numpy() -# y = y.cpu() -# metrics = [eva.evaluate(probs, y) for eva in self.evaluation] -# return metrics, loss - -@register_nas_estimator("oneshot_hardware") -class OneShotEstimator_HardwareAware(BaseEstimator): - """ - One shot estimator. - - Use model directly to get estimations. - """ - def __init__(self, loss_f="nll_loss", evaluation=[Acc()], hardware_evaluation="parameter"): - super().__init__(loss_f, evaluation) - self.evaluation = evaluation - self.hardware_evaluation=hardware_evaluation - - def infer(self, model: BaseSpace, dataset, mask="train"): - device = next(model.parameters()).device - dset = dataset[0].to(device) - mask=bk_mask(dset,mask) - - pred = model(dset)[mask] - label=bk_label(dset) - y = label[mask] - - loss = getattr(F, self.loss_f)(pred, y) - # acc=sum(pred.max(1)[1]==y).item()/y.size(0) - probs = F.softmax(pred, dim=1).detach().cpu().numpy() - y = y.cpu() - # print(model_info) - # print(self.hardware_evaluation) - metrics = [eva.evaluate(probs, y) for eva in self.evaluation] - if isinstance(self.hardware_evaluation, str): - model_info = model.get_model_info() - metrics.append(model_info[self.hardware_evaluation]()) - else: - metrics.append(self.hardware_evaluation(model)) - return metrics, loss diff --git a/autogl/module/nas/estimator/train_scratch.py b/autogl/module/nas/estimator/train_scratch.py index 38036cc..aeb13a3 100644 --- a/autogl/module/nas/estimator/train_scratch.py +++ b/autogl/module/nas/estimator/train_scratch.py @@ -1,10 +1,7 @@ -import torch.nn as nn -import torch.nn.functional as F - from . import register_nas_estimator from ..space import BaseSpace from .base import BaseEstimator -from .one_shot import OneShotEstimator +from .one_shot import OneShotEstimator, OneShotEstimator_HardwareAware import torch from autogl.module.train import NodeClassificationFullTrainer, Acc @@ -22,8 +19,6 @@ class TrainEstimator(BaseEstimator): self.estimator = OneShotEstimator(self.loss_f, self.evaluation) def infer(self, model: BaseSpace, dataset, mask="train"): - # self.trainer.model=model - # self.trainer.device=model.device boxmodel = model.wrap() self.trainer = NodeClassificationFullTrainer( model=boxmodel, @@ -49,3 +44,14 @@ class TrainEstimator(BaseEstimator): return fin, 0 else: raise e + + +@register_nas_estimator("scratch_hardware") +class TrainEstimator_HardwareAware(TrainEstimator): + """ + An hardware-aware estimator which trans from scratch + """ + + def __init__(self, loss_f="nll_loss", evaluation=[Acc()], hardware_evaluation="parameter"): + super().__init__(loss_f, evaluation) + self.estimator = OneShotEstimator_HardwareAware(self.loss_f, self.evaluation, hardware_evaluation) diff --git a/autogl/module/nas/estimator/train_scratch_hardware_aware.py b/autogl/module/nas/estimator/train_scratch_hardware_aware.py deleted file mode 100644 index ab990c9..0000000 --- a/autogl/module/nas/estimator/train_scratch_hardware_aware.py +++ /dev/null @@ -1,53 +0,0 @@ -import torch.nn as nn -import torch.nn.functional as F - -from . import register_nas_estimator -from ..space import BaseSpace -from .base import BaseEstimator -from .one_shot_hardware_aware import OneShotEstimator_HardwareAware -import torch - -from autogl.module.train import NodeClassificationFullTrainer, Acc - - -@register_nas_estimator("scratch_hardware") -class TrainEstimator_HardwareAware(BaseEstimator): - """ - An estimator which trans from scratch - """ - - def __init__(self, loss_f="nll_loss", evaluation=[Acc()], hardware_evaluation="parameter"): - super().__init__(loss_f, evaluation) - self.evaluation = evaluation - self.hardware_evaluation = hardware_evaluation - self.estimator = OneShotEstimator_HardwareAware(self.loss_f, self.evaluation, self.hardware_evaluation) - - - def infer(self, model: BaseSpace, dataset, mask="train"): - # self.trainer.model=model - # self.trainer.device=model.device - boxmodel = model.wrap() - self.trainer = NodeClassificationFullTrainer( - model=boxmodel, - optimizer=torch.optim.Adam, - lr=0.005, - max_epoch=300, - early_stopping_round=30, - weight_decay=5e-4, - device="auto", - init=False, - feval=self.evaluation, - loss=self.loss_f, - lr_scheduler_type=None, - ) - try: - self.trainer.train(dataset) - with torch.no_grad(): - return self.estimator.infer(boxmodel.model, dataset, mask) - except RuntimeError as e: - if "cuda" in str(e) or "CUDA" in str(e): - INF = 100 - fin = [-INF if eva.is_higher_better else INF for eva in self.evaluation] - return fin, 0 - else: - raise e