Skip to content

Comments

Api redesign#2117

Closed
steremma wants to merge 7 commits intoroot-project:masterfrom
steremma:api-redesign
Closed

Api redesign#2117
steremma wants to merge 7 commits intoroot-project:masterfrom
steremma:api-redesign

Conversation

@steremma
Copy link
Contributor

API Redesign

Goal

The goal is this PR is to improve the API of the CNN layers (MaxPooling and Conv currently), by eliminating redundant constructor arguments and fields. By redundant in this context, I refer to arguments that can be directly computed from others, and fields that unnecesseraly exist in multiple classes.

Key points

Below some discussion points on design decisions I made, but still consider debatable.
Since my experience in production level C++ is very limited I highly value opinions from experienced colleagues and previous authors of the module.

Making MaxPoolingLayer a sub-class of ConvLayer

Every layer type in a convolutional network follows the logic existing in our ConvLayer:

A filter is sliding over the input and at each step applies an operation to the input elements within its receptive field to produce a single output element.

  • In a convolutional layer this operation is a linear combination with learnable parameters.
  • In an average pooling layer the operation is a linear combination with constant parameters (and equal to the inverse of the receptive field's size).
  • In a max pooling layer its a non linear operation.

As we can see they all share the same logic and therefore fields.

Results

  1. Common fields between the 2 layer types in the CNN module are now not duplicated (strides sizes, padding sizes, filter sizes). The same for the 4 protected methods in ConvLayer.

  2. We now have a cleaner API, as the constructor arguments where reduced from 26 to 16 without any change in the functionality).

@steremma steremma requested a review from lmoneta as a code owner May 30, 2018 22:07
@phsft-bot
Copy link

Can one of the admins verify this patch?

@ashlaban
Copy link
Contributor

@phsft-bot build!

@phsft-bot
Copy link

Starting build on slc6/gcc48, slc6-i686/gcc49, centos7/clang39, centos7/gcc62, centos7/gcc7, centos7/gcc7, centos7/gcc7, fedora28/native, ubuntu16/native, mac1013/native, windows10/vc15 with flags -Dvc=OFF -Dimt=ON -Dccache=ON
How to customize builds

@phsft-bot
Copy link

Build failed on fedora28/native.
See console output.

Warnings:

  • /mnt/build/workspace/root-pullrequests-build/root/core/meta/src/TClass.cxx:686:14: warning: ‘char* strncpy(char*, const char*, size_t)’ specified bound depends on the length of the source argument [-Wstringop-overflow=]
  • /mnt/build/workspace/root-pullrequests-build/root/net/rpdutils/src/rpdutils.cxx:5203:14: warning: ‘char* strncpy(char*, const char*, size_t)’ specified bound depends on the length of the source argument [-Wstringop-overflow=]
  • /mnt/build/workspace/root-pullrequests-build/root/net/rpdutils/src/rpdutils.cxx:4757:26: warning: ‘char* strncpy(char*, const char*, size_t)’ output may be truncated copying 10 bytes from a string of length 10 [-Wstringop-truncation]
  • /mnt/build/workspace/root-pullrequests-build/root/net/rootd/src/rootd.cxx:490:14: warning: ‘char* strncat(char*, const char*, size_t)’ accessing between 2147483648 and 2147483647 bytes at offsets 0 and [-2147483647, 2147483648] may overlap 1 byte at offset [0, 4294967295] [-Wrestrict]
  • /mnt/build/workspace/root-pullrequests-build/root/net/rootd/src/rootd.cxx:761:21: warning: ‘%lu’ directive writing between 1 and 20 bytes into a region of size between 0 and 8191 [-Wformat-overflow=]
  • /mnt/build/workspace/root-pullrequests-build/root/proof/proofd/src/XrdProofdProtocol.cxx:527:45: warning: ‘void* memset(void*, int, size_t)’ clearing an object of non-trivial type ‘class XrdSecEntity’; use assignment or value-initialization instead [-Wclass-memaccess]
  • /mnt/build/workspace/root-pullrequests-build/root/core/base/src/TSystem.cxx:1148:14: warning: ‘char* strncat(char*, const char*, size_t)’ accessing 1 byte at offsets 0 and [-2147483647, 2147483648] may overlap 1 byte at offset [0, 2147483649] [-Wrestrict]
  • /mnt/build/workspace/root-pullrequests-build/root/core/base/src/TROOT.cxx:1770:96: warning: cast between incompatible function types from ‘TVirtualPad*& ()()’ to ‘TGlobalMappedFunction::GlobalFunc_t’ {aka ‘void (*)()’} [-Wcast-function-type]
  • /mnt/build/workspace/root-pullrequests-build/root/core/base/src/TROOT.cxx:1774:94: warning: cast between incompatible function types from ‘TVirtualX*& ()()’ to ‘TGlobalMappedFunction::GlobalFunc_t’ {aka ‘void (*)()’} [-Wcast-function-type]
  • /mnt/build/workspace/root-pullrequests-build/root/core/base/src/TROOT.cxx:1776:95: warning: cast between incompatible function types from ‘TDirectory*& ()()’ to ‘TGlobalMappedFunction::GlobalFunc_t’ {aka ‘void (*)()’} [-Wcast-function-type]

And 174 more

Failing tests:

And 93 more

@phsft-bot
Copy link

Build failed on centos7/gcc7.
See console output.

Warnings:

  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:217:11: warning: ‘operator new’ must not return NULL unless it is declared ‘throw()’ (or -fcheck-new is in effect)
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:214:27: warning: unused parameter ‘size’ [-Wunused-parameter]
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:214:50: warning: unused parameter ‘al’ [-Wunused-parameter]
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:220:27: warning: unused parameter ‘size’ [-Wunused-parameter]
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:220:50: warning: unused parameter ‘al’ [-Wunused-parameter]
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:300:28: warning: unused parameter ‘ptr’ [-Wunused-parameter]
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:300:50: warning: unused parameter ‘al’ [-Wunused-parameter]
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:304:28: warning: unused parameter ‘ptr’ [-Wunused-parameter]
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:304:50: warning: unused parameter ‘al’ [-Wunused-parameter]
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:344:11: warning: ‘operator new’ must not return NULL unless it is declared ‘throw()’ (or -fcheck-new is in effect)

And 32 more

Failing tests:

@phsft-bot
Copy link

Build failed on centos7/gcc7.
See console output.

Warnings:

  • include/TMVA/DNN/CNN/ConvLayer.h:76:11: warning: ‘TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fPaddingWidth’ will be initialized after [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:69:11: warning: ‘size_t TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fNLocalViewPixels’ [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:160:1: warning: when initialized here [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:76:11: warning: ‘TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fPaddingWidth’ will be initialized after [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:69:11: warning: ‘size_t TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fNLocalViewPixels’ [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:160:1: warning: when initialized here [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:76:11: warning: ‘TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TReference<double> >::fPaddingWidth’ will be initialized after [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:69:11: warning: ‘size_t TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TReference<double> >::fNLocalViewPixels’ [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:160:1: warning: when initialized here [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:76:11: warning: ‘TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fPaddingWidth’ will be initialized after [-Wreorder]

And 14 more

Failing tests:

@phsft-bot
Copy link

Build failed on slc6-i686/gcc49.
See console output.

Warnings:

  • include/TMVA/DNN/CNN/ConvLayer.h:76:11: warning: ‘TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fPaddingWidth’ will be initialized after [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:69:11: warning: ‘size_t TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fNLocalViewPixels’ [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:160:1: warning: when initialized here [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:76:11: warning: ‘TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TReference<double> >::fPaddingWidth’ will be initialized after [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:69:11: warning: ‘size_t TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TReference<double> >::fNLocalViewPixels’ [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:160:1: warning: when initialized here [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:76:11: warning: ‘TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fPaddingWidth’ will be initialized after [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:69:11: warning: ‘size_t TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fNLocalViewPixels’ [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:160:1: warning: when initialized here [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:76:11: warning: ‘TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<float> >::fPaddingWidth’ will be initialized after [-Wreorder]

And 14 more

Failing tests:

@ashlaban
Copy link
Contributor

ashlaban commented Jun 1, 2018

@phsft-bot build it and to it quick-like!

@phsft-bot
Copy link

Starting build on slc6/gcc48, slc6/gcc62, slc6-i686/gcc49, centos7/clang39, centos7/gcc62, centos7/gcc7, fedora28/native, ubuntu16/native, mac1013/native, windows10/vc15 with flags -Dvc=OFF -Dimt=ON -Dccache=ON
How to customize builds

@phsft-bot
Copy link

Build failed on slc6/gcc62.
See console output.

Warnings:

  • include/TMVA/DNN/CNN/ConvLayer.h:76:11: warning: ‘TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fPaddingWidth’ will be initialized after [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:69:11: warning: ‘size_t TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fNLocalViewPixels’ [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:160:1: warning: when initialized here [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:76:11: warning: ‘TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TReference<double> >::fPaddingWidth’ will be initialized after [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:69:11: warning: ‘size_t TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TReference<double> >::fNLocalViewPixels’ [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:160:1: warning: when initialized here [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:76:11: warning: ‘TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fPaddingWidth’ will be initialized after [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:69:11: warning: ‘size_t TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fNLocalViewPixels’ [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:160:1: warning: when initialized here [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:76:11: warning: ‘TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fPaddingWidth’ will be initialized after [-Wreorder]

And 14 more

Failing tests:

@phsft-bot
Copy link

Build failed on centos7/gcc7.
See console output.

Warnings:

  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:217:11: warning: ‘operator new’ must not return NULL unless it is declared ‘throw()’ (or -fcheck-new is in effect)
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:214:27: warning: unused parameter ‘size’ [-Wunused-parameter]
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:214:50: warning: unused parameter ‘al’ [-Wunused-parameter]
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:220:27: warning: unused parameter ‘size’ [-Wunused-parameter]
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:220:50: warning: unused parameter ‘al’ [-Wunused-parameter]
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:300:28: warning: unused parameter ‘ptr’ [-Wunused-parameter]
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:300:50: warning: unused parameter ‘al’ [-Wunused-parameter]
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:304:28: warning: unused parameter ‘ptr’ [-Wunused-parameter]
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:304:50: warning: unused parameter ‘al’ [-Wunused-parameter]
  • /mnt/build/jenkins/workspace/root-pullrequests-build/root/core/newdelete/src/NewDelete.cxx:344:11: warning: ‘operator new’ must not return NULL unless it is declared ‘throw()’ (or -fcheck-new is in effect)

And 32 more

Failing tests:

@phsft-bot
Copy link

Build failed on slc6-i686/gcc49.
See console output.

Warnings:

  • include/TMVA/DNN/CNN/ConvLayer.h:76:11: warning: ‘TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fPaddingWidth’ will be initialized after [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:69:11: warning: ‘size_t TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fNLocalViewPixels’ [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:160:1: warning: when initialized here [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:76:11: warning: ‘TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TReference<double> >::fPaddingWidth’ will be initialized after [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:69:11: warning: ‘size_t TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TReference<double> >::fNLocalViewPixels’ [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:160:1: warning: when initialized here [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:76:11: warning: ‘TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fPaddingWidth’ will be initialized after [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:69:11: warning: ‘size_t TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<double> >::fNLocalViewPixels’ [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:160:1: warning: when initialized here [-Wreorder]
  • include/TMVA/DNN/CNN/ConvLayer.h:76:11: warning: ‘TMVA::DNN::CNN::TConvLayer<TMVA::DNN::TCpu<float> >::fPaddingWidth’ will be initialized after [-Wreorder]

And 14 more

Failing tests:

bool inline isInteger(Scalar_t x) const { return x == floor(x); }

/* Calculate the output dimension of the convolutional layer */
size_t calculateDimension(int imgDim, int fltDim, int padding, int stride);
Copy link
Member

Choose a reason for hiding this comment

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

for consistency it is maybe better to have the parameters defined as size_t

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right I will make the change ASAP

Scalar_t fDropoutProbability; ///< Probability that an input is active.

private:
size_t fPaddingHeight; ///< The number of zero layers added top and bottom of the input.
Copy link
Member

Choose a reason for hiding this comment

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

why now fFilterDepdth, height are now protected and the paddingheight is private ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Everything that is needed on MaxPoolingLayer (which is now a subclass of ConvLayer) is now protected to reduce duplication. Since pooling uses 0 padding by definition, this field remains private to ConvLayer

for (size_t i = 0; i < outputNSlices; i++) {
fDerivatives.emplace_back(outputNRows, outputNCols);
for (size_t i = 0; i < batchSize; i++) {
fDerivatives.emplace_back(depth, fNLocalViews);
Copy link
Member

Choose a reason for hiding this comment

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

We should have a comment here explaining the meaning of the variables, (i.e. that batchsize is the outputnslices, etc...)

Copy link
Contributor

Choose a reason for hiding this comment

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

I would also request that explanations for e.g. depth is provided. What is called depth here is called "number of units" in some literature and "number of channels" in some. Tying our naming convention into what others are using will help people absorb the code quicker!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Both valid points, i will address them and update the PR

@lmoneta
Copy link
Member

lmoneta commented Jun 20, 2018

Hi Manos,
Can you please fix the warnings and address the other comments.
There is a small backward uncompatible change in the XML file, changing from "frameWidth" to "FilterWidth", but I think is fine and it is better to be consistent.
Afterwards I think the PR is ready to be merged

@steremma
Copy link
Contributor Author

Hi Lorenzo,
Thanks for the constructive feedback, I will soon address the comments and the build warnings and update the PR. Regarding the XML incompatibility, I changed that on purpose so that we always have the same naming. Let me know if I need to revert this.

@pcanal
Copy link
Member

pcanal commented Jun 22, 2018

We try to avoid merge commits inside PRs, please use rebase instead. Thanks.

@steremma
Copy link
Contributor Author

Good point @pcanal thanks! Should I start doing that in other PRs or somehow change the current one as well?

@pcanal
Copy link
Member

pcanal commented Jun 22, 2018

Could you change the current one? (It can't be brought into the master with a merge commit :) ).

@steremma
Copy link
Contributor Author

steremma commented Jun 22, 2018

Yes I will do that.

My plan is to revert the merge (by resetting to the last feature commit), rebase and force push.

I will open a new PR with a cleaner history altogether. I know its suboptimal because we will lose the current code review discussion but I want to be sure that the history is clean before merging. I have also taken notes of all the points raised here.

@steremma
Copy link
Contributor Author

Opened a new PR with simplified history here. All comments have been addressed. Closing this one.

@steremma steremma closed this Jun 23, 2018
@steremma steremma deleted the api-redesign branch August 6, 2018 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants