mlx-swift-examples icon indicating copy to clipboard operation
mlx-swift-examples copied to clipboard

some models fail to prepare tokens: No chat template

Open davidkoski opened this issue 1 year ago • 4 comments

Model loaded -> id("mlx-community/phi-2-hf-4bit-mlx")
Error: chatTemplate("No chat template was specified")

For models that have a chat template this is fine, but for those that do not:

  • I don't see a way to probe for the presence of a chat template
  • nor a way to provide a default

I think we need to do the config-mutate path and inject a template where needed.

davidkoski avatar Nov 01 '24 21:11 davidkoski

Base models don't usually have a chat template. Is there something like https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/generate.py#L213C1-L214C1 in Swift Transformers?

awni avatar Nov 01 '24 23:11 awni

Base models don't usually have a chat template. Is there something like https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/generate.py#L213C1-L214C1 in Swift Transformers?

No, nothing like that: https://github.com/huggingface/swift-transformers/blob/main/Sources/Tokenizers/Tokenizer.swift#L359

Perhaps there should be? @pcuenca @maiqingqiang

davidkoski avatar Nov 01 '24 23:11 davidkoski

Base models don't usually have a chat template. Is there something like https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/generate.py#L213C1-L214C1 in Swift Transformers?

No, nothing like that: https://github.com/huggingface/swift-transformers/blob/main/Sources/Tokenizers/Tokenizer.swift#L359

Perhaps there should be? @pcuenca @maiqingqiang

I agree. Similar to https://github.com/huggingface/transformers/blob/33868a057c02f0368ba63bd1edb746be38fe3d90/src/transformers/tokenization_utils_base.py#L1628-L1629

Alternatively, it could capture throw TokenizerError.chatTemplate and use tokenizer.encode instead.

johnmai-dev avatar Nov 02 '24 05:11 johnmai-dev

Per above I added a workaround -- it will try to apply the template, but if that throws an error it will fall back to a built in template and tokenization.

davidkoski avatar Dec 16 '24 16:12 davidkoski

We now have proper support for this as of #288

davidkoski avatar May 12 '25 21:05 davidkoski