GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Subtle speed optimization by beru · Pull Request #419 · tiny-dnn/tiny-dnn

Conversation

@beru

@beru beru commented Nov 23, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Achieved about 1% speed gain by adjusting 1st parameter (bool parallelize) of for_i function in parallel_for.h file. When the size of data to process is not big enough, suppress parallelization to reduce thread creation overhead.

The downsides of this change

  • code gets difficult to read (can be difficult to debug too?)
  • may not be a good idea when thread creation is very cheap (thread pooling.. Intel TBB not tested)
  • execution flow is not consistent

Here's the outputs of example_mnist_train project release builds to compare the performance.
before.txt
after.txt

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Randl reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
beru added 2 commits November 23, 2016 22:30
@beru
test/test_core.h
0de0c71
  change type of variables cl_platform and cl_device from size_t to int to reduce warnings

tiny_dnn/layers/convolutional_layer.h
  change the way to set data to vector container

tiny_dnn/layers/layer.h
  reuse local variable diff to suppress heap memory allocation/deallocation (maybe futile...)
  only parallelize if size of data to process is big enough

tiny_dnn/layers/slice_layer.h
  remove supposedly garbled characters

tiny_dnn/optimizers/optimizer.h
  to control parallel_for.h `for_i` function, add 3rd argument `bool parallelize` to optimizer::update method

tiny_dnn/util/image.h
  remove implicit downcast warning by using holy C style casts
@beru
optimize further by tuning up iterative call of activation function.
b0db424
@beru

beru commented Nov 23, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Achieved additional 10% ish speed gain by tuning up iterative call of activation function.
Here's the output to compare. after2.txt

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bhack

bhack commented Nov 23, 2016

Copy link
Copy Markdown
Contributor

What code are you running?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@beru

beru commented Nov 23, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@bhack I compiled and ran examples/mnist/train.cpp program to check execution speed.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bhack

bhack commented Nov 23, 2016

Copy link
Copy Markdown
Contributor

And with what engine? Internal?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@beru

beru commented Nov 23, 2016

Copy link
Copy Markdown
Contributor Author

@bhack I modified around layer and optimization function a little bit, so this PR isn't related to any backend engines.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

edgarriba
edgarriba reviewed Nov 23, 2016
View reviewed changes
Comment thread tiny_dnn/layers/layer.h
diff.begin(), [&](float_t x) { // NOLINT
return x * rcp_batch_size; });
o->update(diff, target);
bool parallelize = (target.size() >= 512);

@edgarriba edgarriba Nov 23, 2016

Copy link
Copy Markdown
Member

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.

@beru what's the reason for this? Could you add a short comment to the source?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@beru beru Nov 24, 2016

Copy link
Copy Markdown
Contributor Author

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 just added below comment in recent commit.

// parallelize only when target size is big enough to mitigate thread spawning overhead
bool parallelize = (target.size() >= 512);
o->update(diff, target, parallelize);

I added bool parallelize parameter to optimizer::update method and its descendants.
The newly added parameter is used for 1st parameter of for_i function (that has triple argument prototype). for_i function can selectively create multi-threads but doing so is not favourable when size of data is small.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
edgarriba
edgarriba reviewed Nov 23, 2016
View reviewed changes
Comment thread test/test_core.h Outdated

#define TINY_DNN_GET_DEVICE_AND_PLATFORM \
size_t cl_platform = 0, cl_device = 0; \
int cl_platform = 0, cl_device = 0; \

@edgarriba edgarriba Nov 23, 2016

Copy link
Copy Markdown
Member

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.

Why an int here?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@beru beru Nov 24, 2016

Copy link
Copy Markdown
Contributor Author

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've just reverted the change in here. I'll send another PR for this in future.

The reason why I changed the type : 2nd and 3rd parameters of Device::Device in tiny_dnn/core/framework/device.fwd.h is int, maybe I should have used size_t -> int casts.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@edgarriba edgarriba added the PR: Needs Review label Nov 23, 2016
@beru
add comment
33a0128
@bhack

bhack commented Nov 24, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

@beru Are you sure that something like https://github.com/Maratyszcza/pthreadpool/ that we already used trough upstream cannot ammortised many of this costs?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@beru
revert change in test/test_core.h
f4bcdfe
@beru

beru commented Nov 24, 2016

Copy link
Copy Markdown
Contributor Author

bhack wrote:
@beru Are you sure that something like https://github.com/Maratyszcza/pthreadpool/ that we already used trough upstream cannot ammortise many of this costs?

I still haven't extensively checked the performance difference on many platforms, I only tested it on Win10 64bit, YMMV.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bhack

bhack commented Nov 24, 2016

Copy link
Copy Markdown
Contributor

But what code path are you testing? This one

tiny-dnn/tiny_dnn/util/parallel_for.h

Line 108 in 5693b92

void parallel_for(int start, int end, const Func &f, int /*grainsize*/) {
?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@patrikhuber

patrikhuber commented Nov 24, 2016

Copy link
Copy Markdown
Contributor

@bhack The thread pool you linked looks horrible. Are we going C-style library now?

Have a look at this in comparison - proper modern C++: https://github.com/progschj/ThreadPool

nyanp reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bhack

bhack commented Nov 24, 2016

Copy link
Copy Markdown
Contributor

No I don't want to use that. I mean that our upstream NNPACK is using that internally and I talked about ThreadPool cause I don't know if we are handling in this optimizzation only the c++11 thread backend of our parallel_for

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@beru

beru commented Nov 24, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

bhack wrote:
But what code path are you testing? This one

tiny-dnn/tiny_dnn/util/parallel_for.h

Line 108 in 5693b92

void parallel_for(int start, int end, const Func &f, int /*grainsize*/) {
?

@bhack Yes, that one. Here's the simplified call stack.

parallel_for
for_
for_
for_i
kernels::conv2d_op_avx
Conv2dOp::compute
convolutional_layer
layer::forward
sequential::forward
network::fprop
network::train_onebatch
network::train_once
network::fit
network::train
train_lenet
main
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@beru

beru commented Nov 24, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

bhack wrote:
@beru Are you sure that something like https://github.com/Maratyszcza/pthreadpool/ that we already used trough upstream cannot ammortised many of this costs?

@bhack I'm not so sure about that. I can only guess without having and measuring actual implementations.
We can rely on profilers but I'd like to add our own mechanism to profile the code so that we can easily see percentages of execution time.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@edgarriba

edgarriba commented Nov 26, 2016

Copy link
Copy Markdown
Member

@beru can I merge this PR?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@beru

beru commented Nov 26, 2016

Copy link
Copy Markdown
Contributor Author

@edgarriba It's up to you. I think b0db424 is much effective. So, just merging it would be enough for a while. Do you agree with the idea?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@edgarriba

edgarriba commented Nov 26, 2016

Copy link
Copy Markdown
Member

@beru sure

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@edgarriba edgarriba added PR: Good to Merge and removed PR: Needs Review labels Nov 26, 2016
@edgarriba
edgarriba merged commit 8ac87a9 into tiny-dnn:master Nov 26, 2016
@beru
beru deleted the subtle_optimization branch January 8, 2017 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@edgarriba edgarriba edgarriba left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

PR: Good to Merge

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@beru @bhack @patrikhuber @edgarriba
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.