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.

test_gpu.py 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import sys
  2. import pytest
  3. import pickle
  4. import shutil
  5. def test_xgboost():
  6. from flaml import AutoML
  7. from sklearn.datasets import make_moons
  8. import scipy.sparse
  9. import numpy as np
  10. from xgboost.core import XGBoostError
  11. try:
  12. X_train = scipy.sparse.eye(900000)
  13. y_train = np.random.randint(2, size=900000)
  14. automl = AutoML()
  15. automl.fit(
  16. X_train,
  17. y_train,
  18. estimator_list=["xgb_limitdepth", "xgboost"],
  19. time_budget=5,
  20. gpu_per_trial=1,
  21. )
  22. train, label = make_moons(
  23. n_samples=300000, shuffle=True, noise=0.3, random_state=None
  24. )
  25. automl = AutoML()
  26. automl.fit(
  27. train,
  28. label,
  29. estimator_list=["xgb_limitdepth", "xgboost"],
  30. time_budget=5,
  31. gpu_per_trial=1,
  32. )
  33. automl.fit(
  34. train,
  35. label,
  36. estimator_list=["xgb_limitdepth", "xgboost"],
  37. time_budget=5,
  38. )
  39. except XGBoostError:
  40. # No visible GPU is found for XGBoost.
  41. return
  42. @pytest.mark.skipif(sys.platform == "darwin", reason="do not run on mac os")
  43. def _test_hf_data():
  44. from flaml import AutoML
  45. import requests
  46. from datasets import load_dataset
  47. try:
  48. train_dataset = load_dataset("glue", "mrpc", split="train[:1%]").to_pandas()
  49. dev_dataset = load_dataset("glue", "mrpc", split="validation[:1%]").to_pandas()
  50. test_dataset = load_dataset("glue", "mrpc", split="test[:1%]").to_pandas()
  51. except requests.exceptions.ConnectionError:
  52. return
  53. custom_sent_keys = ["sentence1", "sentence2"]
  54. label_key = "label"
  55. X_train = train_dataset[custom_sent_keys]
  56. y_train = train_dataset[label_key]
  57. X_val = dev_dataset[custom_sent_keys]
  58. y_val = dev_dataset[label_key]
  59. X_test = test_dataset[custom_sent_keys]
  60. automl = AutoML()
  61. automl_settings = {
  62. "gpu_per_trial": 1,
  63. "max_iter": 2,
  64. "time_budget": 5000,
  65. "task": "seq-classification",
  66. "metric": "accuracy",
  67. "log_file_name": "seqclass.log",
  68. "use_ray": True,
  69. }
  70. automl_settings["fit_kwargs_by_estimator"] = {
  71. "transformer": {
  72. "model_path": "facebook/muppet-roberta-base",
  73. "output_dir": "test/data/output/",
  74. "ckpt_per_epoch": 5,
  75. "fp16": True,
  76. }
  77. }
  78. automl.fit(
  79. X_train=X_train, y_train=y_train, X_val=X_val, y_val=y_val, **automl_settings
  80. )
  81. automl = AutoML()
  82. automl.retrain_from_log(
  83. X_train=X_train,
  84. y_train=y_train,
  85. train_full=True,
  86. record_id=0,
  87. **automl_settings
  88. )
  89. with open("automl.pkl", "wb") as f:
  90. pickle.dump(automl, f, pickle.HIGHEST_PROTOCOL)
  91. with open("automl.pkl", "rb") as f:
  92. automl = pickle.load(f)
  93. shutil.rmtree("test/data/output/")
  94. automl.predict(X_test)
  95. automl.predict(["test test", "test test"])
  96. automl.predict(
  97. [
  98. ["test test", "test test"],
  99. ["test test", "test test"],
  100. ["test test", "test test"],
  101. ]
  102. )
  103. automl.predict_proba(X_test)
  104. print(automl.classes_)
  105. if __name__ == "__main__":
  106. _test_hf_data()