Clean up required subgraph cfg file entries
[tidl/tidl-api.git] / tidl_api / src / configuration.cpp
1 /******************************************************************************
2  * Copyright (c) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *      * Redistributions of source code must retain the above copyright
8  *        notice, this list of conditions and the following disclaimer.
9  *      * Redistributions in binary form must reproduce the above copyright
10  *        notice, this list of conditions and the following disclaimer in the
11  *        documentation and/or other materials provided with the distribution.
12  *      * Neither the name of Texas Instruments Incorporated nor the
13  *        names of its contributors may be used to endorse or promote products
14  *        derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  *  THE POSSIBILITY OF SUCH DAMAGE.
27  *****************************************************************************/
29 #include <string>
30 #include <fstream>
32 #include "configuration.h"
33 #include "parameters.h"
35 using namespace tidl;
37 Configuration::Configuration(): numFrames(0), inHeight(0), inWidth(0),
38                      inNumChannels(0),
39                      noZeroCoeffsPercentage(100),
40                      preProcType(0),
41                      runFullNet(false),
42                      NETWORK_HEAP_SIZE(internal::DEFAULT_NETWORK_HEAP_SIZE),
43                      PARAM_HEAP_SIZE(internal::DEFAULT_PARAM_HEAP_SIZE),
44                      enableOutputTrace(false),
45                      enableApiTrace(false),
46                      showHeapStats(false),
47                      quantHistoryParam1(20),
48                      quantHistoryParam2(5),
49                      quantMargin(0),
50                      isSubgraphCfg(false),
51                      inConvType(),
52                      inIsSigned(),
53                      inScaleF2Q(),
54                      inIsNCHW(),
55                      outConvType(),
56                      outIsSigned(),
57                      outScaleF2Q(),
58                      outIsNCHW()
59 {
60 }
62 void Configuration::Print(std::ostream &os) const
63 {
64     os << "Configuration"
65        << "\nFrame=      " << numFrames << " " << inWidth << "x"
66                            << inHeight << "x" << inNumChannels
67        << "\nPreProcType              " << preProcType
68        << "\nRunFullNet               " << runFullNet
69        << "\nInputFile                " << inData
70        << "\nOutputFile               " << outData
71        << "\nNetwork                  " << netBinFile
72        << "\nParameters               " << paramsBinFile
73        << "\nEO Heap Size (MB)        " << (NETWORK_HEAP_SIZE >> 20)
74        << "\nParameter heap size (MB) " << (PARAM_HEAP_SIZE >> 20)
75        << "\n";
76 }
78 #include <sys/stat.h>
79 bool Configuration::Validate() const
80 {
81     int errors = 0;
82     struct stat buffer;
84     if (! isSubgraphCfg)
85     {
86         if (inHeight == 0 || inWidth == 0)
87         {
88             std::cerr << "cfg: inHeight, inWidth must be > 0" << std::endl;
89             errors++;
90         }
92         if (inNumChannels < 1)
93         {
94             std::cerr << "cfg: inNumChannels must be > 1" << std::endl;
95             errors++;
96         }
98         if (!inData.empty() && stat(inData.c_str(), &buffer) != 0)
99         {
100             std::cerr << "cfg: inData not found: " << inData << std::endl;
101             errors++;
102         }
103     }
104     else
105     {
106         if (inConvType.size() == 0 || inIsSigned.size() == 0 ||
107             inScaleF2Q.size() == 0 || inIsNCHW.size() == 0 ||
108             outConvType.size() == 0 || outIsSigned.size() == 0 ||
109             outScaleF2Q.size() == 0 || outIsNCHW.size() == 0)
110         {
111             std::cerr << "cfg: subgraph data info not found" << std::endl;
112             errors++;
113         }
115         if (inConvType.size() != inIsNCHW.size() ||
116             outConvType.size() != outIsNCHW.size())
117         {
118             std::cerr << "cfg: Mismatching subgraph data info" << std::endl;
119             errors++;
120         }
121     }
123     if (stat(netBinFile.c_str(), &buffer) != 0)
124     {
125         std::cerr << "cfg: netBinFile not found: " << netBinFile << std::endl;
126         errors++;
127     }
129     if (stat(paramsBinFile.c_str(), &buffer) != 0)
130     {
131         std::cerr << "cfg: paramsBinFile not found: " << paramsBinFile
132                   << std::endl;
133         errors++;
134     }
136     if (errors > 0)
137         return false;
139     return true;