• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020-2021 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ===========================================================================
15"""generate json desc for softmax"""
16from mindspore._extends.graph_kernel.model.model import DataFormat as DF
17from ._utils import Expander, ExpanderInfoValidator as VLD
18from ._utils import infer_shape_from_fractalnz, get_reduced_ori_shape, to_frac_z_axis
19
20
21@VLD.add_format(DF.FRAC_NZ)
22@VLD.add_format(DF.DEFAULT)
23@VLD.check_attrs('axis')
24class Softmax(Expander):
25    """Softmax expander"""
26
27    def _expand(self, graph_builder):
28        input_x = self.inputs[0]
29        processor = self.processor
30        axis = self.attrs['axis']
31
32        ori_shape = input_x.shape
33        if input_x.data_format == DF.FRAC_NZ:
34            ori_shape = infer_shape_from_fractalnz(input_x.shape)
35
36        for i, _ in enumerate(list(axis)):
37            if axis[i] < 0:
38                axis[i] += len(ori_shape)
39
40        ori_reduced_shape = get_reduced_ori_shape(ori_shape, axis)
41
42        if input_x.data_format == DF.FRAC_NZ:
43            axis = to_frac_z_axis(ori_shape, axis)
44
45        ori_dtype = input_x.dtype
46        if ori_dtype != "float16" and processor == "aicore":
47            input_x_f16 = graph_builder.emit('Cast', [input_x], attrs={'dst_type': 'float16'})
48            max_x_f16 = graph_builder.emit('ReduceMax', [input_x_f16], attrs={'reduce_axis': axis, 'keep_dims': True})
49            max_x = graph_builder.emit('Cast', [max_x_f16], attrs={'dst_type': ori_dtype})
50        else:
51            max_x = graph_builder.emit('ReduceMax', [input_x], attrs={'reduce_axis': axis, 'keep_dims': True})
52
53        if ori_dtype == "float16" and processor == "aicore":
54            max_x = graph_builder.emit('Cast', [max_x], attrs={'dst_type': "float32"})
55            input_x = graph_builder.emit('Cast', [input_x], attrs={'dst_type': "float32"})
56
57        if input_x.data_format == DF.FRAC_NZ:
58            max_x = graph_builder.emit('Reshape', [max_x], attrs={'shape': ori_reduced_shape})
59        data_sub = graph_builder.emit('Sub', [input_x, max_x])
60        data_exp = graph_builder.emit('Exp', [data_sub])
61        data_expsum = graph_builder.emit('ReduceSum', [data_exp], attrs={'reduce_axis': axis, 'keep_dims': True})
62        if input_x.data_format == DF.FRAC_NZ:
63            data_expsum = graph_builder.emit('Reshape', [data_expsum], attrs={'shape': ori_reduced_shape})
64        result = graph_builder.emit('RealDiv', [data_exp, data_expsum])
65        if ori_dtype == "float16" and processor == "aicore":
66            result = graph_builder.emit('Cast', [result], attrs={'dst_type': ori_dtype})
67
68        return result
69