Skip to content

handle the softmax error form ms_2.0alpha and clean warning #304

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

Merged
merged 1 commit into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mindocr/models/heads/rec_attn_head.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def __init__(
self.one = Tensor(1.0, ms.float32)
self.zero = Tensor(0.0, ms.float32)

self.argmax = ops.Argmax(axis=1)

def _char_to_onehot(self, input_char: Tensor, onehot_dim: int) -> Tensor:
input_one_hot = ops.one_hot(input_char, onehot_dim, self.one, self.zero)
return input_one_hot
Expand Down Expand Up @@ -68,7 +70,7 @@ def construct(self, inputs: Tensor, targets: Optional[Tensor] = None) -> Tensor:
hidden, _ = self.attention_cell(hidden, inputs, char_onehots)
probs_step = self.generator(hidden)
probs.append(probs_step)
next_input = ops.argmax(probs_step, axis=1)
next_input = self.argmax(probs_step)
targets = next_input
probs = ops.stack(probs, axis=1)
probs = ops.softmax(probs, axis=2)
Expand Down
10 changes: 0 additions & 10 deletions mindocr/models/utils/rnn_cells.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""RNN Cells that supports FP16 inputs
"""
import mindspore.ops as P
import mindspore.common.dtype as mstype
from mindspore.ops.primitive import constexpr
from mindspore.nn.layer.rnn_cells import RNNCellBase

Expand All @@ -27,13 +26,6 @@ def _gru_cell(inputs, hidden, w_ih, w_hh, b_ih, b_hh):

return hy

@constexpr
def _check_is_tensor(param_name, input_data, cls_name):
"""Internal function, used to check whether the input data is Tensor."""
if input_data is not None and not isinstance(P.typeof(input_data), mstype.tensor_type):
raise TypeError(f"For '{cls_name}', the '{param_name}' must be '{mstype.tensor_type}', "
f"but got '{P.typeof(input_data)}'")

@constexpr
def _check_batch_size_equal(batch_size_x, batch_size_hx, cls_name):
if batch_size_x != batch_size_hx:
Expand Down Expand Up @@ -101,8 +93,6 @@ def __init__(self, input_size: int, hidden_size: int, has_bias: bool = True):
super().__init__(input_size, hidden_size, has_bias, num_chunks=3)

def construct(self, x, hx):
_check_is_tensor('x', x, self.cls_name)
_check_is_tensor('hx', hx, self.cls_name)
_check_batch_size_equal(x.shape[0], hx.shape[0], self.cls_name)

# FIX: make sure the weight and bias dtype is same as the data type from x
Expand Down