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.2 kB

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