Skip to content

Comments

[onert] Implement buffer sharing optimization for BulkPipeline#16349

Merged
chunseoklee merged 1 commit intoSamsung:masterfrom
batcheu:implement_buffer_sharing_for_bulkpipeline
Jan 29, 2026
Merged

[onert] Implement buffer sharing optimization for BulkPipeline#16349
chunseoklee merged 1 commit intoSamsung:masterfrom
batcheu:implement_buffer_sharing_for_bulkpipeline

Conversation

@batcheu
Copy link
Contributor

@batcheu batcheu commented Jan 21, 2026

This commit adds buffer sharing mechanism to reduce memory usage in bulk pipeline execution.
Link models for async buffer preparation and optimize execution performance when models have identical program and weight sizes.

ONE-DCO-1.0-Signed-off-by: Jonghwa Lee jonghwa3.lee@samsung.com


This commit adds buffer sharing mechanism to reduce memory usage
in bulk pipeline execution.
Link models for async buffer preparation and optimize execution
performance when models have identical program and weight sizes.

ONE-DCO-1.0-Signed-off-by: Jonghwa Lee <jonghwa3.lee@samsung.com>
BulkPipelineModel(const std::string &model_path, int device_id);
enum class BufferOwnership
{
OWNER,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note for reviewers

BufferOwnership shows whther the buffer is shared (=BufferOwnerShip::SHARED) or maintained directly (=BufferOwnerShip::OWNER).

}
}

void BulkPipelineManager::linkModels()
Copy link
Contributor Author

@batcheu batcheu Jan 21, 2026

Choose a reason for hiding this comment

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

Note for reviewers

The linked models will share program and weight buffers.
For example, if we have 6 models will be executed in serial, then the linkage between models can be like,

  • 6 models : [model_0, model_1, model_2, model_3, model_4, model_5]
  • First linkage : model_0 -> model_2 -> model_4
  • Seconde linkage : model_1 -> model_3 -> model_5

And model_0, model_1 will have BufferOwnership::OWNER buffers.

if (model_idx++ < _config.n_owner_models)
{
// First n_shared_owner_models models are OWNERS
continue;
Copy link
Contributor

Choose a reason for hiding this comment

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

If _use_buffer_sharing becomes true, it seems that the first model and other models are all the same.
But if n_owner_models is 2, is it intended to have 2 owners?
I think only the first should be owner and the rest should be shared.
(If I misunderstood, please correct it. 😅 )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the first 2 models are OWNER.
As I wrote in below (https://github.com/Samsung/ONE/pull/16349/files#r2710531814) model_0 and model_1 will have OWNER buffers and other models in the same linkage will share them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for reviewing ;)

Copy link
Contributor

@ys44kim ys44kim left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@hseok-oh hseok-oh left a comment

Choose a reason for hiding this comment

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

LGTM

{
openDevice();
allocateBuffers();
fillBuffers();
Copy link
Contributor

Choose a reason for hiding this comment

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

In fillBuffers(), _fp can be leaked if an exception is thrown.
Could you please add logic to check and close _fp before throwing an exception to avoid a file handle leak?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

_fp will be closed on release() method which is called in exception handler and destructor.

if (_fp)
{
fclose(_fp);
_fp = nullptr;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for your information. I missed release().
But, there is no release() in startAsyncBufferFill(). Could you check it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMHO, the failure in startAsyncBufferFill() should be handled in caller side.
In that case, release() can be called explictly by BulkPipelineManager which has BulkPipelineModel.
I'll update the error handling BulkPipelineManager code in another PR too.

Comment on lines +203 to +211
try
{
fillBuffers();
markBufferReady();
}
catch (const std::exception &e)
{
std::cerr << "Failed to fill buffers asynchronously: " << e.what() << std::endl;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

_buffer_ready can leave permanently false if fillBuffers() throws before markBufferReady(). In other words, waitForBufferReady() may block forever.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's possibly right. If you don't mind I'll upload a separate PR to fix it.

Copy link
Contributor

@ragmani ragmani Jan 28, 2026

Choose a reason for hiding this comment

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

Thanks. I hope it gets fixed in a separate PR.

@batcheu
Copy link
Contributor Author

batcheu commented Jan 27, 2026

@ragmani
Thanks for your detailed review ;)
I left the comments, please check if it is the answer to the question.

Copy link
Contributor

@ragmani ragmani left a comment

Choose a reason for hiding this comment

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

LGTM

@chunseoklee chunseoklee merged commit 83f476d into Samsung:master Jan 29, 2026
10 checks passed
@batcheu batcheu deleted the implement_buffer_sharing_for_bulkpipeline branch February 2, 2026 07:52
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.

5 participants