# Parallel processing in Neuraxle¶

This demonstrates how to stream data in parallel in a Neuraxle pipeline. The pipeline steps’ parallelism here will be obvious.

The pipeline has two steps: 1. Preprocessing: the step that process the data simply sleeps. 2. Model: the model simply multiplies the data by two.

This can be used with scikit-learn as well to transform things in parallel, and any other library such as tensorflow.

Pipelines benchmarked: 1. We first use a classical pipeline and evaluate the time. 2. Then we use a minibatched pipeline and we evaluate the time. 3. Then we use a parallel pipeline and we evaluate the time.

We expect the parallel pipeline to be faster due to having more workers in parallel, as well as starting the model’s transformations at the same time that other batches are being preprocessed, using queues.

Out:

Classical 'Pipeline' execution time: 2.022265672683716 seconds.
Minibatched 'MiniBatchSequentialPipeline' execution time: 2.0235798358917236 seconds.
Parallel 'SequentialQueuedPipeline' execution time: 0.5125672817230225 seconds.


import time
import numpy as np
from neuraxle.base import ExecutionContext as CX

from neuraxle.distributed.streaming import SequentialQueuedPipeline
from neuraxle.pipeline import BasePipeline, Pipeline, MiniBatchSequentialPipeline
from neuraxle.steps.loop import ForEach
from neuraxle.steps.misc import Sleep
from neuraxle.steps.numpy import MultiplyByN

def eval_run_time(pipeline: BasePipeline):
pipeline.setup(CX())
a = time.time()
output = pipeline.transform(list(range(100)))
b = time.time()
seconds = b - a
return seconds, output

def main():
"""
The task is to sleep 0.02 seconds for each data input and then multiply by 2.
"""
sleep_time = 0.02
preprocessing_and_model_steps = [ForEach(Sleep(sleep_time=sleep_time)), MultiplyByN(2)]

# Classical pipeline - all at once with one big batch:
p = Pipeline(preprocessing_and_model_steps)
time_vanilla_pipeline, output_classical = eval_run_time(p)
print(f"Classical 'Pipeline' execution time: {time_vanilla_pipeline} seconds.")

# Classical minibatch pipeline - minibatch size 5:
p = MiniBatchSequentialPipeline(preprocessing_and_model_steps,
batch_size=5)
time_minibatch_pipeline, output_minibatch = eval_run_time(p)
print(f"Minibatched 'MiniBatchSequentialPipeline' execution time: {time_minibatch_pipeline} seconds.")

# Parallel pipeline - minibatch size 5 with 4 parallel workers per step that
# have a max queue size of 10 batches between preprocessing and the model:
p = SequentialQueuedPipeline(preprocessing_and_model_steps,
n_workers_per_step=4, max_queued_minibatches=10, batch_size=5)
time_parallel_pipeline, output_parallel = eval_run_time(p)
print(f"Parallel 'SequentialQueuedPipeline' execution time: {time_parallel_pipeline} seconds.")

assert np.array_equal(output_classical, output_minibatch)
assert np.array_equal(output_classical, output_parallel)
assert time_parallel_pipeline < time_minibatch_pipeline, str((time_parallel_pipeline, time_vanilla_pipeline))

if __name__ == '__main__':
main()


Total running time of the script: ( 0 minutes 4.562 seconds)

Gallery generated by Sphinx-Gallery
{"ft_lang_label":"__label__en","ft_lang_prob":0.7145603,"math_prob":0.9717485,"size":3192,"snap":"2022-27-2022-33","text_gpt3_token_len":748,"char_repetition_ratio":0.16844417,"word_repetition_ratio":0.010695187,"special_character_ratio":0.22681704,"punctuation_ratio":0.16494845,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9877791,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-08T14:31:29Z\",\"WARC-Record-ID\":\"<urn:uuid:cd74e130-38eb-4b3f-945b-a88294a7ea9e>\",\"Content-Length\":\"79805\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:6f81952c-e404-4435-8c4f-765916b3ea5c>\",\"WARC-Concurrent-To\":\"<urn:uuid:1bdc2af5-5683-47d9-b495-5bd4e9f71025>\",\"WARC-IP-Address\":\"185.199.108.153\",\"WARC-Target-URI\":\"https://www.neuraxle.org/stable/examples/parallel/plot_streaming_pipeline.html\",\"WARC-Payload-Digest\":\"sha1:HEK3JACSCNRWM4UHMKVUXFLIU7VNP26M\",\"WARC-Block-Digest\":\"sha1:ZP4HJ5D5Y2OXNJTQMLLRQN6SH6YW6JVD\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882570827.41_warc_CC-MAIN-20220808122331-20220808152331-00356.warc.gz\"}"}