You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tune-HuggingFace.md 7.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # Tune - HuggingFace
  2. This example uses flaml to finetune a transformer model from Huggingface transformers library.
  3. *Note*: `flaml.AutoML` has built-in support for certain finetuning tasks with a
  4. [higher-level API](AutoML-NLP).
  5. It may be easier to use that API unless you have special requirements not handled by that API.
  6. ### Requirements
  7. This example requires GPU. Install dependencies:
  8. ```python
  9. pip install torch transformers datasets "flaml[blendsearch,ray]"
  10. ```
  11. ### Prepare for tuning
  12. #### Tokenizer
  13. ```python
  14. from transformers import AutoTokenizer
  15. MODEL_NAME = "distilbert-base-uncased"
  16. tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=True)
  17. COLUMN_NAME = "sentence"
  18. def tokenize(examples):
  19. return tokenizer(examples[COLUMN_NAME], truncation=True)
  20. ```
  21. #### Define training method
  22. ```python
  23. import flaml
  24. import datasets
  25. from transformers import AutoModelForSequenceClassification
  26. TASK = "cola"
  27. NUM_LABELS = 2
  28. def train_distilbert(config: dict):
  29. # Load CoLA dataset and apply tokenizer
  30. cola_raw = datasets.load_dataset("glue", TASK)
  31. cola_encoded = cola_raw.map(tokenize, batched=True)
  32. train_dataset, eval_dataset = cola_encoded["train"], cola_encoded["validation"]
  33. model = AutoModelForSequenceClassification.from_pretrained(
  34. MODEL_NAME, num_labels=NUM_LABELS
  35. )
  36. metric = datasets.load_metric("glue", TASK)
  37. def compute_metrics(eval_pred):
  38. predictions, labels = eval_pred
  39. predictions = np.argmax(predictions, axis=1)
  40. return metric.compute(predictions=predictions, references=labels)
  41. training_args = TrainingArguments(
  42. output_dir='.',
  43. do_eval=False,
  44. disable_tqdm=True,
  45. logging_steps=20000,
  46. save_total_limit=0,
  47. **config,
  48. )
  49. trainer = Trainer(
  50. model,
  51. training_args,
  52. train_dataset=train_dataset,
  53. eval_dataset=eval_dataset,
  54. tokenizer=tokenizer,
  55. compute_metrics=compute_metrics,
  56. )
  57. # train model
  58. trainer.train()
  59. # evaluate model
  60. eval_output = trainer.evaluate()
  61. # report the metric to optimize & the metric to log
  62. flaml.tune.report(
  63. loss=eval_output["eval_loss"],
  64. matthews_correlation=eval_output["eval_matthews_correlation"],
  65. )
  66. ```
  67. ### Define the search
  68. We are now ready to define our search. This includes:
  69. - The `search_space` for our hyperparameters
  70. - The `metric` and the `mode` ('max' or 'min') for optimization
  71. - The constraints (`n_cpus`, `n_gpus`, `num_samples`, and `time_budget_s`)
  72. ```python
  73. max_num_epoch = 64
  74. search_space = {
  75. # You can mix constants with search space objects.
  76. "num_train_epochs": flaml.tune.loguniform(1, max_num_epoch),
  77. "learning_rate": flaml.tune.loguniform(1e-6, 1e-4),
  78. "adam_epsilon": flaml.tune.loguniform(1e-9, 1e-7),
  79. "adam_beta1": flaml.tune.uniform(0.8, 0.99),
  80. "adam_beta2": flaml.tune.loguniform(98e-2, 9999e-4),
  81. }
  82. # optimization objective
  83. HP_METRIC, MODE = "matthews_correlation", "max"
  84. # resources
  85. num_cpus = 4
  86. num_gpus = 4 # change according to your GPU resources
  87. # constraints
  88. num_samples = -1 # number of trials, -1 means unlimited
  89. time_budget_s = 3600 # time budget in seconds
  90. ```
  91. ### Launch the tuning
  92. We are now ready to launch the tuning using `flaml.tune.run`:
  93. ```python
  94. import ray
  95. ray.init(num_cpus=num_cpus, num_gpus=num_gpus)
  96. print("Tuning started...")
  97. analysis = flaml.tune.run(
  98. train_distilbert,
  99. search_alg=flaml.CFO(
  100. space=search_space,
  101. metric=HP_METRIC,
  102. mode=MODE,
  103. low_cost_partial_config={"num_train_epochs": 1}),
  104. resources_per_trial={"gpu": num_gpus, "cpu": num_cpus},
  105. local_dir='logs/',
  106. num_samples=num_samples,
  107. time_budget_s=time_budget_s,
  108. use_ray=True,
  109. )
  110. ```
  111. This will run tuning for one hour. At the end we will see a summary.
  112. ```
  113. == Status ==
  114. Memory usage on this node: 32.0/251.6 GiB
  115. Using FIFO scheduling algorithm.
  116. Resources requested: 0/4 CPUs, 0/4 GPUs, 0.0/150.39 GiB heap, 0.0/47.22 GiB objects (0/1.0 accelerator_type:V100)
  117. Result logdir: /home/chiw/FLAML/notebook/logs/train_distilbert_2021-05-07_02-35-58
  118. Number of trials: 22/infinite (22 TERMINATED)
  119. Trial name status loc adam_beta1 adam_beta2 adam_epsilon learning_rate num_train_epochs iter total time (s) loss matthews_correlation
  120. train_distilbert_a0c303d0 TERMINATED 0.939079 0.991865 7.96945e-08 5.61152e-06 1 1 55.6909 0.587986 0
  121. train_distilbert_a0c303d1 TERMINATED 0.811036 0.997214 2.05111e-09 2.05134e-06 1.44427 1 71.7663 0.603018 0
  122. train_distilbert_c39b2ef0 TERMINATED 0.909395 0.993715 1e-07 5.26543e-06 1 1 53.7619 0.586518 0
  123. train_distilbert_f00776e2 TERMINATED 0.968763 0.990019 4.38943e-08 5.98035e-06 1.02723 1 56.8382 0.581313 0
  124. train_distilbert_11ab3900 TERMINATED 0.962198 0.991838 7.09296e-08 5.06608e-06 1 1 54.0231 0.585576 0
  125. train_distilbert_353025b6 TERMINATED 0.91596 0.991892 8.95426e-08 6.21568e-06 2.15443 1 98.3233 0.531632 0.388893
  126. train_distilbert_5728a1de TERMINATED 0.926933 0.993146 1e-07 1.00902e-05 1 1 55.3726 0.538505 0.280558
  127. train_distilbert_9394c2e2 TERMINATED 0.928106 0.990614 4.49975e-08 3.45674e-06 2.72935 1 121.388 0.539177 0.327295
  128. train_distilbert_b6543fec TERMINATED 0.876896 0.992098 1e-07 7.01176e-06 1.59538 1 76.0244 0.527516 0.379177
  129. train_distilbert_0071f998 TERMINATED 0.955024 0.991687 7.39776e-08 5.50998e-06 2.90939 1 126.871 0.516225 0.417157
  130. train_distilbert_2f830be6 TERMINATED 0.886931 0.989628 7.6127e-08 4.37646e-06 1.53338 1 73.8934 0.551629 0.0655887
  131. train_distilbert_7ce03f12 TERMINATED 0.984053 0.993956 8.70144e-08 7.82557e-06 4.08775 1 174.027 0.523732 0.453549
  132. train_distilbert_aaab0508 TERMINATED 0.940707 0.993946 1e-07 8.91979e-06 3.40243 1 146.249 0.511288 0.45085
  133. train_distilbert_14262454 TERMINATED 0.99 0.991696 4.60093e-08 4.83405e-06 3.4954 1 152.008 0.53506 0.400851
  134. train_distilbert_6d211fe6 TERMINATED 0.959277 0.994556 5.40791e-08 1.17333e-05 6.64995 1 271.444 0.609851 0.526802
  135. train_distilbert_c980bae4 TERMINATED 0.99 0.993355 1e-07 5.21929e-06 2.51275 1 111.799 0.542276 0.324968
  136. train_distilbert_6d0d29d6 TERMINATED 0.965773 0.995182 9.9752e-08 1.15549e-05 13.694 1 527.944 0.923802 0.549474
  137. train_distilbert_b16ea82a TERMINATED 0.952781 0.993931 2.93182e-08 1.19145e-05 3.2293 1 139.844 0.533466 0.451307
  138. train_distilbert_eddf7cc0 TERMINATED 0.99 0.997109 8.13498e-08 1.28515e-05 15.5807 1 614.789 0.983285 0.56993
  139. train_distilbert_43008974 TERMINATED 0.929089 0.993258 1e-07 1.03892e-05 12.0357 1 474.387 0.857461 0.520022
  140. train_distilbert_b3408a4e TERMINATED 0.99 0.993809 4.67441e-08 1.10418e-05 11.9165 1 474.126 0.828205 0.526164
  141. train_distilbert_cfbfb220 TERMINATED 0.979454 0.9999 1e-07 1.49578e-05 20.3715
  142. ```
  143. ### Retrieve the results
  144. ```python
  145. best_trial = analysis.get_best_trial(HP_METRIC, MODE, "all")
  146. metric = best_trial.metric_analysis[HP_METRIC][MODE]
  147. print(f"n_trials={len(analysis.trials)}")
  148. print(f"time={time.time()-start_time}")
  149. print(f"Best model eval {HP_METRIC}: {metric:.4f}")
  150. print(f"Best model parameters: {best_trial.config}")
  151. # n_trials=22
  152. # time=3999.769361972809
  153. # Best model eval matthews_correlation: 0.5699
  154. # Best model parameters: {'num_train_epochs': 15.580684188655825, 'learning_rate': 1.2851507818900338e-05, 'adam_epsilon': 8.134982521948352e-08, 'adam_beta1': 0.99, 'adam_beta2': 0.9971094424784387}
  155. ```
  156. [Link to notebook](https://github.com/microsoft/FLAML/blob/main/notebook/tune_huggingface.ipynb) | [Open in colab](https://colab.research.google.com/github/microsoft/FLAML/blob/main/notebook/tune_huggingface.ipynb)