]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - tidl/tidl-api.git/blob - examples/pybind/one_eo_per_frame.py
Added Python 3 bindings for TIDL API
[tidl/tidl-api.git] / examples / pybind / one_eo_per_frame.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 from tidl import DeviceId, DeviceType, Configuration, Executor, TidlError
31 from tidl import allocate_memory, free_memory
33 from tidl_app_utils import read_frame, write_output, report_time
35 import argparse
37 def main(config_file, num_frames):
38     c = Configuration()
39     c.read_from_file(config_file)
40     c.enable_api_trace = False
41     c.num_frames = num_frames
43     num_dsp = Executor.get_num_devices(DeviceType.DSP)
44     num_eve = Executor.get_num_devices(DeviceType.EVE)
46     if (num_dsp == 0 and num_eve == 0):
47         print('No TIDL API capable devices available')
48         return
50     run(num_eve, num_dsp, c)
52     return
54 def run(num_eve, num_dsp, c):
55     """ Run the network on the specified device type and number of devices"""
57     print('Running network across {} EVEs, {} DSPs'.format(num_eve, num_dsp))
59     dsp_device_ids = set([DeviceId.ID0, DeviceId.ID1,
60                           DeviceId.ID2, DeviceId.ID3][0:num_dsp])
61     eve_device_ids = set([DeviceId.ID0, DeviceId.ID1,
62                           DeviceId.ID2, DeviceId.ID3][0:num_eve])
64     # Heap sizes for this network determined using Configuration.showHeapStats
65     c.param_heap_size   = (3 << 20)
66     c.network_heap_size = (20 << 20)
69     try:
70         print('TIDL API: performing one time initialization ...')
72         eve = Executor(DeviceType.EVE, eve_device_ids, c, 1)
73         dsp = Executor(DeviceType.DSP, dsp_device_ids, c, 1)
75         # Collect all EOs from EVE and DSP executors
76         eos = []
77         for i in range(eve.get_num_execution_objects()):
78             eos.append(eve.at(i))
80         for i in range(dsp.get_num_execution_objects()):
81             eos.append(dsp.at(i))
83         allocate_memory(eos)
85         # Open input, output files
86         f_in  = open(c.in_data, 'rb')
87         f_out = open(c.out_data, 'wb')
90         print('TIDL API: processing input frames ...')
92         num_eos = len(eos)
93         for frame_index in range(c.num_frames+num_eos):
94             eo = eos [frame_index % num_eos]
96             if (eo.process_frame_wait()):
97                 report_time(eo)
98                 write_output(eo, f_out)
100             if (read_frame(eo, frame_index, c, f_in)):
101                 eo.process_frame_start_async()
104         f_in.close()
105         f_out.close()
107         free_memory(eos)
108     except TidlError as err:
109         print (err)
111     return
113 if __name__ == '__main__':
114     parser = argparse.ArgumentParser(description=
115                        'Process frames using all available Execution Objects. '
116                        'Each ExecutionObject processes a single frame')
117     parser.add_argument('-n', '--num_frames',
118                         type=int,
119                         default=1,
120                         help='Number of frames to process')
121     args = parser.parse_args()
123     # Heaps are sized for the j11_v2 network. Changing the network will
124     # require updating network_heap_size and param_heap_size
125     main('../test/testvecs/config/infer/tidl_config_j11_v2.txt', 
126          args.num_frames)