]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - tidl/tidl-api.git/blob - examples/pybind/two_eo_per_frame.py
a1cd58c0dc9d547b94a6980f31c8b14ae0162499
[tidl/tidl-api.git] / examples / pybind / two_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.
29 """ Process frames using ExecutionObjectPipeline.
30     Each ExecutionObjectPipeline processes a single frame.
31 """
33 import argparse
35 from tidl import DeviceId, DeviceType, Configuration, TidlError
36 from tidl import Executor, ExecutionObjectPipeline
37 from tidl import allocate_memory, free_memory, enable_time_stamps
39 from tidl_app_utils import read_frame, write_output
42 def main():
43     """Read the configuration and run the network"""
45     args = parse_args()
47     # Heaps are sized for the j11_v2 network. Changing the network will
48     # require updating network_heap_size and param_heap_size
49     config_file = '../test/testvecs/config/infer/tidl_config_j11_v2.txt'
51     configuration = Configuration()
52     configuration.read_from_file(config_file)
53     configuration.enable_api_trace = False
54     configuration.num_frames = args.num_frames
56     # Heap sizes for this network determined using Configuration.showHeapStats
57     configuration.param_heap_size = (3 << 20)
58     configuration.network_heap_size = (20 << 20)
60     num_dsp = Executor.get_num_devices(DeviceType.DSP)
61     num_eve = Executor.get_num_devices(DeviceType.EVE)
63     if num_dsp == 0 or num_eve == 0:
64         print('This example required EVEs and DSPs.')
65         return
67     enable_time_stamps("2eo_timestamp.log", 16)
68     run(num_eve, num_dsp, configuration)
70 # Run layer group 1 on EVE, 2 on DSP
71 EVE_LAYER_GROUP_ID = 1
72 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         eops = []
98         num_pipe = max(num_eve_eos, num_dsp_eos)
99         for i in range(num_pipe):
100             eops.append(ExecutionObjectPipeline([eve.at(i % num_eve_eos),
101                                                  dsp.at(i % num_dsp_eos)]))
103         allocate_memory(eops)
105         # Open input, output files
106         f_in = open(c.in_data, 'rb')
107         f_out = open(c.out_data, 'wb')
110         print('TIDL API: processing input frames ...')
112         num_eops = len(eops)
113         for frame_index in range(c.num_frames+num_eops):
114             eop = eops[frame_index % num_eops]
116             if eop.process_frame_wait():
117                 write_output(eop, f_out)
119             if read_frame(eop, frame_index, c, f_in):
120                 eop.process_frame_start_async()
122         f_in.close()
123         f_out.close()
125         free_memory(eops)
126     except TidlError as err:
127         print(err)
130 DESCRIPTION = 'Process frames using ExecutionObjectPipeline. '\
131               'Each ExecutionObjectPipeline processes a single frame'
133 def parse_args():
134     """Parse input arguments"""
136     parser = argparse.ArgumentParser(description=DESCRIPTION)
137     parser.add_argument('-n', '--num_frames',
138                         type=int,
139                         default=16,
140                         help='Number of frames to process')
141     args = parser.parse_args()
143     return args
146 if __name__ == '__main__':
147     main()