From 9a23323df70c78c804967decaf67139478aa6514 Mon Sep 17 00:00:00 2001 From: dsbarinov1 Date: Thu, 26 Jan 2023 21:07:47 +0300 Subject: [PATCH 1/9] Assertion failed during tuning --- src/runtime/contrib/random/mt_random_engine.cc | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/runtime/contrib/random/mt_random_engine.cc b/src/runtime/contrib/random/mt_random_engine.cc index ac5259436005..895e60715c30 100644 --- a/src/runtime/contrib/random/mt_random_engine.cc +++ b/src/runtime/contrib/random/mt_random_engine.cc @@ -192,12 +192,12 @@ class RandomEngine { struct ParallelTask { static int RunTask(int task_id, TVMParallelGroupEnv* penv, void* cdata) { ParallelTask* task = static_cast(cdata); - task->Run(task_id); + task->Run(task_id, penv->num_task); return 0; } - void Run(int i) { - int64_t chunk_size = size / num_threads; + void Run(int i, int num_threads) { + int64_t chunk_size = ceil(size / num_threads); int64_t st = i * chunk_size; int64_t ed = std::min(st + chunk_size, size); self->FillDataImpl(data, st, ed, dtype); @@ -220,8 +220,7 @@ class RandomEngine { } if (dtype.bits == 1 || dtype.bits == 4 || dtype.bits == 8 || dtype.bits == 16 || dtype.bits == 32 || dtype.bits == 64) { - int num_threads = task.num_threads = runtime::threading::MaxConcurrency(); - int res = TVMBackendParallelLaunch(ParallelTask::RunTask, &task, num_threads); + int res = TVMBackendParallelLaunch(ParallelTask::RunTask, &task, 0); ICHECK_EQ(res, 0) << "RandomFillForMeasure: TVMBackendParallelLaunch failed"; } else { LOG(FATAL) << "Doesn't support dtype code " << dtype.code << " dtype bits " << dtype.bits; From 6e3d8ffd3945fc1561e2dbbd1c89400122034a09 Mon Sep 17 00:00:00 2001 From: dsbarinov1 Date: Fri, 27 Jan 2023 14:35:51 +0300 Subject: [PATCH 2/9] Cleanup --- src/runtime/contrib/random/mt_random_engine.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/runtime/contrib/random/mt_random_engine.cc b/src/runtime/contrib/random/mt_random_engine.cc index 895e60715c30..1d6ce351fb0e 100644 --- a/src/runtime/contrib/random/mt_random_engine.cc +++ b/src/runtime/contrib/random/mt_random_engine.cc @@ -197,7 +197,7 @@ class RandomEngine { } void Run(int i, int num_threads) { - int64_t chunk_size = ceil(size / num_threads); + int64_t chunk_size = size / num_threads; int64_t st = i * chunk_size; int64_t ed = std::min(st + chunk_size, size); self->FillDataImpl(data, st, ed, dtype); @@ -205,7 +205,6 @@ class RandomEngine { RandomEngine* self; void* data; - int num_threads; int64_t size; DLDataType dtype; }; From 992bd2217ae5a2bbd3e92a54e3b3c554d6b67b7e Mon Sep 17 00:00:00 2001 From: dsbarinov1 Date: Fri, 27 Jan 2023 15:34:59 +0300 Subject: [PATCH 3/9] Do not commit --- src/runtime/contrib/random/mt_random_engine.cc | 3 +++ src/runtime/thread_pool.cc | 1 + 2 files changed, 4 insertions(+) diff --git a/src/runtime/contrib/random/mt_random_engine.cc b/src/runtime/contrib/random/mt_random_engine.cc index 1d6ce351fb0e..9e81f2a2ea92 100644 --- a/src/runtime/contrib/random/mt_random_engine.cc +++ b/src/runtime/contrib/random/mt_random_engine.cc @@ -209,6 +209,9 @@ class RandomEngine { DLDataType dtype; }; + int num_workers = tvm::runtime::threading::MaxConcurrency(); + LOG(FATAL) << "MaxConcurrency = " << num_workers; + ParallelTask task; task.self = this; task.data = tensor->data; diff --git a/src/runtime/thread_pool.cc b/src/runtime/thread_pool.cc index 665244d3d1bd..ca21d165e99e 100644 --- a/src/runtime/thread_pool.cc +++ b/src/runtime/thread_pool.cc @@ -338,6 +338,7 @@ class ThreadPool { // The SpscTaskQueue only hosts ONE item at a time queues_.emplace_back(std::make_unique()); } + ICHECK_EQ(tvm::runtime::threading::MaxConcurrency(), 1) << "MaxConcurrency is not equal to 1"; threads_ = std::make_unique( num_workers_, [this](int worker_id) { this->RunWorker(worker_id); }, exclude_worker0_ /* include_main_thread */); From 308783fa987963f5fc6481d62017141a4f7873cf Mon Sep 17 00:00:00 2001 From: dsbarinov1 Date: Mon, 30 Jan 2023 13:55:53 +0300 Subject: [PATCH 4/9] Do not commit --- src/runtime/thread_pool.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/runtime/thread_pool.cc b/src/runtime/thread_pool.cc index ca21d165e99e..665244d3d1bd 100644 --- a/src/runtime/thread_pool.cc +++ b/src/runtime/thread_pool.cc @@ -338,7 +338,6 @@ class ThreadPool { // The SpscTaskQueue only hosts ONE item at a time queues_.emplace_back(std::make_unique()); } - ICHECK_EQ(tvm::runtime::threading::MaxConcurrency(), 1) << "MaxConcurrency is not equal to 1"; threads_ = std::make_unique( num_workers_, [this](int worker_id) { this->RunWorker(worker_id); }, exclude_worker0_ /* include_main_thread */); From 5acea54caf29c4f79f618d7d472b67d2b4334b0f Mon Sep 17 00:00:00 2001 From: dsbarinov1 Date: Tue, 31 Jan 2023 18:29:03 +0300 Subject: [PATCH 5/9] Undo fix + provide test for multithread random filling --- src/runtime/contrib/random/mt_random_engine.cc | 11 +++++------ tests/python/contrib/test_random.py | 13 +++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/runtime/contrib/random/mt_random_engine.cc b/src/runtime/contrib/random/mt_random_engine.cc index 9e81f2a2ea92..ac5259436005 100644 --- a/src/runtime/contrib/random/mt_random_engine.cc +++ b/src/runtime/contrib/random/mt_random_engine.cc @@ -192,11 +192,11 @@ class RandomEngine { struct ParallelTask { static int RunTask(int task_id, TVMParallelGroupEnv* penv, void* cdata) { ParallelTask* task = static_cast(cdata); - task->Run(task_id, penv->num_task); + task->Run(task_id); return 0; } - void Run(int i, int num_threads) { + void Run(int i) { int64_t chunk_size = size / num_threads; int64_t st = i * chunk_size; int64_t ed = std::min(st + chunk_size, size); @@ -205,13 +205,11 @@ class RandomEngine { RandomEngine* self; void* data; + int num_threads; int64_t size; DLDataType dtype; }; - int num_workers = tvm::runtime::threading::MaxConcurrency(); - LOG(FATAL) << "MaxConcurrency = " << num_workers; - ParallelTask task; task.self = this; task.data = tensor->data; @@ -222,7 +220,8 @@ class RandomEngine { } if (dtype.bits == 1 || dtype.bits == 4 || dtype.bits == 8 || dtype.bits == 16 || dtype.bits == 32 || dtype.bits == 64) { - int res = TVMBackendParallelLaunch(ParallelTask::RunTask, &task, 0); + int num_threads = task.num_threads = runtime::threading::MaxConcurrency(); + int res = TVMBackendParallelLaunch(ParallelTask::RunTask, &task, num_threads); ICHECK_EQ(res, 0) << "RandomFillForMeasure: TVMBackendParallelLaunch failed"; } else { LOG(FATAL) << "Doesn't support dtype code " << dtype.code << " dtype bits " << dtype.bits; diff --git a/tests/python/contrib/test_random.py b/tests/python/contrib/test_random.py index 7a52c0dbf1ea..7010cdf6f4b9 100644 --- a/tests/python/contrib/test_random.py +++ b/tests/python/contrib/test_random.py @@ -154,9 +154,22 @@ def check_remote(server): test_local(dev, dtype) test_rpc(dtype) +def test_random_fill_mt(): + """ Check random filler applicability in case of nontrivial thread pool configuration. + Particularly when MaxConcurrency != num_workers_used_ which is actual for big-little systems. + """ + num_threads_used = 1 + + configure_threads = tvm.get_global_func("runtime.config_threadpool") + configure_threads(1, num_threads_used) + + test_input = tvm.runtime.ndarray.empty((64, 64)) + random_fill = tvm.get_global_func("tvm.contrib.random.random_fill_for_measure") + random_fill(test_input) if __name__ == "__main__": test_randint() test_uniform() test_normal() test_random_fill() + test_random_fill_mt() From 66c718c5f851a0220187c6ff6842c1e7ce7a433a Mon Sep 17 00:00:00 2001 From: dsbarinov1 Date: Tue, 31 Jan 2023 20:52:55 +0300 Subject: [PATCH 6/9] Random fill test with fix enabled --- src/runtime/contrib/random/mt_random_engine.cc | 10 ++++------ tests/python/contrib/test_random.py | 4 +++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/runtime/contrib/random/mt_random_engine.cc b/src/runtime/contrib/random/mt_random_engine.cc index ac5259436005..dc01114af0a1 100644 --- a/src/runtime/contrib/random/mt_random_engine.cc +++ b/src/runtime/contrib/random/mt_random_engine.cc @@ -192,12 +192,12 @@ class RandomEngine { struct ParallelTask { static int RunTask(int task_id, TVMParallelGroupEnv* penv, void* cdata) { ParallelTask* task = static_cast(cdata); - task->Run(task_id); + task->Run(task_id, penv->num_task); return 0; } - void Run(int i) { - int64_t chunk_size = size / num_threads; + void Run(int i, int num_tasks) { + int64_t chunk_size = size / num_tasks; int64_t st = i * chunk_size; int64_t ed = std::min(st + chunk_size, size); self->FillDataImpl(data, st, ed, dtype); @@ -205,7 +205,6 @@ class RandomEngine { RandomEngine* self; void* data; - int num_threads; int64_t size; DLDataType dtype; }; @@ -220,8 +219,7 @@ class RandomEngine { } if (dtype.bits == 1 || dtype.bits == 4 || dtype.bits == 8 || dtype.bits == 16 || dtype.bits == 32 || dtype.bits == 64) { - int num_threads = task.num_threads = runtime::threading::MaxConcurrency(); - int res = TVMBackendParallelLaunch(ParallelTask::RunTask, &task, num_threads); + int res = TVMBackendParallelLaunch(ParallelTask::RunTask, &task, 0); ICHECK_EQ(res, 0) << "RandomFillForMeasure: TVMBackendParallelLaunch failed"; } else { LOG(FATAL) << "Doesn't support dtype code " << dtype.code << " dtype bits " << dtype.bits; diff --git a/tests/python/contrib/test_random.py b/tests/python/contrib/test_random.py index 7010cdf6f4b9..3db835604d66 100644 --- a/tests/python/contrib/test_random.py +++ b/tests/python/contrib/test_random.py @@ -154,8 +154,9 @@ def check_remote(server): test_local(dev, dtype) test_rpc(dtype) + def test_random_fill_mt(): - """ Check random filler applicability in case of nontrivial thread pool configuration. + """Check random filler applicability in case of nontrivial thread pool configuration. Particularly when MaxConcurrency != num_workers_used_ which is actual for big-little systems. """ num_threads_used = 1 @@ -167,6 +168,7 @@ def test_random_fill_mt(): random_fill = tvm.get_global_func("tvm.contrib.random.random_fill_for_measure") random_fill(test_input) + if __name__ == "__main__": test_randint() test_uniform() From 224f6bede0b5abec072fd570c81f0154aa0958bb Mon Sep 17 00:00:00 2001 From: dsbarinov1 Date: Wed, 1 Feb 2023 01:40:42 +0300 Subject: [PATCH 7/9] Isolate the effect of this test on the other tests --- tests/python/contrib/test_random.py | 30 ++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/tests/python/contrib/test_random.py b/tests/python/contrib/test_random.py index 3db835604d66..1c888e7c17de 100644 --- a/tests/python/contrib/test_random.py +++ b/tests/python/contrib/test_random.py @@ -155,18 +155,30 @@ def check_remote(server): test_rpc(dtype) -def test_random_fill_mt(): +def test_random_fill_for_measure_mt(): """Check random filler applicability in case of nontrivial thread pool configuration. Particularly when MaxConcurrency != num_workers_used_ which is actual for big-little systems. """ - num_threads_used = 1 - - configure_threads = tvm.get_global_func("runtime.config_threadpool") - configure_threads(1, num_threads_used) - - test_input = tvm.runtime.ndarray.empty((64, 64)) - random_fill = tvm.get_global_func("tvm.contrib.random.random_fill_for_measure") - random_fill(test_input) + no_exception_happened = True + + def test_body(): + try: + num_thread_used = 1 + configure_threads = tvm.get_global_func("runtime.config_threadpool") + configure_threads(1, num_thread_used) + + test_input = tvm.runtime.ndarray.empty((10, 10)) + random_fill = tvm.get_global_func("tvm.contrib.random.random_fill_for_measure") + random_fill(test_input) + except: + nonlocal no_exception_happened + no_exception_happened = False + + # ThreadPool object is thread local. To eliminate effect on other test cases put it into thread + x = threading.Thread(target=test_body) + x.start() + x.join() + assert no_exception_happened if __name__ == "__main__": From 3ded119c74390c4d50567f14c59b7046f8822940 Mon Sep 17 00:00:00 2001 From: dsbarinov1 Date: Wed, 1 Feb 2023 01:43:00 +0300 Subject: [PATCH 8/9] Correct the typo in the function name --- tests/python/contrib/test_random.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/contrib/test_random.py b/tests/python/contrib/test_random.py index 1c888e7c17de..1b426bbbcb5c 100644 --- a/tests/python/contrib/test_random.py +++ b/tests/python/contrib/test_random.py @@ -155,7 +155,7 @@ def check_remote(server): test_rpc(dtype) -def test_random_fill_for_measure_mt(): +def test_random_fill_mt(): """Check random filler applicability in case of nontrivial thread pool configuration. Particularly when MaxConcurrency != num_workers_used_ which is actual for big-little systems. """ From 7a7a8f3c34f2ea54c3563b09844412e5dce884c5 Mon Sep 17 00:00:00 2001 From: dsbarinov1 Date: Wed, 1 Feb 2023 12:08:02 +0300 Subject: [PATCH 9/9] Import threading + lint --- tests/python/contrib/test_random.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/python/contrib/test_random.py b/tests/python/contrib/test_random.py index 1b426bbbcb5c..ddc06b07110e 100644 --- a/tests/python/contrib/test_random.py +++ b/tests/python/contrib/test_random.py @@ -20,6 +20,7 @@ from tvm.contrib import random from tvm import rpc import tvm.testing +import threading def test_randint(): @@ -160,7 +161,7 @@ def test_random_fill_mt(): Particularly when MaxConcurrency != num_workers_used_ which is actual for big-little systems. """ no_exception_happened = True - + def test_body(): try: num_thread_used = 1 @@ -178,7 +179,7 @@ def test_body(): x = threading.Thread(target=test_body) x.start() x.join() - assert no_exception_happened + assert no_exception_happened if __name__ == "__main__":