tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.48k stars 1.93k forks source link

navigator.hardwareConcurrency and native pthread gets different thread pool size #4995

Closed axinging closed 3 years ago

axinging commented 3 years ago

(On Intel i7-9700) By navigator.hardwareConcurrency: https://github.com/tensorflow/tfjs/pull/4994, the WASM_THREAD_POOL_SIZE is 4. WASM_THREAD_POOL_SIZE is got by:

Math.min(4, Math.max(1, (navigator.hardwareConcurrency || 1) / 2));

By native pthread: https://github.com/tensorflow/tfjs/pull/4942, the WASM_THREAD_POOL_SIZE is 1. WASM_THREAD_POOL_SIZE is got by:

// emscripten_num_logical_cores corresponds to navigator.hardwareConcurrency.
// Many x86-64 processors have 2 threads per core, so we are dividing by 2.
#ifdef __EMSCRIPTEN_PTHREADS__
int num_cores = emscripten_num_logical_cores() / 2;
#else
int num_cores = 1;
#endif

int min_num_threads = 1;
int max_num_threads = 4;
int thread_pool_size =  // thread_pool_size is WASM_THREAD_POOL_SIZE
    std::min(std::max(num_cores, min_num_threads), max_num_threads);

Does this possible due to #ifdef __EMSCRIPTEN_PTHREADS__?

BTW, code has rebased, and https://github.com/tensorflow/tfjs/pull/4957 is included.

Reproduce steps:

  1. Use local wasm:
    +++ b/e2e/benchmarks/local-benchmark/index.html
    @@ -91,7 +91,7 @@ limitations under the License.
    <script src="https://unpkg.com/@tensorflow/tfjs-backend-webgl@latest/dist/tf-backend-webgl.js"></script>
    <script src="https://unpkg.com/@tensorflow/tfjs-layers@latest/dist/tf-layers.js"></script>
    <script src="https://unpkg.com/@tensorflow/tfjs-converter@latest/dist/tf-converter.js"></script>
    -  <script src="https://unpkg.com/@tensorflow/tfjs-backend-wasm@latest/dist/tf-backend-wasm.js"></script>
    +  <script src="../../../tfjs-backend-wasm/dist/tf-backend-wasm.js"></script>
  2. Build: cd tfjs-backend-wasm tfjs-backend-wasm$ yarn & yarn build-npm cd ../e2e e2e$ yarn build-deps
  3. Serup server cd ../../ npx http-server
  4. Open http://127.0.0.1:8080/tfjs/e2e/benchmarks/local-benchmark/
google-ml-butler[bot] commented 3 years ago

Are you satisfied with the resolution of your issue? Yes No