commit
7cd2417fe2
@ -0,0 +1,46 @@
|
|||||||
|
INCLUDE(ExternalProject)
|
||||||
|
|
||||||
|
set(XXHASH_SOURCE_DIR ${THIRD_PARTY_PATH}/xxhash)
|
||||||
|
set(XXHASH_INSTALL_DIR ${THIRD_PARTY_PATH}/install/xxhash)
|
||||||
|
set(XXHASH_INCLUDE_DIR "${XXHASH_INSTALL_DIR}/include")
|
||||||
|
|
||||||
|
IF(WITH_STATIC_LIB)
|
||||||
|
SET(BUILD_CMD make lib)
|
||||||
|
ELSE()
|
||||||
|
SET(BUILD_CMD sed -i "s/-Wstrict-prototypes -Wundef/-Wstrict-prototypes -Wundef -fPIC/g" ${XXHASH_SOURCE_DIR}/src/extern_xxhash/Makefile && make lib)
|
||||||
|
ENDIF()
|
||||||
|
|
||||||
|
ExternalProject_Add(
|
||||||
|
extern_xxhash
|
||||||
|
${EXTERNAL_PROJECT_LOG_ARGS}
|
||||||
|
GIT_REPOSITORY "https://github.com/Cyan4973/xxHash"
|
||||||
|
GIT_TAG "v0.6.5"
|
||||||
|
PREFIX ${XXHASH_SOURCE_DIR}
|
||||||
|
DOWNLOAD_NAME "xxhash"
|
||||||
|
UPDATE_COMMAND ""
|
||||||
|
CONFIGURE_COMMAND ""
|
||||||
|
BUILD_IN_SOURCE 1
|
||||||
|
PATCH_COMMAND
|
||||||
|
BUILD_COMMAND ${BUILD_CMD}
|
||||||
|
INSTALL_COMMAND export PREFIX=${XXHASH_INSTALL_DIR}/ && make install
|
||||||
|
TEST_COMMAND ""
|
||||||
|
)
|
||||||
|
|
||||||
|
set(XXHASH_LIBRARIES "${XXHASH_INSTALL_DIR}/lib/libxxhash.a")
|
||||||
|
INCLUDE_DIRECTORIES(${XXHASH_INCLUDE_DIR})
|
||||||
|
|
||||||
|
add_library(xxhash STATIC IMPORTED GLOBAL)
|
||||||
|
set_property(TARGET xxhash PROPERTY IMPORTED_LOCATION ${XXHASH_LIBRARIES})
|
||||||
|
include_directories(${XXHASH_INCLUDE_DIR})
|
||||||
|
add_dependencies(xxhash extern_xxhash)
|
||||||
|
|
||||||
|
LIST(APPEND external_project_dependencies xxhash)
|
||||||
|
|
||||||
|
IF(WITH_C_API)
|
||||||
|
INSTALL(DIRECTORY ${XXHASH_INCLUDE_DIR} DESTINATION third_party/xxhash)
|
||||||
|
IF(ANDROID)
|
||||||
|
INSTALL(FILES ${XXHASH_LIBRARIES} DESTINATION third_party/xxhash/lib/${ANDROID_ABI})
|
||||||
|
ELSE()
|
||||||
|
INSTALL(FILES ${XXHASH_LIBRARIES} DESTINATION third_party/xxhash/lib)
|
||||||
|
ENDIF()
|
||||||
|
ENDIF()
|
@ -0,0 +1,50 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
#include "paddle/fluid/inference/api/details/reset_tensor_array.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
// Should be called after the parameters are loaded.
|
||||||
|
void TensorArrayBatchCleaner::CollectTensorArrays(framework::Scope *scope) {
|
||||||
|
if (flag_) {
|
||||||
|
for (auto &var_name : scope->LocalVarNames()) {
|
||||||
|
auto *var = scope->FindVar(var_name);
|
||||||
|
// TODO(Superjomn) should avoid the case when a TensorArray is a
|
||||||
|
// parameter.
|
||||||
|
if (var_name == "feed" || var_name == "fetch") continue;
|
||||||
|
if (var->Type() == typeid(framework::LoDTensorArray)) {
|
||||||
|
VLOG(4) << "collect " << var_name;
|
||||||
|
arrays_.push_back(var->GetMutable<framework::LoDTensorArray>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (auto *kid : scope->kids()) {
|
||||||
|
CollectTensorArrays(kid);
|
||||||
|
}
|
||||||
|
|
||||||
|
VLOG(3) << "Collect " << arrays_.size() << " arrays";
|
||||||
|
flag_ = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should be called when `Run` finished.
|
||||||
|
void TensorArrayBatchCleaner::ResetTensorArray() {
|
||||||
|
for (auto *arr : arrays_) {
|
||||||
|
arr->clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
} // namespace paddle
|
@ -0,0 +1,37 @@
|
|||||||
|
// 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 <vector>
|
||||||
|
#include "paddle/fluid/framework/lod_tensor_array.h"
|
||||||
|
#include "paddle/fluid/framework/scope.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
// Clean the TensorArray each batch to make the behavior the same with the
|
||||||
|
// training phase.
|
||||||
|
struct TensorArrayBatchCleaner {
|
||||||
|
// Fix the tensor array not clear in the inference scenarios.
|
||||||
|
void CollectTensorArrays(framework::Scope *scope);
|
||||||
|
void ResetTensorArray();
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool flag_{true};
|
||||||
|
std::vector<framework::LoDTensorArray *> arrays_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
} // namespace paddle
|
@ -0,0 +1,74 @@
|
|||||||
|
/* 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. */
|
||||||
|
|
||||||
|
#include "paddle/fluid/operators/hash_op.h"
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace operators {
|
||||||
|
|
||||||
|
class HashOp : public framework::OperatorWithKernel {
|
||||||
|
public:
|
||||||
|
HashOp(const std::string &type, const framework::VariableNameMap &inputs,
|
||||||
|
const framework::VariableNameMap &outputs,
|
||||||
|
const framework::AttributeMap &attrs)
|
||||||
|
: OperatorWithKernel(type, inputs, outputs, attrs) {}
|
||||||
|
|
||||||
|
void InferShape(framework::InferShapeContext *ctx) const override {
|
||||||
|
PADDLE_ENFORCE(ctx->HasInput("X"),
|
||||||
|
"Input(X) of HashOp should not be null.");
|
||||||
|
PADDLE_ENFORCE(ctx->HasOutput("Out"),
|
||||||
|
"Output(Out) of HashOp should not be null.");
|
||||||
|
|
||||||
|
auto dims = ctx->GetInputDim("X");
|
||||||
|
PADDLE_ENFORCE_EQ(dims.size(), 2UL,
|
||||||
|
"The input of hash_op's dimensions must be 2");
|
||||||
|
std::vector<int64_t> out_dims;
|
||||||
|
out_dims.reserve(dims.size() + 1);
|
||||||
|
// copy all dims except the last one
|
||||||
|
for (size_t i = 0u; i != dims.size() - 1; ++i) {
|
||||||
|
out_dims.emplace_back(dims[i]);
|
||||||
|
}
|
||||||
|
int num_hash = ctx->Attrs().Get<int>("num_hash");
|
||||||
|
out_dims.emplace_back(num_hash);
|
||||||
|
// keep the last dim to 1
|
||||||
|
out_dims.emplace_back(1);
|
||||||
|
|
||||||
|
ctx->SetOutputDim("Out", framework::make_ddim(out_dims));
|
||||||
|
ctx->ShareLoD("X", /*->*/ "Out");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class HashOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||||
|
public:
|
||||||
|
void Make() override {
|
||||||
|
AddInput("X", "(Tensor) Input tensor of scale operator.");
|
||||||
|
AddOutput("Out", "(Tensor) Output tensor of scale operator.");
|
||||||
|
AddComment(R"DOC(
|
||||||
|
**Hash Operator**
|
||||||
|
$$Out = scale * X$$
|
||||||
|
)DOC");
|
||||||
|
AddAttr<int>("num_hash", "").SetDefault(1);
|
||||||
|
AddAttr<int>("mod_by", "").SetDefault(100000);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace operators
|
||||||
|
} // namespace paddle
|
||||||
|
|
||||||
|
namespace ops = paddle::operators;
|
||||||
|
|
||||||
|
REGISTER_OP_WITHOUT_GRADIENT(hash, ops::HashOp, ops::HashOpMaker);
|
||||||
|
REGISTER_OP_CPU_KERNEL(hash, ops::HashKerel<int>, ops::HashKerel<int64_t>);
|
@ -0,0 +1,56 @@
|
|||||||
|
/* 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. */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include <xxhash.h>
|
||||||
|
}
|
||||||
|
#include "paddle/fluid/framework/eigen.h"
|
||||||
|
#include "paddle/fluid/framework/op_registry.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace operators {
|
||||||
|
// template <typename DeviceContext, typename T>
|
||||||
|
template <typename T>
|
||||||
|
class HashKerel : public framework::OpKernel<T> {
|
||||||
|
public:
|
||||||
|
virtual void Compute(const framework::ExecutionContext& context) const {
|
||||||
|
auto* out_t = context.Output<framework::LoDTensor>("Out");
|
||||||
|
auto* in_t = context.Input<framework::LoDTensor>("X");
|
||||||
|
int mod_by = context.Attr<int>("mod_by");
|
||||||
|
int num_hash = context.Attr<int>("num_hash");
|
||||||
|
auto* output = out_t->mutable_data<T>(context.GetPlace());
|
||||||
|
|
||||||
|
auto in_dims = in_t->dims();
|
||||||
|
auto in_lod = in_t->lod();
|
||||||
|
PADDLE_ENFORCE_EQ(
|
||||||
|
static_cast<uint64_t>(in_dims[0]), in_lod[0].back(),
|
||||||
|
"The actual input data's size mismatched with LoD information.");
|
||||||
|
|
||||||
|
auto seq_length = in_dims[0];
|
||||||
|
auto last_dim = in_dims[in_dims.size() - 1];
|
||||||
|
auto* input = in_t->data<T>();
|
||||||
|
for (int idx = 0; idx < seq_length; ++idx) {
|
||||||
|
for (int ihash = 0; ihash != num_hash; ++ihash) {
|
||||||
|
output[idx * num_hash + ihash] =
|
||||||
|
XXH64(input, sizeof(int) * last_dim, ihash) % mod_by;
|
||||||
|
}
|
||||||
|
input += last_dim;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace operators
|
||||||
|
} // namespace paddle
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue