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

[Framework] Support custom allocator #10013

Merged

Conversation

shentanyue
Copy link
Collaborator

@shentanyue shentanyue commented Feb 20, 2023

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);

@paddle-bot
Copy link

paddle-bot bot commented Feb 20, 2023

Thanks for your contribution!

lite/api/paddle_api.cc Outdated Show resolved Hide resolved
lite/api/paddle_api.h Outdated Show resolved Hide resolved
lite/api/paddle_place.h Outdated Show resolved Hide resolved
lite/api/paddle_api.cc Outdated Show resolved Hide resolved
lite/core/memory.cc Outdated Show resolved Hide resolved
@shentanyue shentanyue changed the title [Framework] Support external allocator [Framework] Support custom allocator Feb 20, 2023
lite/core/memory.cc Outdated Show resolved Hide resolved
lite/api/paddle_place.h Outdated Show resolved Hide resolved
CustomAllocator GetCustomAllocator() { return custom_allocator_; }

private:
CustomAllocator custom_allocator_;
Copy link
Collaborator

Choose a reason for hiding this comment

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

是否有必要进行初始化?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

不需要初始化,用的Lazy Singleton,实际使用的时候才初始化。

Copy link
Collaborator Author

@shentanyue shentanyue Feb 21, 2023

Choose a reason for hiding this comment

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

CustomAllocator本身也已经初始化

struct LITE_API CustomAllocator {
  void* (*alloc)(size_t size, size_t alignment) = nullptr;
  void (*free)(void* ptr) = nullptr;
};

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

@shentanyue shentanyue merged commit cde8d2a into PaddlePaddle:develop Feb 22, 2023
shentanyue added a commit to shentanyue/Paddle-Lite that referenced this pull request Feb 22, 2023
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