-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[infer] Add Bloom inference policy and replaced methods #4553
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
yuanheng-zhao
merged 17 commits into
hpcaitech:feature/colossal-inference
from
yuanheng-zhao:colossal-inference/bloom
Aug 30, 2023
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7ea5d51
add bloom inference methods and policy
yuanheng-zhao 5f2e841
enable pass BatchInferState from model forward
yuanheng-zhao 686e56e
revise bloom infer layers/policies
yuanheng-zhao 0cbb251
add engine for inference (draft)
yuanheng-zhao 0ec07ca
add test for bloom infer
yuanheng-zhao 960eea3
fix bloom infer policy and flow
yuanheng-zhao f511cb1
revise bloom test
yuanheng-zhao 41a3bf5
fix bloom file path
yuanheng-zhao 77674d1
remove unused codes
yuanheng-zhao 87498cf
merge branch feature/colossal-inference
yuanheng-zhao cb88328
fix bloom modeling
yuanheng-zhao df40671
fix dir typo
yuanheng-zhao 1e4ecbb
fix trivial
yuanheng-zhao 63bcb94
fix policy
yuanheng-zhao 274fbf5
clean pr
yuanheng-zhao 25a9a21
trivial fix
yuanheng-zhao 474c45d
trivial
yuanheng-zhao 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,4 @@ | ||
| from .modeling.llama import LlamaInferenceForwards | ||
| from .pollcies.llama import LlamaModelInferPolicy | ||
| from .engine import TPInferEngine | ||
| from .kvcache_manager import MemoryManager | ||
| __all__ = ['LlamaInferenceForwards', 'LlamaModelInferPolicy', 'MemoryManager', 'TPInferEngine'] | ||
|
|
||
| __all__ = ['MemoryManager', 'TPInferEngine'] | ||
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| from .bloom import BloomInferenceForwards | ||
| from .llama import LlamaInferenceForwards | ||
|
|
||
| __all__ = ['LlamaInferenceForwards'] | ||
| __all__ = ['BloomInferenceForwards', 'LlamaInferenceForwards'] |
Large diffs are not rendered by default.
Oops, something went wrong.
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,4 @@ | ||
| from .bloom import BloomModelInferPolicy | ||
| from .llama import LlamaModelInferPolicy | ||
|
|
||
| __all__ = ['BloomModelInferPolicy', 'LlamaModelInferPolicy'] |
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,44 @@ | ||
| from colossalai.shardformer.policies.bloom import BloomForCausalLMPolicy | ||
|
|
||
| from ..modeling.bloom import BloomInferenceForwards | ||
|
|
||
|
|
||
| class BloomModelInferPolicy(BloomForCausalLMPolicy): | ||
|
|
||
| def __init__(self) -> None: | ||
| super().__init__() | ||
|
|
||
| def module_policy(self): | ||
| from transformers.models.bloom.modeling_bloom import BloomAttention, BloomBlock, BloomForCausalLM, BloomModel | ||
| policy = super().module_policy() | ||
| # NOTE set inference mode to shard config | ||
| self.shard_config._infer() | ||
|
|
||
| if self.shard_config.enable_tensor_parallelism: | ||
|
|
||
| method_replacement = { | ||
| 'forward': | ||
| BloomInferenceForwards.bloom_for_causal_lm_forward, | ||
| 'prepare_inputs_for_generation': | ||
| BloomInferenceForwards.bloom_for_causal_lm_prepare_inputs_for_generation | ||
| } | ||
| self.append_or_create_method_replacement(description=method_replacement, | ||
| policy=policy, | ||
| target_key=BloomForCausalLM) | ||
|
|
||
| method_replacement = {'forward': BloomInferenceForwards.bloom_model_forward} | ||
| self.append_or_create_method_replacement(description=method_replacement, | ||
| policy=policy, | ||
| target_key=BloomModel) | ||
|
|
||
| method_replacement = {'forward': BloomInferenceForwards.bloom_block_forward} | ||
| self.append_or_create_method_replacement(description=method_replacement, | ||
| policy=policy, | ||
| target_key=BloomBlock) | ||
|
|
||
| method_replacement = {'forward': BloomInferenceForwards.bloom_attention_forward} | ||
| self.append_or_create_method_replacement(description=method_replacement, | ||
| policy=policy, | ||
| target_key=BloomAttention) | ||
|
|
||
| return policy |
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 was deleted.
Oops, something went wrong.
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,60 @@ | ||
| import pytest | ||
| import torch | ||
| import torch.distributed as dist | ||
| from transformers import AutoModelForCausalLM, AutoTokenizer, BloomForCausalLM | ||
|
|
||
| import colossalai | ||
| from colossalai.inference.tensor_parallel import TPInferEngine | ||
| from colossalai.logging import disable_existing_loggers | ||
| from colossalai.shardformer import ShardConfig, ShardFormer | ||
| from colossalai.testing import clear_cache_before_run, rerun_if_address_is_in_use, spawn | ||
|
|
||
| TP_SIZE = 2 | ||
| MAX_BATCH_SIZE = 4 | ||
| MAX_INPUT_LEN = 16 | ||
| MAX_OUTPUT_LEN = 32 | ||
|
|
||
|
|
||
| def run(): | ||
|
|
||
| model_path = "/data3/data/model_eval_for_commerical_use/phoenix-inst-chat-7b" | ||
| tokenizer = AutoTokenizer.from_pretrained(model_path) | ||
| tokenizer.pad_token = tokenizer.eos_token | ||
|
|
||
| text = "Introduce some landmarks in Beijing" | ||
| input_ids = tokenizer.batch_encode_plus([text], return_tensors='pt') | ||
|
|
||
| model = BloomForCausalLM.from_pretrained(model_path, pad_token_id=tokenizer.eos_token_id) | ||
| model = model.half() | ||
| model.to(torch.cuda.current_device()) | ||
|
|
||
| shard_config = ShardConfig(enable_tensor_parallelism=True, inference_only=True) | ||
| shardformer = ShardFormer(shard_config=shard_config) | ||
|
|
||
| infer_engine = TPInferEngine(model, MAX_BATCH_SIZE, MAX_INPUT_LEN, MAX_OUTPUT_LEN) | ||
| infer_engine.prepare_with_shard_config(shard_config=shard_config) | ||
| infer_engine.shard_model_by(shardformer) | ||
|
|
||
| generate_kwargs = dict(do_sample=False) | ||
| outputs = infer_engine.generate(input_ids, generate_kwargs) | ||
|
|
||
| if not dist.is_initialized() or dist.get_rank() == 0: | ||
| output_text = tokenizer.decode(outputs[0]) | ||
| print(output_text) | ||
|
|
||
|
|
||
| def check_engine(rank, world_size, port): | ||
| disable_existing_loggers() | ||
| colossalai.launch(config={}, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl') | ||
| run() | ||
|
|
||
|
|
||
| @pytest.mark.dist | ||
| @rerun_if_address_is_in_use() | ||
| @clear_cache_before_run() | ||
| def test_engine_infer(): | ||
| spawn(check_engine, TP_SIZE) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| test_engine_infer() |
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.