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

[Cherry-pick][Framework] Support custom allocator (#10013) #10017

Merged
merged 1 commit into from
Feb 23, 2023

Conversation

shentanyue
Copy link
Collaborator

PR devices

Framework

PR types

New features

PR changes

API

Description

新增外部 API 用于设置 Allocator,其中 PaddleLite 框架传入默认 alignment 值为64,用于Byte alignment,用户可自定义。

使用方法:


void* allocfunc(size_t size, size_t alignment) {
  size_t offset = sizeof(void*) + alignment - 1;
  char* p = static_cast<char*>(std::malloc(offset + size));
  // Byte alignment
  void* r = reinterpret_cast<void*>(reinterpret_cast<size_t>(p + offset) &
                                    (~(alignment - 1)));
  static_cast<void**>(r)[-1] = p;
  return r;
}

void freefunc(void* ptr) {
  if (ptr) {
    std::free(static_cast<void**>(ptr)[-1]);
  }
}

paddle::lite_api::MobileConfig mobile_config;
paddle::lite_api::CustomAllocator custom_allocator;
custom_allocator.alloc = allocfunc;
custom_allocator.free = freefunc;
mobile_config.set_custom_allocator(TARGET(kHost), custom_allocator);

Copy link
Collaborator

@hong19860320 hong19860320 left a comment

Choose a reason for hiding this comment

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

LGTM

@hong19860320 hong19860320 merged commit 5004867 into PaddlePaddle:release/v2.12 Feb 23, 2023
@shentanyue shentanyue deleted the allocator_2_12 branch February 23, 2023 03:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants