PyTorch 2.0 implementation on Hugging Face

Is there a way to easily pass the torch.compile directly to Hugging Face’s pipeline? Was thinking of something like this.

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM,pipeline

tokenizer = AutoTokenizer.from_pretrained(“distilgpt2”)
model = torch.compile(AutoModelForCausalLM.from_pretrained(“distilgpt2”))

generator = pipeline(task=“text-generation”, model=model, tokenizer=tokenizer)

But was met with this error:

The model ‘OptimizedModule’ is not supported for text-generation. Supported models are [‘BartForCausalLM’, ‘BertLMHeadModel’, ‘BertGenerationDecoder’, ‘BigBirdForCausalLM’, ‘BigBirdPegasusForCausalLM’, ‘BlenderbotForCausalLM’, ‘BlenderbotSmallForCausalLM’, ‘BloomForCausalLM’, ‘CamembertForCausalLM’, ‘CodeGenForCausalLM’, ‘CTRLLMHeadModel’, ‘Data2VecTextForCausalLM’, ‘ElectraForCausalLM’, ‘ErnieForCausalLM’, ‘GPT2LMHeadModel’, ‘GPTNeoForCausalLM’, ‘GPTNeoXForCausalLM’, ‘GPTNeoXJapaneseForCausalLM’, ‘GPTJForCausalLM’, ‘MarianForCausalLM’, ‘MBartForCausalLM’, ‘MegatronBertForCausalLM’, ‘MvpForCausalLM’, ‘OpenAIGPTLMHeadModel’, ‘OPTForCausalLM’, ‘PegasusForCausalLM’, ‘PLBartForCausalLM’, ‘ProphetNetForCausalLM’, ‘QDQBertLMHeadModel’, ‘ReformerModelWithLMHead’, ‘RemBertForCausalLM’, ‘RobertaForCausalLM’, ‘RoCBertForCausalLM’, ‘RoFormerForCausalLM’, ‘Speech2Text2ForCausalLM’, ‘TransfoXLLMHeadModel’, ‘TrOCRForCausalLM’, ‘XGLMForCausalLM’, ‘XLMWithLMHeadModel’, ‘XLMProphetNetForCausalLM’, ‘XLMRobertaForCausalLM’, ‘XLMRobertaXLForCausalLM’, ‘XLNetLMHeadModel’].

I could pass the torch.compile function in Hugging Face’s codebase, but that seems like a lot of work for maintenance until Hugging Face implements PyTorch 2.0. Your suggestion is appreciated!

HuggingFace has an example of how this would work here Getting started with Pytorch 2.0 and Hugging Face Transformers