Port phi3 to TF
Author: a8nova
Pull request: yes
Labels: name: TensorFlow

name: Text

# What does this PR do?
This PR ports phi3 to Tensorflow
<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Fixes # (issue)


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- text models: @ArthurZucker and @younesbelkada
- vision models: @amyeroberts
- speech models: @sanchit-gandhi
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @gante
- pipelines: @Narsil
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @pacman100

Integrations:

- deepspeed: HF Trainer/Accelerate: @pacman100
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc and @younesbelkada

Documentation: @stevhliu and @MKhalusova

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @sanchit-gandhi
- PyTorch: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->


Comments:
@merronmuche You will be porting this :)

Hi @Rocketknight1! I tried creating a gpt4 draft using the prompt you shared with me but it wasn't translating all the classes, not sure why, did you have to translate all the classes one by one? Do you mind creating a TF draught for phi3? Thank you!

Hey @a8nova, I'm sorry I lost track of this! There's actually been some changes - we're currently trying to deprioritize adding new TF models to Transformers, in favour of improving KerasNLP cross-compatibility instead.

The reason for this is that we don't think we can cleanly upgrade Transformers to Keras 3 in place, because we need to maintain backward compatibility - a lot of TF users are production users and that means they can be slow to update!

Unfortunately, that means that TF models added to Transformers will be stuck on Keras 2 even as it becomes obsolete. Instead, we're collaborating with the Keras team on an integration with KerasNLP instead - it fully supports Keras 3, and it can now [save/load checkpoints on the Hub](https://x.com/fchollet/status/1806413337816858661) in the same format as Transformers!

If you're interested, I can check with the KerasNLP team, and maybe help with opening KerasNLP PRs to port the work you've done so far to KerasNLP?

Hi @Rocketknight1 - Good to hear from you! Thank you for letting me know.

One reason I was adding TF models to Transformers was so i can then easily (optimize &) convert them to tflite for on-device execution. I think adding these models to KerasNLP could mean that I can easily go from KerasNLP (using keras3 under the hood) -> tflite? If this is true, then yes i can try porting the work done so far to KerasNLP, do you know how much work that will be? There is also a new python library [AI Edge Torch ](https://github.com/google-ai-edge/ai-edge-torch) that lets you go from pytorch -> tflite though it is experimental and limited, most of the models I converted don't work or the converter itself OOMs for 3B+ mdoels! 

Also, should I close the Gemma PR https://github.com/huggingface/transformers/pull/29400 since there is no point of merging it into master if it is going to be stuck on keras 2?

@a8nova yes! Once a model is in Keras 3, you can load it as a TF, JAX or PyTorch model, so you should be able to get a TFLite conversion easily.

Also, yes, I think you can close the Gemma PR. KerasNLP now supports loading Gemma models from HF, and @ariG23498 is working on support for the very recent Gemma-2 models right now. 

You're actually one of the first active contributors to try this area, so if you try out TFLite conversion of a HF model via KerasNLP, or you want to move any of the work you did on TF/Keras ports to KerasNLP, please let us know! We're definitely interested in your feedback, or supporting moving those PRs.

Sounds good @Rocketknight1! Let me play with the KeradNLP api and get back to you. 

On a different note, are there any beginner level bugs you can point me too? I want my intern to be familiar with transformers and want to start with a simple bug, I checked the issues page but most seem a little involved.. thanks!

closing this PR in favor of keras-nlp