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

[Allocator] Enable EVAllocator to speedup EmbeddingVariable performance. #171

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions tensorflow/core/framework/embedding/embedding_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class EmbeddingVar : public ResourceBase {
} else {
emb_config_.default_value_dim = default_value_dim;
value_len_ = default_tensor.NumElements()/emb_config_.default_value_dim;
default_value_ = TypedAllocator::Allocate<V>(alloc_, default_tensor.NumElements(), AllocationAttributes());
default_value_ = TypedAllocator::Allocate<V>(cpu_allocator(),
default_tensor.NumElements(), AllocationAttributes());
auto default_tensor_flat = default_tensor.flat<V>();
memcpy(default_value_, &default_tensor_flat(0), default_tensor.TotalBytes());
if (LayoutType::NORMAL_CONTIGUOUS == storage_manager_->GetLayoutType()) {
Expand Down Expand Up @@ -269,7 +270,7 @@ class EmbeddingVar : public ResourceBase {
Destroy();
delete storage_manager_;
}
TypedAllocator::Deallocate(alloc_, default_value_, value_len_);
TypedAllocator::Deallocate(cpu_allocator(), default_value_, value_len_);
}
TF_DISALLOW_COPY_AND_ASSIGN(EmbeddingVar);
};
Expand Down
19 changes: 10 additions & 9 deletions tensorflow/core/framework/ev_allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,11 @@ class EVAllocator : public Allocator {
<< "% of system memory.";
}

alignment = 8;
void* p = port::AlignedMalloc(num_bytes, alignment);
// support 4B no fragment allocation.
alignment = (num_bytes <= 4) ? 4 : 8;
void* p = impl_.Allocate(num_bytes);
if (ev_allocator_collect_stats) {
const std::size_t alloc_size = port::MallocExtension_GetAllocatedSize(p);
const std::size_t alloc_size = impl_.AllocatedSize(p);
mutex_lock l(mu_);
++stats_.num_allocs;
stats_.bytes_in_use += alloc_size;
Expand All @@ -457,7 +458,6 @@ class EVAllocator : public Allocator {
return p;
}

/*
size_t BatchAllocateRaw(size_t num, size_t alignment,
size_t num_bytes, void** ret) override {
if (num_bytes > LargeAllocationWarningBytes() &&
Expand Down Expand Up @@ -495,18 +495,17 @@ class EVAllocator : public Allocator {
}
}
return allocated_num;
}*/
}

void DeallocateRaw(void* ptr) override {
if (ev_allocator_collect_stats) {
const std::size_t alloc_size =
port::MallocExtension_GetAllocatedSize(ptr);
const std::size_t alloc_size = impl_.AllocatedSize(ptr);

mutex_lock l(mu_);
stats_.bytes_in_use -= alloc_size;
}

port::AlignedFree(ptr);
impl_.Deallocate(ptr);
}

absl::optional<AllocatorStats> GetStats() override {
Expand All @@ -522,7 +521,7 @@ class EVAllocator : public Allocator {
}

size_t AllocatedSizeSlow(const void* ptr) const override {
return port::MallocExtension_GetAllocatedSize(ptr);
return impl_.AllocatedSize(ptr);
}

private:
Expand All @@ -534,6 +533,8 @@ class EVAllocator : public Allocator {
std::atomic<int> single_allocation_warning_count_;
int total_allocation_warning_count_ GUARDED_BY(mu_);

EVAllocatorImpl impl_;

TF_DISALLOW_COPY_AND_ASSIGN(EVAllocator);
};

Expand Down