Skip to content

Add parallel matching for tf.gfile.Glob - #44269

Closed
firejq wants to merge 15 commits into
tensorflow:masterfrom
firejq:patch-3
Closed

firejq wants to merge 15 commits into
tensorflow:masterfrom
firejq:patch-3

Conversation

@firejq

@firejq firejq commented Oct 23, 2020 •

Copy link
Copy Markdown
Contributor

This is a PR from JIZHI Team & TaiJi AI platform in Tencent.

Add a argument max_parallel_num for tf.gfile.Glob, which will setup a threadpool to parallelize the matching for mulitple pattern strings. It can bring nice performance improvement if max_parallel_num is set appropriately.

Thanks for your review.

Add a argument `max_parallel_num` for  `tf.gfile.Glob`, which will setup a threadpool to parallelize the matching for mulitple pattern strings. It can bring nice performance improvement if `max_parallel_num` is set appropriately.

Thanks for your review.
@google-ml-butler google-ml-butler Bot added the size:S CL Change Size: Small label Oct 23, 2020
@google-cla google-cla Bot added the cla: yes label Oct 23, 2020
@firejq firejq changed the title Add feature: Parallel match for tf.gfile.Glob Add parallel matching for tf.gfile.Glob Oct 23, 2020
@mihaimaruseac

Copy link
Copy Markdown
Contributor

Since the matching happensinside C++ code, I'm afraid this PR does not do what it says it does.

@gbaned gbaned self-assigned this Oct 24, 2020
@gbaned
gbaned requested a review from mihaimaruseac October 24, 2020 05:21
@firejq

firejq commented Oct 24, 2020 •

Copy link
Copy Markdown
Contributor Author

@mihaimaruseac Thanks for your reply!

I also considered the issue you mentioned in fact. In the C++ implement of different FileSystem, there may be some mutexes which cause the multi-threaded parallel from python client does not work. However, glob is a I/O-bound operation as we known, which always consumes a lot of time in blocking wait and multi-threads can ease this overhead a lot.

BTW, I have obtained nice effect in my case that using tf.gfile.Glob for hdfs:

pattern = ['hdfs://xxxx_1/*', 'hdfs://xxxx_2/', 'hdfs://xxxx_3/, 'hdfs://xxxx_4/', 'hdfs://xxxx_5/']
begin_time = int(time.time())
files = tf.gfile.Glob(pattern, max_parallel_num=5)
print('size: {}, used time: {} s'.format(len(files), int(time.time()) - begin_time))

As the final size of files is 20000, the above code costed 203 seconds as it printed, and 809 seconds while max_parallel_num is set to 1 (It is too slow for me so I have to make some optimization...).

The above is my reason for proposing this pr and there may be some incompleteness. I am looking forward to your valuable suggestions for improvement.

Change max size of threadpool for parallel BFS of `GetMatchingPaths` to use more available resource.
@mihaimaruseac

Copy link
Copy Markdown
Contributor

This looks good to me now. Since it changes API, let's wait for API owners approval too

@mihaimaruseac mihaimaruseac added the API review API Review label Oct 28, 2020

@fchollet fchollet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the choice of the number of threads to use should be automated, rather than specified by the user. Most users will not be aware of this argument. What do you think?

Comment thread tensorflow/python/lib/io/file_io.py Resolved
Comment thread tensorflow/python/lib/io/file_io.py Resolved
@firejq

firejq commented Oct 29, 2020

Copy link
Copy Markdown
Contributor Author

@fchollet Thanks for your review!

Maybe the choice of the number of threads to use should be automated, rather than specified by the user. Most users will not be aware of this argument. What do you think?

In fact I have implemented automatically using len(pattern) as the number of parallel processes inside the api, however, I overturned my own ideas in my practice. As for the cases which run multiple tf instance processes on the same machine (e.g. tf with horovod), users tend to control the parallel number of different processes based on the CPU resources on their machine. If the api automatically tries the maximum parallelism, it may cause unexpected resource preemption.

This is my reason for setting it as a configurable parameter and I would like to discuss further with you.

@firejq
firejq requested a review from fchollet October 29, 2020 07:35
@tensorflowbutler tensorflowbutler removed the awaiting review Pull request awaiting review label Nov 1, 2020
@firejq

firejq commented Nov 2, 2020

Copy link
Copy Markdown
Contributor Author

@fchollet Could you please check this again?

@mihaimaruseac mihaimaruseac left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't forget about posting some benchmark results

Comment thread tensorflow/python/lib/io/file_io.py Resolved
Comment thread tensorflow/python/lib/io/file_io.py Resolved
Comment thread tensorflow/python/lib/io/file_io.py Outdated

@tf_export("io.gfile.glob")
def get_matching_files_v2(pattern):
def get_matching_files_v2(pattern, num_threads=1):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we were going to automatically determine the best number of threads if user didn't supply any.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have done.

@firejq firejq Nov 5, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry that I reopened this conversation. As for the default values of num_threads, I think we may need to have more discuss.

I have made benchmarks on a multi-core (80 threads) server and the default value of num_threads (len(patterns)) can produce a quite positive optimization effect. However, when I apply this to another machine with poorer performance, it may produces negative effect and even sometimes makes the process hang. I think the cause is excessive CPU resource preemption with the parallelism enhanced at the C++ level (#44310) landed at the same time. P.S. I also tested the optimization of C++ in the same environment, and that one can always achieve a considerable optimization effect.

Therefore, considering of better versatility, I prefer to set the default value of num_threads to 1, i.e. modify the value here to num_threads = num_threads or 1, which can keep consistent with the previous codes by default, and work positively if user set it manually. In addition, we can explain the risk and recommended value of this parameter in the comments.

Look forward to your suggestions.

@firejq
firejq requested a review from fchollet November 5, 2020 06:52
@mihaimaruseac

Copy link
Copy Markdown
Contributor

I would also go with 1 thread by default to maintain compatibility. Alternatively, minimum between number of patterns and number of cores.

@firejq

firejq commented Nov 5, 2020

Copy link
Copy Markdown
Contributor Author

@mihaimaruseac
Thanks! I have finished the update of default value setting and I think it has reached a more robust state now. Could you please check anything need enhancement more again? 😄

@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Nov 5, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Nov 5, 2020
@gbaned gbaned added ready to pull PR ready for merge process and removed ready to pull PR ready for merge process labels Nov 6, 2020
@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Nov 6, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Nov 6, 2020
@firejq

firejq commented Nov 9, 2020

Copy link
Copy Markdown
Contributor Author

Hi, @mihaimaruseac @gbaned

There seems to be a internal failed check in the ci process. Could you please help to see if something went wrong?

@firejq
firejq requested a review from gbaned November 10, 2020 09:22
@mihaimaruseac

Copy link
Copy Markdown
Contributor

It is imported internally, pending some additional review. I'll come back here if there's more to do, but for now this is all good.

@firejq

firejq commented Nov 12, 2020 •

Copy link
Copy Markdown
Contributor Author

It is imported internally, pending some additional review. I'll come back here if there's more to do, but for now this is all good.

@mihaimaruseac Okay, thanks for pushing forward!

@mihaimaruseac

Copy link
Copy Markdown
Contributor

An API owners comment:

Can we push this into C++ too? Multiprocessing in Python has been the source of several bugs in the past

I think this should possible. What do you think?

@firejq

firejq commented Nov 19, 2020 •

Copy link
Copy Markdown
Contributor Author

An API owners comment:

Can we push this into C++ too? Multiprocessing in Python has been the source of several bugs in the past

I think this should possible. What do you think?

@mihaimaruseac
Does he mean that C++ OP _pywrap_file_io.GetMatchingFiles directly receives a list of patterns, and then processes the multiple patterns in parallel?

If so, first of all, I think push this into C++ too is worth considering. As we discussed above, changing the default value to 1 is to avoid introducing uncertainty from Python multiprocesses in fact. In this pr, python's parallelism is actually chosen by the user according to the specific running environment (just like other dataset's parallelism parameters). The default parameters are always the same value as before and do not pose any uncertainty to the existing user code.

On the other hand, this will also result in more changes, including input formats for C++ Op. So I prefer to preserve python's multiprocesses for now. Of course, if the owner sticks to this point, I can also make the changes, but it may take more time and more disscussion. Thanks anyway!

@mihaimaruseac

Copy link
Copy Markdown
Contributor

We can change C++ API (and update the goldens files), it is not an issue, as long as Python API maintains compatibility.

Seems python multiprocessing has issues so it's preferable to push this code to C++.

@firejq

firejq commented Nov 24, 2020

Copy link
Copy Markdown
Contributor Author

We can change C++ API (and update the goldens files), it is not an issue, as long as Python API maintains compatibility.

Seems python multiprocessing has issues so it's preferable to push this code to C++.

Sorry for the late reply...
Okay, I will migrate the relevant logic to C++ later. Thanks!

@gbaned

gbaned commented Dec 29, 2020

Copy link
Copy Markdown
Contributor

@firejq Can you please resolve conflicts? Thanks!

@gbaned gbaned removed the ready to pull PR ready for merge process label Dec 29, 2020
@firejq

firejq commented Dec 31, 2020

Copy link
Copy Markdown
Contributor Author

Due to other tighter issues in the near future, I will temporarily close this pr, and reopen it if there are new developments in the future. Thanks to all reviewers!

@firejq firejq closed this Dec 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API review API Review cla: yes size:S CL Change Size: Small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants