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.
29 """ Process each frame using a single ExecutionObject.
30 Increase throughput by using multiple ExecutionObjects.
31 """
33 import argparse
35 from tidl import DeviceId, DeviceType, Configuration, Executor, TidlError
36 from tidl import allocate_memory, free_memory, enable_time_stamps
38 from tidl_app_utils import read_frame, write_output, report_time
41 def main():
42 """Read the configuration and run the network"""
44 args = parse_args()
46 # Heaps are sized for the j11_v2 network. Changing the network will
47 # require updating network_heap_size and param_heap_size
48 config_file = '../test/testvecs/config/infer/tidl_config_j11_v2.txt'
50 configuration = Configuration()
51 configuration.read_from_file(config_file)
52 configuration.enable_api_trace = False
53 configuration.num_frames = args.num_frames
55 num_dsp = Executor.get_num_devices(DeviceType.DSP)
56 num_eve = Executor.get_num_devices(DeviceType.EVE)
58 if num_dsp == 0 and num_eve == 0:
59 print('No TIDL API capable devices available')
60 return
62 enable_time_stamps("1eo_timestamp.log", 16)
63 run(num_eve, num_dsp, configuration)
65 return
68 DESCRIPTION = 'Process frames using all available Execution Objects. '\
69 'Each ExecutionObject processes a single frame'
71 def parse_args():
72 """Parse input arguments"""
74 parser = argparse.ArgumentParser(description=DESCRIPTION)
75 parser.add_argument('-n', '--num_frames',
76 type=int,
77 default=16,
78 help='Number of frames to process')
79 args = parser.parse_args()
81 return args
84 def run(num_eve, num_dsp, configuration):
85 """ Run the network on the specified device type and number of devices"""
87 print('Running network across {} EVEs, {} DSPs'.format(num_eve, num_dsp))
89 dsp_device_ids = set([DeviceId.ID0, DeviceId.ID1,
90 DeviceId.ID2, DeviceId.ID3][0:num_dsp])
91 eve_device_ids = set([DeviceId.ID0, DeviceId.ID1,
92 DeviceId.ID2, DeviceId.ID3][0:num_eve])
94 # Heap sizes for this network determined using Configuration.showHeapStats
95 configuration.param_heap_size = (3 << 20)
96 configuration.network_heap_size = (20 << 20)
99 try:
100 print('TIDL API: performing one time initialization ...')
102 eve = Executor(DeviceType.EVE, eve_device_ids, configuration, 1)
103 dsp = Executor(DeviceType.DSP, dsp_device_ids, configuration, 1)
105 # Collect all EOs from EVE and DSP executors
106 eos = []
107 for i in range(eve.get_num_execution_objects()):
108 eos.append(eve.at(i))
110 for i in range(dsp.get_num_execution_objects()):
111 eos.append(dsp.at(i))
113 allocate_memory(eos)
115 # Open input, output files
116 f_in = open(configuration.in_data, 'rb')
117 f_out = open(configuration.out_data, 'wb')
120 print('TIDL API: processing input frames ...')
122 num_eos = len(eos)
123 for frame_index in range(configuration.num_frames+num_eos):
124 execution_object = eos[frame_index % num_eos]
126 if execution_object.process_frame_wait():
127 report_time(execution_object)
128 write_output(execution_object, f_out)
130 if read_frame(execution_object, frame_index, configuration, f_in):
131 execution_object.process_frame_start_async()
134 f_in.close()
135 f_out.close()
137 free_memory(eos)
138 except TidlError as err:
139 print(err)
142 if __name__ == '__main__':
143 main()