forked from PaddlePaddle/FastDeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reduce functions for FDTensor (PaddlePaddle#81)
* Add eigen tensor structure * Add Reduce function * Add cpp unittest framework * Add reduce function unittest * Add pr template * Add comments and docs for reduce function * Fix typo * Add ENABLE_FDTENSOR_FUNC macro * Add Todo comment * Add CheckData overload * Fix CheckData overload operator() Co-authored-by: Jason <[email protected]>
- Loading branch information
1 parent
9918374
commit c7d37b6
Showing
17 changed files
with
1,568 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!-- Demo: https://github.com/PaddlePaddle/Paddle/pull/24810 --> | ||
### PR types | ||
<!-- One of [ New features | Bug fixes | Function optimization | Performance optimization | Breaking changes | Others ] --> | ||
|
||
### PR changes | ||
<!-- One of [ OPs | APIs | Docs | Others ] --> | ||
|
||
### Describe | ||
<!-- Describe what this PR does --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2022 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 "fastdeploy/function/eigen.h" | ||
|
||
namespace fastdeploy { | ||
|
||
std::shared_ptr<EigenDeviceWrapper> EigenDeviceWrapper::instance_ = nullptr; | ||
|
||
std::shared_ptr<EigenDeviceWrapper> EigenDeviceWrapper::GetInstance() { | ||
if (instance_ == nullptr) { | ||
instance_ = std::make_shared<EigenDeviceWrapper>(); | ||
} | ||
return instance_; | ||
} | ||
|
||
const Eigen::DefaultDevice* EigenDeviceWrapper::GetDevice() const { | ||
return &device_; | ||
} | ||
|
||
} // namespace fastdeploy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright (c) 2022 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 <algorithm> | ||
#include <memory> | ||
#include <vector> | ||
#include "fastdeploy/core/fd_tensor.h" | ||
#include "unsupported/Eigen/CXX11/Tensor" | ||
|
||
namespace fastdeploy { | ||
// EigenDim converts shape into Eigen::DSizes. | ||
template <int D> | ||
struct EigenDim { | ||
using Type = Eigen::DSizes<Eigen::DenseIndex, D>; | ||
|
||
static Type From(const std::vector<int64_t>& dims) { | ||
Type ret; | ||
for (int64_t d = 0; d < dims.size(); d++) { | ||
ret[d] = dims[d]; | ||
} | ||
return ret; | ||
} | ||
}; | ||
|
||
// Interpret FDTensor as EigenTensor and EigenConstTensor. | ||
template <typename T, size_t D, int MajorType = Eigen::RowMajor, | ||
typename IndexType = Eigen::DenseIndex> | ||
struct EigenTensor { | ||
using Type = Eigen::TensorMap<Eigen::Tensor<T, D, MajorType, IndexType>>; | ||
|
||
using ConstType = | ||
Eigen::TensorMap<Eigen::Tensor<const T, D, MajorType, IndexType>>; | ||
|
||
static Type From(FDTensor& tensor, | ||
const std::vector<int64_t>& dims) { // NOLINT | ||
return Type(reinterpret_cast<T*>(tensor.Data()), EigenDim<D>::From(dims)); | ||
} | ||
|
||
static Type From(FDTensor& tensor) { // NOLINT | ||
return From(tensor, tensor.shape); | ||
} // NOLINT | ||
|
||
static ConstType From(const FDTensor& tensor, | ||
const std::vector<int64_t>& dims) { | ||
return ConstType(reinterpret_cast<const T*>(tensor.Data()), | ||
EigenDim<D>::From(dims)); | ||
} | ||
|
||
static ConstType From(const FDTensor& tensor) { | ||
return From(tensor, tensor.shape); | ||
} | ||
}; | ||
|
||
template <typename T, int MajorType = Eigen::RowMajor, | ||
typename IndexType = Eigen::DenseIndex> | ||
struct EigenScalar { | ||
// Scalar tensor (implemented as a rank-0 tensor) of scalar type T. | ||
using Type = Eigen::TensorMap< | ||
Eigen::TensorFixedSize<T, Eigen::Sizes<>, MajorType, IndexType>>; | ||
using ConstType = Eigen::TensorMap< | ||
Eigen::TensorFixedSize<const T, Eigen::Sizes<>, MajorType, IndexType>>; | ||
|
||
static Type From(FDTensor& tensor) { | ||
return Type(reinterpret_cast<T*>(tensor.Data())); | ||
} // NOLINT | ||
|
||
static ConstType From(const FDTensor& tensor) { | ||
return ConstType(reinterpret_cast<const T*>(tensor.Data())); | ||
} | ||
}; | ||
|
||
template <typename T, int MajorType = Eigen::RowMajor, | ||
typename IndexType = Eigen::DenseIndex> | ||
struct EigenVector : public EigenTensor<T, 1, MajorType, IndexType> { | ||
// Flatten reshapes a Tensor into an EigenVector. | ||
static typename EigenVector::Type Flatten(FDTensor& tensor) { // NOLINT | ||
return EigenVector::From(tensor, {tensor.Numel()}); | ||
} | ||
|
||
static typename EigenVector::ConstType Flatten( | ||
const FDTensor& tensor) { // NOLINT | ||
return EigenVector::From(tensor, {tensor.Numel()}); | ||
} | ||
}; | ||
|
||
class EigenDeviceWrapper { | ||
public: | ||
static std::shared_ptr<EigenDeviceWrapper> GetInstance(); | ||
const Eigen::DefaultDevice* GetDevice() const; | ||
|
||
private: | ||
Eigen::DefaultDevice device_; | ||
static std::shared_ptr<EigenDeviceWrapper> instance_; | ||
}; | ||
|
||
} // namespace fastdeploy |
Oops, something went wrong.