Skip to content
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

Fix compiler warning & speed up compile stage. #1197

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions paddle/function/BufferArg.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ class SequenceIdArg : public BufferArg {
public:
SequenceIdArg(const TensorShape& shape, ArgType argType = UNSPECIFIED)
: BufferArg(VALUE_TYPE_INT32, shape, argType) {
CHECK_EQ(shape_.ndims(), (size_t)1);
CHECK_GT(shape_[0], 1);
CHECK_EQ(shape_.ndims(), 1UL);
CHECK_GT(shape_[0], 1UL);
numSeqs_ = shape_[0] - 1;
}

Expand All @@ -201,7 +201,7 @@ class SequenceIdArg : public BufferArg {
ArgType argType = UNSPECIFIED)
: BufferArg(buf, VALUE_TYPE_INT32, shape, argType) {
bufferType_ = TENSOR_SEQUENCE_ID;
CHECK_EQ(shape_.ndims(), (size_t)1);
CHECK_EQ(shape_.ndims(), 1UL);
numSeqs_ = shape_[0] - 1;
}

Expand Down Expand Up @@ -280,9 +280,9 @@ class SparseMatrixArg : public BufferArg {
type_(type) {
bufferType_ = TENSOR_SPARSE;
CHECK((valueType == VALUE_TYPE_FLOAT) || (valueType == VALUE_TYPE_DOUBLE));
CHECK_EQ(shape_.ndims(), (size_t)2);
CHECK_EQ(row_.shape().ndims(), (size_t)1);
CHECK_EQ(col_.shape().ndims(), (size_t)1);
CHECK_EQ(shape_.ndims(), 2UL);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is simpler to use a 'U' or 'UL' suffix.

Copy link
Contributor

Choose a reason for hiding this comment

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

哦,warning来自于unsigned。

CHECK_EQ(row_.shape().ndims(), 1UL);
CHECK_EQ(col_.shape().ndims(), 1UL);
if (format == SPARSE_CSR_FORMAT) {
CHECK_EQ(nnz, col.shape()[0]);
} else if (format == SPARSE_CSC_FORMAT) {
Expand Down
2 changes: 1 addition & 1 deletion paddle/function/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
file(GLOB h_files . *Op.h)
file(GLOB cpp_files . *Op.cpp)

list(APPEND h_files Function.h)
list(APPEND h_files Function.h FunctionBase.h)
list(APPEND cpp_files Function.cpp)
list(APPEND cpp_files BufferArg.cpp)

Expand Down
40 changes: 1 addition & 39 deletions paddle/function/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License. */
#include <map>
#include <vector>
#include "BufferArg.h"
#include "FunctionBase.h"
#include "paddle/math/Matrix.h"
#include "paddle/utils/ClassRegistrar.h"

Expand Down Expand Up @@ -126,43 +127,4 @@ class BufferArgs {
std::vector<BufferArg*> _args_;
};

/**
* \brief Base class for Function.
* The basic Function implementation requires override init and calc interfaces.
*
* The caller needs to ensure the validity of the arguments
* during Function execution.
*
* Function inputs are readonly, Function outputs have two modes: ASSIGN_TO
* and ADD_TO.
* If output.getArgType() == ASSIGN_TO, this is assign mode, and the calculation
* result of Function assigned to the output BufferArg.
* If output.getArgType() == ADD_TO, this is add mode, and the calculation
* result of Function need added to the output BufferArg.
*
* For example:
* ASSIGN_TO: output = Function(inputs)
* ADD_TO: output += Function(inputs)
* If Function has more than one output, each output can have different modes.
*/
class FunctionBase {
public:
virtual ~FunctionBase() {}

virtual void init(const FuncConfig& config) {}

virtual void calc(const BufferArgs& inputs, const BufferArgs& outputs) {}

static ClassRegistrar<FunctionBase> funcRegistrar_;
};

#define FUNC_NAME(typeName, deviceName) #typeName "-" #deviceName

#define REGISTER_TYPED_FUNC(typeName, deviceName, className) \
static InitFunction __reg_type_##typeName##deviceName([]() { \
FunctionBase::funcRegistrar_ \
.registerClass<className<DEVICE_TYPE_##deviceName>>( \
FUNC_NAME(typeName, deviceName)); \
})

} // namespace paddle
64 changes: 64 additions & 0 deletions paddle/function/FunctionBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once
#include "paddle/utils/ClassRegistrar.h"

namespace paddle {
class FuncConfig;
class BufferArgs;

/**
* \brief Base class for Function.
* The basic Function implementation requires override init and calc interfaces.
*
* The caller needs to ensure the validity of the arguments
* during Function execution.
*
* Function inputs are readonly, Function outputs have two modes: ASSIGN_TO
* and ADD_TO.
* If output.getArgType() == ASSIGN_TO, this is assign mode, and the calculation
* result of Function assigned to the output BufferArg.
* If output.getArgType() == ADD_TO, this is add mode, and the calculation
* result of Function need added to the output BufferArg.
*
* For example:
* ASSIGN_TO: output = Function(inputs)
* ADD_TO: output += Function(inputs)
* If Function has more than one output, each output can have different modes.
*/
class FunctionBase {
public:
virtual ~FunctionBase() {}

virtual void init(const FuncConfig& config) { (void)(config); }

virtual void calc(const BufferArgs& inputs, const BufferArgs& outputs) {
(void)(inputs);
(void)(outputs);
}

static ClassRegistrar<FunctionBase> funcRegistrar_;
};

#define FUNC_NAME(typeName, deviceName) #typeName "-" #deviceName

#define REGISTER_TYPED_FUNC(typeName, deviceName, className) \
static InitFunction __reg_type_##typeName##deviceName([]() { \
FunctionBase::funcRegistrar_ \
.registerClass<className<DEVICE_TYPE_##deviceName>>( \
FUNC_NAME(typeName, deviceName)); \
})

} // namespace paddle
1 change: 1 addition & 0 deletions paddle/gserver/layers/ContextProjection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */

#include "ContextProjection.h"
#include "paddle/function/Function.h"
#include "paddle/utils/Stat.h"

namespace paddle {
Expand Down
2 changes: 1 addition & 1 deletion paddle/gserver/layers/Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License. */
#include <functional>
#include <memory>
#include "ModelConfig.pb.h"
#include "paddle/function/Function.h"
#include "paddle/function/FunctionBase.h"
Copy link
Collaborator Author

@reyoung reyoung Jan 19, 2017

Choose a reason for hiding this comment

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

Make Layer.h depend on FunctionBase.h.

It could speed up compiling time very much, and it makes us not to recompile the whole Paddle when we change something in Function library.

Copy link
Contributor

Choose a reason for hiding this comment

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

Layer.h还是需要知道完整的BufferArgs类型的;Function的Prepare arguments阶段是将Layer的参数Argument类型,转换成Function的参数BufferArg类型,这个对于很多Layer的实现是一样的,后续会考虑在class Layer里面实现。
编译耗时的问题可能来自于,当前BufferArg的一些函数的实现是在类声明里面的,这些可以移入cpp里面。

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

好,那我先close了。。其他的fix回头顺手改一下吧 :)

#include "paddle/math/CpuSparseMatrix.h"
#include "paddle/parameter/Parameter.h"
#include "paddle/utils/ClassRegistrar.h"
Expand Down
1 change: 1 addition & 0 deletions paddle/gserver/layers/NormProjectionLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License. */

#include <vector>
#include "NormLayer.h"
#include "paddle/function/Function.h"
#include "paddle/math/Matrix.h"

namespace paddle {
Expand Down