huggingface / optimum-intel

🤗 Optimum Intel: Accelerate inference with Intel optimization tools
https://huggingface.co/docs/optimum/main/en/intel/index
Apache License 2.0
355 stars 99 forks source link

auto mapping class #780

Closed jiqing-feng closed 4 days ago

jiqing-feng commented 1 week ago

Hi @echarlaix. This PR enables auto mapping for model class, which mean we can use IPEXModel to load any supported task, for example IPEXModel.from_pretrained("meta-llama/Llama-2-7b-chat-hf") will map the IPEXModelForCausalLM class to export the model.

The usage is the same as AutoModel from transformers.

jiqing-feng commented 1 week ago

Hi @echarlaix , you were right; it's difficult to get the task from the local model. I think we should let users assign tasks in the API because it is hard to remember all IPEXModel classes, such as IPEXModelForCausalLM. With my changes, users can easily use it by IPEXModel.from_pretrained("gpt2", task="text-generation").

It is more friendly for users, WDYT?

IlyasMoutawwakil commented 1 week ago

The transformers AutoModel doesn't map task classes, but rather feature-extraction models. For example using AutoModel with a bert-base-uncased model will return the core BertModel (with no task head) https://github.com/huggingface/transformers/blob/dc76e9fa7f0d19ff7cfc33bd3a22acd7df167fce/src/transformers/models/bert/modeling_bert.py#L956. same thing for llama for example it will return the LlamaModel class https://github.com/huggingface/transformers/blob/dc76e9fa7f0d19ff7cfc33bd3a22acd7df167fce/src/transformers/models/llama/modeling_llama.py#L869.

model = AutoModel.from_pretrained("facebook/opt-125m")
print(model.__class__) # <class 'transformers.models.opt.modeling_opt.OPTModel'>

This feature will result in a big API difference and confusion around the feature-extraction task and its specific auto class.

jiqing-feng commented 1 week ago

The transformers AutoModel doesn't map task classes, but rather feature-extraction models. For example using AutoModel with a bert-base-uncased model will return the core BertModel (with no task head) https://github.com/huggingface/transformers/blob/dc76e9fa7f0d19ff7cfc33bd3a22acd7df167fce/src/transformers/models/bert/modeling_bert.py#L956. same thing for llama for example it will return the LlamaModel class https://github.com/huggingface/transformers/blob/dc76e9fa7f0d19ff7cfc33bd3a22acd7df167fce/src/transformers/models/llama/modeling_llama.py#L869.

model = AutoModel.from_pretrained("facebook/opt-125m")
print(model.__class__) # <class 'transformers.models.opt.modeling_opt.OPTModel'>

This feature will result in a big API difference and confusion around the feature-extraction task.

Thanks @IlyasMoutawwakil ! I realized it and have changed the API to support assigned task instead of mapping task by the model. Do you mind take a review? Thx!

Refer to https://github.com/huggingface/optimum-intel/pull/780#issuecomment-2192981222

IlyasMoutawwakil commented 1 week ago

My last comment is about the new changes.

This feature will result in a big API difference and confusion around the feature-extraction task and its specific auto class. (AutoModel vs IPEXModel)

The current API is based on the simplicity of going from the transformers AutoModelForxxx to IPEXModelForxxx or OVModelForxxxx, which allows them to go from one backend to another and use the one that gives the best hardware compatibility and/or utilization.

Also the pipeline function already allow for the "single entry point" API.

jiqing-feng commented 1 week ago

My last comment is about the new changes.

This feature will result in a big API difference and confusion around the feature-extraction task and its specific auto class. (AutoModel vs IPEXModel)

The current API is based on the simplicity of going from the transformers AutoModelForxxx to IPEXModelForxxx or OVModelForxxxx, which allows them to go from one backend to another and use the one that gives the best hardware compatibility and/or utilization.

Also the pipeline function already allow for the "single entry point" API.

Sorry for not making it clear; my change has no impact on IPEXModel except you pass the parameter task. I will close it if you think exposing the parameter task is not acceptable, but I want to make sure I have clarified it. Thx :)

echarlaix commented 4 days ago

Sorry for not making it clear; my change has no impact on IPEXModel except you pass the parameter task. I will close it if you think exposing the parameter task is not acceptable, but I want to make sure I have clarified it. Thx :)

Thanks a lot @jiqing-feng, closing it