damccorm commented on code in PR #23554: URL: https://github.com/apache/beam/pull/23554#discussion_r991276197
########## website/www/site/content/en/documentation/ml/runinference-metrics.md: ########## @@ -0,0 +1,103 @@ +--- +title: "RunInference Metrics" +--- +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> + +# RunInference Metrics Example + +The main purpose of the example is to demonstrate and explain different metrics that are available when using [RunInference](https://beam.apache.org/documentation/transforms/python/elementwise/runinference/) for doing inference using a machine learning model. We use a pipeline that reads a list of sentences, tokeinze the text, uses a Transformer based model `distilbert-base-uncased-finetuned-sst-2-english` for classifies the texts into two different classes using `RunInference`. + +We showcase different RunInference metrics when the pipeline is executed using Dataflow Runner on CPU and GPU. The full example code can be found [here](https://github.com/apache/beam/tree/master/sdks/python/apache_beam/examples/inference/runinference_metrics/). Review Comment: ```suggestion We showcase different RunInference metrics when the pipeline is executed using the Dataflow Runner on CPU and GPU. The full example code can be found [here](https://github.com/apache/beam/tree/master/sdks/python/apache_beam/examples/inference/runinference_metrics/). ``` ########## website/www/site/content/en/documentation/ml/runinference-metrics.md: ########## @@ -0,0 +1,103 @@ +--- +title: "RunInference Metrics" +--- +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> + +# RunInference Metrics Example + +The main purpose of the example is to demonstrate and explain different metrics that are available when using [RunInference](https://beam.apache.org/documentation/transforms/python/elementwise/runinference/) for doing inference using a machine learning model. We use a pipeline that reads a list of sentences, tokeinze the text, uses a Transformer based model `distilbert-base-uncased-finetuned-sst-2-english` for classifies the texts into two different classes using `RunInference`. Review Comment: ```suggestion The main purpose of the example is to demonstrate and explain different metrics that are available when using the [RunInference](https://beam.apache.org/documentation/transforms/python/elementwise/runinference/) transform to perform inference using a machine learning model. We use a pipeline that reads a list of sentences, tokenizes the text, and uses a Transformer based model `distilbert-base-uncased-finetuned-sst-2-english` for classifying the texts into two different classes using `RunInference`. ``` ########## website/www/site/content/en/documentation/ml/runinference-metrics.md: ########## @@ -0,0 +1,103 @@ +--- +title: "RunInference Metrics" +--- +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> + +# RunInference Metrics Example + +The main purpose of the example is to demonstrate and explain different metrics that are available when using [RunInference](https://beam.apache.org/documentation/transforms/python/elementwise/runinference/) for doing inference using a machine learning model. We use a pipeline that reads a list of sentences, tokeinze the text, uses a Transformer based model `distilbert-base-uncased-finetuned-sst-2-english` for classifies the texts into two different classes using `RunInference`. + +We showcase different RunInference metrics when the pipeline is executed using Dataflow Runner on CPU and GPU. The full example code can be found [here](https://github.com/apache/beam/tree/master/sdks/python/apache_beam/examples/inference/runinference_metrics/). + + +The file structure for entire pipeline is: + + runinference_metrics/ + ├── pipeline/ + │ ├── __init__.py + │ ├── options.py + │ └── transformations.py + ├── __init__.py + ├── config.py + ├── main.py + └── setup.py + +`pipeline/transormations.py` contains the code for `beam.DoFn` and additional functions that are used for pipeline + +`pipeline/options.py` contains the pipeline options to configure the Dataflow pipeline + +`config.py` defines some variables like GCP PROJECT_ID, NUM_WORKERS that are used multiple times + +`setup.py` defines the packages/requirements for the pipeline to run + +`main.py` contains the pipeline code and some additional functions used for running the pipeline + + +### How to Run the Pipeline ? +First, make sure you have installed the required packages. One should have access to a Google Cloud Project and then correctly configure the GCP variables like `PROJECT_ID`, `REGION`, and others in `config.py`. For using Dataflow with GPU, all the necessary setup instructions are mentioned here: https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/dataflow/gpu-examples/pytorch-minimal. + + +1. Dataflow with CPU: `python main.py --mode cloud --device CPU` +2. Dataflow with GPU: `python main.py --mode cloud --device GPU` + +The pipeline can be broken down into few simple steps: +1. Create a list of texts to use it as an input using `beam.Create` Review Comment: ```suggestion 1. Create a list of texts to use as an input using `beam.Create` ``` ########## website/www/site/content/en/documentation/ml/runinference-metrics.md: ########## @@ -0,0 +1,103 @@ +--- +title: "RunInference Metrics" +--- +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> + +# RunInference Metrics Example + +The main purpose of the example is to demonstrate and explain different metrics that are available when using [RunInference](https://beam.apache.org/documentation/transforms/python/elementwise/runinference/) for doing inference using a machine learning model. We use a pipeline that reads a list of sentences, tokeinze the text, uses a Transformer based model `distilbert-base-uncased-finetuned-sst-2-english` for classifies the texts into two different classes using `RunInference`. + +We showcase different RunInference metrics when the pipeline is executed using Dataflow Runner on CPU and GPU. The full example code can be found [here](https://github.com/apache/beam/tree/master/sdks/python/apache_beam/examples/inference/runinference_metrics/). + + +The file structure for entire pipeline is: + + runinference_metrics/ + ├── pipeline/ + │ ├── __init__.py + │ ├── options.py + │ └── transformations.py + ├── __init__.py + ├── config.py + ├── main.py + └── setup.py + +`pipeline/transormations.py` contains the code for `beam.DoFn` and additional functions that are used for pipeline + +`pipeline/options.py` contains the pipeline options to configure the Dataflow pipeline + +`config.py` defines some variables like GCP PROJECT_ID, NUM_WORKERS that are used multiple times + +`setup.py` defines the packages/requirements for the pipeline to run + +`main.py` contains the pipeline code and some additional functions used for running the pipeline + + +### How to Run the Pipeline ? +First, make sure you have installed the required packages. One should have access to a Google Cloud Project and then correctly configure the GCP variables like `PROJECT_ID`, `REGION`, and others in `config.py`. For using Dataflow with GPU, all the necessary setup instructions are mentioned here: https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/dataflow/gpu-examples/pytorch-minimal. Review Comment: ```suggestion First, make sure you have installed the required packages. One should have access to a Google Cloud Project and then correctly configure the GCP variables like `PROJECT_ID`, `REGION`, and others in `config.py`. To use GPUs, follow the setup instructions here: https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/dataflow/gpu-examples/pytorch-minimal. You can then run your pipeline with the following commands: ``` ########## website/www/site/content/en/documentation/ml/runinference-metrics.md: ########## @@ -0,0 +1,103 @@ +--- +title: "RunInference Metrics" +--- +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> + +# RunInference Metrics Example + +The main purpose of the example is to demonstrate and explain different metrics that are available when using [RunInference](https://beam.apache.org/documentation/transforms/python/elementwise/runinference/) for doing inference using a machine learning model. We use a pipeline that reads a list of sentences, tokeinze the text, uses a Transformer based model `distilbert-base-uncased-finetuned-sst-2-english` for classifies the texts into two different classes using `RunInference`. + +We showcase different RunInference metrics when the pipeline is executed using Dataflow Runner on CPU and GPU. The full example code can be found [here](https://github.com/apache/beam/tree/master/sdks/python/apache_beam/examples/inference/runinference_metrics/). + + +The file structure for entire pipeline is: + + runinference_metrics/ + ├── pipeline/ + │ ├── __init__.py + │ ├── options.py + │ └── transformations.py + ├── __init__.py + ├── config.py + ├── main.py + └── setup.py + +`pipeline/transormations.py` contains the code for `beam.DoFn` and additional functions that are used for pipeline + +`pipeline/options.py` contains the pipeline options to configure the Dataflow pipeline + +`config.py` defines some variables like GCP PROJECT_ID, NUM_WORKERS that are used multiple times + +`setup.py` defines the packages/requirements for the pipeline to run + +`main.py` contains the pipeline code and some additional functions used for running the pipeline + + +### How to Run the Pipeline ? +First, make sure you have installed the required packages. One should have access to a Google Cloud Project and then correctly configure the GCP variables like `PROJECT_ID`, `REGION`, and others in `config.py`. For using Dataflow with GPU, all the necessary setup instructions are mentioned here: https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/dataflow/gpu-examples/pytorch-minimal. + + +1. Dataflow with CPU: `python main.py --mode cloud --device CPU` +2. Dataflow with GPU: `python main.py --mode cloud --device GPU` + +The pipeline can be broken down into few simple steps: +1. Create a list of texts to use it as an input using `beam.Create` +2. Tokenizing the text +3. Using RunInference to do inference +4. Postprocessing the output of RunInference + +The code snippet for the pipeline is: + +{{< highlight >}} + with beam.Pipeline(options=pipeline_options) as pipeline: + _ = ( + pipeline + | "Create inputs" >> beam.Create(inputs) + | "Tokenize" >> beam.ParDo(Tokenize(cfg.TOKENIZER_NAME)) + | "Inference" >> + RunInference(model_handler=KeyedModelHandler(model_handler)) + | "Decode Predictions" >> beam.ParDo(PostProcessor())) +{{< /highlight >}} + + +## RunInference Metrics + +As mentioned above, we benchmarked the performance of RunInference using Dataflow on both CPU and GPU. These metrics can be seen in the GCP UI and can also be printed using +{{< highlight >}} +metrics = pipeline.result.metrics().query(beam.metrics.MetricsFilter()) +{{< /highlight >}} + + +A snapshot of different metrics from GCP UI when using Dataflow on GPU: + +  + +Some metrics commonly used for benchmarking are: + +* `num_inferences`: represents the total number of elements passed to `run_inference()`. + +* `inference_batch_latency_micro_secs_MEAN`: represents the average time taken to perform the inference across all batches of examples, measured in microseconds. + +* `inference_request_batch_size_COUNT`: represents the total number of samples across all batches of examples (created from `beam.BatchElements`) to be passed to run_inference() + +* `inference_request_batch_byte_size_MEAN`: represents the average size of all elements for all samples in all batches of examples (created from `beam.BatchElements`) to be passed to run_inference(). It is measured in bytes. + +* `model_byte_size_MEAN`: It represents the average memory consumed to load and initialize the model. It is measured in bytes. + +* `load_model_latency_milli_secs_MEAN`: represents the average time taken to load and initialize the model. It is measured in milliseconds. Review Comment: ```suggestion * `inference_request_batch_byte_size_MEAN`: represents the average size of all elements for all samples in all batches of examples (created from `beam.BatchElements`) to be passed to run_inference(). This is measured in bytes. * `model_byte_size_MEAN`: Irepresents the average memory consumed to load and initialize the model. This is measured in bytes. * `load_model_latency_milli_secs_MEAN`: represents the average time taken to load and initialize the model. This is measured in milliseconds. ``` ########## sdks/python/apache_beam/examples/inference/runinference_metrics/pipeline/transformations.py: ########## @@ -0,0 +1,93 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +"""This file contains the transformations and utility functions for +the pipeline.""" +import apache_beam as beam +import torch +from apache_beam.io.filesystems import FileSystems +from apache_beam.ml.inference.pytorch_inference import PytorchModelHandlerKeyedTensor +from transformers import DistilBertForSequenceClassification +from transformers import DistilBertTokenizer + + +class CustomPytorchModelHandlerKeyedTensor(PytorchModelHandlerKeyedTensor): + """Wrapper around PytorchModelHandlerKeyedTensor to load a model on CPU.""" + def load_model(self) -> torch.nn.Module: + """Loads and initializes a Pytorch model for processing.""" + model = self._model_class(**self._model_params) + model.to(self._device) + file = FileSystems.open(self._state_dict_path, "rb") + model.load_state_dict(torch.load(file, map_location=self._device)) + model.eval() + return model + + +class HuggingFaceStripBatchingWrapper(DistilBertForSequenceClassification): + """Wrapper around HuggingFace model because RunInference requires a batch + as a list of dicts instead of a dict of lists. Another workaround Review Comment: Could you please create an issue to support this and then link to the issue in the code here? This is something we should probably be able to just infer/handle automatically ########## website/www/site/content/en/documentation/ml/runinference-metrics.md: ########## @@ -0,0 +1,103 @@ +--- +title: "RunInference Metrics" +--- +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> + +# RunInference Metrics Example + +The main purpose of the example is to demonstrate and explain different metrics that are available when using [RunInference](https://beam.apache.org/documentation/transforms/python/elementwise/runinference/) for doing inference using a machine learning model. We use a pipeline that reads a list of sentences, tokeinze the text, uses a Transformer based model `distilbert-base-uncased-finetuned-sst-2-english` for classifies the texts into two different classes using `RunInference`. + +We showcase different RunInference metrics when the pipeline is executed using Dataflow Runner on CPU and GPU. The full example code can be found [here](https://github.com/apache/beam/tree/master/sdks/python/apache_beam/examples/inference/runinference_metrics/). + + +The file structure for entire pipeline is: + + runinference_metrics/ + ├── pipeline/ + │ ├── __init__.py + │ ├── options.py + │ └── transformations.py + ├── __init__.py + ├── config.py + ├── main.py + └── setup.py + +`pipeline/transormations.py` contains the code for `beam.DoFn` and additional functions that are used for pipeline Review Comment: ```suggestion `pipeline/transformations.py` contains the code for `beam.DoFn` and additional functions that are used for pipeline ``` ########## website/www/site/content/en/documentation/ml/runinference-metrics.md: ########## @@ -0,0 +1,103 @@ +--- +title: "RunInference Metrics" +--- +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> + +# RunInference Metrics Example + +The main purpose of the example is to demonstrate and explain different metrics that are available when using [RunInference](https://beam.apache.org/documentation/transforms/python/elementwise/runinference/) for doing inference using a machine learning model. We use a pipeline that reads a list of sentences, tokeinze the text, uses a Transformer based model `distilbert-base-uncased-finetuned-sst-2-english` for classifies the texts into two different classes using `RunInference`. + +We showcase different RunInference metrics when the pipeline is executed using Dataflow Runner on CPU and GPU. The full example code can be found [here](https://github.com/apache/beam/tree/master/sdks/python/apache_beam/examples/inference/runinference_metrics/). + + +The file structure for entire pipeline is: + + runinference_metrics/ + ├── pipeline/ + │ ├── __init__.py + │ ├── options.py + │ └── transformations.py + ├── __init__.py + ├── config.py + ├── main.py + └── setup.py + +`pipeline/transormations.py` contains the code for `beam.DoFn` and additional functions that are used for pipeline + +`pipeline/options.py` contains the pipeline options to configure the Dataflow pipeline + +`config.py` defines some variables like GCP PROJECT_ID, NUM_WORKERS that are used multiple times + +`setup.py` defines the packages/requirements for the pipeline to run + +`main.py` contains the pipeline code and some additional functions used for running the pipeline + + +### How to Run the Pipeline ? +First, make sure you have installed the required packages. One should have access to a Google Cloud Project and then correctly configure the GCP variables like `PROJECT_ID`, `REGION`, and others in `config.py`. For using Dataflow with GPU, all the necessary setup instructions are mentioned here: https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/dataflow/gpu-examples/pytorch-minimal. + + +1. Dataflow with CPU: `python main.py --mode cloud --device CPU` +2. Dataflow with GPU: `python main.py --mode cloud --device GPU` + +The pipeline can be broken down into few simple steps: +1. Create a list of texts to use it as an input using `beam.Create` +2. Tokenizing the text +3. Using RunInference to do inference +4. Postprocessing the output of RunInference Review Comment: ```suggestion 2. Tokenize the text 3. Use RunInference to do inference 4. Postprocess the output of RunInference ``` ########## website/www/site/content/en/documentation/ml/runinference-metrics.md: ########## @@ -0,0 +1,103 @@ +--- +title: "RunInference Metrics" +--- +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> + +# RunInference Metrics Example + +The main purpose of the example is to demonstrate and explain different metrics that are available when using [RunInference](https://beam.apache.org/documentation/transforms/python/elementwise/runinference/) for doing inference using a machine learning model. We use a pipeline that reads a list of sentences, tokeinze the text, uses a Transformer based model `distilbert-base-uncased-finetuned-sst-2-english` for classifies the texts into two different classes using `RunInference`. + +We showcase different RunInference metrics when the pipeline is executed using Dataflow Runner on CPU and GPU. The full example code can be found [here](https://github.com/apache/beam/tree/master/sdks/python/apache_beam/examples/inference/runinference_metrics/). + + +The file structure for entire pipeline is: + + runinference_metrics/ + ├── pipeline/ + │ ├── __init__.py + │ ├── options.py + │ └── transformations.py + ├── __init__.py + ├── config.py + ├── main.py + └── setup.py + +`pipeline/transormations.py` contains the code for `beam.DoFn` and additional functions that are used for pipeline + +`pipeline/options.py` contains the pipeline options to configure the Dataflow pipeline + +`config.py` defines some variables like GCP PROJECT_ID, NUM_WORKERS that are used multiple times + +`setup.py` defines the packages/requirements for the pipeline to run + +`main.py` contains the pipeline code and some additional functions used for running the pipeline + + +### How to Run the Pipeline ? +First, make sure you have installed the required packages. One should have access to a Google Cloud Project and then correctly configure the GCP variables like `PROJECT_ID`, `REGION`, and others in `config.py`. For using Dataflow with GPU, all the necessary setup instructions are mentioned here: https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/dataflow/gpu-examples/pytorch-minimal. + + +1. Dataflow with CPU: `python main.py --mode cloud --device CPU` +2. Dataflow with GPU: `python main.py --mode cloud --device GPU` + +The pipeline can be broken down into few simple steps: +1. Create a list of texts to use it as an input using `beam.Create` +2. Tokenizing the text +3. Using RunInference to do inference +4. Postprocessing the output of RunInference + +The code snippet for the pipeline is: + +{{< highlight >}} Review Comment: ```suggestion {{< highlight >}} ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
