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

replace with flat_hash_map, small_vector #19

Merged
merged 3 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 21 additions & 13 deletions paddle/tcmpt/core/kernel_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

// See Note [ Why still include the fluid headers? ]
#include "paddle/fluid/platform/enforce.h"
#include "paddle/utils/flat_hash_map.h"
#include "paddle/utils/small_vector.h"

namespace pt {

Expand Down Expand Up @@ -209,25 +211,31 @@ class KernelArgsDef {
attribute_defs_.emplace_back(AttributeArgDef(type_index));
}

const std::vector<TensorArgDef>& input_defs() const { return input_defs_; }
const paddle::SmallVector<TensorArgDef>& input_defs() const {
return input_defs_;
}

const std::vector<TensorArgDef>& output_defs() const { return output_defs_; }
const paddle::SmallVector<TensorArgDef>& output_defs() const {
return output_defs_;
}

const std::vector<AttributeArgDef>& attribute_defs() const {
const paddle::SmallVector<AttributeArgDef>& attribute_defs() const {
return attribute_defs_;
}

std::vector<TensorArgDef>& input_defs() { return input_defs_; }
paddle::SmallVector<TensorArgDef>& input_defs() { return input_defs_; }

std::vector<TensorArgDef>& output_defs() { return output_defs_; }
paddle::SmallVector<TensorArgDef>& output_defs() { return output_defs_; }

std::vector<AttributeArgDef>& attribute_defs() { return attribute_defs_; }
paddle::SmallVector<AttributeArgDef>& attribute_defs() {
return attribute_defs_;
}

private:
// TODO(chenweihang): replaced by paddle::small_vector
Copy link
Owner

Choose a reason for hiding this comment

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

remove this TODO

Copy link
Author

Choose a reason for hiding this comment

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

Done, thanks.

std::vector<TensorArgDef> input_defs_{{}};
std::vector<TensorArgDef> output_defs_{{}};
std::vector<AttributeArgDef> attribute_defs_{{}};
paddle::SmallVector<TensorArgDef> input_defs_{{}};
paddle::SmallVector<TensorArgDef> output_defs_{{}};
paddle::SmallVector<AttributeArgDef> attribute_defs_{{}};
};

class Kernel {
Expand Down Expand Up @@ -263,10 +271,10 @@ class Kernel {
class KernelFactory {
public:
// replaced by paddle::flat_hash_map later
using KernelMap =
std::unordered_map<KernelName,
std::unordered_map<KernelKey, Kernel, KernelKey::Hash>,
KernelName::Hash>;
using KernelMap = paddle::flat_hash_map<
KernelName,
paddle::flat_hash_map<KernelKey, Kernel, KernelKey::Hash>,
KernelName::Hash>;

static KernelFactory& Instance();

Expand Down
12 changes: 7 additions & 5 deletions paddle/utils/small_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// 1. remove macro
// 2. remove LLVM_LIKELY and LLVM_UNLIKELY
// 3. add at(index) method for small vector
// 4. wrap the call to max and min with parenthesis to prevent the macro
// expansion to fix the build error on windows platform

//===- llvm/ADT/SmallVector.h - 'Normally small' vectors --------*- C++ -*-===//
//
Expand Down Expand Up @@ -90,7 +92,7 @@ class SmallVectorBase {

/// The maximum value of the Size_T used.
static constexpr size_t SizeTypeMax() {
return std::numeric_limits<Size_T>::max();
return (std::numeric_limits<Size_T>::max)();
}

SmallVectorBase() = delete;
Expand Down Expand Up @@ -309,7 +311,7 @@ class SmallVectorTemplateCommon

size_type size_in_bytes() const { return size() * sizeof(T); }
size_type max_size() const {
return std::min(this->SizeTypeMax(), size_type(-1) / sizeof(T));
return (std::min)(this->SizeTypeMax(), size_type(-1) / sizeof(T));
}

size_t capacity_in_bytes() const { return capacity() * sizeof(T); }
Expand Down Expand Up @@ -727,7 +729,7 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> {
}

// Assign over existing elements.
std::fill_n(this->begin(), std::min(NumElts, this->size()), Elt);
std::fill_n(this->begin(), (std::min)(NumElts, this->size()), Elt);
if (NumElts > this->size())
std::uninitialized_fill_n(this->end(), NumElts - this->size(), Elt);
else if (NumElts < this->size())
Expand Down Expand Up @@ -1393,7 +1395,7 @@ static void report_at_maximum_capacity(size_t MaxSize) {
// Note: Moving this function into the header may cause performance regression.
template <class Size_T>
static size_t getNewCapacity(size_t MinSize, size_t TSize, size_t OldCapacity) {
constexpr size_t MaxSize = std::numeric_limits<Size_T>::max();
constexpr size_t MaxSize = (std::numeric_limits<Size_T>::max)();

// Ensure we can fit the new capacity.
// This is only going to be applicable when the capacity is 32 bit.
Expand All @@ -1408,7 +1410,7 @@ static size_t getNewCapacity(size_t MinSize, size_t TSize, size_t OldCapacity) {
// In theory 2*capacity can overflow if the capacity is 64 bit, but the
// original capacity would never be large enough for this to be a problem.
size_t NewCapacity = 2 * OldCapacity + 1; // Always grow.
return std::min(std::max(NewCapacity, MinSize), MaxSize);
return (std::min)((std::max)(NewCapacity, MinSize), MaxSize);
}

// Note: Moving this function into the header may cause performance regression.
Expand Down