blob: 633176219cfb1874700895d646a78bf9974144cf [file] [log] [blame]
Jim Flynne571d332019-04-15 14:34:17 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#pragma once
6
7#include "InferenceTest.hpp"
8#include "DeepSpeechV1Database.hpp"
9
10#include <boost/assert.hpp>
11#include <boost/log/trivial.hpp>
12#include <boost/numeric/conversion/cast.hpp>
13#include <boost/test/tools/floating_point_comparison.hpp>
14
15#include <vector>
16
17namespace
18{
19
20template<typename Model>
21class DeepSpeechV1TestCase : public InferenceModelTestCase<Model>
22{
23public:
24 DeepSpeechV1TestCase(Model& model,
25 unsigned int testCaseId,
26 const DeepSpeechV1TestCaseData& testCaseData)
27 : InferenceModelTestCase<Model>(model,
28 testCaseId,
29 { testCaseData.m_InputData.m_InputSeq,
30 testCaseData.m_InputData.m_StateC,
31 testCaseData.m_InputData.m_StateH},
32 { k_OutputSize1, k_OutputSize2, k_OutputSize3 })
33 , m_FloatComparer(boost::math::fpc::percent_tolerance(1.0f))
34 , m_ExpectedOutputs({testCaseData.m_ExpectedOutputData.m_InputSeq, testCaseData.m_ExpectedOutputData.m_StateC,
35 testCaseData.m_ExpectedOutputData.m_StateH})
36 {}
37
38 TestCaseResult ProcessResult(const InferenceTestOptions& options) override
39 {
40 const std::vector<float>& output1 = boost::get<std::vector<float>>(this->GetOutputs()[0]); // logits
41 BOOST_ASSERT(output1.size() == k_OutputSize1);
42
43 const std::vector<float>& output2 = boost::get<std::vector<float>>(this->GetOutputs()[1]); // new_state_c
44 BOOST_ASSERT(output2.size() == k_OutputSize2);
45
46 const std::vector<float>& output3 = boost::get<std::vector<float>>(this->GetOutputs()[2]); // new_state_h
47 BOOST_ASSERT(output3.size() == k_OutputSize3);
48
49 // Check each output to see whether it is the expected value
50 for (unsigned int j = 0u; j < output1.size(); j++)
51 {
52 if(!m_FloatComparer(output1[j], m_ExpectedOutputs.m_InputSeq[j]))
53 {
54 BOOST_LOG_TRIVIAL(error) << "InputSeq for Lstm " << this->GetTestCaseId() <<
55 " is incorrect at" << j;
56 return TestCaseResult::Failed;
57 }
58 }
59
60 for (unsigned int j = 0u; j < output2.size(); j++)
61 {
62 if(!m_FloatComparer(output2[j], m_ExpectedOutputs.m_StateC[j]))
63 {
64 BOOST_LOG_TRIVIAL(error) << "StateC for Lstm " << this->GetTestCaseId() <<
65 " is incorrect";
66 return TestCaseResult::Failed;
67 }
68 }
69
70 for (unsigned int j = 0u; j < output3.size(); j++)
71 {
72 if(!m_FloatComparer(output3[j], m_ExpectedOutputs.m_StateH[j]))
73 {
74 BOOST_LOG_TRIVIAL(error) << "StateH for Lstm " << this->GetTestCaseId() <<
75 " is incorrect";
76 return TestCaseResult::Failed;
77 }
78 }
79 return TestCaseResult::Ok;
80 }
81
82private:
83
84 static constexpr unsigned int k_OutputSize1 = 464u;
85 static constexpr unsigned int k_OutputSize2 = 2048u;
86 static constexpr unsigned int k_OutputSize3 = 2048u;
87
88 boost::math::fpc::close_at_tolerance<float> m_FloatComparer;
89 LstmInput m_ExpectedOutputs;
90};
91
92template <typename Model>
93class DeepSpeechV1TestCaseProvider : public IInferenceTestCaseProvider
94{
95public:
96 template <typename TConstructModelCallable>
97 explicit DeepSpeechV1TestCaseProvider(TConstructModelCallable constructModel)
98 : m_ConstructModel(constructModel)
99 {}
100
101 virtual void AddCommandLineOptions(boost::program_options::options_description& options) override
102 {
103 namespace po = boost::program_options;
104
105 options.add_options()
106 ("input-seq-dir,s", po::value<std::string>(&m_InputSeqDir)->required(),
107 "Path to directory containing test data for m_InputSeq");
108 options.add_options()
109 ("prev-state-c-dir,c", po::value<std::string>(&m_PrevStateCDir)->required(),
110 "Path to directory containing test data for m_PrevStateC");
111 options.add_options()
112 ("prev-state-h-dir,h", po::value<std::string>(&m_PrevStateHDir)->required(),
113 "Path to directory containing test data for m_PrevStateH");
114 options.add_options()
115 ("logits-dir,l", po::value<std::string>(&m_LogitsDir)->required(),
116 "Path to directory containing test data for m_Logits");
117 options.add_options()
118 ("new-state-c-dir,C", po::value<std::string>(&m_NewStateCDir)->required(),
119 "Path to directory containing test data for m_NewStateC");
120 options.add_options()
121 ("new-state-h-dir,H", po::value<std::string>(&m_NewStateHDir)->required(),
122 "Path to directory containing test data for m_NewStateH");
123
124 Model::AddCommandLineOptions(options, m_ModelCommandLineOptions);
125 }
126
Jim Flynnc2ebc632019-04-17 10:16:58 +0100127 virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) override
Jim Flynne571d332019-04-15 14:34:17 +0100128 {
129 if (!ValidateDirectory(m_InputSeqDir))
130 {
131 return false;
132 }
133
134 if (!ValidateDirectory(m_PrevStateCDir))
135 {
136 return false;
137 }
138
139 if (!ValidateDirectory(m_PrevStateHDir))
140 {
141 return false;
142 }
143
144 if (!ValidateDirectory(m_LogitsDir))
145 {
146 return false;
147 }
148
149 if (!ValidateDirectory(m_NewStateCDir))
150 {
151 return false;
152 }
153
154 if (!ValidateDirectory(m_NewStateHDir))
155 {
156 return false;
157 }
158
Jim Flynnc2ebc632019-04-17 10:16:58 +0100159 m_Model = m_ConstructModel(commonOptions, m_ModelCommandLineOptions);
Jim Flynne571d332019-04-15 14:34:17 +0100160 if (!m_Model)
161 {
162 return false;
163 }
164 m_Database = std::make_unique<DeepSpeechV1Database>(m_InputSeqDir.c_str(), m_PrevStateCDir.c_str(),
165 m_PrevStateHDir.c_str(), m_LogitsDir.c_str(),
166 m_NewStateCDir.c_str(), m_NewStateHDir.c_str());
167 if (!m_Database)
168 {
169 return false;
170 }
171
172 return true;
173 }
174
175 std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) override
176 {
177 std::unique_ptr<DeepSpeechV1TestCaseData> testCaseData = m_Database->GetTestCaseData(testCaseId);
178 if (!testCaseData)
179 {
180 return nullptr;
181 }
182
183 return std::make_unique<DeepSpeechV1TestCase<Model>>(*m_Model, testCaseId, *testCaseData);
184 }
185
186private:
187 typename Model::CommandLineOptions m_ModelCommandLineOptions;
Jim Flynnc2ebc632019-04-17 10:16:58 +0100188 std::function<std::unique_ptr<Model>(const InferenceTestOptions&,
189 typename Model::CommandLineOptions)> m_ConstructModel;
Jim Flynne571d332019-04-15 14:34:17 +0100190 std::unique_ptr<Model> m_Model;
191
192 std::string m_InputSeqDir;
193 std::string m_PrevStateCDir;
194 std::string m_PrevStateHDir;
195 std::string m_LogitsDir;
196 std::string m_NewStateCDir;
197 std::string m_NewStateHDir;
198
199 std::unique_ptr<DeepSpeechV1Database> m_Database;
200};
201
202} // anonymous namespace