Repository: PaddlePaddle/PaddleNLP
Pull request: fix 2.0.1 crf bug
<!-- Demo: https://github.com/PaddlePaddle/PaddleNLP/pull/26 -->
### PR types
<!-- One of [ New features | Bug fixes | Function optimization | Performance optimization | Breaking changes | Others ] -->
Bug fixes
### PR changes
<!-- One of [ Models | APIs | Docs | Others ] -->
Models
### Description
<!-- Describe what this PR does -->
1. fix slice error in paddle 2.0.1
2. Pad, Stack function will return int32 data when input's type is int, which will lead to training error in Windows. Specify the  dtype as int64 to fix it.

Problem statement:
window系统运行出现普遍性的报错The current variable type is (int64_t), but the previous variable type is (int). 
InvalidArgumentError: The DataType of greater_than Op's duplicable Variable Y must be consistent. The current variable type is (int64_t), but the previous variable type is (int). (at C:\home\workspace\Paddle_release\paddle\fluid\framework\operator.cc:1574)
  [operator < greater_than > error] 运行PaddleNLP\examples\information_extraction\waybill_ie里面的run_bigru_crf.py出现报错

应该是windows系统运行的原因，这些代码应该要linux和window都验证过就好了,能省开发者很多时间，你们维护起来估计也会轻松很多

Hints from issue discussion:
希望解决下

Commit message: 1.fix 2.0.1 slice bug; 2. cast int32 label which returned in Windows to int64

Patch to review:
diff --git a/examples/information_extraction/waybill_ie/run_bigru_crf.py b/examples/information_extraction/waybill_ie/run_bigru_crf.py
index d506944f67db..d43bc8b207a9 100644
--- a/examples/information_extraction/waybill_ie/run_bigru_crf.py
+++ b/examples/information_extraction/waybill_ie/run_bigru_crf.py
@@ -87,9 +87,9 @@ def predict(model, data_loader, ds, label_vocab):
     test_ds.map(trans_func)
 
     batchify_fn = lambda samples, fn=Tuple(
-        Pad(axis=0, pad_val=word_vocab.get('OOV', 0)),  # token_ids
-        Stack(),  # seq_len
-        Pad(axis=0, pad_val=label_vocab.get('O', 0))  # label_ids
+        Pad(axis=0, pad_val=word_vocab.get('OOV', 0), dtype='int64'),  # token_ids
+        Stack(dtype='int64'),  # seq_len
+        Pad(axis=0, pad_val=label_vocab.get('O', 0), dtype='int64')  # label_ids
     ): fn(samples)
 
     train_loader = paddle.io.DataLoader(
diff --git a/examples/information_extraction/waybill_ie/run_ernie.py b/examples/information_extraction/waybill_ie/run_ernie.py
index fd10e100dbfa..678f13a2b1f0 100644
--- a/examples/information_extraction/waybill_ie/run_ernie.py
+++ b/examples/information_extraction/waybill_ie/run_ernie.py
@@ -83,10 +83,10 @@ def predict(model, data_loader, ds, label_vocab):
 
     ignore_label = -1
     batchify_fn = lambda samples, fn=Tuple(
-        Pad(axis=0, pad_val=tokenizer.pad_token_id),  # input_ids
-        Pad(axis=0, pad_val=tokenizer.pad_token_type_id),  # token_type_ids
-        Stack(),  # seq_len
-        Pad(axis=0, pad_val=ignore_label)  # labels
+        Pad(axis=0, pad_val=tokenizer.pad_token_id, dtype='int64'),  # input_ids
+        Pad(axis=0, pad_val=tokenizer.pad_token_type_id, dtype='int64'),  # token_type_ids
+        Stack(dtype='int64'),  # seq_len
+        Pad(axis=0, pad_val=ignore_label, dtype='int64')  # labels
     ): fn(samples)
 
     train_loader = paddle.io.DataLoader(
diff --git a/examples/information_extraction/waybill_ie/run_ernie_crf.py b/examples/information_extraction/waybill_ie/run_ernie_crf.py
index f2d428fb8b67..54d9bea711cf 100644
--- a/examples/information_extraction/waybill_ie/run_ernie_crf.py
+++ b/examples/information_extraction/waybill_ie/run_ernie_crf.py
@@ -81,10 +81,10 @@ def predict(model, data_loader, ds, label_vocab):
     test_ds.map(trans_func)
 
     batchify_fn = lambda samples, fn=Tuple(
-        Pad(axis=0, pad_val=tokenizer.pad_token_id),  # input_ids
-        Pad(axis=0, pad_val=tokenizer.pad_token_type_id),  # token_type_ids
-        Stack(),  # seq_len
-        Pad(axis=0, pad_val=label_vocab.get("O", 0))  # labels
+        Pad(axis=0, pad_val=tokenizer.pad_token_id, dtype='int64'),  # input_ids
+        Pad(axis=0, pad_val=tokenizer.pad_token_type_id, dtype='int64'),  # token_type_ids
+        Stack(dtype='int64'),  # seq_len
+        Pad(axis=0, pad_val=label_vocab.get("O", 0), dtype='int64')  # labels
     ): fn(samples)
 
     train_loader = paddle.io.DataLoader(
diff --git a/paddlenlp/layers/crf.py b/paddlenlp/layers/crf.py
index 780bbec188d1..cddcf19c5819 100644
--- a/paddlenlp/layers/crf.py
+++ b/paddlenlp/layers/crf.py
@@ -395,7 +395,8 @@ def forward(self, inputs, lengths):
         # avoid call fill_constant in iteration
         zeros = paddle.zeros([1], dtype='int64')
         ones = paddle.ones([1], dtype='int64')
-        for i, logit in enumerate(inputs_t[:max_seq_len]):
+        emission = paddle.slice(inputs_t, [0], [0], [max_seq_len])
+        for i, logit in enumerate(emission):
             # if not with_start_stop_tag, the first label has not antecedent tag.
             if i == 0 and not self.with_start_stop_tag:
                 alpha = logit


Code review comments:
diff_hunk: @@ -83,10 +83,10 @@ def predict(model, data_loader, ds, label_vocab):
 
     ignore_label = -1
     batchify_fn = lambda samples, fn=Tuple(
-        Pad(axis=0, pad_val=tokenizer.pad_token_id),  # input_ids
-        Pad(axis=0, pad_val=tokenizer.pad_token_type_id),  # token_type_ids
-        Stack(),  # seq_len
-        Pad(axis=0, pad_val=ignore_label)  # labels
+        Pad(axis=0, pad_val=tokenizer.pad_token_id, dtype='int64'),  # input_ids
path: examples/information_extraction/waybill_ie/run_ernie.py
text: @user1:
确定下哪些地方要再加，不要全部升级变量，会减慢计算速度的。

@author:
已对所有变量指定了dtype，input_ids、token_type_ids指定为int32，其余指定为int64