-
Notifications
You must be signed in to change notification settings - Fork 88
feats(transformers)add mobilevit,mobilevitv2,deepseek_v3,xlnet models #1211
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @iugoood, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request significantly expands the mindone/transformers
library by integrating four new transformer models: MobileViT, MobileViTV2, DeepseekV3, and XLNet. The changes involve adding the complete model architectures, updating the library's import mechanisms, and registering these models for automatic configuration and usage. Crucially, comprehensive unit tests have been added for each new model to validate their implementation and ensure stability. A minor code fix was also included for a padding operation.
Highlights
- New Model: MobileViT: Introduced the MobileViT model, including its core components for image classification and semantic segmentation.
- New Model: MobileViTV2: Added the MobileViTV2 model, an updated version of MobileViT, also supporting image classification and semantic segmentation.
- New Model: DeepseekV3: Integrated the DeepseekV3 model, a causal language model with a Mixture-of-Experts (MoE) architecture.
- New Model: XLNet: Included the XLNet model, a generalized autoregressive pretraining method for natural language understanding.
- Module Imports and Exposure: Updated the
__init__.py
files across themindone/transformers
andmindone/transformers/models
directories to properly import and expose the new models and their classes. - Auto-Configuration and Auto-Modeling: Modified the
auto
configuration and modeling files to enable automatic loading and usage of the new MobileViT, MobileViTV2, DeepseekV3, and XLNet models. - Unit Test Coverage: Added comprehensive unit tests for MobileViT, MobileViTV2, DeepseekV3, and XLNet models to ensure their correct functionality and integration.
- Minor Code Fix: Applied a minor fix in
mindone/transformers/modeling_utils.py
to usemindspore.mint.nn.functional.pad
instead ofops.pad
for consistency and correctness.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces support for several new models: mobilevit
, mobilevitv2
, deepseek_v3
, and xlnet
. The implementation for these models and their corresponding tests are included.
My review focuses on a few key areas:
- Performance: The
deepseek_v3
model contains some inefficient operations, particularly in the Mixture of Experts (MoE) layer and rotary position embeddings, which are acknowledged withTODO
comments in the code. These could become performance bottlenecks and should be addressed. - Code Style and Efficiency: I've pointed out minor issues in
mobilevit
related to inefficient tensor creation and the use of in-place dropout, which could be improved for better performance and code clarity. - Completeness: It appears that the
deepseek_v3
model, while its files are added, has not been fully integrated into the library's__init__.py
andauto
modules. This might be intentional for a follow-up PR, but as it stands, the model is not usable through the standard factory functions.
Overall, this is a substantial contribution. Addressing the identified points will help improve the quality and performance of the newly added models.
for expert_idx in range(len(self.experts)): | ||
expert = self.experts[expert_idx] | ||
mask = expert_mask[expert_idx] | ||
token_indices, weight_indices = mindspore.mint.where(mask) | ||
|
||
if token_indices.numel() > 0: | ||
expert_weights = topk_weights[token_indices, weight_indices] | ||
expert_input = hidden_states[token_indices] | ||
expert_output = expert(expert_input) | ||
weighted_output = expert_output * expert_weights.unsqueeze(-1) | ||
final_hidden_states.index_add_(0, token_indices, weighted_output) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of the Mixture of Experts (MoE) layer iterates through experts using a for
loop. This is inefficient, especially for models with a large number of experts. The docstring even includes a 'CALL FOR CONTRIBUTION' to optimize this. This loop is a significant performance bottleneck and should be vectorized. Consider using batched matrix multiplication (bmm
) or other techniques to process all experts in parallel. Since this is a generated file, this change should likely be applied to the source file src/transformers/models/deepseek_v3/modular_deepseek_v3.py
.
b, h, s, d = q.shape | ||
q = q.view(b, h, s, d // 2, 2).transpose(4, 3).reshape(b, h, s, d) | ||
|
||
b, h, s, d = k.shape | ||
k = k.view(b, h, s, d // 2, 2).transpose(4, 3).reshape(b, h, s, d) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The apply_rotary_pos_emb_interleave
function uses multiple view
, transpose
, and reshape
operations, which is inefficient. The TODO
comment in the docstring acknowledges this. This implementation should be refactored to avoid these expensive data layout transformations. Since this is a generated file, this change should likely be applied to the source file src/transformers/models/deepseek_v3/modular_deepseek_v3.py
.
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from .modeling_deepseek_v3 import * |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new_height = int(mindspore.mint.ceil(mindspore.tensor(orig_height / patch_height)) * patch_height) | ||
new_width = int(mindspore.mint.ceil(mindspore.tensor(orig_width / patch_width)) * patch_width) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating a mindspore.tensor
just to use mindspore.mint.ceil
is inefficient. You can achieve the same result more efficiently by using Python's math.ceil
, which avoids the overhead of tensor creation and device transfer.
new_height = int(mindspore.mint.ceil(mindspore.tensor(orig_height / patch_height)) * patch_height) | |
new_width = int(mindspore.mint.ceil(mindspore.tensor(orig_width / patch_width)) * patch_width) | |
new_height = int(math.ceil(orig_height / patch_height) * patch_height) | |
new_width = int(math.ceil(orig_width / patch_width) * patch_width) |
self.mobilevit = MobileViTModel(config) | ||
|
||
# Classifier head | ||
self.dropout = mindspore.mint.nn.Dropout(config.classifier_dropout_prob, inplace=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using inplace=True
for dropout can be problematic as it modifies tensors in-place, which can lead to unexpected side effects and make debugging difficult. It's generally safer to avoid in-place operations. The MobileViTV2
implementation in this same PR does not use in-place dropout, suggesting it's not a necessary pattern. Please remove inplace=True
for better code clarity and safety.
self.dropout = mindspore.mint.nn.Dropout(config.classifier_dropout_prob, inplace=True) | |
self.dropout = mindspore.mint.nn.Dropout(config.classifier_dropout_prob) |
87128f9
to
474c47c
Compare
Add
1 add mobilevit,mobilevitv2,deepseek_v3,xlnet models
2 add UT
Usage
mobilevit
mobilevitv2
deepseek_v3
xlnet
Performance
Experiments are tested on Ascend Atlas 800T A2 machines with mindspore 2.6.0.