Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transformers): api(image processor/feature extractor/automodel/pipelines) #802

Open
wants to merge 24 commits into
base: master
Choose a base branch
from

Conversation

wcrzlh
Copy link
Contributor

@wcrzlh wcrzlh commented Dec 25, 2024

What does this PR do?

This PR(based on #748) contains following features:

  • image processor
  • feature extractor
  • automodel
  • pipelines
    • FillMaskPipeline
    • TextGenerationPipeline

Image processor and feature extractor would be served as part of VLLM. It could be tested based on PR749 and test scripts

AutoModel could be used to call model existed on mindone.transformers or could be integrated in Pipelines api:

from mindone.transformers import AutoModel

model = AutoModel.from_pretrained("google-bert/bert-base-uncased")
print(model)

Pipelines part could tested based on following codes:

from mindone.transformers.pipelines import pipeline

generator = pipeline(model="google-bert/bert-base-uncased")
outputs = generator("This is a simple [MASK]")
print(outputs)

Expected output:

[{'score': 0.041297122836112976, 'token':3291, 'token_str': 'problem', 'sequence': 'this is a simple problem.'},
{'score': 0.03821507468819618, 'token':8522, 'token_str': 'equation', 'sequence': 'this is a simple equation'},
{'score': 0.029827609658241272, 'token':3160, 'token_str': 'question', 'sequence': 'this is a simple question'},
{'score': 0.027154073119163513, 'token':7709, 'token_str': 'procedure', 'sequence': 'this is a simple procedure'},
{'score': 0.025617485865950584, 'token':7577, 'token_str': 'trick', 'sequence': 'this is a simple trick'}]

GPT-2 Model could be tested based on Text Generation Pipe:

from mindone.transformers.pipelines import pipeline

generator = pipeline(model="openai-community/gpt2")
outputs = generator("I can't believe you did such a ", do_sample=False)
print(outputs)

Expected Output:

[{"generated_text": "I can't believe you did such a icky thing to me. I'm so sorry. I'm so sorry. I'm so sorry. 
 I'm so sorry.  I'm so sorry.  I'm so sorry.  I'm so sorry. I"}]

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?
  • Did you make sure to update the documentation with your changes? E.g. record bug fixes or new features in What's New. Here are the
    documentation guidelines
  • Did you build and run the code without any errors?
  • Did you report the running environment (NPU type/MS version) and performance in the doc? (better record it for data loading, model inference, or training tasks)
  • 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.

@xxx

@wcrzlh wcrzlh requested a review from vigo999 as a code owner December 25, 2024 04:49
@zhanghuiyao
Copy link
Collaborator

冲突可以先解一下


def torch_int(x):
"""
Casts an input to a torch int64 tensor if we are in a tracing context, otherwise to a Python int.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

torch -> mindspore 全局换一下


logger = logging.get_logger(__name__)

MODEL_MAPPING_NAMES = OrderedDict(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some supported models in mindone/transformers/models are missing, like llama and gemma2. pls check and add.

("pegasus_x", "PegasusXForConditionalGeneration"),
("plbart", "PLBartForConditionalGeneration"),
("prophetnet", "ProphetNetForConditionalGeneration"),
("qwen2_audio", "Qwen2AudioForConditionalGeneration"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qwen2_audio not implemented

]
)

MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES = OrderedDict(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

either comment those models that are not supported or simply assert and claim will be supported in the future


# Downcast (if necessary) back to V's dtype (if in mixed-precision) -- No-Op if otherwise
if attn_weights.dtype != ms.float32:
raise RuntimeError("Error with upcasting, attn_weights does not have dtype torch.float32")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

torch.float32 -> ms.float32

scale_factor /= float(self.layer_idx + 1)

# Upcast (turn off autocast) and reorder (Scale K by 1 / root(dk))
with ms.amp.autocast(query.device.type, enabled=False):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ms2.4.1 doesn't support amp.autocast or tensor.device. how is it from


class GPT2SdpaAttention(GPT2Attention):
"""
GPT2 attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update docstring for mindspore

Base class for outputs of models predicting if two sentences are consecutive or not.

Args:
loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same docstring problem here

@CaitinZhao
Copy link
Collaborator

新增模型加下tests,看下和torch的精度误差

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants