Skip to content

Commit

Permalink
[Major] shortened context for edge device
Browse files Browse the repository at this point in the history
  • Loading branch information
ys-2020 committed Sep 26, 2023
1 parent ca11f3e commit 5efd875
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
16 changes: 13 additions & 3 deletions tinychat/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ def stream_output(output_stream):
action="store_true",
help="enable mem_efficient_load mod",
)
parser.add_argument(
"--single_round",
action="store_true",
help="whether to memorize previous conversations",
)

args = parser.parse_args()
assert args.model_type.lower() in [
Expand Down Expand Up @@ -201,11 +206,15 @@ def skip(*args, **kwargs):
# Optimize AWQ quantized model
if args.precision == "W4A16" and args.model_type.lower() == "llama":
from tinychat.modules import make_quant_norm, make_quant_attn, make_fused_mlp

make_quant_attn(model, args.device)
make_quant_norm(model)
make_fused_mlp(model)
model_prompter = get_prompter(args.model_type, args.model_path)

if args.max_seq_len <= 1024:
short_prompt = True
else:
short_prompt = False
model_prompter = get_prompter(args.model_type, args.model_path, short_prompt)
stop_token_ids = get_stop_token_ids(args.model_type, args.model_path)
count = 0
while True:
Expand All @@ -224,5 +233,6 @@ def skip(*args, **kwargs):
stop_token_ids=stop_token_ids,
)
outputs = stream_output(output_stream)
model_prompter.update_template(outputs)
if args.single_round is not True and args.max_seq_len > 512: # Only memorize previous conversations when kv_cache_size > 512
model_prompter.update_template(outputs)
count += 1
38 changes: 23 additions & 15 deletions tinychat/utils/prompt_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,33 @@ def __init__(self):


class Llama2Prompter(OneShotBasePrompter):
def __init__(self):
def __init__(self, short_prompt=False):
system_inst = "A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions."
role1 = "### Human"
role2 = "### Assistant"
sen_spliter = "\n"
qa_spliter = "</s>"
user_example = "Got any creative ideas for a 10 year old's birthday?"
assistant_example = (
"Of course! Here are some creative ideas for a 10-year-old's birthday party:\n"
+ "1. Treasure Hunt: Organize a treasure hunt in your backyard or nearby park. Create clues and riddles for the kids to solve, leading them to hidden treasures and surprises.\n"
+ "2. Science Party: Plan a science-themed party where kids can engage in fun and interactive experiments. You can set up different stations with activities like making slime, erupting volcanoes, or creating simple chemical reactions.\n"
+ "3. Outdoor Movie Night: Set up a backyard movie night with a projector and a large screen or white sheet. Create a cozy seating area with blankets and pillows, and serve popcorn and snacks while the kids enjoy a favorite movie under the stars.\n"
+ "4. DIY Crafts Party: Arrange a craft party where kids can unleash their creativity. Provide a variety of craft supplies like beads, paints, and fabrics, and let them create their own unique masterpieces to take home as party favors.\n"
+ "5. Sports Olympics: Host a mini Olympics event with various sports and games. Set up different stations for activities like sack races, relay races, basketball shooting, and obstacle courses. Give out medals or certificates to the participants.\n"
+ "6. Cooking Party: Have a cooking-themed party where the kids can prepare their own mini pizzas, cupcakes, or cookies. Provide toppings, frosting, and decorating supplies, and let them get hands-on in the kitchen.\n"
+ "7. Superhero Training Camp: Create a superhero-themed party where the kids can engage in fun training activities. Set up an obstacle course, have them design their own superhero capes or masks, and organize superhero-themed games and challenges.\n"
+ "8. Outdoor Adventure: Plan an outdoor adventure party at a local park or nature reserve. Arrange activities like hiking, nature scavenger hunts, or a picnic with games. Encourage exploration and appreciation for the outdoors.\n"
+ "Remember to tailor the activities to the birthday child's interests and preferences. Have a great celebration!"
)
if short_prompt:
assistant_example = (
"Of course! Here are some creative ideas for a 10-year-old's birthday party:\n"
+ "1. Treasure Hunt: Organize a treasure hunt in your backyard or nearby park. Create clues and riddles for the kids to solve, leading them to hidden treasures and surprises.\n"
+ "2. Science Party: Plan a science-themed party where kids can engage in fun and interactive experiments. You can set up different stations with activities like making slime, erupting volcanoes, or creating simple chemical reactions.\n"
+ "Remember to tailor the activities to the birthday child's interests and preferences. Have a great celebration!"
)
else:
assistant_example = (
"Of course! Here are some creative ideas for a 10-year-old's birthday party:\n"
+ "1. Treasure Hunt: Organize a treasure hunt in your backyard or nearby park. Create clues and riddles for the kids to solve, leading them to hidden treasures and surprises.\n"
+ "2. Science Party: Plan a science-themed party where kids can engage in fun and interactive experiments. You can set up different stations with activities like making slime, erupting volcanoes, or creating simple chemical reactions.\n"
+ "3. Outdoor Movie Night: Set up a backyard movie night with a projector and a large screen or white sheet. Create a cozy seating area with blankets and pillows, and serve popcorn and snacks while the kids enjoy a favorite movie under the stars.\n"
+ "4. DIY Crafts Party: Arrange a craft party where kids can unleash their creativity. Provide a variety of craft supplies like beads, paints, and fabrics, and let them create their own unique masterpieces to take home as party favors.\n"
+ "5. Sports Olympics: Host a mini Olympics event with various sports and games. Set up different stations for activities like sack races, relay races, basketball shooting, and obstacle courses. Give out medals or certificates to the participants.\n"
+ "6. Cooking Party: Have a cooking-themed party where the kids can prepare their own mini pizzas, cupcakes, or cookies. Provide toppings, frosting, and decorating supplies, and let them get hands-on in the kitchen.\n"
+ "7. Superhero Training Camp: Create a superhero-themed party where the kids can engage in fun training activities. Set up an obstacle course, have them design their own superhero capes or masks, and organize superhero-themed games and challenges.\n"
+ "8. Outdoor Adventure: Plan an outdoor adventure party at a local park or nature reserve. Arrange activities like hiking, nature scavenger hunts, or a picnic with games. Encourage exploration and appreciation for the outdoors.\n"
+ "Remember to tailor the activities to the birthday child's interests and preferences. Have a great celebration!"
)
oneshot_example = [user_example, assistant_example]
super().__init__(
oneshot_example, system_inst, role1, role2, sen_spliter, qa_spliter
Expand Down Expand Up @@ -182,12 +190,12 @@ def __init__(self):
super().__init__(system_inst, role1, role2, sen_spliter, qa_spliter, decorator)


def get_prompter(model_type, model_path=""):
def get_prompter(model_type, model_path="", short_prompt=False):
if model_type.lower() == "llama":
if "vicuna" in model_path:
return VicunaPrompter()
else:
return Llama2Prompter()
return Llama2Prompter(short_prompt)
elif model_type.lower() == "falcon":
# return FalconPrompter()
return FalconSimplePrompter()
Expand Down

0 comments on commit 5efd875

Please sign in to comment.