-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[RUNTIME] Add min_repeat_ms to time_evaluator #2200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,8 @@ | |
| #include <chrono> | ||
| #include <vector> | ||
| #include <utility> | ||
| #include <cmath> | ||
| #include <algorithm> | ||
| #include "rpc_session.h" | ||
| #include "../../common/ring_buffer.h" | ||
|
|
||
|
|
@@ -1002,9 +1004,9 @@ void RPCSession::CopyFromRemote(void* from, | |
| } | ||
|
|
||
| RPCFuncHandle RPCSession::GetTimeEvaluator( | ||
| RPCFuncHandle fhandle, TVMContext ctx, int number, int repeat) { | ||
| RPCFuncHandle fhandle, TVMContext ctx, int number, int repeat, int min_repeat_ms) { | ||
| return this->CallRemote( | ||
| RPCCode::kGetTimeEvaluator, fhandle, ctx, number, repeat); | ||
| RPCCode::kGetTimeEvaluator, fhandle, ctx, number, repeat, min_repeat_ms); | ||
| } | ||
|
|
||
| // Event handler functions | ||
|
|
@@ -1138,7 +1140,7 @@ void RPCNDArrayFree(TVMArgs args, TVMRetValue *rv) { | |
|
|
||
| void RPCGetTimeEvaluator(TVMArgs args, TVMRetValue *rv) { | ||
| PackedFunc *pf = static_cast<PackedFunc*>(args[0].operator void*()); | ||
| void *fhandle = new PackedFunc(WrapTimeEvaluator(*pf, args[1], args[2], args[3])); | ||
| void *fhandle = new PackedFunc(WrapTimeEvaluator(*pf, args[1], args[2], args[3], args[4])); | ||
| delete pf; | ||
| *rv = fhandle; | ||
| } | ||
|
|
@@ -1190,21 +1192,41 @@ void RPCSession::EventHandler::HandlePackedCall() { | |
| CHECK_EQ(state_, kRecvCode); | ||
| } | ||
|
|
||
| PackedFunc WrapTimeEvaluator(PackedFunc pf, TVMContext ctx, int number, int repeat) { | ||
| auto ftimer = [pf, ctx, number, repeat](TVMArgs args, TVMRetValue *rv) { | ||
| PackedFunc WrapTimeEvaluator(PackedFunc pf, | ||
| TVMContext ctx, | ||
| int number, | ||
| int repeat, | ||
| int min_repeat_ms) { | ||
| auto ftimer = [pf, ctx, number, repeat, min_repeat_ms](TVMArgs args, TVMRetValue *rv) mutable { | ||
| TVMRetValue temp; | ||
| std::ostringstream os; | ||
| // skip first time call, to activate lazy compilation components. | ||
| pf.CallPacked(args, &temp); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this definition (1 + number * repeat) is the correct formulation after we have introduced
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the definition is not correct. I will add a note to the doc string of |
||
| DeviceAPI::Get(ctx)->StreamSync(ctx, nullptr); | ||
|
|
||
| for (int i = 0; i < repeat; ++i) { | ||
| // start timing | ||
| auto tbegin = std::chrono::high_resolution_clock::now(); | ||
| for (int i = 0; i < number; ++i) { | ||
| pf.CallPacked(args, &temp); | ||
| } | ||
| DeviceAPI::Get(ctx)->StreamSync(ctx, nullptr); | ||
| auto tend = std::chrono::high_resolution_clock::now(); | ||
| std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> tbegin, tend; | ||
| double duration_ms = 0.0; | ||
|
|
||
| do { | ||
| if (duration_ms > 0.0) { | ||
| number = static_cast<int>( | ||
| std::max((min_repeat_ms / (duration_ms / number) + 1), | ||
| number * 1.618)); // 1.618 is chosen by random | ||
| } | ||
|
|
||
| tbegin = std::chrono::high_resolution_clock::now(); | ||
| // start timing | ||
| for (int i = 0; i < number; ++i) { | ||
| pf.CallPacked(args, &temp); | ||
| } | ||
| DeviceAPI::Get(ctx)->StreamSync(ctx, nullptr); | ||
| tend = std::chrono::high_resolution_clock::now(); | ||
|
|
||
| duration_ms = std::chrono::duration_cast<std::chrono::duration<double> > | ||
| (tend - tbegin).count() * 1000; | ||
| } while (duration_ms < min_repeat_ms); | ||
|
|
||
| double speed = std::chrono::duration_cast<std::chrono::duration<double> >( | ||
| tend - tbegin).count() / number; | ||
| os.write(reinterpret_cast<char*>(&speed), sizeof(speed)); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this break some current tests if we do not give a default value for
min_repeat_ms?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a default argument to python side