Skip to content

Commit a549eb5

Browse files
authored
Recipe/running on aws (#86)
* Working example for running on AWS * Fix & refactor final AWS example * Do actually stop the cluster * Running on AWS: docs 📚 * Running on AWS zip
1 parent 59306b6 commit a549eb5

File tree

10 files changed

+224
-0
lines changed

10 files changed

+224
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import hydra
2+
from sklearn.base import BaseEstimator
3+
from sklearn.feature_selection import f_classif, mutual_info_classif
4+
5+
from fseval.config import PipelineConfig
6+
from fseval.main import run_pipeline
7+
8+
9+
class ANOVAFValueClassifier(BaseEstimator):
10+
def fit(self, X, y):
11+
scores, _ = f_classif(X, y)
12+
self.feature_importances_ = scores
13+
14+
15+
class MutualInfoClassifier(BaseEstimator):
16+
def fit(self, X, y):
17+
scores = mutual_info_classif(X, y)
18+
self.feature_importances_ = scores
19+
20+
21+
@hydra.main(config_path="conf", config_name="my_config", version_base="1.1")
22+
def main(cfg: PipelineConfig) -> None:
23+
run_pipeline(cfg)
24+
25+
26+
if __name__ == "__main__":
27+
main()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: My synthetic dataset
2+
task: classification
3+
adapter:
4+
_target_: sklearn.datasets.make_classification
5+
n_samples: 10000
6+
n_informative: 2
7+
n_classes: 2
8+
n_features: 20
9+
n_redundant: 0
10+
random_state: 0
11+
shuffle: false
12+
feature_importances:
13+
X[:, 0:2]: 1.0
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
defaults:
2+
- ray_aws
3+
4+
env_setup:
5+
pip_packages:
6+
fseval: 3.0.3
7+
8+
ray:
9+
cluster:
10+
# Mount our code to the execution directory on both the head- and worker nodes.
11+
# See: https://docs.ray.io/en/master/cluster/vms/references/ray-cluster-configuration.html#cluster-configuration-file-mounts
12+
file_mounts:
13+
/home/ubuntu: benchmark.py
14+
15+
initialization_commands:
16+
- mkdir -p /home/ubuntu/results
17+
18+
sync_down:
19+
source_dir: /home/ubuntu/results
20+
target_dir: .
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defaults:
2+
- base_pipeline_config
3+
- _self_
4+
- override dataset: synthetic
5+
- override validator: knn
6+
- override /callbacks:
7+
- to_sql
8+
- override hydra/launcher: custom_ray_aws
9+
10+
n_bootstraps: 1
11+
callbacks:
12+
to_sql:
13+
url: sqlite:////home/ubuntu/results/results.sqlite # any well-defined database URL
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: ANOVA F-value
2+
estimator:
3+
_target_: benchmark.ANOVAFValueClassifier
4+
_estimator_type: classifier
5+
estimates_feature_importances: true
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: Mutual Info
2+
estimator:
3+
_target_: benchmark.MutualInfoClassifier
4+
_estimator_type: classifier
5+
multioutput: false
6+
estimates_feature_importances: true
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: k-NN
2+
estimator:
3+
_target_: sklearn.neighbors.KNeighborsClassifier
4+
_estimator_type: classifier
5+
multioutput: false
6+
estimates_target: true
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Running on AWS
2+
3+
4+
Let's see how easy it is to run `fseval` jobs on AWS. We are going to do this using the [Ray launcher](https://hydra.cc/docs/1.2/plugins/ray_launcher/) from Hydra. We are going to take the quick start example and run it on an AWS cluster. Specifically, EC2.
5+
6+
## Prerequisites
7+
8+
We are going to use more or less the same configuration as we did in the [Quick start](../../quick-start) example. Again, start by downloading the example project: [running-on-aws-using-ray.zip](pathname:///fseval/zipped-examples/running-on-aws-using-ray.zip)
9+
10+
### Installing the required packages
11+
12+
Now, let's install the required packages:
13+
14+
```
15+
pip install hydra-ray-launcher --upgrade
16+
pip install ray[default]==1.13.0
17+
```
18+
19+
:::note
20+
21+
We require Ray version 1.13 and up, because it contains a fix regarding `protobuf` that is necessary for our setup.
22+
23+
:::
24+
25+
### Authenticating to AWS
26+
Make sure you are authenticated to AWS. Ray uses either the [environment variables](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvar) or your AWS profile stored in `~/.aws` (run `aws configure` to install a profile) to read your authentication details. Make sure you have the [AWS V2](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) installed.
27+
28+
You can test your authentication as follows:
29+
30+
```shell
31+
aws sts get-caller-identity
32+
```
33+
34+
✓ which should give you some output.
35+
36+
✕ if this does not yet work, see AWS's elorate guide on authenticating the CLI for more info: [AWS CLI Configuration basics](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html).
37+
38+
## Experiment setup
39+
In the experiment, we configured the main config file like so:
40+
41+
42+
```yaml title="conf/my_config.yaml"
43+
defaults:
44+
- base_pipeline_config
45+
- _self_
46+
- override dataset: synthetic
47+
- override validator: knn
48+
- override /callbacks:
49+
- to_sql
50+
// highlight-start
51+
- override hydra/launcher: custom_ray_aws
52+
// highlight-end
53+
54+
n_bootstraps: 1
55+
callbacks:
56+
to_sql:
57+
url: sqlite:////home/ubuntu/results/results.sqlite # any well-defined database URL
58+
59+
```
60+
61+
Here, we are configuring to use a new launcher called `custom_ray_aws`.
62+
63+
```yaml title="conf/hydra/launcher/custom_ray_aws.yaml"
64+
defaults:
65+
- ray_aws
66+
67+
env_setup:
68+
pip_packages:
69+
fseval: 3.0.3
70+
71+
ray:
72+
cluster:
73+
# Mount our code to the execution directory on both the head- and worker nodes.
74+
# See: https://docs.ray.io/en/master/cluster/vms/references/ray-cluster-configuration.html#cluster-configuration-file-mounts
75+
file_mounts:
76+
/home/ubuntu: benchmark.py
77+
78+
initialization_commands:
79+
- mkdir -p /home/ubuntu/results
80+
81+
sync_down:
82+
source_dir: /home/ubuntu/results
83+
target_dir: .
84+
```
85+
86+
In this launcher config, a lot of stuff is happening. In short:
87+
88+
1. `fseval` is installed on the EC2 cluster node
89+
2. Once a node has been started, `benchmark.py` is mounted into the home folder. Ray by default runs all experiments from this folder. This is such that we can correctly instantiate any classes that were defined in the config as a `_target_`.
90+
91+
:::note
92+
If you would like to explore with a terminal inside a node, try setting `stop_cluster: false` in `custom_ray_config.yaml`, run an experiment, and then [connect to your EC2 instance with SSH](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html).
93+
:::
94+
95+
3. An initialization command is run to make sure the `/home/ubuntu/results` directory exists. We configured our SQLite table to be stored inside this folder.
96+
4. Finally, once the experiment has been finished, download everything inside of `/home/ubuntu/results` to the current working directory (`.`).
97+
98+
## Running the experiment
99+
100+
101+
```
102+
python benchmark.py --multirun ranker='glob(*)'
103+
```
104+
105+
106+
Now, the experiment should start running, on AWS! Ray automatically instantiates and configures nodes on EC2, ships your code, installs `fseval`, and runs the experiments. Cool!
107+
108+
![feature selectors algorithm stability](/img/recipes/aws/running-on-aws.gif)
109+
110+
🙌🏻
111+
112+
The results are downloaded back to your local computer, and are available in the `results` folder:
113+
114+
```
115+
(base) ➜ running-on-aws-using-ray git:(recipe/running-on-aws) ✗ tree results
116+
results
117+
└── results.sqlite
118+
119+
0 directories, 1 file
120+
```
121+
122+
In this way, it's possible to run experiments on a massive scale, by using Amazon's datacentres for scaling.
123+
124+
125+
---
126+
127+
## 🌐 Sources
128+
For more information, see the following sources:
129+
130+
- [Hydra's Ray Launcher plugin docs](https://hydra.cc/docs/1.2/plugins/ray_launcher/)
131+
- [Ray YAML configuration options](https://docs.ray.io/en/master/cluster/vms/references/ray-cluster-configuration.html)
132+
- [EC2 instances types](https://aws.amazon.com/ec2/instance-types/)
133+
- [EC2 custom AMI types](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html)
134+
- [Connecting to a live database instead of saving to a local `.sqlite` file](https://dev.to/chrisgreening/connecting-to-a-relational-database-using-sqlalchemy-and-python-1619)
13.7 MB
Loading
Binary file not shown.

0 commit comments

Comments
 (0)