Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / tests / nnapi / specs / V1_0 / softmax_dynamic_nnfw.mod.py
1 #
2 # Copyright (C) 2018 The Android Open Source Project
3 # Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 # refer to tanh_v1_dynamic.mod.py about the structore
19
20 # This adds reshape as the first op in a model and
21 # returns output of reshape, which is dynamic tensor
22
23 '''
24 Testing Softmax op when the input is dynamic.
25
26       input [2, 5]  shape [2]  (value of shape will be [2, 5])
27           |             |
28           +-------------+
29           |
30        Reshape (added by DynamicInputGenerator since it generates its output to be dynamic)
31           |
32           | dynamic tensor at compilation time but the shape will be [2, 5] at execution time
33           |
34          Softmax
35           |
36         output (dynamic tensor, [2, 5] at execution time)
37 '''
38
39 import dynamic_tensor
40
41 model = Model()
42
43 model_input_shape = [2, 5]
44
45 beta = Float32Scalar("beta", 1.)
46
47 dynamic_layer = dynamic_tensor.DynamicInputGenerator(model, model_input_shape, "TENSOR_FLOAT32")
48
49 test_node_input = dynamic_layer.getTestNodeInput()
50
51 # write SOFTMAX test. input is `test_input`
52
53 # note output shape is used by expected output's shape
54 model_output = Output("output", "TENSOR_FLOAT32", "{2, 5}")
55
56 model.Operation("SOFTMAX", test_node_input, beta).To(model_output)
57
58 model_input_data = [1., 2., 3., 4., 5.,
59                     -1., -2., -3., -4., -5.]
60
61 model_output_data = [0.011656231, 0.031684921, 0.086128544, 0.234121657, 0.636408647,
62                     0.636408647, 0.234121657, 0.086128544, 0.031684921, 0.011656231]
63
64 Example({
65     # use these two as input
66     dynamic_layer.getModelInput(): model_input_data,
67     dynamic_layer.getShapeInput() : model_input_shape,
68
69     model_output: model_output_data,
70 })