From 5c8e5b3709d03faa8121181e0648dda4508ed191 Mon Sep 17 00:00:00 2001 From: Victor SANH Date: Thu, 28 May 2020 00:26:39 -0400 Subject: [PATCH] commplying with isort --- examples/movement-pruning/bertarize.py | 10 +++++----- examples/movement-pruning/counts_parameters.py | 4 ++-- examples/movement-pruning/emmental/__init__.py | 8 +++----- .../emmental/configuration_bert_masked.py | 3 ++- .../movement-pruning/emmental/modeling_bert_masked.py | 11 +++++++---- .../movement-pruning/emmental/modules/__init__.py | 2 +- .../movement-pruning/emmental/modules/masked_nn.py | 6 +++--- examples/movement-pruning/masked_run_glue.py | 6 +++--- examples/movement-pruning/masked_run_squad.py | 7 +++---- 9 files changed, 29 insertions(+), 28 deletions(-) diff --git a/examples/movement-pruning/bertarize.py b/examples/movement-pruning/bertarize.py index 1fa99dcd95..7b6b5b834b 100644 --- a/examples/movement-pruning/bertarize.py +++ b/examples/movement-pruning/bertarize.py @@ -17,13 +17,13 @@ For instance, once the a model from the :class:`~emmental.MaskedBertForSequenceC as a standard :class:`~transformers.BertForSequenceClassification`. """ +import argparse import os import shutil -import argparse import torch -from emmental.modules import MagnitudeBinarizer, TopKBinarizer, ThresholdBinarizer +from emmental.modules import MagnitudeBinarizer, ThresholdBinarizer, TopKBinarizer def main(args): @@ -40,13 +40,13 @@ def main(args): for name, tensor in model.items(): if "embeddings" in name or "LayerNorm" in name or "pooler" in name: pruned_model[name] = tensor - print(f"Pruned layer {name}") + print(f"Copied layer {name}") elif "classifier" in name or "qa_output" in name: pruned_model[name] = tensor - print(f"Pruned layer {name}") + print(f"Copied layer {name}") elif "bias" in name: pruned_model[name] = tensor - print(f"Pruned layer {name}") + print(f"Copied layer {name}") else: if pruning_method == "magnitude": mask = MagnitudeBinarizer.apply(inputs=tensor, threshold=threshold) diff --git a/examples/movement-pruning/counts_parameters.py b/examples/movement-pruning/counts_parameters.py index 250850f352..183c125c4a 100644 --- a/examples/movement-pruning/counts_parameters.py +++ b/examples/movement-pruning/counts_parameters.py @@ -15,12 +15,12 @@ Count remaining (non-zero) weights in the encoder (i.e. the transformer layers). Sparsity and remaining weights levels are equivalent: sparsity % = 100 - remaining weights %. """ -import os import argparse +import os import torch -from emmental.modules import TopKBinarizer, ThresholdBinarizer +from emmental.modules import ThresholdBinarizer, TopKBinarizer def main(args): diff --git a/examples/movement-pruning/emmental/__init__.py b/examples/movement-pruning/emmental/__init__.py index ee0f1a1334..6646667ea8 100644 --- a/examples/movement-pruning/emmental/__init__.py +++ b/examples/movement-pruning/emmental/__init__.py @@ -1,11 +1,9 @@ -from .modules import * - from .configuration_bert_masked import MaskedBertConfig - from .modeling_bert_masked import ( - MaskedBertModel, + MaskedBertForMultipleChoice, MaskedBertForQuestionAnswering, MaskedBertForSequenceClassification, MaskedBertForTokenClassification, - MaskedBertForMultipleChoice, + MaskedBertModel, ) +from .modules import * diff --git a/examples/movement-pruning/emmental/configuration_bert_masked.py b/examples/movement-pruning/emmental/configuration_bert_masked.py index bc17d4e05e..2af4ea683a 100644 --- a/examples/movement-pruning/emmental/configuration_bert_masked.py +++ b/examples/movement-pruning/emmental/configuration_bert_masked.py @@ -19,8 +19,9 @@ and adapts it to the specificities of MaskedBert (`pruning_method`, `mask_init` import logging -from transformers.configuration_utils import PretrainedConfig from transformers.configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP +from transformers.configuration_utils import PretrainedConfig + logger = logging.getLogger(__name__) diff --git a/examples/movement-pruning/emmental/modeling_bert_masked.py b/examples/movement-pruning/emmental/modeling_bert_masked.py index 4915265290..83a8f7c630 100644 --- a/examples/movement-pruning/emmental/modeling_bert_masked.py +++ b/examples/movement-pruning/emmental/modeling_bert_masked.py @@ -26,13 +26,16 @@ import torch from torch import nn from torch.nn import CrossEntropyLoss, MSELoss +from emmental import MaskedBertConfig, MaskedLinear from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_callable +from transformers.modeling_bert import ( + ACT2FN, + BERT_PRETRAINED_MODEL_ARCHIVE_MAP, + BertLayerNorm, + load_tf_weights_in_bert, +) from transformers.modeling_utils import PreTrainedModel, prune_linear_layer -from transformers.modeling_bert import BERT_PRETRAINED_MODEL_ARCHIVE_MAP -from transformers.modeling_bert import load_tf_weights_in_bert, ACT2FN, BertLayerNorm -from emmental import MaskedLinear -from emmental import MaskedBertConfig logger = logging.getLogger(__name__) diff --git a/examples/movement-pruning/emmental/modules/__init__.py b/examples/movement-pruning/emmental/modules/__init__.py index 0eb6d7bbdf..761a6343d6 100644 --- a/examples/movement-pruning/emmental/modules/__init__.py +++ b/examples/movement-pruning/emmental/modules/__init__.py @@ -1,2 +1,2 @@ -from .binarizer import ThresholdBinarizer, TopKBinarizer, MagnitudeBinarizer +from .binarizer import MagnitudeBinarizer, ThresholdBinarizer, TopKBinarizer from .masked_nn import MaskedLinear diff --git a/examples/movement-pruning/emmental/modules/masked_nn.py b/examples/movement-pruning/emmental/modules/masked_nn.py index 2caf76b389..298c7e5e51 100644 --- a/examples/movement-pruning/emmental/modules/masked_nn.py +++ b/examples/movement-pruning/emmental/modules/masked_nn.py @@ -19,14 +19,14 @@ the weight matrix to prune a portion of the weights. The pruned weight matrix is then multiplied against the inputs (and if necessary, the bias is added). """ +import math + import torch from torch import nn from torch.nn import functional as F from torch.nn import init -import math - -from .binarizer import ThresholdBinarizer, TopKBinarizer, MagnitudeBinarizer +from .binarizer import MagnitudeBinarizer, ThresholdBinarizer, TopKBinarizer class MaskedLinear(nn.Linear): diff --git a/examples/movement-pruning/masked_run_glue.py b/examples/movement-pruning/masked_run_glue.py index c39bb17701..331bb87c3f 100644 --- a/examples/movement-pruning/masked_run_glue.py +++ b/examples/movement-pruning/masked_run_glue.py @@ -24,12 +24,13 @@ import random import numpy as np import torch -from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset -from torch.utils.data.distributed import DistributedSampler import torch.nn as nn import torch.nn.functional as F +from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset +from torch.utils.data.distributed import DistributedSampler from tqdm import tqdm, trange +from emmental import MaskedBertConfig, MaskedBertForSequenceClassification from transformers import ( WEIGHTS_NAME, AdamW, @@ -43,7 +44,6 @@ from transformers import glue_convert_examples_to_features as convert_examples_t from transformers import glue_output_modes as output_modes from transformers import glue_processors as processors -from emmental import MaskedBertConfig, MaskedBertForSequenceClassification try: from torch.utils.tensorboard import SummaryWriter diff --git a/examples/movement-pruning/masked_run_squad.py b/examples/movement-pruning/masked_run_squad.py index 25bb1675e0..1df626e1dd 100644 --- a/examples/movement-pruning/masked_run_squad.py +++ b/examples/movement-pruning/masked_run_squad.py @@ -25,12 +25,13 @@ import timeit import numpy as np import torch -from torch.utils.data import DataLoader, RandomSampler, SequentialSampler -from torch.utils.data.distributed import DistributedSampler import torch.nn as nn import torch.nn.functional as F +from torch.utils.data import DataLoader, RandomSampler, SequentialSampler +from torch.utils.data.distributed import DistributedSampler from tqdm import tqdm, trange +from emmental import MaskedBertConfig, MaskedBertForQuestionAnswering from transformers import ( WEIGHTS_NAME, AdamW, @@ -48,8 +49,6 @@ from transformers.data.metrics.squad_metrics import ( from transformers.data.processors.squad import SquadResult, SquadV1Processor, SquadV2Processor -from emmental import MaskedBertConfig, MaskedBertForQuestionAnswering - try: from torch.utils.tensorboard import SummaryWriter except ImportError: