-
Notifications
You must be signed in to change notification settings - Fork 243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NNCF] NNCF common accuracy aware training code pass mypy checks #2521
Open
siddhant-0707
wants to merge
3
commits into
openvinotoolkit:develop
Choose a base branch
from
siddhant-0707:issue2492
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
import pathlib | ||
from abc import ABC | ||
from abc import abstractmethod | ||
from typing import Callable, Dict, List, Optional, Tuple, TypeVar, Union | ||
from typing import Any, Callable, Dict, List, Optional, Tuple, TypeVar, Union | ||
|
||
from nncf.api.compression import CompressionAlgorithmController | ||
from nncf.api.compression import CompressionStage | ||
|
@@ -127,8 +127,8 @@ def initialize_training_loop_fns( | |
validate_fn: Callable[[TModel, Optional[int]], float], | ||
configure_optimizers_fn: Callable[[], Tuple[OptimizerType, LRSchedulerType]], | ||
dump_checkpoint_fn: Callable[[TModel, CompressionAlgorithmController, "TrainingRunner", str], None], | ||
**kwargs, | ||
): | ||
**kwargs: Any, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type hint is incorrect, review your general Python knowledge as to what type the |
||
) -> None: | ||
""" | ||
Register the user-supplied functions to be used to control the training process. | ||
|
||
|
@@ -146,7 +146,7 @@ def initialize_logging( | |
self, | ||
log_dir: Optional[Union[str, pathlib.Path]] = None, | ||
tensorboard_writer: Optional[TensorboardWriterType] = None, | ||
): | ||
) -> None: | ||
""" | ||
Initialize logging related variables | ||
|
||
|
@@ -164,7 +164,7 @@ def load_best_checkpoint(self, model: TModel) -> float: | |
""" | ||
|
||
@abstractmethod | ||
def is_model_fully_compressed(self, compression_controller) -> bool: | ||
def is_model_fully_compressed(self, compression_controller: CompressionAlgorithmController) -> bool: | ||
""" | ||
Check if model is fully compressed | ||
|
||
|
@@ -194,7 +194,7 @@ def __init__( | |
self.maximal_absolute_accuracy_drop = accuracy_aware_training_params.get( | ||
"maximal_absolute_accuracy_degradation" | ||
) | ||
self.maximal_total_epochs = accuracy_aware_training_params.get("maximal_total_epochs", AA_MAXIMAL_TOTAL_EPOCHS) | ||
self.maximal_total_epochs: int = accuracy_aware_training_params.get("maximal_total_epochs", AA_MAXIMAL_TOTAL_EPOCHS) | ||
|
||
self.verbose = verbose | ||
self.dump_checkpoints = dump_checkpoints | ||
|
@@ -213,8 +213,8 @@ def __init__( | |
self.current_val_metric_value = 0 | ||
self.current_loss = 0 | ||
|
||
self._compressed_training_history = [] | ||
self._best_checkpoint = None | ||
self._compressed_training_histor: List[Tuple[float, float]] = [] | ||
self._best_checkpoint: Optional[Tuple[str, float]] = None | ||
|
||
self._train_epoch_fn = None | ||
self._validate_fn = None | ||
|
@@ -224,11 +224,15 @@ def __init__( | |
self._early_stopping_fn = None | ||
self._update_learning_rate_fn = None | ||
|
||
self._log_dir = None | ||
self._checkpoint_save_dir = None | ||
self._tensorboard_writer = None | ||
self._log_dir: Optional[Union[str, pathlib.Path]] = None | ||
self._checkpoint_save_dir: Optional[Union[str, pathlib.Path]] = None | ||
self._tensorboard_writer: Optional[TensorboardWriterType] = None | ||
|
||
def train_epoch(self, model, compression_controller): | ||
def train_epoch( | ||
self, | ||
model: TModel, | ||
compression_controller: CompressionAlgorithmController, | ||
) -> None: | ||
compression_controller.scheduler.epoch_step() | ||
# assuming that epoch number is only used for logging in train_fn: | ||
self.current_loss = self._train_epoch_fn( | ||
|
@@ -241,7 +245,7 @@ def train_epoch(self, model, compression_controller): | |
self.training_epoch_count += 1 | ||
self.cumulative_epoch_count += 1 | ||
|
||
def dump_statistics(self, model, compression_controller): | ||
def dump_statistics(self, model: TModel, compression_controller: CompressionAlgorithmController) -> None: | ||
statistics = compression_controller.statistics() | ||
|
||
if self.verbose: | ||
|
@@ -259,15 +263,19 @@ def dump_statistics(self, model, compression_controller): | |
|
||
self.dump_checkpoint(model, compression_controller) | ||
|
||
def calculate_minimal_tolerable_accuracy(self, uncompressed_model_accuracy: float): | ||
def calculate_minimal_tolerable_accuracy(self, uncompressed_model_accuracy: float) -> None: | ||
if self.maximal_absolute_accuracy_drop is not None: | ||
self.minimal_tolerable_accuracy = uncompressed_model_accuracy - self.maximal_absolute_accuracy_drop | ||
else: | ||
self.minimal_tolerable_accuracy = uncompressed_model_accuracy * ( | ||
1 - 0.01 * self.maximal_relative_accuracy_drop | ||
) | ||
|
||
def dump_checkpoint(self, model, compression_controller): | ||
def dump_checkpoint( | ||
self, | ||
model: TModel, | ||
compression_controller: CompressionAlgorithmController | ||
) -> None: | ||
is_best_checkpoint = ( | ||
self.best_val_metric_value == self.current_val_metric_value | ||
and self.is_model_fully_compressed(compression_controller) | ||
|
@@ -285,19 +293,19 @@ def dump_checkpoint(self, model, compression_controller): | |
if is_best_checkpoint: | ||
self._save_best_checkpoint(model, compression_controller) | ||
|
||
def configure_optimizers(self): | ||
def configure_optimizers(self) -> None: | ||
self.optimizer, self.lr_scheduler = self._configure_optimizers_fn() | ||
|
||
def initialize_training_loop_fns( | ||
self, | ||
train_epoch_fn, | ||
validate_fn, | ||
configure_optimizers_fn, | ||
dump_checkpoint_fn, | ||
load_checkpoint_fn=None, | ||
early_stopping_fn=None, | ||
update_learning_rate_fn=None, | ||
): | ||
train_epoch_fn: Callable[[TModel, CompressionAlgorithmController], None], | ||
validate_fn: Callable[[TModel, Optional[int]], float], | ||
configure_optimizers_fn: Callable[[], Tuple[OptimizerType, LRSchedulerType]], | ||
dump_checkpoint_fn: Callable[[TModel, CompressionAlgorithmController, "TrainingRunner", str], None], | ||
load_checkpoint_fn: Callable[[TModel, str], None] = None, | ||
early_stopping_fn: Callable[[float], bool] = None, | ||
update_learning_rate_fn: Callable[[LRSchedulerType, float, float, float], None] = None, | ||
) -> None: | ||
self._train_epoch_fn = train_epoch_fn | ||
self._validate_fn = validate_fn | ||
self._configure_optimizers_fn = configure_optimizers_fn | ||
|
@@ -306,34 +314,34 @@ def initialize_training_loop_fns( | |
self._early_stopping_fn = early_stopping_fn | ||
self._update_learning_rate_fn = update_learning_rate_fn | ||
|
||
def initialize_logging(self, log_dir=None, tensorboard_writer=None): | ||
self._log_dir = log_dir if log_dir is not None else osp.join(os.getcwd(), "runs") | ||
def initialize_logging(self, log_dir: Optional[Union[str, pathlib.Path]] = None, tensorboard_writer: Optional[TensorboardWriterType] = None) -> None: | ||
self._log_dir = str(log_dir) if log_dir is not None else osp.join(os.getcwd(), "runs") | ||
self._log_dir = configure_accuracy_aware_paths(self._log_dir) | ||
self._checkpoint_save_dir = self._log_dir | ||
self._tensorboard_writer = tensorboard_writer | ||
|
||
def stop_training(self, compression_controller): | ||
def stop_training(self, compression_controller: CompressionAlgorithmController) -> bool: | ||
if self.is_model_fully_compressed(compression_controller) and self._early_stopping_fn is not None: | ||
return self._early_stopping_fn(self.current_val_metric_value) | ||
return False | ||
|
||
def _save_best_checkpoint(self, model, compression_controller): | ||
def _save_best_checkpoint(self, model: TModel, compression_controller: CompressionAlgorithmController) -> None: | ||
best_path = self._make_checkpoint_path(is_best=True) | ||
self._best_checkpoint = (best_path, compression_controller.compression_rate) | ||
self._save_checkpoint(model, compression_controller, best_path) | ||
nncf_logger.info(f"Saved the best model to {best_path}") | ||
|
||
def load_best_checkpoint(self, model): | ||
def load_best_checkpoint(self, model: TModel) -> float: | ||
resuming_checkpoint_path, compression_rate = self._best_checkpoint | ||
nncf_logger.info(f"Loading the best checkpoint found during training: {resuming_checkpoint_path}") | ||
self._load_checkpoint(model, resuming_checkpoint_path) | ||
return compression_rate | ||
|
||
def is_model_fully_compressed(self, compression_controller) -> bool: | ||
def is_model_fully_compressed(self, compression_controller: CompressionAlgorithmController) -> bool: | ||
return compression_controller.compression_stage() == CompressionStage.FULLY_COMPRESSED | ||
|
||
@abstractmethod | ||
def add_tensorboard_scalar(self, key, data, step): | ||
def add_tensorboard_scalar(self, key: str, data: float, step: int) -> None: | ||
""" | ||
Add a scalar to tensorboard | ||
|
||
|
@@ -343,7 +351,7 @@ def add_tensorboard_scalar(self, key, data, step): | |
""" | ||
|
||
@abstractmethod | ||
def add_tensorboard_image(self, key, data, step): | ||
def add_tensorboard_image(self, key: str, data: PIL.Image.Image, step: int) -> None: | ||
""" | ||
Add an image to tensorboard | ||
|
||
|
@@ -375,7 +383,7 @@ def _load_checkpoint(self, model: TModel, checkpoint_path: str) -> None: | |
""" | ||
|
||
@abstractmethod | ||
def _make_checkpoint_path(self, is_best, compression_rate=None): | ||
def _make_checkpoint_path(self, is_best: bool, compression_rate: float = None) -> str: | ||
""" | ||
Make a path to save the checkpoint there | ||
|
||
|
@@ -423,15 +431,15 @@ def __init__( | |
self.maximal_compression_rate = maximal_compression_rate | ||
|
||
self._best_checkpoints = {} | ||
self._compression_rate_target = None | ||
self.adaptive_controller = None | ||
self.was_compression_increased_on_prev_step = None | ||
self._compression_rate_target: Optional[float] = None | ||
self.adaptive_controller: Optional[CompressionAlgorithmController] = None | ||
self.was_compression_increased_on_prev_step: Optional[bool] = None | ||
|
||
def dump_statistics(self, model, compression_controller): | ||
def dump_statistics(self, model: TModel, compression_controller: CompressionAlgorithmController) -> None: | ||
self.update_training_history(self.compression_rate_target, self.current_val_metric_value) | ||
super().dump_statistics(model, compression_controller) | ||
|
||
def _save_best_checkpoint(self, model, compression_controller): | ||
def _save_best_checkpoint(self, model: TModel, compression_controller: CompressionAlgorithmController) -> None: | ||
best_path = self._make_checkpoint_path(is_best=True, compression_rate=self.compression_rate_target) | ||
|
||
accuracy_budget = self.best_val_metric_value - self.minimal_tolerable_accuracy | ||
|
@@ -445,7 +453,7 @@ def _save_best_checkpoint(self, model, compression_controller): | |
self._save_checkpoint(model, compression_controller, best_path) | ||
nncf_logger.info(f"Saved the best model to {best_path}") | ||
|
||
def load_best_checkpoint(self, model): | ||
def load_best_checkpoint(self, model: TModel) -> float: | ||
# load checkpoint with the highest compression rate and positive acc budget | ||
possible_checkpoint_rates = self.get_compression_rates_with_positive_acc_budget() | ||
if len(possible_checkpoint_rates) == 0: | ||
|
@@ -473,16 +481,16 @@ def load_best_checkpoint(self, model): | |
return best_checkpoint_compression_rate | ||
|
||
@property | ||
def compression_rate_target(self): | ||
def compression_rate_target(self) -> float: | ||
if self._compression_rate_target is None: | ||
return self.adaptive_controller.compression_rate | ||
return self._compression_rate_target | ||
|
||
@compression_rate_target.setter | ||
def compression_rate_target(self, value): | ||
def compression_rate_target(self, value: float) -> None: | ||
self._compression_rate_target = value | ||
|
||
def update_training_history(self, compression_rate, metric_value): | ||
def update_training_history(self, compression_rate: float, metric_value: float) -> None: | ||
accuracy_budget = metric_value - self.minimal_tolerable_accuracy | ||
self._compressed_training_history.append((compression_rate, accuracy_budget)) | ||
|
||
|
@@ -500,7 +508,7 @@ def update_training_history(self, compression_rate, metric_value): | |
plt.close(fig) | ||
|
||
@property | ||
def compressed_training_history(self): | ||
def compressed_training_history(self) -> Dict[float, float]: | ||
return dict(self._compressed_training_history) | ||
|
||
def get_compression_rates_with_positive_acc_budget(self) -> List[float]: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't replace the path, you should extend it.