Model implmenetation using Liger Kernel layers
Author: helloworld1
Pull request: no
Labels: name: Feature request

### Feature request

In the new and existing model implementation,check if Liger Kernel and `use_liger_kernel` flag is on is installed and provide an alternative and more efficient efficient layer implementation. This moves the monkey-patch process in Liger-Kernel to transformes library.

```
self.norm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps) if not is_liger_kernel_enabled() else LigerLlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
```


### Motivation

Transformer is changing model implementation between releases. Liger kernel monkey patch process has ran into compatiblity issues multiple times.
Example: https://github.com/linkedin/Liger-Kernel/issues/723

I would like to make Liger Kernel as part of transformers model implementation to ensure the stability of efficient layers being used. This will also give modeller ability to use efficient kernels during modelling.

### Your contribution

I can help PR the change

Comments:
This could be a good opportunity to add it to [huggingface/kernels](https://github.com/huggingface/kernels), maybe? cc @Narsil @mekkcyber

Hi @helloworld1! Yes I totally agree, we are planning to do the same thing using the `kernels` library. I already started adding some `liger kernels` here : https://huggingface.co/kernels-community/liger_kernels

Excellent work @MekkCyber ! Currently your code copied the kernels from liger kernel. Since Liger Kernel is evolving, it would be best to depend on liger-kernel library in HF kernels. We can work on the Liger kernel side change to make it easy for HF kernels to consume the kernel / layers.

Yes sounds good @helloworld1 ! Let me know if you need any help with the process

@MekkCyber In the HF kernel's guide (https://github.com/huggingface/kernels/blob/main/docs/kernel-requirements.md), it said

  No dynamic library dependencies outside:

  Only modules from the Python standard library, Torch, or the kernel itself can be imported.

I wonder what is the recommendation of reusing / exposing liger-kernel's library and make it usable as HF kernels? We don't want to copy the kernels in both HF repo and Liger-kernel github repo.



Also, I think PyTorch core now wishes to accept most of these optimizations into core:
- fused rms (almost merged)
- rope (there was an okay for inclusion in core)
- fused linear CE (also there was an okay)

@vadimkantorov Thanks for the info. Is there any reference / threads regarding those kernels in pytorch? Would be interested to help to facilitate it also.

- https://github.com/pytorch/pytorch/pull/153666
- https://github.com/pytorch/pytorch/issues/149534#issuecomment-2738010453
- https://github.com/pytorch/torchtune/pull/2741#issuecomment-2992624831

- https://github.com/pytorch/pytorch/issues/139908
- https://github.com/pytorch/pytorch/pull/158536

Hi @helloworld1 sorry for this late response, 

Triton kernels are straightforward to use via the kernels library, so you don't need to worry about managing extra dependencies.

Regarding Liger kernels—or any custom kernels—the goal is to establish a single source of truth that contributors can rely on and improve. In the long term, we envision centralizing this on the Hub in different organizations (the liger kernels would be hosted by your org, for example). This would allow updates, bug fixes, and improvements to be made directly on the Hub, avoiding fragmentation across various repositories.

The plan would be to initially mirror the Liger kernel code onto the Hub, under a dedicated "liger-kernels" organization. From there, you could maintain it by gradually upstreaming changes from the original GitHub repository. Eventually, the Hub version would become the authoritative and up-to-date source.