2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
5 #include "WorkloadData.hpp"
7 #include "CpuTensorHandle.hpp"
14 #include <boost/format.hpp>
19 //---------------------------------------------------------------
20 DataType GetBiasDataType(DataType inputDataType)
22 switch (inputDataType)
24 case DataType::Float16:
25 return DataType::Float16;
26 case DataType::Float32:
27 return DataType::Float32;
28 case DataType::QuantisedAsymm8:
29 return DataType::Signed32;
31 BOOST_ASSERT_MSG(false, "Invalid input data type");
32 return DataType::Float32;
39 //---------------------------------------------------------------
40 //android ndk does not support std::to_string function.
42 std::string to_string(T value)
44 std::ostringstream os;
49 //---------------------------------------------------------------
50 void ValidatePointer(const void* ptr, std::string const& descName, std::string const& paramName)
54 throw InvalidArgumentException(descName + ": Invalid null pointer. The " +
55 paramName + " parameter must be set.");
59 //---------------------------------------------------------------
60 void ValidateTensorShapesMatch(const TensorInfo& first,
61 const TensorInfo& second,
62 std::string const& descName,
63 std::string const& firstName,
64 std::string const& secondName)
66 if (first.GetShape() != second.GetShape())
68 throw InvalidArgumentException(descName + ": "
69 + firstName + " & " + secondName + " must have identical shapes");
73 //---------------------------------------------------------------
74 void ValidateNoInputs(const WorkloadInfo& workloadInfo, std::string const& descName)
76 if (workloadInfo.m_InputTensorInfos.size() != 0)
78 throw InvalidArgumentException(descName +
79 ": Requires no inputs. " +
80 to_string(workloadInfo.m_InputTensorInfos.size()) + " has been provided.");
84 //---------------------------------------------------------------
85 void ValidateSingleInput(const WorkloadInfo& workloadInfo, std::string const& descName)
87 if (workloadInfo.m_InputTensorInfos.size() != 1)
89 throw InvalidArgumentException(descName +
90 ": Requires exactly one input. " +
91 to_string(workloadInfo.m_InputTensorInfos.size()) + " has been provided." );
95 //---------------------------------------------------------------
96 void ValidateTwoInputs(const WorkloadInfo& workloadInfo, std::string const& descName)
98 if (workloadInfo.m_InputTensorInfos.size() != 2)
100 throw InvalidArgumentException(descName +
101 ": Requires exactly two workloadInfo.m_InputTensorInfos. " +
102 to_string(workloadInfo.m_InputTensorInfos.size()) + " have been provided.");
106 //---------------------------------------------------------------
107 void ValidateSingleOutput(const WorkloadInfo& workloadInfo, std::string const& descName)
109 if (workloadInfo.m_OutputTensorInfos.size() != 1)
111 throw InvalidArgumentException(descName +
112 ": Requires exactly one output. " +
113 to_string(workloadInfo.m_OutputTensorInfos.size()) + " has been provided.");
117 //---------------------------------------------------------------
118 void ValidateTensorNumDimensions(const TensorInfo& tensor,
119 std::string const& descName,
120 unsigned int numDimensions,
121 std::string const& tensorName)
123 if (tensor.GetNumDimensions() != numDimensions)
125 throw InvalidArgumentException(descName + ": Expected " + to_string(numDimensions) + " but got " +
126 to_string(tensor.GetNumDimensions()) + " dimensions for " +
127 tensorName + " tensor.");
131 //---------------------------------------------------------------
132 void ValidateTensorDataType(const TensorInfo& tensor, DataType dataType,
133 const std::string& descName, std::string const& tensorName)
135 if (tensor.GetDataType() != dataType)
137 throw InvalidArgumentException(descName + ": Expected data type " + GetDataTypeName(dataType) + " but got " +
138 GetDataTypeName(tensor.GetDataType()) + " for " + tensorName + " tensor.");
142 //---------------------------------------------------------------
143 void ValidateBiasTensorQuantization(const TensorInfo& biasTensor, const TensorInfo& inputTensorInfo,
144 const TensorInfo& weightsTensorInfo, const std::string& descName)
146 if (biasTensor.GetQuantizationOffset() != 0)
148 throw InvalidArgumentException(descName + ": Expected zero quantization offset for bias tensor but got " +
149 to_string(biasTensor.GetQuantizationOffset()));
151 const float expectedScale = inputTensorInfo.GetQuantizationScale() * weightsTensorInfo.GetQuantizationScale();
152 if (std::abs(biasTensor.GetQuantizationScale() - expectedScale) > 0.000000001f)
154 // Print the float values with extra precision to see very small differences
155 std::stringstream msg;
156 msg << std::setprecision(10) << descName << ": Expected " << expectedScale <<
157 " quantization scale for bias tensor (the product of the input and weight scales), but got " <<
158 biasTensor.GetQuantizationScale();
159 throw InvalidArgumentException(msg.str());
163 //---------------------------------------------------------------
164 void ValidateTensors(const std::vector<ITensorHandle*>& vec,
165 unsigned int numExpected,
166 const std::string& descName,
167 const std::string& varName)
169 if (vec.empty() && numExpected > 0)
171 throw InvalidArgumentException(descName + ": Invalid empty " + varName + " array.");
174 for (unsigned int i = 0; i < numExpected; ++i)
178 throw InvalidArgumentException(descName + ": Invalid NULL for " + varName + to_string(i));
183 //---------------------------------------------------------------
184 void ValidateBroadcastTensorShapesMatch(const TensorInfo& first,
185 const TensorInfo& second,
186 const TensorInfo& output,
187 std::string const& descName,
188 std::string const& firstName,
189 std::string const& secondName)
191 // Tensors must have the same number of dimensions in order to be explicit about which dimensions will get
193 if (first.GetNumDimensions() != second.GetNumDimensions())
195 throw InvalidArgumentException(descName + ": Tensors "
196 + firstName + " & " + secondName
197 + " must have the same number of dimensions in order to be broadcasted");
199 uint32_t numDims = first.GetNumDimensions();
200 std::vector<uint32_t> outputDims(numDims, 0u);
201 for (uint32_t i = 0; i < numDims; i++)
203 const bool dimsNotEqual = first.GetShape()[i] != second.GetShape()[i];
204 const bool dimsNotOne = (first.GetShape()[i] != 1) && (second.GetShape()[i] != 1);
205 if (dimsNotEqual && dimsNotOne)
207 throw InvalidArgumentException("Broadcasting is not possible for incompatible shapes");
209 outputDims[i] = std::max(first.GetShape()[i], second.GetShape()[i]);
211 TensorShape broadcastShape = TensorShape(boost::numeric_cast<unsigned int>(outputDims.size()), outputDims.data());
212 if (broadcastShape != output.GetShape())
214 throw InvalidArgumentException(descName + ": The tensor shape resulting from adding "
215 + firstName + " & " + secondName
216 + " does not match the output shape");
220 //---------------------------------------------------------------
221 /// Validates that the output tensor's quantization scale is greater than the product
222 /// of the two input tensors' quantization scales. This is a requirement of the implementation of
223 /// the quantized multiplication.
224 void ValidateTensorQuantizationMultiplier(const TensorInfo& inputTensor1, const TensorInfo& inputTensor2,
225 const TensorInfo& outputTensorInfo, std::string const& descName,
226 const std::string& inputTensor1Name, const std::string& inputTensor2Name, const std::string& outputTensorName)
228 if (outputTensorInfo.GetDataType() == DataType::QuantisedAsymm8)
230 if (outputTensorInfo.GetQuantizationScale() <=
231 inputTensor1.GetQuantizationScale() * inputTensor2.GetQuantizationScale())
233 std::stringstream msg;
234 msg << descName << ": Quantization scale of " << outputTensorName << " is not greater than " <<
235 "the product of the " << inputTensor1Name << " and " << inputTensor2Name << " tensors";
236 throw InvalidArgumentException(msg.str());
243 void QueueDescriptor::ValidateInputsOutputs(const std::string& descName,
244 unsigned int numExpectedIn, unsigned int numExpectedOut) const
246 ValidateTensors(m_Inputs, numExpectedIn, descName, "input");
247 ValidateTensors(m_Outputs, numExpectedOut, descName, "output");
250 //---------------------------------------------------------------
251 void MemCopyQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
253 ValidateSingleInput(workloadInfo, "MemCopyQueueDescriptor");
254 ValidateSingleOutput(workloadInfo, "MemCopyQueueDescriptor");
256 if (workloadInfo.m_InputTensorInfos.size() != workloadInfo.m_OutputTensorInfos.size())
258 throw InvalidArgumentException(boost::str(
259 boost::format("Number of input infos (%1%) does not match the number of output infos (%2%)")
260 % workloadInfo.m_InputTensorInfos.size() % workloadInfo.m_OutputTensorInfos.size()));
263 for (std::size_t i = 0; i < workloadInfo.m_InputTensorInfos.size(); ++i)
265 if (workloadInfo.m_InputTensorInfos[i].GetNumElements() !=
266 workloadInfo.m_OutputTensorInfos[i].GetNumElements())
268 throw InvalidArgumentException(boost::str(
269 boost::format("Number of elements for tensor input and output %1% does not match")
274 if (m_Inputs.size() != m_Outputs.size())
276 throw InvalidArgumentException(boost::str(
277 boost::format("Number of inputs (%1%) does not match the number of outputs (%2%)")
278 % m_Inputs.size() % m_Outputs.size()));
281 for (unsigned int i = 0; i < m_Inputs.size(); ++i)
285 throw InvalidArgumentException(boost::str(boost::format("Invalid null input %1%") % i));
290 throw InvalidArgumentException(boost::str(boost::format("Invalid null output %1%") % i));
295 //---------------------------------------------------------------
296 void ActivationQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
298 ValidateSingleInput(workloadInfo, "ActivationQueueDescriptor");
299 ValidateSingleOutput(workloadInfo, "ActivationQueueDescriptor");
300 ValidateTensorShapesMatch(workloadInfo.m_InputTensorInfos[0],
301 workloadInfo.m_OutputTensorInfos[0],
302 "ActivationQueueDescriptor",
307 //---------------------------------------------------------------
308 void SoftmaxQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
310 ValidateSingleInput(workloadInfo, "SoftmaxQueueDescriptor");
311 ValidateSingleOutput(workloadInfo, "SoftmaxQueueDescriptor");
312 ValidateTensorNumDimensions(workloadInfo.m_InputTensorInfos[0], "SoftmaxQueueDescriptor", 2, "input");
313 ValidateTensorNumDimensions(workloadInfo.m_OutputTensorInfos[0], "SoftmaxQueueDescriptor", 2, "output");
315 ValidateTensorShapesMatch(workloadInfo.m_InputTensorInfos[0],
316 workloadInfo.m_OutputTensorInfos[0],
317 "SoftmaxQueueDescriptor",
322 //---------------------------------------------------------------
323 void SplitterQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
325 ValidateSingleInput(workloadInfo, "SplitterQueueDescriptor");
327 if (workloadInfo.m_OutputTensorInfos.size() <= 0)
329 throw InvalidArgumentException("SplitterQueueDescriptor: At least one output needs to be provided.");
332 if (workloadInfo.m_OutputTensorInfos.size() != m_ViewOrigins.size())
334 throw InvalidArgumentException(
335 "SplitterQueueDescriptor: Number of split windows "
336 "has to match number of workloadInfo.m_OutputTensorInfos. "
337 "Number of windows: " +
338 to_string(m_ViewOrigins.size()) +
339 ". Number of workloadInfo.m_OutputTensorInfos: " + to_string(workloadInfo.m_OutputTensorInfos.size()));
342 //The dimensionality of all the windows has to match the dimensionality (not shape) of the input.
343 std::size_t inputDims = workloadInfo.m_InputTensorInfos[0].GetNumDimensions();
344 for(unsigned int w = 0; w < m_ViewOrigins.size(); ++w )
346 //Checks that the dimensionality of input is same as the split windows.
347 ViewOrigin const& e = m_ViewOrigins[w];
348 if (e.m_Origin.size() != inputDims)
350 throw InvalidArgumentException("SplitterQueueDescriptor: Window origin have to "
351 "have the same dimensionality as the input tensor. "
352 "Window origin (index: " +
353 to_string(w) + ") has " + to_string(e.m_Origin.size()) +
354 " dimensions, the input "
356 to_string(inputDims) + " dimensions.");
358 for (unsigned int i = 0; i < e.m_Origin.size(); ++i)
360 if (e.m_Origin[i] + workloadInfo.m_OutputTensorInfos[w].GetShape()[i] >
361 workloadInfo.m_InputTensorInfos[0].GetShape()[i])
363 throw InvalidArgumentException("SplitterQueueDescriptor: Window extent coordinates have to "
364 "be smaller or equal than the size of the input in that coord.");
370 //---------------------------------------------------------------
371 void MergerQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
373 ValidateSingleOutput(workloadInfo, "MergerQueueDescriptor");
375 if (m_Inputs.size() <= 0)
377 throw InvalidArgumentException("MergerQueueDescriptor: At least one input needs to be provided.");
379 if (m_Outputs.size() <= 0)
381 throw InvalidArgumentException("MergerQueueDescriptor: At least one output needs to be provided.");
384 if (workloadInfo.m_InputTensorInfos.size() <= 0)
386 throw InvalidArgumentException("MergerQueueDescriptor: At least one TensorInfo input needs to be provided.");
388 if (workloadInfo.m_OutputTensorInfos.size() <= 0)
390 throw InvalidArgumentException("MergerQueueDescriptor: At least one TensorInfo output needs to be provided.");
393 if (workloadInfo.m_InputTensorInfos.size() != m_ViewOrigins.size())
395 throw InvalidArgumentException(
396 "MergerQueueDescriptor: Number of split windows "
397 "has to match number of workloadInfo.m_InputTensorInfos. "
398 "Number of windows: " +
399 to_string(m_ViewOrigins.size()) +
400 ". Number of workloadInfo.m_InputTensorInfos: " + to_string(workloadInfo.m_InputTensorInfos.size()));
403 //The dimensionality of all the windows has to match the dimensionality (not shape) of the output.
404 std::size_t outputDims = workloadInfo.m_OutputTensorInfos[0].GetNumDimensions();
405 for(unsigned int w = 0; w < m_ViewOrigins.size(); ++w )
407 //Checks that the dimensionality of output is same as the split windows.
408 ViewOrigin const& e = m_ViewOrigins[w];
409 if (e.m_Origin.size() != outputDims)
411 throw InvalidArgumentException("MergerQueueDescriptor: Window origin have to "
412 "have the same dimensionality as the output tensor. "
413 "Window origin (index: " +
414 to_string(w) + ") has " + to_string(e.m_Origin.size()) +
415 " dimensions, the output "
417 to_string(outputDims) + " dimensions.");
419 //Checks that the merge windows are within the output tensor.
420 for (unsigned int i = 0; i < e.m_Origin.size(); ++i)
422 if (e.m_Origin[i] + workloadInfo.m_InputTensorInfos[w].GetShape()[i]
423 > workloadInfo.m_OutputTensorInfos[0].GetShape()[i])
425 throw InvalidArgumentException("MergerQueueDescriptor: Window extent coordinates have to "
426 "be smaller or equal than the size of the output in that coord.");
432 //---------------------------------------------------------------
433 void FullyConnectedQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
435 ValidateSingleInput(workloadInfo, "FullyConnectedQueueDescriptor");
436 ValidateSingleOutput(workloadInfo, "FullyConnectedQueueDescriptor");
437 ValidateTensorNumDimensions(workloadInfo.m_OutputTensorInfos[0], "FullyConnectedQueueDescriptor", 2, "output");
439 if (!(workloadInfo.m_InputTensorInfos[0].GetNumDimensions() == 2 ||
440 workloadInfo.m_InputTensorInfos[0].GetNumDimensions() == 4))
442 throw InvalidArgumentException("FullyConnectedQueueDescriptor: Input tensor must have 2 or 4 dimensions.");
445 if (m_Weight == nullptr)
447 throw InvalidArgumentException("FullyConnectedQueueDescriptor: Weight tensor descriptor is missing.");
450 ValidateTensorNumDimensions(m_Weight->GetTensorInfo(), "FullyConnectedQueueDescriptor", 2, "weight");
452 if (m_Parameters.m_BiasEnabled)
454 if (m_Bias == nullptr)
456 throw InvalidArgumentException("FullyConnectedQueueDescriptor: Bias is enabled but "
457 "bias value tensor descriptor is missing.");
460 // Validates type and quantization values.
461 ValidateBiasTensorQuantization(m_Bias->GetTensorInfo(),
462 workloadInfo.m_InputTensorInfos[0], m_Weight->GetTensorInfo(), "FullyConnectedQueueDescriptor");
464 ValidateTensorDataType(m_Bias->GetTensorInfo(),
465 GetBiasDataType(workloadInfo.m_InputTensorInfos[0].GetDataType()),
466 "FullyConnectedQueueDescriptor", "bias");
468 ValidateTensorNumDimensions(m_Bias->GetTensorInfo(), "FullyConnectedQueueDescriptor", 1, "bias");
471 ValidateTensorQuantizationMultiplier(workloadInfo.m_InputTensorInfos[0], m_Weight->GetTensorInfo(),
472 workloadInfo.m_OutputTensorInfos[0], "FullyConnectedQueueDescriptor", "input", "weights", "output");
475 //---------------------------------------------------------------
476 void NormalizationQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
478 ValidateSingleInput(workloadInfo, "NormalizationQueueDescriptor");
479 ValidateSingleOutput(workloadInfo, "NormalizationQueueDescriptor");
480 ValidateTensorShapesMatch(workloadInfo.m_InputTensorInfos[0],
481 workloadInfo.m_OutputTensorInfos[0],
482 "NormalizationQueueDescriptor",
487 void AdditionQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
489 ValidateTwoInputs(workloadInfo, "AdditionQueueDescriptor");
490 ValidateSingleOutput(workloadInfo, "AdditionQueueDescriptor");
492 ValidateBroadcastTensorShapesMatch(workloadInfo.m_InputTensorInfos[0],
493 workloadInfo.m_InputTensorInfos[1],
494 workloadInfo.m_OutputTensorInfos[0],
495 "AdditionQueueDescriptor",
501 //---------------------------------------------------------------
502 void MultiplicationQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
504 ValidateTwoInputs(workloadInfo, "MultiplicationQueueDescriptor");
505 ValidateSingleOutput(workloadInfo, "MultiplicationQueueDescriptor");
507 ValidateBroadcastTensorShapesMatch(workloadInfo.m_InputTensorInfos[0],
508 workloadInfo.m_InputTensorInfos[1],
509 workloadInfo.m_OutputTensorInfos[0],
510 "MultiplicationQueueDescriptor",
515 void BatchNormalizationQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
517 ValidateSingleInput(workloadInfo, "BatchNormalizationQueueDescriptor");
518 ValidateSingleOutput(workloadInfo, "BatchNormalizationQueueDescriptor");
519 ValidateTensorShapesMatch(workloadInfo.m_InputTensorInfos[0],
520 workloadInfo.m_OutputTensorInfos[0],
521 "BatchNormalizationQueueDescriptor",
524 ValidatePointer(m_Mean, "BatchNormalizationQueueDescriptor", "mean");
525 ValidatePointer(m_Variance, "BatchNormalizationQueueDescriptor", "variance");
526 ValidatePointer(m_Beta, "BatchNormalizationQueueDescriptor", "beta");
527 ValidatePointer(m_Gamma, "BatchNormalizationQueueDescriptor", "gamma");
530 ValidateTensorNumDimensions(m_Mean->GetTensorInfo(), "BatchNormalizationQueueDescriptor", 1, "mean");
531 ValidateTensorNumDimensions(m_Variance->GetTensorInfo(), "BatchNormalizationQueueDescriptor", 1, "variance");
532 ValidateTensorNumDimensions(m_Beta->GetTensorInfo(), "BatchNormalizationQueueDescriptor", 1, "beta");
533 ValidateTensorNumDimensions(m_Gamma->GetTensorInfo(), "BatchNormalizationQueueDescriptor", 1, "gamma");
535 ValidateTensorShapesMatch(
536 m_Mean->GetTensorInfo(), m_Variance->GetTensorInfo(), "BatchNormalizationQueueDescriptor", "mean", "variance");
537 ValidateTensorShapesMatch(
538 m_Mean->GetTensorInfo(), m_Beta->GetTensorInfo(), "BatchNormalizationQueueDescriptor", "mean", "beta");
539 ValidateTensorShapesMatch(
540 m_Mean->GetTensorInfo(), m_Gamma->GetTensorInfo(), "BatchNormalizationQueueDescriptor", "mean", "gamma");
543 void Convolution2dQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
545 ValidateSingleInput(workloadInfo, "Convolution2dQueueDescriptor");
546 ValidateSingleOutput(workloadInfo, "Convolution2dQueueDescriptor");
548 ValidateTensorNumDimensions(workloadInfo.m_InputTensorInfos[0], "Convolution2dQueueDescriptor", 4, "input");
549 ValidateTensorNumDimensions(workloadInfo.m_OutputTensorInfos[0], "Convolution2dQueueDescriptor", 4, "output");
551 ValidatePointer(m_Weight, "Convolution2dQueueDescriptor", "weight");
552 ValidateTensorNumDimensions(m_Weight->GetTensorInfo(), "Convolution2dQueueDescriptor", 4, "weight");
553 ValidateTensorDataType(m_Weight->GetTensorInfo(), workloadInfo.m_InputTensorInfos[0].GetDataType(),
554 "Convolution2dQueueDescriptor", "weight");
555 if (m_Parameters.m_BiasEnabled)
557 ValidateTensorNumDimensions(m_Bias->GetTensorInfo(), "Convolution2dQueueDescriptor", 1, "bias");
558 ValidateTensorDataType(m_Bias->GetTensorInfo(),
559 GetBiasDataType(workloadInfo.m_InputTensorInfos[0].GetDataType()),
560 "Convolution2dQueueDescriptor", "bias");
561 ValidateBiasTensorQuantization(m_Bias->GetTensorInfo(),
562 workloadInfo.m_InputTensorInfos[0], m_Weight->GetTensorInfo(), "Convolution2dQueueDescriptor");
565 ValidateTensorQuantizationMultiplier(workloadInfo.m_InputTensorInfos[0], m_Weight->GetTensorInfo(),
566 workloadInfo.m_OutputTensorInfos[0], "Convolution2dQueueDescriptor", "input", "weights", "output");
569 void DepthwiseConvolution2dQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
571 ValidateSingleInput(workloadInfo, "DepthwiseConvolution2dQueueDescriptor");
572 ValidateSingleOutput(workloadInfo, "DepthwiseConvolution2dQueueDescriptor");
574 ValidateTensorNumDimensions(
575 workloadInfo.m_InputTensorInfos[0], "DepthwiseConvolution2dQueueDescriptor", 4, "input");
576 ValidateTensorNumDimensions(
577 workloadInfo.m_OutputTensorInfos[0], "DepthwiseConvolution2dQueueDescriptor", 4, "output");
579 ValidatePointer(m_Weight, "DepthwiseConvolution2dQueueDescriptor", "weight");
580 ValidateTensorNumDimensions(m_Weight->GetTensorInfo(), "DepthwiseConvolution2dQueueDescriptor", 4, "weight");
582 const unsigned int channelIndex = (m_Parameters.m_DataLayout == DataLayout::NCHW) ? 1 : 3;
584 //inputChannels * channelMultiplier should be equal to outputChannels.
585 const unsigned int numWeightChannelMultiplier = m_Weight->GetTensorInfo().GetShape()[0];
586 const unsigned int numWeightInputChannels = m_Weight->GetTensorInfo().GetShape()[channelIndex];
587 const unsigned int numWeightOutputChannels = workloadInfo.m_OutputTensorInfos[0].GetShape()[channelIndex];
588 if (numWeightChannelMultiplier * numWeightInputChannels != numWeightOutputChannels)
590 throw InvalidArgumentException(
591 boost::str(boost::format("DepthwiseConvolution2dQueueDescriptor: output_channels (provided %1%) should be "
592 "equal to input_channels (provided %2%) multiplied by channel_multiplier "
594 % numWeightOutputChannels % numWeightInputChannels % numWeightChannelMultiplier));
597 if (m_Parameters.m_BiasEnabled)
599 ValidatePointer(m_Bias, "DepthwiseConvolution2dQueueDescriptor", "bias");
600 ValidateTensorNumDimensions(m_Bias->GetTensorInfo(), "DepthwiseConvolution2dQueueDescriptor", 1, "bias");
601 ValidateBiasTensorQuantization(m_Bias->GetTensorInfo(),
602 workloadInfo.m_InputTensorInfos[0], m_Weight->GetTensorInfo(), "DepthwiseConvolution2dQueueDescriptor");
604 ValidateTensorDataType(m_Bias->GetTensorInfo(),
605 GetBiasDataType(workloadInfo.m_InputTensorInfos[0].GetDataType()),
606 "DepthwiseConvolution2dQueueDescriptor", "bias");
609 ValidateTensorQuantizationMultiplier(workloadInfo.m_InputTensorInfos[0], m_Weight->GetTensorInfo(),
610 workloadInfo.m_OutputTensorInfos[0], "DepthwiseConvolution2dQueueDescriptor", "input", "weights", "output");
613 void PermuteQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
615 ValidateSingleInput(workloadInfo, "PermuteQueueDescriptor");
616 ValidateSingleOutput(workloadInfo, "PermuteQueueDescriptor");
618 const PermutationVector& mapping = m_Parameters.m_DimMappings;
620 const TensorInfo& input = workloadInfo.m_InputTensorInfos[0];
621 const TensorInfo& output = workloadInfo.m_OutputTensorInfos[0];
623 ValidateTensorNumDimensions(input, "PermuteQueueDescriptor", mapping.GetSize(), "input");
624 ValidateTensorNumDimensions(output, "PermuteQueueDescriptor", mapping.GetSize(), "output");
626 for (unsigned int i = 0; i < mapping.GetSize(); ++i)
628 if (input.GetShape()[i] != output.GetShape()[mapping[i]])
630 throw InvalidArgumentException("PermuteQueueDescriptor: src dimension " + to_string(i) +
631 " (=" + to_string(input.GetShape()[i]) + ") " +
632 "must match dst dimension " + to_string(mapping[i]) +
633 " (=" + to_string(output.GetShape()[mapping[i]]) + ")");
638 void Pooling2dQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
640 ValidateSingleInput(workloadInfo, "Pooling2dQueueDescriptor");
641 ValidateSingleOutput(workloadInfo, "Pooling2dQueueDescriptor");
643 ValidateTensorNumDimensions(workloadInfo.m_InputTensorInfos[0], "Pooling2dQueueDescriptor", 4, "input");
644 ValidateTensorNumDimensions(workloadInfo.m_OutputTensorInfos[0], "Pooling2dQueueDescriptor", 4, "output");
647 void ResizeBilinearQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
649 ValidateSingleInput(workloadInfo, "ResizeBilinearQueueDescriptor");
650 ValidateSingleOutput(workloadInfo, "ResizeBilinearQueueDescriptor");
652 ValidateTensorNumDimensions(workloadInfo.m_InputTensorInfos[0], "ResizeBilinearQueueDescriptor", 4, "input");
653 ValidateTensorNumDimensions(workloadInfo.m_OutputTensorInfos[0], "ResizeBilinearQueueDescriptor", 4, "output");
655 // Resizes bilinear only changes width and height: batch and channel count must match.
657 const unsigned int inputBatchSize = workloadInfo.m_InputTensorInfos[0].GetShape()[0];
658 const unsigned int outputBatchSize = workloadInfo.m_OutputTensorInfos[0].GetShape()[0];
659 if (inputBatchSize != outputBatchSize)
661 throw InvalidArgumentException(
662 boost::str(boost::format("ResizeBilinearQueueDescriptor: Input batch size (%1%) "
663 "does not match output batch size (%2%)") % inputBatchSize % outputBatchSize));
668 const unsigned int inputChannelCount =
669 workloadInfo.m_InputTensorInfos[0].GetShape()[this->m_Parameters.m_DataLayout.GetChannelsIndex()];
670 const unsigned int outputChannelCount =
671 workloadInfo.m_OutputTensorInfos[0].GetShape()[this->m_Parameters.m_DataLayout.GetChannelsIndex()];
672 if (inputChannelCount != outputChannelCount)
674 throw InvalidArgumentException(
675 boost::str(boost::format("ResizeBilinearQueueDescriptor: Input channel count (%1%) "
676 "does not match output channel count (%2%)") % inputChannelCount % outputChannelCount));
681 void FakeQuantizationQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
683 ValidateSingleInput(workloadInfo, "FakeQuantizationQueueDescriptor");
684 ValidateSingleOutput(workloadInfo, "FakeQuantizationQueueDescriptor");
686 ValidateTensorNumDimensions(workloadInfo.m_InputTensorInfos[0], "FakeQuantizationQueueDescriptor", 2, "input");
687 ValidateTensorNumDimensions(workloadInfo.m_OutputTensorInfos[0], "FakeQuantizationQueueDescriptor", 2, "output");
688 ValidateTensorShapesMatch(workloadInfo.m_InputTensorInfos[0],
689 workloadInfo.m_OutputTensorInfos[0],
690 "FakeQuantizationQueueDescriptor",
693 if (m_Parameters.m_Min > m_Parameters.m_Max)
695 throw InvalidArgumentException("FakeQuantizationQueueDescriptor: min cannot be greater than max");
700 void L2NormalizationQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
702 ValidateSingleInput(workloadInfo, "L2NormalizationQueueDescriptor");
703 ValidateSingleOutput(workloadInfo, "L2NormalizationQueueDescriptor");
705 ValidateTensorNumDimensions(workloadInfo.m_InputTensorInfos[0], "L2NormalizationQueueDescriptor", 4, "input");
706 ValidateTensorNumDimensions(workloadInfo.m_OutputTensorInfos[0], "L2NormalizationQueueDescriptor", 4, "output");
707 ValidateTensorShapesMatch(workloadInfo.m_InputTensorInfos[0],
708 workloadInfo.m_OutputTensorInfos[0],
709 "L2NormalizationQueueDescriptor",
714 void ConstantQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
716 ValidateNoInputs(workloadInfo, "ConstantQueueDescriptor");
717 ValidateSingleOutput(workloadInfo, "ConstantQueueDescriptor");
721 throw InvalidArgumentException("ConstantQueueDescriptor: No const input specified");
724 ValidateTensorShapesMatch(m_LayerOutput->GetTensorInfo(),
725 workloadInfo.m_OutputTensorInfos[0],
726 "ConstantQueueDescriptor",
731 void ReshapeQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
733 ValidateSingleInput(workloadInfo, "ReshapeQueueDescriptor");
734 ValidateSingleOutput(workloadInfo, "ReshapeQueueDescriptor");
736 if (workloadInfo.m_InputTensorInfos[0].GetNumElements() != workloadInfo.m_OutputTensorInfos[0].GetNumElements())
738 throw InvalidArgumentException("ReshapeQueueDescriptor: Input tensor has " +
739 to_string(workloadInfo.m_InputTensorInfos[0].GetNumElements()) + " but output tensor has " +
740 to_string(workloadInfo.m_OutputTensorInfos[0].GetNumElements()) + " elements.");
744 void SpaceToBatchNdQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
746 ValidateSingleInput(workloadInfo, "SpaceToBatchNdQueueDescriptor");
747 ValidateSingleOutput(workloadInfo, "SpaceToBatchNdQueueDescriptor");
749 ValidateTensorNumDimensions(workloadInfo.m_InputTensorInfos[0], "SpaceToBatchNdQueueDescriptor", 4, "input");
750 ValidateTensorNumDimensions(workloadInfo.m_OutputTensorInfos[0], "SpaceToBatchNdQueueDescriptor", 4, "output");
752 if (workloadInfo.m_InputTensorInfos[0].GetNumElements() != workloadInfo.m_OutputTensorInfos[0].GetNumElements())
754 throw InvalidArgumentException("SpaceToBatchNdQueueDescriptor: Input tensor has " +
755 to_string(workloadInfo.m_InputTensorInfos[0].GetNumElements()) + " but output tensor has " +
756 to_string(workloadInfo.m_OutputTensorInfos[0].GetNumElements()) + " elements.");
759 if (m_Parameters.m_BlockShape.size() != 2)
761 throw InvalidArgumentException("Block Shape must contains 2 spatial dimensions");
764 if (m_Parameters.m_BlockShape.size() != m_Parameters.m_PadList.size())
766 throw InvalidArgumentException("Pad List must contains the same number of dimensions as Block Shape.");
769 const TensorShape inputShape = workloadInfo.m_InputTensorInfos[0].GetShape();
771 std::pair<unsigned int, unsigned int> heightPad = m_Parameters.m_PadList[0];
772 std::pair<unsigned int, unsigned int> widthPad = m_Parameters.m_PadList[1];
774 if ((inputShape[m_Parameters.m_DataLayout.GetHeightIndex()] + heightPad.first + heightPad.second)
775 % m_Parameters.m_BlockShape[0] != 0 ||
776 (inputShape[m_Parameters.m_DataLayout.GetWidthIndex()] + widthPad.first + widthPad.second)
777 % m_Parameters.m_BlockShape[1] != 0)
779 throw InvalidArgumentException(
780 "Input shape after padding must be divisible by Block Shape in all spatial dimensions");
784 void FloorQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
786 ValidateSingleInput(workloadInfo, "FloorQueueDescriptor");
787 ValidateSingleOutput(workloadInfo, "FlootQueueDescriptor");
789 if (workloadInfo.m_InputTensorInfos[0] != workloadInfo.m_OutputTensorInfos[0])
791 throw InvalidArgumentException("FloorQueueDescriptor: Input and output tensor infos do not match.");
795 void LstmQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
797 ValidateTensorNumDimensions(workloadInfo.m_InputTensorInfos[0], "LstmQueueDescriptor", 2, "input");
798 ValidateTensorNumDimensions(workloadInfo.m_OutputTensorInfos[0], "LstmQueueDescriptor", 2, "output");
801 void ConvertFp32ToFp16QueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
803 ValidateSingleInput(workloadInfo, "ConvertFp32ToFp16QueueDescriptor");
804 ValidateSingleOutput(workloadInfo, "ConvertFp32ToFp16QueueDescriptor");
806 if (workloadInfo.m_InputTensorInfos[0].GetDataType() != DataType::Float32)
808 throw InvalidArgumentException("ConvertFp32ToFp16QueueDescriptor: Input tensor type must be Float32.");
811 if (workloadInfo.m_OutputTensorInfos[0].GetDataType() != DataType::Float16)
813 throw InvalidArgumentException("ConvertFp32ToFp16QueueDescriptor: Output tensor type must be Float16.");
816 ValidateTensorShapesMatch(workloadInfo.m_InputTensorInfos[0],
817 workloadInfo.m_OutputTensorInfos[0],
818 "ConvertFp32ToFp16QueueDescriptor",
823 void ConvertFp16ToFp32QueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
825 ValidateSingleInput(workloadInfo, "ConvertFp16ToFp32QueueDescriptor");
826 ValidateSingleOutput(workloadInfo, "ConvertFp16ToFp32QueueDescriptor");
828 if (workloadInfo.m_InputTensorInfos[0].GetDataType() != DataType::Float16)
830 throw InvalidArgumentException("ConvertFp16ToFp32QueueDescriptor: Input tensor type must be Float16.");
832 if (workloadInfo.m_OutputTensorInfos[0].GetDataType() != DataType::Float32)
834 throw InvalidArgumentException("ConvertFp16ToFp32QueueDescriptor: Output tensor type must be Float32.");
837 ValidateTensorShapesMatch(workloadInfo.m_InputTensorInfos[0],
838 workloadInfo.m_OutputTensorInfos[0],
839 "ConvertFp16ToFp32QueueDescriptor",
844 void DivisionQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
846 ValidateTwoInputs(workloadInfo, "DivisionQueueDescriptor");
847 ValidateSingleOutput(workloadInfo, "DivisionQueueDescriptor");
849 ValidateBroadcastTensorShapesMatch(workloadInfo.m_InputTensorInfos[0],
850 workloadInfo.m_InputTensorInfos[1],
851 workloadInfo.m_OutputTensorInfos[0],
852 "DivisionQueueDescriptor",
857 void SubtractionQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
859 ValidateTwoInputs(workloadInfo, "SubtractionQueueDescriptor");
860 ValidateSingleOutput(workloadInfo, "SubtractionQueueDescriptor");
862 ValidateBroadcastTensorShapesMatch(workloadInfo.m_InputTensorInfos[0],
863 workloadInfo.m_InputTensorInfos[1],
864 workloadInfo.m_OutputTensorInfos[0],
865 "SubtractionQueueDescriptor",
870 void MeanQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
872 ValidateSingleInput(workloadInfo, "MeanQueueDescriptor");
873 ValidateSingleOutput(workloadInfo, "MeanQueueDescriptor");
875 const TensorInfo& input = workloadInfo.m_InputTensorInfos[0];
876 const TensorInfo& output = workloadInfo.m_OutputTensorInfos[0];
878 if (m_Parameters.m_KeepDims)
880 ValidateTensorNumDimensions(output, "MeanQueueDescriptor", input.GetNumDimensions(), "output");
882 else if (m_Parameters.m_Axis.empty())
884 ValidateTensorNumDimensions(output, "MeanQueueDescriptor", 1, "output");
888 auto outputDim = input.GetNumDimensions() - boost::numeric_cast<unsigned int>(m_Parameters.m_Axis.size());
889 ValidateTensorNumDimensions(output,
890 "MeanQueueDescriptor",
891 outputDim > 0 ? outputDim : 1,
896 void PadQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const
898 ValidateSingleInput(workloadInfo, "PadQueueDescriptor");
899 ValidateSingleOutput(workloadInfo, "PadQueueDescriptor");
901 const TensorInfo& input = workloadInfo.m_InputTensorInfos[0];
902 const TensorInfo& output = workloadInfo.m_OutputTensorInfos[0];
904 // input and output should have the same number of dimensions
905 ValidateTensorNumDimensions(output, "PadQueueDescriptor", input.GetNumDimensions(), "output");
906 // there should be entry in the pad list for each dimension in the input tensor
907 if (m_Parameters.m_PadList.size() != input.GetNumDimensions()) {
908 throw InvalidArgumentException("Pad List should contain the same number of entries as there"
909 " are dimensions in the input tensor that is " +
910 to_string(input.GetNumDimensions()) + " entries " +
911 " not " + to_string(m_Parameters.m_PadList.size()) + " entries.");