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

[LLM] Support gpt3 fine grained dybatch v1 #7080

Merged
merged 10 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions llm/predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ def _preprocess(self, source):
self.architectures,
top_p=self.config.top_p,
temperature=self.config.temperature,
benchmark=self.config.benchmark,
)
for i in range(inputs["input_ids"].shape[0]):
length = inputs["seq_len_encoder"][i][0]
Expand All @@ -375,6 +376,7 @@ def _preprocess(self, source):
self.architectures,
top_p=self.config.top_p,
temperature=self.config.temperature,
benchmark=self.config.benchmark,
)
for i in range(inputs["input_ids"].shape[0]):
length = inputs["seq_len_encoder"][i][0]
Expand Down Expand Up @@ -439,6 +441,7 @@ def _preprocess(self, source):
top_p=self.config.top_p,
temperature=self.config.temperature,
pre_caches_length=pre_caches_length,
benchmark=self.config.benchmark,
)

for i in range(inputs["input_ids"].shape[0]):
Expand Down Expand Up @@ -664,8 +667,6 @@ def create_predictor(
LlamaForCausalLMInferenceModel as LlamaInferenceModel,
)

config.tensor_parallel_degree = tensor_parallel_degree
config.tensor_parallel_rank = tensor_parallel_rank
yuanlehome marked this conversation as resolved.
Show resolved Hide resolved
config.quant_bits = -1

if predictor_args.quant_type.startswith("weight_only_int"):
Expand All @@ -692,15 +693,26 @@ def create_predictor(
BloomForCausalLMInferenceModel,
)

config.tensor_parallel_degree = tensor_parallel_degree
config.tensor_parallel_rank = tensor_parallel_rank
model = BloomForCausalLMInferenceModel.from_pretrained(
predictor_args.model_name_or_path,
config=config,
dtype=predictor_args.dtype,
)
cache_kvs_shape = BloomForCausalLMInferenceModel.get_cache_kvs_shape(config, predictor_args.batch_size)
model.eval()
elif "gpt" in config.architectures[0].lower():
from paddlenlp.experimental.transformers import (
GPTForCausalLMInferenceModel,
)

model = GPTForCausalLMInferenceModel.from_pretrained(
predictor_args.model_name_or_path,
config=config,
dtype=predictor_args.dtype,
)
model.eval()
else:
raise ValueError("the `model type` should be one of [llama, chatglm, bloom, gpt]")
predictor = DygraphInferencePredictor(predictor_args, model=model, tokenizer=tokenizer)
elif predictor_args.mode == "static":
config = AutoConfig.from_pretrained(predictor_args.model_name_or_path)
Expand All @@ -710,7 +722,6 @@ def create_predictor(
)

cache_kvs_shape = LlamaForCausalLMInferenceModel.get_cache_kvs_shape(config, predictor_args.batch_size)
predictor = StaticInferencePredictor(predictor_args, cache_kvs_shape, tokenizer=tokenizer)
elif "chatglm" in config.architectures[0].lower():
from paddlenlp.experimental.transformers import (
ChatGLMForCausalLMInferenceModel,
Expand All @@ -719,16 +730,21 @@ def create_predictor(
cache_kvs_shape = ChatGLMForCausalLMInferenceModel.get_cache_kvs_shape(
config, predictor_args.batch_size
)
predictor = StaticInferencePredictor(predictor_args, cache_kvs_shape, tokenizer=tokenizer)
elif "bloom" in config.architectures[0].lower():
from paddlenlp.experimental.transformers import (
BloomForCausalLMInferenceModel,
)

cache_kvs_shape = BloomForCausalLMInferenceModel.get_cache_kvs_shape(config, predictor_args.batch_size)
predictor = StaticInferencePredictor(
predictor_args, cache_kvs_shape=cache_kvs_shape, tokenizer=tokenizer
elif "gpt" in config.architectures[0].lower():
from paddlenlp.experimental.transformers import (
GPTForCausalLMInferenceModel,
)

cache_kvs_shape = GPTForCausalLMInferenceModel.get_cache_kvs_shape(config, predictor_args.batch_size)
else:
raise ValueError("the `model type` should be one of [llama, chatglm, bloom, gpt]")
predictor = StaticInferencePredictor(predictor_args, cache_kvs_shape, tokenizer=tokenizer)
else:
raise ValueError("the `mode` should be one of [dynamic, static]")
return predictor
Expand Down
31 changes: 28 additions & 3 deletions llm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ def dybatch_preprocess(
benchmark: bool = False,
):
"""Pre-process generation inputs."""
inputs = {}
if "chatglm" in architectures:
input_ids = []
position_ids = []
Expand All @@ -403,7 +404,6 @@ def dybatch_preprocess(
input_ids.append(tokens["input_ids"][0])
position_ids.append(tokens["position_ids"][0])

inputs = {}
pad_token_id = tokenizer([tokenizer.pad_token], return_tensors="np")["input_ids"][0][0]
inputs["input_ids"], seq_len = pad_batch_data(input_ids, pad_id=pad_token_id, return_seq_len=True)
bs = inputs["input_ids"].shape[0]
Expand All @@ -413,9 +413,35 @@ def dybatch_preprocess(
for i in range(len(position_ids)):
inst_data_pos.append(np.array([list(inst) + [0] * (max_len - len(inst)) for inst in position_ids[i]]))
inputs["position_ids"] = paddle.to_tensor(np.array(inst_data_pos))
elif "gpt" in architectures:
input_ids = []
if isinstance(texts, str):
texts = [texts]

for text in texts:
tokens = tokenizer(
text,
return_tensors="np",
padding=False,
max_length=src_length,
return_attention_mask=False,
return_token_type_ids=False,
)
input_ids.append(tokens["input_ids"][0])

pad_token_id = tokenizer([tokenizer.pad_token], return_tensors="np")["input_ids"][0][-1]
inputs["input_ids"], seq_len = pad_batch_data(input_ids, pad_id=pad_token_id, return_seq_len=True)
bs = inputs["input_ids"].shape[0]
max_len = max(map(len, input_ids))

position_ids = paddle.arange(sum(seq_len), dtype="int64")
pre_len = seq_len[0]
for length in seq_len[1:]:
position_ids[pre_len : length + pre_len] = position_ids[pre_len : length + pre_len] - pre_len
pre_len += length
inputs["position_ids"] = position_ids
else:
input_ids = []
position_ids = []
if isinstance(texts, str):
texts = [texts]

Expand All @@ -430,7 +456,6 @@ def dybatch_preprocess(
)
input_ids.append(tokens["input_ids"][0])

inputs = {}
pad_token_id = tokenizer([tokenizer.pad_token], return_tensors="np")["input_ids"][0][-1]
inputs["input_ids"], seq_len = pad_batch_data(input_ids, pad_id=pad_token_id, return_seq_len=True)
bs = inputs["input_ids"].shape[0]
Expand Down
1 change: 1 addition & 0 deletions paddlenlp/experimental/transformers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
from .bloom import *
from .chatglm import *
from .fused_transformer_layers import *
from .gpt import *

Check warning on line 18 in paddlenlp/experimental/transformers/__init__.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/experimental/transformers/__init__.py#L18

Added line #L18 was not covered by tests
from .llama import *
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@
self.norm_type = norm_type
if norm_type == "layernorm":
self.norm_func = fused_layer_norm
else:
elif norm_type == "rmsnorm":

Check warning on line 189 in paddlenlp/experimental/transformers/fused_transformer_layers.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/experimental/transformers/fused_transformer_layers.py#L189

Added line #L189 was not covered by tests
self.norm_func = fused_rms_norm
else:
raise NotImplementedError("Only support norm type of [layernorm, rmsnorm]")

Check warning on line 192 in paddlenlp/experimental/transformers/fused_transformer_layers.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/experimental/transformers/fused_transformer_layers.py#L192

Added line #L192 was not covered by tests
self.use_neox_rotary_style = use_neox_rotary_style
self._norm_weight_dtype = "float32" if self.norm_type == "layernorm" else self._dtype

Expand Down
2 changes: 2 additions & 0 deletions paddlenlp/experimental/transformers/generation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@
shape=[None, None, None], dtype="int64", name="position_ids"
) # position_ids
input_spec[16] = paddle.static.InputSpec(shape=[None, 2, 1], dtype="int64", name="tgt_pos") # tgt_pos
elif self.config["model_type"] and "gpt" in self.config.model_type:
input_spec[2] = paddle.static.InputSpec(shape=[None], dtype="int64", name="position_ids") # position_ids

Check warning on line 119 in paddlenlp/experimental/transformers/generation_utils.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/experimental/transformers/generation_utils.py#L118-L119

Added lines #L118 - L119 were not covered by tests
model = paddle.jit.to_static(self.generate, input_spec=input_spec)
paddle.jit.save(
model, output_path, skip_prune_program=True
Expand Down
15 changes: 15 additions & 0 deletions paddlenlp/experimental/transformers/gpt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .modeling import *

Check warning on line 15 in paddlenlp/experimental/transformers/gpt/__init__.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/experimental/transformers/gpt/__init__.py#L15

Added line #L15 was not covered by tests
Loading