commit
85c3bfc1f9
@ -0,0 +1,42 @@
|
||||
if (NOT WITH_ANAKIN)
|
||||
return()
|
||||
endif()
|
||||
|
||||
set(ANAKIN_INSTALL_DIR "${THIRD_PARTY_PATH}/install/anakin" CACHE PATH
|
||||
"Anakin install path." FORCE)
|
||||
set(ANAKIN_INCLUDE "${ANAKIN_INSTALL_DIR}" CACHE STRING "root of Anakin header files")
|
||||
set(ANAKIN_LIBRARY "${ANAKIN_INSTALL_DIR}" CACHE STRING "path of Anakin library")
|
||||
|
||||
set(ANAKIN_COMPILE_EXTRA_FLAGS -Wno-error=unused-variable -Wno-error=format-extra-args -Wno-error=comment -Wno-error=format -Wno-error=switch -Wno-error=return-type -Wno-error=non-virtual-dtor -Wno-reorder -Wno-error=cpp)
|
||||
|
||||
set(ANAKIN_LIBRARY_URL "https://github.com/pangge/Anakin/releases/download/3.0/anakin_release_simple.tar.gz")
|
||||
|
||||
# A helper function used in Anakin, currently, to use it, one need to recursively include
|
||||
# nearly all the header files.
|
||||
function(fetch_include_recursively root_dir)
|
||||
if (IS_DIRECTORY ${root_dir})
|
||||
include_directories(${root_dir})
|
||||
endif()
|
||||
|
||||
file(GLOB ALL_SUB RELATIVE ${root_dir} ${root_dir}/*)
|
||||
foreach(sub ${ALL_SUB})
|
||||
if (IS_DIRECTORY ${root_dir}/${sub})
|
||||
fetch_include_recursively(${root_dir}/${sub})
|
||||
endif()
|
||||
endforeach()
|
||||
endfunction()
|
||||
|
||||
# download library
|
||||
message(STATUS "Download Anakin library from ${ANAKIN_LIBRARY_URL}")
|
||||
execute_process(COMMAND bash -c "mkdir -p ${ANAKIN_INSTALL_DIR}")
|
||||
execute_process(COMMAND bash -c "rm -rf ${ANAKIN_INSTALL_DIR}/*")
|
||||
execute_process(COMMAND bash -c "cd ${ANAKIN_INSTALL_DIR}; wget -q ${ANAKIN_LIBRARY_URL}")
|
||||
execute_process(COMMAND bash -c "mkdir -p ${ANAKIN_INSTALL_DIR}")
|
||||
execute_process(COMMAND bash -c "cd ${ANAKIN_INSTALL_DIR}; tar xzf anakin_release_simple.tar.gz")
|
||||
|
||||
if (WITH_ANAKIN)
|
||||
message(STATUS "Anakin for inference is enabled")
|
||||
message(STATUS "Anakin is set INCLUDE:${ANAKIN_INCLUDE} LIBRARY:${ANAKIN_LIBRARY}")
|
||||
fetch_include_recursively(${ANAKIN_INCLUDE})
|
||||
link_directories(${ANAKIN_LIBRARY})
|
||||
endif()
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,25 @@
|
||||
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
if(APPLE)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=pessimizing-move")
|
||||
endif(APPLE)
|
||||
|
||||
cc_library(tape_variable SRCS variable.cc DEPS ${FLUID_CORE_MODULES})
|
||||
cc_library(tape SRCS tape.cc DEPS ${FLUID_CORE_MODULES} ${GLOB_OP_LIB} tape_variable)
|
||||
|
||||
cc_test(test_tape
|
||||
SRCS test_tape.cc
|
||||
DEPS tape tape_variable)
|
File diff suppressed because it is too large
Load Diff
After Width: | Height: | Size: 94 KiB |
@ -0,0 +1,131 @@
|
||||
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// 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 "paddle/contrib/tape/tape.h"
|
||||
#include "paddle/contrib/tape/variable.h"
|
||||
#include "paddle/fluid/framework/type_defs.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace tape {
|
||||
|
||||
class Function {};
|
||||
|
||||
class Fill {
|
||||
public:
|
||||
Fill(const std::string &initializer, const framework::AttributeMap &attrs)
|
||||
: initializer_(initializer), attrs_(attrs) {}
|
||||
|
||||
void operator()(VariableHandle var) {
|
||||
get_global_tape().AddOp(initializer_, {}, {{"Out", {var}}}, attrs_);
|
||||
}
|
||||
|
||||
private:
|
||||
const std::string initializer_;
|
||||
const framework::AttributeMap attrs_;
|
||||
};
|
||||
|
||||
class Mean {
|
||||
public:
|
||||
VariableHandle operator()(VariableHandle var) {
|
||||
VariableHandle out(new Variable("mean"));
|
||||
get_global_tape().AddOp("mean", {{"X", {var}}}, {{"Out", {out}}}, {});
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
class Linear {
|
||||
public:
|
||||
Linear(int in_dim, int out_dim, const std::string &act)
|
||||
: w_(new Variable("LinearWeight")),
|
||||
b_(new Variable("LinearBias")),
|
||||
act_(act) {
|
||||
Tape init_tape;
|
||||
|
||||
std::string initializer = "fill_constant";
|
||||
framework::AttributeMap attrs;
|
||||
attrs["dtype"] = paddle::framework::proto::VarType::Type::VarType_Type_FP32;
|
||||
attrs["shape"] = std::vector<int>{in_dim, out_dim};
|
||||
attrs["value"] = 1.0f;
|
||||
init_tape.AddOp(initializer, {}, {{"Out", {w_}}}, attrs);
|
||||
|
||||
attrs["dtype"] = paddle::framework::proto::VarType::Type::VarType_Type_FP32;
|
||||
attrs["shape"] = std::vector<int>{out_dim};
|
||||
attrs["value"] = 1.0f;
|
||||
init_tape.AddOp(initializer, {}, {{"Out", {b_}}}, attrs);
|
||||
|
||||
init_tape.Forward();
|
||||
}
|
||||
|
||||
VariableHandle operator()(VariableHandle input) {
|
||||
VariableHandle pre_bias(new Variable("linear"));
|
||||
get_global_tape().AddOp("mul",
|
||||
{{"X", {input}}, {"Y", {w_}}},
|
||||
{{"Out", {pre_bias}}},
|
||||
{{"x_num_col_dims", 1}, {"y_num_col_dims", 1}});
|
||||
VariableHandle pre_act(new Variable("linear"));
|
||||
get_global_tape().AddOp("elementwise_add",
|
||||
{{"X", {pre_bias}}, {"Y", {b_}}},
|
||||
{{"Out", {pre_act}}},
|
||||
{{"axis", 1}});
|
||||
VariableHandle post_act(new Variable("linear"));
|
||||
get_global_tape().AddOp(
|
||||
act_, {{"X", {pre_act}}}, {{"Out", {post_act}}}, {});
|
||||
return post_act;
|
||||
}
|
||||
|
||||
std::vector<VariableHandle> Params() { return {w_, b_}; }
|
||||
|
||||
private:
|
||||
VariableHandle w_;
|
||||
VariableHandle b_;
|
||||
std::string act_;
|
||||
};
|
||||
|
||||
class SGD {
|
||||
public:
|
||||
SGD(float learning_rate) : learning_rate_(new Variable("sgd")) {
|
||||
Tape init_tape;
|
||||
|
||||
std::string initializer = "fill_constant";
|
||||
framework::AttributeMap attrs;
|
||||
attrs["dtype"] = paddle::framework::proto::VarType::Type::VarType_Type_FP32;
|
||||
attrs["shape"] = std::vector<int>{1};
|
||||
attrs["value"] = learning_rate;
|
||||
init_tape.AddOp(initializer, {}, {{"Out", {learning_rate_}}}, attrs);
|
||||
|
||||
init_tape.Forward();
|
||||
}
|
||||
|
||||
void operator()(VariableHandle input) {
|
||||
PADDLE_ENFORCE(get_global_tape().HasBeenBackwarded(),
|
||||
"optimization must happen after the backward");
|
||||
Tape temp_tape;
|
||||
temp_tape.AddOp("sgd",
|
||||
{{"Param", {input}},
|
||||
{"LearningRate", {learning_rate_}},
|
||||
{"Grad", {input->Grad()}}},
|
||||
{{"ParamOut", {input}}},
|
||||
{});
|
||||
temp_tape.Forward();
|
||||
}
|
||||
|
||||
private:
|
||||
VariableHandle learning_rate_;
|
||||
};
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,64 @@
|
||||
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// 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 <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "paddle/contrib/tape/variable.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace tape {
|
||||
|
||||
using VariableHandleMap = std::map<std::string, std::vector<VariableHandle>>;
|
||||
|
||||
struct OpHandle {
|
||||
OpHandle(const std::string &type,
|
||||
const VariableHandleMap &in_vars,
|
||||
const VariableHandleMap &out_vars,
|
||||
const framework::AttributeMap &attrs)
|
||||
: type_(type), inputs_(in_vars), outputs_(out_vars), attrs_(attrs) {}
|
||||
|
||||
std::string type_;
|
||||
VariableHandleMap inputs_;
|
||||
VariableHandleMap outputs_;
|
||||
framework::AttributeMap attrs_;
|
||||
};
|
||||
|
||||
class Tape {
|
||||
public:
|
||||
void AddOp(const std::string &type,
|
||||
const VariableHandleMap &in_vars,
|
||||
VariableHandleMap out_vars,
|
||||
const framework::AttributeMap &attrs);
|
||||
void Forward();
|
||||
void Backward(VariableHandle target);
|
||||
|
||||
bool HasBeenBackwarded() { return has_been_backwarded_; }
|
||||
|
||||
private:
|
||||
bool has_been_backwarded_ = false;
|
||||
size_t current_position_ = 0;
|
||||
|
||||
std::vector<OpHandle> tape_;
|
||||
std::shared_ptr<Tape> backward_tape_;
|
||||
};
|
||||
|
||||
Tape &get_global_tape();
|
||||
|
||||
void reset_global_tape();
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue