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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmake/flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ set(COMMON_FLAGS
-Wno-unused-function
-Wno-error=literal-suffix
-Wno-error=sign-compare
-Wno-error=unused-local-typedefs)
-Wno-error=unused-local-typedefs
-Wno-error=deprecated-declarations)

set(GPU_COMMON_FLAGS
-fPIC
Expand Down
1 change: 0 additions & 1 deletion paddle/function/BufferArg.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ limitations under the License. */
#pragma once

#include <glog/logging.h>

#include "TensorShape.h"
#include "TensorType.h"
#include "paddle/math/Matrix.h"
Expand Down
99 changes: 99 additions & 0 deletions paddle/function/BufferArgs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* 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 "BufferArg.h"

namespace paddle {
/**
* Argument type for Function::calc().
* A BufferArgs contains a set of BufferArg,
* because Function can have multiple inputs and outputs.
*
* addArg() with Matix object used to adapt Layer Argument.
* Will create a BufferArg object in addArg(),
* and free in destructor of BufferArgs.
*
* addArg() with BufferArg object, just save BufferArg object address,
* and the caller needs to guarantee the validity of the BufferArg object
* in the BufferArgs life time.
*/
class BufferArgs {
public:
BufferArgs() {}

~BufferArgs() {
for (auto arg : _args_) {
delete arg;
}
}

size_t size() const { return args_.size(); }

// add argument into BufferArgs
// Tensor can be Matrix, Vector, IVector.
// For inputs, do not need argType.
// For outputs, the argType needs to be specified as ASSIGN_TO or ADD_TO.
void addArg(const Matrix& arg, ArgType argType = UNSPECIFIED) {
_args_.push_back(new BufferArg(arg, argType));
addArg(*_args_.back());
}

void addArg(const Vector& arg, ArgType argType = UNSPECIFIED) {
_args_.push_back(new BufferArg(arg, argType));
addArg(*_args_.back());
}

void addArg(const IVector& arg, ArgType argType = UNSPECIFIED) {
_args_.push_back(new BufferArg(arg, argType));
addArg(*_args_.back());
}

// Add arg into BufferArgs and reshape the arg.
//
// For example, arg represents an image buffer,
// but Matrix can only represent a two-dimensional Tensor.
// So need an extra argument to describe the shape of the image buffer.
void addArg(const Matrix& arg,
const TensorShape& shape,
ArgType argType = UNSPECIFIED);

void addArg(const CpuSparseMatrix& arg, ArgType argType = UNSPECIFIED);
void addArg(const GpuSparseMatrix& arg, ArgType argType = UNSPECIFIED);

void addArg(const Matrix& matrix,
const IVector& vector,
ArgType argType = UNSPECIFIED);

// get argument
const BufferArg& operator[](size_t num) const {
CHECK_LT(num, args_.size());
return *args_[num];
}

void addArg(BufferArg& arg) { args_.push_back(&arg); }

void addArg(SequenceIdArg& arg) { args_.push_back(&arg); }

void addArg(SequenceArg& arg) { args_.push_back(&arg); }

void addArg(SparseMatrixArg& arg) { args_.push_back(&arg); }

private:
std::vector<BufferArg*> args_;
// The BufferArg object is constructed and freed by BufferArgs.
std::vector<BufferArg*> _args_;
};

} // namespace paddle
2 changes: 2 additions & 0 deletions paddle/function/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ file(GLOB cpp_files . *Op.cpp)

list(APPEND h_files Function.h)
list(APPEND cpp_files Function.cpp)
list(APPEND h_files FunctionList.h FuncConfig.h BufferArgs.h)
list(APPEND cpp_files FunctionList.cpp)
list(APPEND cpp_files BufferArg.cpp)

if(WITH_GPU)
Expand Down
8 changes: 4 additions & 4 deletions paddle/function/ContextProjectionOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void ContextProjectionForward<DEVICE_TYPE_CPU>(CpuMatrix& out_mat,
template <DeviceType Device>
class ContextProjectionForwardFunc : public FunctionBase {
public:
void init(const FuncConfig& config) override {
void init(const function::Config& config) override {
context_length_ = config.get<size_t>("context_length");
context_start_ = config.get<int>("context_start");
begin_pad_ = config.get<size_t>("begin_pad");
Expand Down Expand Up @@ -224,7 +224,7 @@ void ContextProjectionBackward<DEVICE_TYPE_CPU>(const CpuMatrix& out_grad_mat,
template <DeviceType Device>
class ContextProjectionBackwardFunc : public FunctionBase {
public:
void init(const FuncConfig& config) override {
void init(const function::Config& config) override {
context_length_ = config.get<size_t>("context_length");
context_start_ = config.get<int>("context_start");
begin_pad_ = config.get<size_t>("begin_pad");
Expand Down Expand Up @@ -300,7 +300,7 @@ class ContextProjectionBackwardFunc : public FunctionBase {
template <DeviceType Device>
class ContextProjectionBackwardDataFunc : public FunctionBase {
public:
void init(const FuncConfig& config) override {
void init(const function::Config& config) override {
context_length_ = config.get<size_t>("context_length");
context_start_ = config.get<int>("context_start");
}
Expand Down Expand Up @@ -349,7 +349,7 @@ class ContextProjectionBackwardDataFunc : public FunctionBase {
template <DeviceType Device>
class ContextProjectionBackwardWeightFunc : public FunctionBase {
public:
void init(const FuncConfig& config) override {
void init(const function::Config& config) override {
context_length_ = config.get<size_t>("context_length");
context_start_ = config.get<int>("context_start");
begin_pad_ = config.get<size_t>("begin_pad");
Expand Down
4 changes: 2 additions & 2 deletions paddle/function/CosSimOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void CosSimForward<DEVICE_TYPE_CPU>(CpuMatrix& out_mat,

template <DeviceType Device>
class CosSimForwardFunc : public FunctionBase {
void init(const FuncConfig& config) override {
void init(const function::Config& config) override {
scale_ = config.get<real>("scale");
}

Expand Down Expand Up @@ -191,7 +191,7 @@ void CosSimBackward<DEVICE_TYPE_CPU>(const CpuMatrix& out_grad,
*/
template <DeviceType Device>
class CosSimBackwardFunc : public FunctionBase {
void init(const FuncConfig& config) override {
void init(const function::Config& config) override {
scale_ = config.get<real>("scale");
}

Expand Down
4 changes: 2 additions & 2 deletions paddle/function/CrossMapNormalOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void CrossMapNormalGrad<DEVICE_TYPE_CPU>(real* inputsGrad,
template <DeviceType Device>
class CrossMapNormalFunc : public FunctionBase {
public:
void init(const FuncConfig& config) override {
void init(const function::Config& config) override {
// function arguments
size_ = config.get<size_t>("size");
scale_ = config.get<real>("scale");
Expand Down Expand Up @@ -261,7 +261,7 @@ class CrossMapNormalFunc : public FunctionBase {
template <DeviceType Device>
class CrossMapNormalGradFunc : public FunctionBase {
public:
void init(const FuncConfig& config) override {
void init(const function::Config& config) override {
// function arguments
size_ = config.get<size_t>("size");
scale_ = config.get<real>("scale");
Expand Down
64 changes: 64 additions & 0 deletions paddle/function/FuncConfig.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 <string>
#include <unordered_map>
#include "paddle/utils/Any.h"
#include "paddle/utils/Error.h"

namespace paddle {
namespace function {

/**
* Function Configuration.
* The argument type of Function::init.
*/
class Config {
public:
template <typename T>
T get(const std::string& key, Error* err = nullptr) const {
try {
return any_cast<T>(valueMap_.at(key));
} catch (std::exception& e) { // could be cast or out of range exception.
if (err) {
*err = Error(e.what());
} else {
LOG(FATAL) << "Cannot get key " << key << " with error " << e.what();
}
return T();
}
}

template <typename T>
Config& set(const std::string& key, T v, Error* err = nullptr) {
auto it = valueMap_.find(key);
if (it != valueMap_.end()) { // already contains key.
if (err) {
*err = Error("Key %s is already set in FuncConfig", key.c_str());
} else {
LOG(FATAL) << "Key " << key << " is already set in FuncConfig.";
}
return *this;
}
valueMap_[key] = any(v);
return *this;
}

protected:
mutable std::unordered_map<std::string, any> valueMap_;
};
} // namespace function
} // namespace paddle
Loading