86
86
#
87
87
# .. code:: shell
88
88
#
89
- # !pip install sklearn
90
- # !pip install transformers
89
+ # pip install sklearn
90
+ # pip install transformers
91
+ #
91
92
92
93
93
94
######################################################################
98
99
# Mac:
99
100
#
100
101
# .. code:: shell
101
- # !yes y | pip uninstall torch tochvision
102
- # !yes y | pip install --pre torch -f https://download.pytorch.org/whl/nightly/cu101/torch_nightly.html
102
+ #
103
+ # yes y | pip uninstall torch tochvision
104
+ # yes y | pip install --pre torch -f https://download.pytorch.org/whl/nightly/cu101/torch_nightly.html
105
+ #
103
106
104
107
105
108
######################################################################
155
158
# https://github.com/nyu-mll/GLUE-baselines/blob/master/download_glue_data.py)
156
159
# and unpack it to some directory “glue_data/MRPC”.
157
160
#
158
-
159
- # !python download_glue_data.py --data_dir='glue_data' --tasks='MRPC' --test_labels=True
160
- !p wd
161
- !l s
162
- !w get https : // gist . githubusercontent . com / W4ngatang / 60 c2bdb54d156a41194446737ce03e2e / raw / 17 b8dd0d724281ed7c3b2aeeda662b92809aadd5 / download_glue_data .py
163
- !p ython download_glue_data . py - - data_dir = 'glue_data' - - tasks = ' MRPC'
164
- !l s glue_data / MRPC
161
+ #
162
+ # .. code:: shell
163
+ #
164
+ # wget https://gist.githubusercontent.com/W4ngatang/60c2bdb54d156a41194446737ce03e2e/raw/17b8dd0d724281ed7c3b2aeeda662b92809aadd5/download_glue_data.py
165
+ # python download_glue_data.py --data_dir='glue_data' --tasks='MRPC'
166
+ # ls glue_data/ MRPC
167
+ #
165
168
166
169
167
170
######################################################################
255
258
from google .colab import drive
256
259
drive .mount ('/content/drive' )
257
260
258
- !ls
259
- !pwd
260
-
261
261
262
262
######################################################################
263
263
# Set global configurations
273
273
configs = Namespace ()
274
274
275
275
# The output directory for the fine-tuned model.
276
- # configs.output_dir = "/mnt/homedir/jianyuhuang/public/bert/MRPC/"
277
276
configs .output_dir = "/content/drive/My Drive/BERT_Quant_Tutorial/MRPC/"
278
- # configs.output_dir = "./MRPC/"
279
277
280
278
# The data directory for the MRPC task in the GLUE benchmark.
281
- # configs.data_dir = "/mnt/homedir/jianyuhuang/public/bert/glue_data/MRPC"
282
- # configs.data_dir = "./glue_data/MRPC"
283
279
configs .data_dir = "/content/glue_data/MRPC"
284
280
285
281
# The model name or path for the pre-trained model.
@@ -493,30 +489,6 @@ def load_and_cache_examples(args, task, tokenizer, evaluate=False):
493
489
print (quantized_model )
494
490
495
491
496
-
497
- ######################################################################
498
- # In PyTorch 1.4 release, we further add the per-channel quantization
499
- # support for dynamic quantization.
500
- #
501
- # .. figure:: https://drive.google.com/open?id=1N6P70MR6jJ2tcFnFJ2lROLSFqmiOY--g
502
- # :alt: Per Tensor Quantization for Weight
503
- #
504
- # Per Tensor Quantization for Weight
505
- #
506
- # .. figure:: https://drive.google.com/open?id=1nyjUKP5qtkRCJPKtUaXXwhglLMQQ0Dfs
507
- # :alt: Per Channel Quantization for Weight
508
- #
509
- # Per Channel Quantization for Weight
510
- #
511
-
512
- qconfig_dict = {
513
- torch .nn .Linear : torch .quantization .per_channel_dynamic_qconfig
514
- }
515
- per_channel_quantized_model = torch .quantization .quantize_dynamic (
516
- model , qconfig_dict , dtype = torch .qint8
517
- )
518
-
519
-
520
492
######################################################################
521
493
# Check the model size
522
494
# --------------------
@@ -532,9 +504,6 @@ def print_size_of_model(model):
532
504
533
505
print_size_of_model (model )
534
506
print_size_of_model (quantized_model )
535
- # print_size_of_model(per_channel_quantized_model)
536
-
537
-
538
507
539
508
540
509
######################################################################
@@ -606,10 +575,6 @@ def time_model_evaluation(model, configs, tokenizer):
606
575
# processing the evaluation of MRPC dataset.
607
576
#
608
577
609
- # Evaluate the INT8 BERT model after the per-channel dynamic quantization
610
- time_model_evaluation (per_channel_quantized_model , configs , tokenizer )
611
-
612
-
613
578
614
579
######################################################################
615
580
# Serialize the quantized model
@@ -621,7 +586,7 @@ def time_model_evaluation(model, configs, tokenizer):
621
586
quantized_output_dir = configs .output_dir + "quantized/"
622
587
if not os .path .exists (quantized_output_dir ):
623
588
os .makedirs (quantized_output_dir )
624
- quantized_model .save_pretrained (quantized_output_dir )
589
+ quantized_model .save_pretrained (quantized_output_dir )
625
590
626
591
627
592
######################################################################
0 commit comments