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 = (20 << 20)
56 num_dsp = Executor.get_num_devices(DeviceType.DSP)
57 num_eve = Executor.get_num_devices(DeviceType.EVE)
59 if num_dsp == 0 or num_eve == 0:
60 print('This example requires EVEs and DSPs.')
61 return
63 enable_time_stamps("2eo_opt_timestamp.log", 16)
64 run(num_eve, num_dsp, configuration)
66 # Run layer group 1 on EVE, 2 on DSP
67 EVE_LAYER_GROUP_ID = 1
68 DSP_LAYER_GROUP_ID = 2
71 def run(num_eve, num_dsp, c):
72 """ Run the network on the specified device type and number of devices"""
74 print('Running on {} EVEs, {} DSPs'.format(num_eve, num_dsp))
76 dsp_device_ids = set([DeviceId.ID0, DeviceId.ID1,
77 DeviceId.ID2, DeviceId.ID3][0:num_dsp])
78 eve_device_ids = set([DeviceId.ID0, DeviceId.ID1,
79 DeviceId.ID2, DeviceId.ID3][0:num_eve])
81 c.layer_index_to_layer_group_id = {12:DSP_LAYER_GROUP_ID,
82 13:DSP_LAYER_GROUP_ID,
83 14:DSP_LAYER_GROUP_ID}
85 try:
86 print('TIDL API: performing one time initialization ...')
88 eve = Executor(DeviceType.EVE, eve_device_ids, c, EVE_LAYER_GROUP_ID)
89 dsp = Executor(DeviceType.DSP, dsp_device_ids, c, DSP_LAYER_GROUP_ID)
91 num_eve_eos = eve.get_num_execution_objects()
92 num_dsp_eos = dsp.get_num_execution_objects()
94 # On AM5749, create a total of 4 pipelines (EOPs):
95 # EOPs[0] : { EVE1, DSP1 }
96 # EOPs[1] : { EVE1, DSP1 } for double buffering
97 # EOPs[2] : { EVE2, DSP2 }
98 # EOPs[3] : { EVE2, DSP2 } for double buffering
99 PIPELINE_DEPTH = 2
101 eops = []
102 num_pipe = max(num_eve_eos, num_dsp_eos)
103 for i in range(num_pipe):
104 for i in range(PIPELINE_DEPTH):
105 eops.append(ExecutionObjectPipeline([eve.at(i % num_eve_eos),
106 dsp.at(i % num_dsp_eos)]))
108 allocate_memory(eops)
110 # Open input, output files
111 f_in = open(c.in_data, 'rb')
112 f_out = open(c.out_data, 'wb')
115 print('TIDL API: processing input frames ...')
117 num_eops = len(eops)
118 for frame_index in range(c.num_frames+num_eops):
119 eop = eops[frame_index % num_eops]
121 if eop.process_frame_wait():
122 write_output(eop, f_out)
124 if read_frame(eop, frame_index, c, f_in):
125 eop.process_frame_start_async()
128 f_in.close()
129 f_out.close()
131 free_memory(eops)
132 except TidlError as err:
133 print(err)
136 DESCRIPTION = 'Process frames using ExecutionObjectPipeline. '\
137 'Each ExecutionObjectPipeline processes a single frame. '\
138 'The example also uses double buffering on the EOPs to '\
139 'hide frame read overhead.'
141 def parse_args():
142 """Parse input arguments"""
144 parser = argparse.ArgumentParser(description=DESCRIPTION)
145 parser.add_argument('-n', '--num_frames',
146 type=int,
147 default=16,
148 help='Number of frames to process')
149 args = parser.parse_args()
151 return args
154 if __name__ == '__main__':
155 main()