]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - tidl/tidl-api.git/blob - examples/pybind/two_eo_per_frame_opt.py
More example sizes adjustment with new TIDL lib
[tidl/tidl-api.git] / examples / pybind / two_eo_per_frame_opt.py
1 #!/usr/bin/python3
3 # Copyright (c) 2018 Texas Instruments Incorporated - http://www.ti.com/
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of Texas Instruments Incorporated nor the
14 # names of its contributors may be used to endorse or promote products
15 # derived from this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 # THE POSSIBILITY OF SUCH DAMAGE.
30 import argparse
31 from tidl import DeviceId, DeviceType, Configuration, TidlError
32 from tidl import Executor, ExecutionObjectPipeline
33 from tidl import allocate_memory, free_memory, enable_time_stamps
35 from tidl_app_utils import read_frame, write_output
38 def main():
39     """Read the configuration and run the network"""
41     args = parse_args()
43     # Heaps are sized for the j11_v2 network. Changing the network will
44     # require updating network_heap_size and param_heap_size
45     config_file = '../test/testvecs/config/infer/tidl_config_j11_v2.txt'
47     configuration = Configuration()
48     configuration.read_from_file(config_file)
49     configuration.enable_api_trace = False
50     configuration.num_frames = args.num_frames
52     # Heap sizes for this network determined using Configuration.showHeapStats
53     configuration.param_heap_size = (3 << 20)
54     configuration.network_heap_size = (34 << 20)
56     num_dsp = Executor.get_num_devices(DeviceType.DSP)
57     num_eve = Executor.get_num_devices(DeviceType.EVE)
58     # AM572x default CMEM size is 160MB, 4 EVEs + 2DSPs won't fit
59     if num_eve > 2:
60         num_eve = 2
62     if num_dsp == 0 or num_eve == 0:
63         print('This example requires EVEs and DSPs.')
64         return
66     enable_time_stamps("2eo_opt_timestamp.log", 16)
67     run(num_eve, num_dsp, configuration)
69 # Run layer group 1 on EVE, 2 on DSP
70 EVE_LAYER_GROUP_ID = 1
71 DSP_LAYER_GROUP_ID = 2
74 def run(num_eve, num_dsp, c):
75     """ Run the network on the specified device type and number of devices"""
77     print('Running on {} EVEs, {} DSPs'.format(num_eve, num_dsp))
79     dsp_device_ids = set([DeviceId.ID0, DeviceId.ID1,
80                           DeviceId.ID2, DeviceId.ID3][0:num_dsp])
81     eve_device_ids = set([DeviceId.ID0, DeviceId.ID1,
82                           DeviceId.ID2, DeviceId.ID3][0:num_eve])
84     c.layer_index_to_layer_group_id = {12:DSP_LAYER_GROUP_ID,
85                                        13:DSP_LAYER_GROUP_ID,
86                                        14:DSP_LAYER_GROUP_ID}
88     try:
89         print('TIDL API: performing one time initialization ...')
91         eve = Executor(DeviceType.EVE, eve_device_ids, c, EVE_LAYER_GROUP_ID)
92         dsp = Executor(DeviceType.DSP, dsp_device_ids, c, DSP_LAYER_GROUP_ID)
94         num_eve_eos = eve.get_num_execution_objects()
95         num_dsp_eos = dsp.get_num_execution_objects()
97         # On AM5749, create a total of 4 pipelines (EOPs):
98         # EOPs[0] : { EVE1, DSP1 }
99         # EOPs[1] : { EVE1, DSP1 } for double buffering
100         # EOPs[2] : { EVE2, DSP2 }
101         # EOPs[3] : { EVE2, DSP2 } for double buffering
102         PIPELINE_DEPTH = 2
104         eops = []
105         num_pipe = max(num_eve_eos, num_dsp_eos)
106         for j in range(PIPELINE_DEPTH):
107             for i in range(num_pipe):
108                 eops.append(ExecutionObjectPipeline([eve.at(i % num_eve_eos),
109                                                      dsp.at(i % num_dsp_eos)]))
111         allocate_memory(eops)
113         # Open input, output files
114         f_in = open(c.in_data, 'rb')
115         f_out = open(c.out_data, 'wb')
118         print('TIDL API: processing input frames ...')
120         num_eops = len(eops)
121         for frame_index in range(c.num_frames+num_eops):
122             eop = eops[frame_index % num_eops]
124             if eop.process_frame_wait():
125                 write_output(eop, f_out)
127             if read_frame(eop, frame_index, c, f_in):
128                 eop.process_frame_start_async()
131         f_in.close()
132         f_out.close()
134         free_memory(eops)
135     except TidlError as err:
136         print(err)
139 DESCRIPTION = 'Process frames using ExecutionObjectPipeline. '\
140               'Each ExecutionObjectPipeline processes a single frame. '\
141               'The example also uses double buffering on the EOPs to '\
142               'hide frame read overhead.'
144 def parse_args():
145     """Parse input arguments"""
147     parser = argparse.ArgumentParser(description=DESCRIPTION)
148     parser.add_argument('-n', '--num_frames',
149                         type=int,
150                         default=16,
151                         help='Number of frames to process')
152     args = parser.parse_args()
154     return args
157 if __name__ == '__main__':
158     main()