Repository: huggingface/transformers
Pull request: [Bugfix] Fix flash-attention func param mismatch and softmax_scale default value mistake on Ascend NPU

# What does this PR do?
After we support using Flash Attention on Ascend NPU[(PR)](https://github.com/huggingface/transformers/pull/36696), we found 2 bugs in the implementation of Flash Attention on Ascend NPU. This PR is committed for solving both.

**Bugfix1:** 
We found that there exists a kind of situation in the code of `main` branch, where func `flash_attn_varlen_func` is used **without key-value style params passing**, like following code: https://github.com/huggingface/transformers/blob/3bc44eaaeee01b7f0d2d55c9991900b43cafe62d/src/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py#L203

In that case, params order between func `flash_attn_varlen_func` in package `flash-attn` and func `npu_flash_attn_varlen_func` in package `transformers` **is not aligned**, which may cause unexpected errors 😞 

Therefore, we solve this problem by aligning mismatch params `max_seqlen_q` and `max_seqlen_k`.

At the same time, we have also checked the func `npu_flash_attn_func`, and it does not have param order mismatch problem.

**Bugfix2:**
For func `npu_flash_attn_func` and `npu_flash_attn_varlen_func`, when param `softmax_scale` is set to `None`, it should be set to `1.0 / sqrt(q.shape(-1))` as default value. 

Fixes # (issue)
Not related.

## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [x] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


Changed files:
filename: src/transformers/integrations/npu_flash_attention.py
patch: @@ -19,6 +19,8 @@
 
 
 if is_torch_npu_available():
+    import math
+
     import torch_npu
     from einops import rearrange, repeat
 
@@ -162,6 +164,9 @@ def npu_flash_attn_func(
 ):
     keep_prob = 1.0 - dropout_p
 
+    if softmax_scale is None:
+        softmax_scale = 1.0 / math.sqrt(q.shape[-1])
+
     if not causal:
         head_num = q.shape[2]
         output = torch_npu.npu_fusion_attention(q, k, v, head_num, "BSND", keep_prob=keep_prob, scale=softmax_scale)[0]
@@ -189,13 +194,18 @@ def npu_flash_attn_varlen_func(
     v,
     cu_seqlens_q,
     cu_seqlens_k,
+    max_seqlen_q=None,  # defined for aligning params order with corresponding function in `flash-attn`
+    max_seqlen_k=None,  # defined for aligning params order with corresponding function in `flash-attn`
     dropout_p=0.0,
     softmax_scale=None,
     causal=False,
     **kwargs,
 ):
     keep_prob = 1.0 - dropout_p
 
+    if softmax_scale is None:
+        softmax_scale = 1.0 / math.sqrt(q.shape[-1])
+
     if not causal:
         head_num = q.shape[1]
         output = torch_npu.npu_fusion_attention(