You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
What is the right way to load Qwen2's chat interface? #1040
custom_template="""{%- if tools %}\n {{- '<|im_start|>system\\n' }}\n {%- if messages[0]['role'] == 'system' %}\n {{- messages[0]['content'] }}\n {%- else %}\n {{- 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.' }}\n {%- endif %}\n {{- \"\\n\\n# Tools\\n\\nYou may call one or more functions to assist with the user query.\\n\\nYou are provided with function signatures within <tools></tools> XML tags:\\n<tools>\" }}\n {%- for tool in tools %}\n {{- \"\\n\" }}\n {{- tool | tojson }}\n {%- endfor %}\n {{- \"\\n</tools>\\n\\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\\n<tool_call>\\n{\\\"name\\\": <function-name>, \\\"arguments\\\": <args-json-object>}\\n</tool_call><|im_end|>\\n\" }}\n{%- else %}\n {%- if messages[0]['role'] == 'system' %}\n {{- '<|im_start|>system\\n' + messages[0]['content'] + '<|im_end|>\\n' }}\n {%- else %}\n {{- '<|im_start|>system\\nYou are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>\\n' }}\n {%- endif %}\n{%- endif %}\n{%- for message in messages %}\n {%- if (message.role == \"user\") or (message.role == \"system\" and not loop.first) or (message.role == \"assistant\" and not message.tool_calls) %}\n {{- '<|im_start|>' + message.role + '\\n' + message.content + '<|im_end|>' + '\\n' }}\n {%- elif message.role == \"assistant\" %}\n {{- '<|im_start|>' + message.role }}\n {%- if message.content %}\n {{- '\\n' + message.content }}\n {%- endif %}\n {%- for tool_call in message.tool_calls %}\n {%- if tool_call.function is defined %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n {{- '\\n<tool_call>\\n{\"name\": \"' }}\n {{- tool_call.name }}\n {{- '\", \"arguments\": ' }}\n {{- tool_call.arguments | tojson }}\n {{- '}\\n</tool_call>' }}\n {%- endfor %}\n {{- '<|im_end|>\\n' }}\n {%- elif message.role == \"tool\" %}\n {%- if (loop.index0 == 0) or (messages[loop.index0 - 1].role != \"tool\") %}\n {{- '<|im_start|>user' }}\n {%- endif %}\n {{- '\\n<tool_response>\\n' }}\n {{- message.content }}\n {{- '\\n</tool_response>' }}\n {%- if loop.last or (messages[loop.index0 + 1].role != \"tool\") %}\n {{- '<|im_end|>\\n' }}\n {%- endif %}\n {%- endif %}\n{%- endfor %}\n{%- if add_generation_prompt %}\n {{- '<|im_start|>assistant\\n' }}\n{%- endif %}\n"""
custom_eos_token = "eos_token"
tokenizer = get_chat_template(
tokenizer,
chat_template = (custom_template, custom_eos_token),
mapping = {"role" : "from", "content" : "value", "user" : "human", "assistant" : "gpt"}, # ShareGPT style
)
Since the eos token seems to be hard coded in the template (it might not be a good practice, since it may be tokenized differently), I'm not sur of what to do of the custom_eos_token.
only thing I don't understand is why when I do inference the code automatically prints to the screen. This will slow down my code a lot. How do I stop it?
deftest_unsloth_inference_efficient(
max_length: int=8192,
use_4bit: bool=False,
):
""" interence notebook: https://colab.research.google.com/drive/1aqlNQi7MMJbynFDyOQteD2t0yVfjb9Zh?usp=sharing """print('--> Running: test_unsloth_inference_efficient')
importosfromtransformersimportAutoTokenizerfromunslothimportFastLanguageModelfromunsloth.chat_templatesimportget_chat_templatemodel_name=os.path.expanduser('~/data/runs/09192024_12h35m27s_run/train/checkpoint-820')
print(f'{model_name=}')
model: FastLanguageModeltokenizer: AutoTokenizermodel, tokenizer=FastLanguageModel.from_pretrained(
model_name=model_name,
max_seq_length=max_length,
dtype=None, # Auto-detection for Float16/BFloat16load_in_4bit=use_4bit,
)
print(f'{type(test_unsloth_inference)=}')
FastLanguageModel.for_inference(model) # Enable native 2x faster inferenceprompt="Continue the fibonnaci sequence for 1 step only please: 1, 1, 2, 3, 5, 8,"print('prompt: ', prompt)
messages= [
{"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
{"role": "user", "content": prompt}
]
inputs=tokenizer.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_tensors='pt'
).to(model.device)
print(f'{inputs=}')
# res = model.generate(**inputs, max_new_tokens=64)res=model.generate(inputs, max_new_tokens=64)
print(f'{len(res)=}')
completion: str=tokenizer.decode(res[0])
print(f'{completion=}')
no need to return the text, we can just return the tensors immediately and only at the end return the text. I also removed the text streamer that prints to stdout and I dont need that.
I am interested in figuring out how to do batch inferences with unsloth instead than 1 prompt at a time.
Activity
brando90 commentedon Sep 19, 2024
https://discord.com/channels/1179035537009545276/1286411100379938931
brando90 commentedon Sep 19, 2024
maybe this?
Trapper4888 commentedon Sep 20, 2024
Maybe try to change this:
To this:
Since the eos token seems to be hard coded in the template (it might not be a good practice, since it may be tokenized differently), I'm not sur of what to do of the custom_eos_token.
danielhanchen commentedon Sep 21, 2024
I'll look over this again in the weekend - sorry on the delay!!
brando90 commentedon Sep 25, 2024
let us know how it's going or if you need help! @danielhanchen
brando90 commentedon Sep 26, 2024
@danielhanchen I think I got it to work with this:
only thing I don't understand is why when I do inference the code automatically prints to the screen. This will slow down my code a lot. How do I stop it?
brando90 commentedon Sep 26, 2024
ref: https://discord.com/channels/1179035537009545276/1288946827688939591
brando90 commentedon Sep 26, 2024
still does it even if one removes the completions statement:
brando90 commentedon Sep 26, 2024
ok this stoped the stdout
brando90 commentedon Sep 26, 2024
remove HF text streamer
brando90 commentedon Sep 26, 2024
slightly more efficient:
no need to return the text, we can just return the tensors immediately and only at the end return the text. I also removed the text streamer that prints to stdout and I dont need that.
I am interested in figuring out how to do batch inferences with unsloth instead than 1 prompt at a time.
brando90 commentedon Sep 27, 2024
For now this is working, though vLLM is not.
brando90 commentedon Sep 27, 2024
TODO: how to extend my code for multiple generations, right now I've only made it work for 1 as seen by
res[0]danielhanchen commentedon Oct 1, 2024
@brando90 So sorry on the delay!! On the Qwen 2.5 chat template - you can use
qwen2.5vLLM I think I answered in the other thread
For batch inference, you need to use a list of strings