-
Notifications
You must be signed in to change notification settings - Fork 328
Introduce support for dynamic batching #1662
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
859fe1b
Introduce support for dynamic batching
baijumeswani 4b100eb
Fix windows pipelines
baijumeswani 0440794
Support paged attention model
baijumeswani 5ab4cd2
Fix pipelines
baijumeswani dc284da
Address pull-request review comments
baijumeswani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #include "../generators.h" | ||
| #include "block.h" | ||
|
|
||
| #include <numeric> | ||
| #include <algorithm> | ||
|
|
||
| namespace Generators { | ||
|
|
||
| Block::Block(size_t id, size_t slots, size_t block_size) | ||
| : id_(id), size_(slots), capacity_(block_size) {} | ||
|
|
||
| size_t Block::Id() const { | ||
| return id_; | ||
| } | ||
|
|
||
| size_t Block::Size() const { | ||
| return size_; | ||
| } | ||
|
|
||
|
baijumeswani marked this conversation as resolved.
|
||
| bool Block::IsFull() const { | ||
| return Size() == Capacity(); | ||
| } | ||
|
|
||
| size_t Block::EmptySlots() const { | ||
| return Capacity() - Size(); | ||
| } | ||
|
|
||
| size_t Block::Capacity() const { | ||
| return capacity_; | ||
| } | ||
|
|
||
| void Block::AddSlot() { | ||
| if (IsFull()) { | ||
| throw std::runtime_error("Cannot add a slot. The block is full."); | ||
| } | ||
|
|
||
| size_++; | ||
| } | ||
|
|
||
| std::vector<size_t> Block::SlotIds() const { | ||
| std::vector<size_t> slot_ids(Size(), 0); | ||
| std::iota(slot_ids.begin(), slot_ids.end(), Id() * Capacity()); | ||
| return slot_ids; | ||
| } | ||
|
|
||
| BlockPool::BlockPool(size_t block_size, size_t num_blocks) | ||
| : block_size_(block_size), capacity_(num_blocks) {} | ||
|
|
||
| std::vector<std::shared_ptr<Block>> BlockPool::AllocateBlocks(size_t num_slots) { | ||
| const auto allocate_block = [this](size_t num_slots) { | ||
| for (size_t i = 0; i < Capacity(); ++i) { | ||
| if (blocks_[i] == nullptr) { | ||
| blocks_[i] = std::make_shared<Block>(i, num_slots, block_size_); | ||
| return blocks_[i]; | ||
| } | ||
| } | ||
| return std::shared_ptr<Block>(); | ||
| }; | ||
|
|
||
| if (BlocksNeeded(num_slots) > AvailableBlocks()) { | ||
| throw std::runtime_error("Requested number of blocks " + std::to_string(BlocksNeeded(num_slots)) + | ||
| " for number of slots " + std::to_string(num_slots) + | ||
| " exceeds available blocks " + std::to_string(AvailableBlocks()) + "."); | ||
| } | ||
|
|
||
| std::vector<std::shared_ptr<Block>> allocated_blocks; | ||
| for (size_t i = 0; i < num_slots; i += block_size_) { | ||
| auto block = allocate_block(std::min(block_size_, num_slots - i)); | ||
| if (!block) { | ||
| throw std::runtime_error("Failed to allocate a block."); | ||
| } | ||
| allocated_blocks.push_back(block); | ||
| } | ||
| return allocated_blocks; | ||
| } | ||
|
|
||
| void BlockPool::Free(const std::vector<std::shared_ptr<Block>>& blocks) { | ||
| for (const auto& block : blocks) { | ||
| blocks_[block->Id()].reset(); | ||
| } | ||
| } | ||
|
|
||
| size_t BlockPool::AvailableBlocks() const { | ||
| return std::count_if(blocks_.begin(), blocks_.end(), [](const std::shared_ptr<Block>& block) { return block == nullptr; }); | ||
| } | ||
|
|
||
| size_t BlockPool::Size() const { | ||
| return Capacity() - AvailableBlocks(); | ||
| } | ||
|
|
||
| size_t BlockPool::Capacity() const { | ||
| return capacity_; | ||
| } | ||
|
|
||
| size_t BlockPool::BlocksNeeded(size_t num_slots) { | ||
| return (num_slots + block_size_ - 1) / block_size_; | ||
| } | ||
|
|
||
| } // namespace Generators | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| namespace Generators { | ||
|
|
||
| /* | ||
| * Block represents a contiguous set of slots in the paged key-value cache. | ||
| * Each block has a fixed capacity (number of slots it can hold) and tracks | ||
| * the number of currently used slots. | ||
| */ | ||
| struct Block { | ||
| Block(size_t id, size_t slots, size_t block_size); | ||
|
|
||
| size_t Id() const; | ||
|
|
||
| size_t Size() const; | ||
|
|
||
| bool IsFull() const; | ||
|
|
||
| size_t Capacity() const; | ||
|
|
||
| size_t EmptySlots() const; | ||
|
|
||
| void AddSlot(); | ||
|
|
||
| std::vector<size_t> SlotIds() const; | ||
|
|
||
| private: | ||
| size_t id_; | ||
| size_t size_; | ||
| size_t capacity_; | ||
| }; | ||
|
|
||
| /* | ||
| * BlockPool manages a pool of blocks for the paged key-value cache. | ||
| * It allows allocation and deallocation of blocks, and keeps track | ||
| * of the total capacity and currently available blocks. | ||
| */ | ||
| struct BlockPool { | ||
| BlockPool(size_t block_size, size_t num_blocks); | ||
|
|
||
| size_t AvailableBlocks() const; | ||
|
|
||
| size_t Size() const; | ||
|
|
||
| size_t Capacity() const; | ||
|
|
||
| std::vector<std::shared_ptr<Block>> AllocateBlocks(size_t num_slots); | ||
|
|
||
| void Free(const std::vector<std::shared_ptr<Block>>& blocks); | ||
|
|
||
| size_t BlocksNeeded(size_t num_slots); | ||
|
|
||
| private: | ||
| const size_t block_size_; | ||
| const size_t capacity_; | ||
| std::vector<std::shared_ptr<Block>> blocks_{capacity_}; | ||
| }; | ||
|
|
||
| } // namespace Generators |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.