blob: dd17b2352039b11274fb4f7e7fd746ab0a57eb6b [file] [log] [blame]
Rickard Bolinbc6ee582022-11-04 08:24:29 +00001# SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates <open-source-office@arm.com>
Diqing Zhong5e5a7842021-08-16 17:24:09 +02002#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the License); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# 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, WITHOUT
13# 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.
Rickard Bolinbc6ee582022-11-04 08:24:29 +000016#
Diqing Zhong5e5a7842021-08-16 17:24:09 +020017# Description:
18# Functions used to write to a raw format (.npz) file.
19import numpy as np
20
21from .high_level_command_to_npu_op import get_region
22from .nn_graph import PassPlacement
23from .operation import Op
24
25
26def write_rawdata_output(nng, arch, filename):
27 subgraphs_to_write = [sg for sg in nng.subgraphs if sg.placement == PassPlacement.Cpu]
28
29 for sg_idx, sg in enumerate(subgraphs_to_write):
30 custom_op = None
31 for ps in sg.passes:
32 for op in ps.ops:
33 if op.type == Op.CustomNpuOp:
34 custom_op = op
35 break
36 if custom_op:
37 break
38
39 if custom_op:
40 ifm_shapes = []
Diqing Zhonge3d18b02021-11-15 13:53:10 +010041 ifm_elem_sizes = []
Diqing Zhong5e5a7842021-08-16 17:24:09 +020042 ifm_regions = []
43 ifm_offsets = []
44 ofm_shapes = []
Diqing Zhonge3d18b02021-11-15 13:53:10 +010045 ofm_elem_sizes = []
Diqing Zhong5e5a7842021-08-16 17:24:09 +020046 ofm_regions = []
47 ofm_offsets = []
48 cmd_stream_tensor, weight_tensor, scratch_tensor, scratch_fast_tensor = custom_op.inputs[:4]
49 weight_region = get_region(weight_tensor.mem_type, arch)
50 scratch_region = get_region(scratch_tensor.mem_type, arch)
51 scratch_fast_region = get_region(scratch_fast_tensor.mem_type, arch)
52 for ifm in custom_op.inputs[4:]:
53 ifm_shapes.append(ifm.shape)
54 ifm_regions.append(get_region(ifm.mem_type, arch))
55 ifm_offsets.append(ifm.address)
Diqing Zhonge3d18b02021-11-15 13:53:10 +010056 ifm_elem_sizes.append(ifm.element_size())
Diqing Zhong5e5a7842021-08-16 17:24:09 +020057 for ofm in custom_op.outputs:
58 ofm_shapes.append(ofm.shape)
59 ofm_regions.append(get_region(ofm.mem_type, arch))
60 ofm_offsets.append(ofm.address)
Diqing Zhonge3d18b02021-11-15 13:53:10 +010061 ofm_elem_sizes.append(ofm.element_size())
Diqing Zhong5e5a7842021-08-16 17:24:09 +020062
63 filename_sg = f"{filename}_sg{sg_idx}_vela.npz"
64 np.savez(
65 filename_sg,
66 cmd_data=cmd_stream_tensor.values,
67 weight_data=weight_tensor.values,
68 weight_region=weight_region,
69 scratch_shape=scratch_tensor.shape,
70 scratch_region=scratch_region,
71 scratch_fast_shape=scratch_fast_tensor.shape,
72 scratch_fast_region=scratch_fast_region,
73 input_shape=ifm_shapes,
Diqing Zhonge3d18b02021-11-15 13:53:10 +010074 input_elem_size=ifm_elem_sizes,
Diqing Zhong5e5a7842021-08-16 17:24:09 +020075 input_region=ifm_regions,
76 input_offset=ifm_offsets,
77 output_shape=ofm_shapes,
Diqing Zhonge3d18b02021-11-15 13:53:10 +010078 output_elem_size=ofm_elem_sizes,
Diqing Zhong5e5a7842021-08-16 17:24:09 +020079 output_region=ofm_regions,
80 output_offset=ofm_offsets,
81 )