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

[OpenSora-hpcai] T5 refactor #670

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

Conversation

hadipash
Copy link
Collaborator

@hadipash hadipash commented Sep 20, 2024

This PR replaces OpenSora's T5 with the one from mindone.transformers.

Differences between the refactored and the previous T5:

  • In T5LayerNorm, hidden_states are now calculated as hidden_states = hidden_states * ops.rsqrt(variance + self.variance_epsilon) instead of hidden_states = hidden_states / ops.sqrt(variance + self.variance_epsilon) previously.
  • In T5DenseActDense, a custom GELU activation is used (NewGELUActivation), instead of nn.GELU() used previously.
  • [BFloat16 only] In T5Attention._relative_position_bucket, relative_position_if_large is calculated as follows to align with the PyTorch implementation:
relative_position_if_large = (
            max_exact
            + (
                ops.log(relative_position.float() / max_exact)
                / ops.log(Tensor(max_distance, ms.float32) / max_exact)
                * (num_buckets - max_exact)
            ).long()
        )

If the previous and current T5s are aligned (considering the points mentioned above), the outputs will be identical in both Pynative and Graph modes for all data types, i.e., FP32, FP16, and BF16.

Error compared to HF's transformers on 6 random long text samples:

Version MS Precision PT Precision Mean Error Max Error
Previous T5 FP32 FP32 9.1761e-5 4.5508e-2
Refactored T5 FP32 FP32 9.1782e-5 4.5497e-2
Previous T5 BF16 FP32 1.9345e-3 5.1995e-1
Refactored T5 BF16 FP32 1.9730e-3 4.4061e-1
Previous T5 BF16 BF16 2.2863e-3 5.3125e-1
Refactored T5 BF16 BF16 2.3284e-3 4.6875e-1

Copy link
Collaborator

@SamitHuang SamitHuang left a comment

Choose a reason for hiding this comment

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

lgtm

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.

2 participants