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.

Integrate - AzureML.md 6.1 kB

Refactor into automl subpackage (#809) * Refactor into automl subpackage Moved some of the packages into an automl subpackage to tidy before the task-based refactor. This is in response to discussions with the group and a comment on the first task-based PR. Only changes here are moving subpackages and modules into the new automl, fixing imports to work with this structure and fixing some dependencies in setup.py. * Fix doc building post automl subpackage refactor * Fix broken links in website post automl subpackage refactor * Fix broken links in website post automl subpackage refactor * Remove vw from test deps as this is breaking the build * Move default back to the top-level I'd moved this to automl as that's where it's used internally, but had missed that this is actually part of the public interface so makes sense to live where it was. * Re-add top level modules with deprecation warnings flaml.data, flaml.ml and flaml.model are re-added to the top level, being re-exported from flaml.automl for backwards compatability. Adding a deprecation warning so that we can have a planned removal later. * Fix model.py line-endings * Pin pytorch-lightning to less than 1.8.0 We're seeing strange lightning related bugs from pytorch-forecasting since the release of lightning 1.8.0. Going to try constraining this to see if we have a fix. * Fix the lightning version pin Was optimistic with setting it in the 1.7.x range, but that isn't compatible with python 3.6 * Remove lightning version pin * Revert dependency version changes * Minor change to retrigger the build * Fix line endings in ml.py and model.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: EgorKraevTransferwise <egor.kraev@transferwise.com>
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. FLAML can be used together with AzureML. On top of that, using mlflow and ray is easy too.
  2. ### Prerequisites
  3. Install the [automl,azureml] option.
  4. ```bash
  5. pip install "flaml[automl,azureml]"
  6. ```
  7. Setup a AzureML workspace:
  8. ```python
  9. from azureml.core import Workspace
  10. ws = Workspace.create(name='myworkspace', subscription_id='<azure-subscription-id>', resource_group='myresourcegroup')
  11. ```
  12. ### Enable mlflow in AzureML workspace
  13. ```python
  14. import mlflow
  15. from azureml.core import Workspace
  16. ws = Workspace.from_config()
  17. mlflow.set_tracking_uri(ws.get_mlflow_tracking_uri())
  18. ```
  19. ### Start an AutoML run
  20. ```python
  21. from flaml.automl.data import load_openml_dataset
  22. from flaml import AutoML
  23. # Download [Airlines dataset](https://www.openml.org/d/1169) from OpenML. The task is to predict whether a given flight will be delayed, given the information of the scheduled departure.
  24. X_train, X_test, y_train, y_test = load_openml_dataset(dataset_id=1169, data_dir="./")
  25. automl = AutoML()
  26. settings = {
  27. "time_budget": 60, # total running time in seconds
  28. "metric": "accuracy", # metric to optimize
  29. "task": "classification", # task type
  30. "log_file_name": "airlines_experiment.log", # flaml log file
  31. }
  32. experiment = mlflow.set_experiment("flaml") # the experiment name in AzureML workspace
  33. with mlflow.start_run() as run: # create a mlflow run
  34. automl.fit(X_train=X_train, y_train=y_train, **settings)
  35. mlflow.sklearn.log_model(automl, "automl")
  36. ```
  37. The metrics in the run will be automatically logged in an experiment named "flaml" in your AzureML workspace. They can be retrieved by `mlflow.search_runs`:
  38. ```python
  39. mlflow.search_runs(experiment_ids=[experiment.experiment_id], filter_string="params.learner = 'xgboost'")
  40. ```
  41. The logged model can be loaded and used to make predictions:
  42. ```python
  43. automl = mlflow.sklearn.load_model(f"{run.info.artifact_uri}/automl")
  44. print(automl.predict(X_test))
  45. ```
  46. [Link to notebook](https://github.com/microsoft/FLAML/blob/main/notebook/integrate_azureml.ipynb) | [Open in colab](https://colab.research.google.com/github/microsoft/FLAML/blob/main/notebook/integrate_azureml.ipynb)
  47. ### Use ray to distribute across a cluster
  48. When you have a compute cluster in AzureML, you can distribute `flaml.AutoML` or `flaml.tune` with ray.
  49. #### Build a ray environment in AzureML
  50. Create a docker file such as [.Docker/Dockerfile-cpu](https://github.com/microsoft/FLAML/blob/main/test/.Docker/Dockerfile-cpu). Make sure `RUN pip install flaml[blendsearch,ray]` is included in the docker file.
  51. Then build a AzureML environment in the workspace `ws`.
  52. ```python
  53. ray_environment_name = "aml-ray-cpu"
  54. ray_environment_dockerfile_path = "./Docker/Dockerfile-cpu"
  55. # Build CPU image for Ray
  56. ray_cpu_env = Environment.from_dockerfile(name=ray_environment_name, dockerfile=ray_environment_dockerfile_path)
  57. ray_cpu_env.register(workspace=ws)
  58. ray_cpu_build_details = ray_cpu_env.build(workspace=ws)
  59. import time
  60. while ray_cpu_build_details.status not in ["Succeeded", "Failed"]:
  61. print(f"Awaiting completion of ray CPU environment build. Current status is: {ray_cpu_build_details.status}")
  62. time.sleep(10)
  63. ```
  64. You only need to do this step once for one workspace.
  65. #### Create a compute cluster with multiple nodes
  66. ```python
  67. from azureml.core.compute import AmlCompute, ComputeTarget
  68. compute_target_name = "cpucluster"
  69. node_count = 2
  70. # This example uses CPU VM. For using GPU VM, set SKU to STANDARD_NC6
  71. compute_target_size = "STANDARD_D2_V2"
  72. if compute_target_name in ws.compute_targets:
  73. compute_target = ws.compute_targets[compute_target_name]
  74. if compute_target and type(compute_target) is AmlCompute:
  75. if compute_target.provisioning_state == "Succeeded":
  76. print("Found compute target; using it:", compute_target_name)
  77. else:
  78. raise Exception(
  79. "Found compute target but it is in state", compute_target.provisioning_state)
  80. else:
  81. print("creating a new compute target...")
  82. provisioning_config = AmlCompute.provisioning_configuration(
  83. vm_size=compute_target_size,
  84. min_nodes=0,
  85. max_nodes=node_count)
  86. # Create the cluster
  87. compute_target = ComputeTarget.create(ws, compute_target_name, provisioning_config)
  88. # Can poll for a minimum number of nodes and for a specific timeout.
  89. # If no min node count is provided it will use the scale settings for the cluster
  90. compute_target.wait_for_completion(show_output=True, min_node_count=None, timeout_in_minutes=20)
  91. # For a more detailed view of current AmlCompute status, use get_status()
  92. print(compute_target.get_status().serialize())
  93. ```
  94. If the computer target "cpucluster" already exists, it will not be recreated.
  95. #### Run distributed AutoML job
  96. Assuming you have an automl script like [ray/distribute_automl.py](https://github.com/microsoft/FLAML/blob/main/test/ray/distribute_automl.py). It uses `n_concurrent_trials=k` to inform `AutoML.fit()` to perform k concurrent trials in parallel.
  97. Submit an AzureML job as the following:
  98. ```python
  99. from azureml.core import Workspace, Experiment, ScriptRunConfig, Environment
  100. from azureml.core.runconfig import RunConfiguration, DockerConfiguration
  101. command = ["python distribute_automl.py"]
  102. ray_environment_name = "aml-ray-cpu"
  103. env = Environment.get(workspace=ws, name=ray_environment_name)
  104. aml_run_config = RunConfiguration(communicator="OpenMpi")
  105. aml_run_config.target = compute_target
  106. aml_run_config.docker = DockerConfiguration(use_docker=True)
  107. aml_run_config.environment = env
  108. aml_run_config.node_count = 2
  109. config = ScriptRunConfig(
  110. source_directory="ray/",
  111. command=command,
  112. run_config=aml_run_config,
  113. )
  114. exp = Experiment(ws, "distribute-automl")
  115. run = exp.submit(config)
  116. print(run.get_portal_url()) # link to ml.azure.com
  117. run.wait_for_completion(show_output=True)
  118. ```
  119. #### Run distributed tune job
  120. Prepare a script like [ray/distribute_tune.py](https://github.com/microsoft/FLAML/blob/main/test/ray/distribute_tune.py). Replace the command in the above eample with:
  121. ```python
  122. command = ["python distribute_tune.py"]
  123. ```
  124. Everything else is the same.