arm_compute v17.12
[platform/upstream/armcl.git] / utils / TypePrinter.h
1 /*
2  * Copyright (c) 2017 ARM Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef __ARM_COMPUTE_TEST_TYPE_PRINTER_H__
25 #define __ARM_COMPUTE_TEST_TYPE_PRINTER_H__
26
27 #include "arm_compute/core/Dimensions.h"
28 #include "arm_compute/core/Error.h"
29 #include "arm_compute/core/Strides.h"
30 #include "arm_compute/core/TensorInfo.h"
31 #include "arm_compute/core/Types.h"
32
33 #include "tests/Types.h"
34
35 #include <ostream>
36 #include <sstream>
37 #include <string>
38
39 namespace arm_compute
40 {
41 /** Formatted output of the Dimensions type. */
42 template <typename T>
43 inline ::std::ostream &operator<<(::std::ostream &os, const Dimensions<T> &dimensions)
44 {
45     if(dimensions.num_dimensions() > 0)
46     {
47         os << dimensions[0];
48
49         for(unsigned int d = 1; d < dimensions.num_dimensions(); ++d)
50         {
51             os << "x" << dimensions[d];
52         }
53     }
54
55     return os;
56 }
57
58 /** Formatted output of the NonLinearFilterFunction type. */
59 inline ::std::ostream &operator<<(::std::ostream &os, const NonLinearFilterFunction &function)
60 {
61     switch(function)
62     {
63         case NonLinearFilterFunction::MAX:
64             os << "MAX";
65             break;
66         case NonLinearFilterFunction::MEDIAN:
67             os << "MEDIAN";
68             break;
69         case NonLinearFilterFunction::MIN:
70             os << "MIN";
71             break;
72         default:
73             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
74     }
75
76     return os;
77 }
78
79 inline std::string to_string(const NonLinearFilterFunction &function)
80 {
81     std::stringstream str;
82     str << function;
83     return str.str();
84 }
85
86 /** Formatted output of the MatrixPattern type. */
87 inline ::std::ostream &operator<<(::std::ostream &os, const MatrixPattern &pattern)
88 {
89     switch(pattern)
90     {
91         case MatrixPattern::BOX:
92             os << "BOX";
93             break;
94         case MatrixPattern::CROSS:
95             os << "CROSS";
96             break;
97         case MatrixPattern::DISK:
98             os << "DISK";
99             break;
100         case MatrixPattern::OTHER:
101             os << "OTHER";
102             break;
103         default:
104             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
105     }
106
107     return os;
108 }
109
110 inline std::string to_string(const MatrixPattern &pattern)
111 {
112     std::stringstream str;
113     str << pattern;
114     return str.str();
115 }
116
117 /** Formatted output of the RoundingPolicy type. */
118 inline ::std::ostream &operator<<(::std::ostream &os, const RoundingPolicy &rounding_policy)
119 {
120     switch(rounding_policy)
121     {
122         case RoundingPolicy::TO_ZERO:
123             os << "TO_ZERO";
124             break;
125         case RoundingPolicy::TO_NEAREST_UP:
126             os << "TO_NEAREST_UP";
127             break;
128         case RoundingPolicy::TO_NEAREST_EVEN:
129             os << "TO_NEAREST_EVEN";
130             break;
131         default:
132             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
133     }
134
135     return os;
136 }
137
138 /** Formatted output of the WeightsInfo type. */
139 inline ::std::ostream &operator<<(::std::ostream &os, const WeightsInfo &weights_info)
140 {
141     os << weights_info.are_reshaped() << ";";
142     os << weights_info.num_kernels() << ";" << weights_info.kernel_size().first << "," << weights_info.kernel_size().second;
143
144     return os;
145 }
146
147 /** Formatted output of the ROIPoolingInfo type. */
148 inline ::std::ostream &operator<<(::std::ostream &os, const ROIPoolingLayerInfo &pool_info)
149 {
150     os << pool_info.pooled_width() << "x" << pool_info.pooled_height() << "~" << pool_info.spatial_scale();
151     return os;
152 }
153
154 /** Formatted output of the QuantizationInfo type. */
155 inline ::std::ostream &operator<<(::std::ostream &os, const QuantizationInfo &quantization_info)
156 {
157     os << "Scale:" << quantization_info.scale << "~"
158        << "Offset:" << quantization_info.offset;
159     return os;
160 }
161
162 inline std::string to_string(const QuantizationInfo &quantization_info)
163 {
164     std::stringstream str;
165     str << quantization_info;
166     return str.str();
167 }
168
169 inline ::std::ostream &operator<<(::std::ostream &os, const FixedPointOp &op)
170 {
171     switch(op)
172     {
173         case FixedPointOp::ADD:
174             os << "ADD";
175             break;
176         case FixedPointOp::SUB:
177             os << "SUB";
178             break;
179         case FixedPointOp::MUL:
180             os << "MUL";
181             break;
182         case FixedPointOp::EXP:
183             os << "EXP";
184             break;
185         case FixedPointOp::LOG:
186             os << "LOG";
187             break;
188         case FixedPointOp::INV_SQRT:
189             os << "INV_SQRT";
190             break;
191         case FixedPointOp::RECIPROCAL:
192             os << "RECIPROCAL";
193             break;
194         default:
195             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
196     }
197
198     return os;
199 }
200
201 inline std::string to_string(const FixedPointOp &op)
202 {
203     std::stringstream str;
204     str << op;
205     return str.str();
206 }
207
208 /** Formatted output of the activation function type. */
209 inline ::std::ostream &operator<<(::std::ostream &os, const ActivationLayerInfo::ActivationFunction &act_function)
210 {
211     switch(act_function)
212     {
213         case ActivationLayerInfo::ActivationFunction::ABS:
214             os << "ABS";
215             break;
216         case ActivationLayerInfo::ActivationFunction::LINEAR:
217             os << "LINEAR";
218             break;
219         case ActivationLayerInfo::ActivationFunction::LOGISTIC:
220             os << "LOGISTIC";
221             break;
222         case ActivationLayerInfo::ActivationFunction::RELU:
223             os << "RELU";
224             break;
225         case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU:
226             os << "BOUNDED_RELU";
227             break;
228         case ActivationLayerInfo::ActivationFunction::LEAKY_RELU:
229             os << "LEAKY_RELU";
230             break;
231         case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
232             os << "SOFT_RELU";
233             break;
234         case ActivationLayerInfo::ActivationFunction::SQRT:
235             os << "SQRT";
236             break;
237         case ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU:
238             os << "LU_BOUNDED_RELU";
239             break;
240         case ActivationLayerInfo::ActivationFunction::SQUARE:
241             os << "SQUARE";
242             break;
243         case ActivationLayerInfo::ActivationFunction::TANH:
244             os << "TANH";
245             break;
246         default:
247             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
248     }
249
250     return os;
251 }
252
253 inline std::string to_string(const arm_compute::ActivationLayerInfo &info)
254 {
255     std::stringstream str;
256     str << info.activation();
257     return str.str();
258 }
259
260 inline std::string to_string(const arm_compute::ActivationLayerInfo::ActivationFunction &function)
261 {
262     std::stringstream str;
263     str << function;
264     return str.str();
265 }
266
267 /** Formatted output of the NormType type. */
268 inline ::std::ostream &operator<<(::std::ostream &os, const NormType &norm_type)
269 {
270     switch(norm_type)
271     {
272         case NormType::CROSS_MAP:
273             os << "CROSS_MAP";
274             break;
275         case NormType::IN_MAP_1D:
276             os << "IN_MAP_1D";
277             break;
278         case NormType::IN_MAP_2D:
279             os << "IN_MAP_2D";
280             break;
281         default:
282             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
283     }
284
285     return os;
286 }
287
288 inline std::string to_string(const arm_compute::NormalizationLayerInfo &info)
289 {
290     std::stringstream str;
291     str << info.type();
292     return str.str();
293 }
294
295 /** Formatted output of @ref NormalizationLayerInfo. */
296 inline ::std::ostream &operator<<(::std::ostream &os, const NormalizationLayerInfo &info)
297 {
298     os << info.type();
299     return os;
300 }
301
302 /** Formatted output of the PoolingType type. */
303 inline ::std::ostream &operator<<(::std::ostream &os, const PoolingType &pool_type)
304 {
305     switch(pool_type)
306     {
307         case PoolingType::AVG:
308             os << "AVG";
309             break;
310         case PoolingType::MAX:
311             os << "MAX";
312             break;
313         case PoolingType::L2:
314             os << "L2";
315             break;
316         default:
317             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
318     }
319
320     return os;
321 }
322
323 /** Formatted output of @ref PoolingLayerInfo. */
324 inline ::std::ostream &operator<<(::std::ostream &os, const PoolingLayerInfo &info)
325 {
326     os << info.pool_type();
327
328     return os;
329 }
330
331 inline std::string to_string(const RoundingPolicy &rounding_policy)
332 {
333     std::stringstream str;
334     str << rounding_policy;
335     return str.str();
336 }
337
338 /** Formatted output of the DataType type. */
339 inline ::std::ostream &operator<<(::std::ostream &os, const DataType &data_type)
340 {
341     switch(data_type)
342     {
343         case DataType::UNKNOWN:
344             os << "UNKNOWN";
345             break;
346         case DataType::U8:
347             os << "U8";
348             break;
349         case DataType::QS8:
350             os << "QS8";
351             break;
352         case DataType::QASYMM8:
353             os << "QASYMM8";
354             break;
355         case DataType::S8:
356             os << "S8";
357             break;
358         case DataType::U16:
359             os << "U16";
360             break;
361         case DataType::S16:
362             os << "S16";
363             break;
364         case DataType::QS16:
365             os << "QS16";
366             break;
367         case DataType::U32:
368             os << "U32";
369             break;
370         case DataType::S32:
371             os << "S32";
372             break;
373         case DataType::U64:
374             os << "U64";
375             break;
376         case DataType::S64:
377             os << "S64";
378             break;
379         case DataType::F16:
380             os << "F16";
381             break;
382         case DataType::F32:
383             os << "F32";
384             break;
385         case DataType::F64:
386             os << "F64";
387             break;
388         case DataType::SIZET:
389             os << "SIZET";
390             break;
391         default:
392             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
393     }
394
395     return os;
396 }
397
398 inline std::string to_string(const arm_compute::DataType &data_type)
399 {
400     std::stringstream str;
401     str << data_type;
402     return str.str();
403 }
404
405 /** Formatted output of the Format type. */
406 inline ::std::ostream &operator<<(::std::ostream &os, const Format &format)
407 {
408     switch(format)
409     {
410         case Format::UNKNOWN:
411             os << "UNKNOWN";
412             break;
413         case Format::U8:
414             os << "U8";
415             break;
416         case Format::S16:
417             os << "S16";
418             break;
419         case Format::U16:
420             os << "U16";
421             break;
422         case Format::S32:
423             os << "S32";
424             break;
425         case Format::U32:
426             os << "U32";
427             break;
428         case Format::F16:
429             os << "F16";
430             break;
431         case Format::F32:
432             os << "F32";
433             break;
434         case Format::UV88:
435             os << "UV88";
436             break;
437         case Format::RGB888:
438             os << "RGB888";
439             break;
440         case Format::RGBA8888:
441             os << "RGBA8888";
442             break;
443         case Format::YUV444:
444             os << "YUV444";
445             break;
446         case Format::YUYV422:
447             os << "YUYV422";
448             break;
449         case Format::NV12:
450             os << "NV12";
451             break;
452         case Format::NV21:
453             os << "NV21";
454             break;
455         case Format::IYUV:
456             os << "IYUV";
457             break;
458         case Format::UYVY422:
459             os << "UYVY422";
460             break;
461         default:
462             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
463     }
464
465     return os;
466 }
467
468 inline std::string to_string(const Format &format)
469 {
470     std::stringstream str;
471     str << format;
472     return str.str();
473 }
474
475 /** Formatted output of the Channel type. */
476 inline ::std::ostream &operator<<(::std::ostream &os, const Channel &channel)
477 {
478     switch(channel)
479     {
480         case Channel::UNKNOWN:
481             os << "UNKNOWN";
482             break;
483         case Channel::C0:
484             os << "C0";
485             break;
486         case Channel::C1:
487             os << "C1";
488             break;
489         case Channel::C2:
490             os << "C2";
491             break;
492         case Channel::C3:
493             os << "C3";
494             break;
495         case Channel::R:
496             os << "R";
497             break;
498         case Channel::G:
499             os << "G";
500             break;
501         case Channel::B:
502             os << "B";
503             break;
504         case Channel::A:
505             os << "A";
506             break;
507         case Channel::Y:
508             os << "Y";
509             break;
510         case Channel::U:
511             os << "U";
512             break;
513         case Channel::V:
514             os << "V";
515             break;
516         default:
517             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
518     }
519
520     return os;
521 }
522
523 /** Formatted output of the BorderMode type. */
524 inline ::std::ostream &operator<<(::std::ostream &os, const BorderMode &mode)
525 {
526     switch(mode)
527     {
528         case BorderMode::UNDEFINED:
529             os << "UNDEFINED";
530             break;
531         case BorderMode::CONSTANT:
532             os << "CONSTANT";
533             break;
534         case BorderMode::REPLICATE:
535             os << "REPLICATE";
536             break;
537         default:
538             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
539     }
540
541     return os;
542 }
543
544 /** Formatted output of the BorderSize type. */
545 inline ::std::ostream &operator<<(::std::ostream &os, const BorderSize &border)
546 {
547     os << border.top << ","
548        << border.right << ","
549        << border.bottom << ","
550        << border.left;
551
552     return os;
553 }
554
555 /** Formatted output of the InterpolationPolicy type. */
556 inline ::std::ostream &operator<<(::std::ostream &os, const InterpolationPolicy &policy)
557 {
558     switch(policy)
559     {
560         case InterpolationPolicy::NEAREST_NEIGHBOR:
561             os << "NEAREST_NEIGHBOR";
562             break;
563         case InterpolationPolicy::BILINEAR:
564             os << "BILINEAR";
565             break;
566         case InterpolationPolicy::AREA:
567             os << "AREA";
568             break;
569         default:
570             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
571     }
572
573     return os;
574 }
575
576 /** Formatted output of the SamplingPolicy type. */
577 inline ::std::ostream &operator<<(::std::ostream &os, const SamplingPolicy &policy)
578 {
579     switch(policy)
580     {
581         case SamplingPolicy::CENTER:
582             os << "CENTER";
583             break;
584         case SamplingPolicy::TOP_LEFT:
585             os << "TOP_LEFT";
586             break;
587         default:
588             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
589     }
590
591     return os;
592 }
593
594 /** Formatted output of the TensorInfo type. */
595 inline std::string to_string(const TensorInfo &info)
596 {
597     std::stringstream str;
598     str << "{Shape=" << info.tensor_shape() << ","
599         << "Type=" << info.data_type() << ","
600         << "Channels=" << info.num_channels() << ","
601         << "FixedPointPos=" << info.fixed_point_position() << "}";
602     return str.str();
603 }
604
605 template <typename T>
606 inline std::string to_string(const Dimensions<T> &dimensions)
607 {
608     std::stringstream str;
609     str << dimensions;
610     return str.str();
611 }
612
613 inline std::string to_string(const Strides &stride)
614 {
615     std::stringstream str;
616     str << stride;
617     return str.str();
618 }
619
620 /** Formatted output of the TensorShape type. */
621 inline std::string to_string(const TensorShape &shape)
622 {
623     std::stringstream str;
624     str << shape;
625     return str.str();
626 }
627
628 /** Formatted output of the Coordinates type. */
629 inline std::string to_string(const Coordinates &coord)
630 {
631     std::stringstream str;
632     str << coord;
633     return str.str();
634 }
635
636 /** Formatted output of the Rectangle type. */
637 inline ::std::ostream &operator<<(::std::ostream &os, const Rectangle &rect)
638 {
639     os << rect.width << "x" << rect.height;
640     os << "+" << rect.x << "+" << rect.y;
641
642     return os;
643 }
644
645 /** Formatted output of the PadStridInfo type. */
646 inline ::std::ostream &operator<<(::std::ostream &os, const PadStrideInfo &pad_stride_info)
647 {
648     os << pad_stride_info.stride().first << "," << pad_stride_info.stride().second;
649     os << ";";
650     os << pad_stride_info.pad_left() << "," << pad_stride_info.pad_right() << ","
651        << pad_stride_info.pad_top() << "," << pad_stride_info.pad_bottom();
652
653     return os;
654 }
655
656 inline std::string to_string(const PadStrideInfo &pad_stride_info)
657 {
658     std::stringstream str;
659     str << pad_stride_info;
660     return str.str();
661 }
662
663 inline std::string to_string(const BorderMode &mode)
664 {
665     std::stringstream str;
666     str << mode;
667     return str.str();
668 }
669
670 inline std::string to_string(const BorderSize &border)
671 {
672     std::stringstream str;
673     str << border;
674     return str.str();
675 }
676
677 inline std::string to_string(const InterpolationPolicy &policy)
678 {
679     std::stringstream str;
680     str << policy;
681     return str.str();
682 }
683
684 inline std::string to_string(const SamplingPolicy &policy)
685 {
686     std::stringstream str;
687     str << policy;
688     return str.str();
689 }
690
691 /** Formatted output of the ConversionPolicy type. */
692 inline ::std::ostream &operator<<(::std::ostream &os, const ConvertPolicy &policy)
693 {
694     switch(policy)
695     {
696         case ConvertPolicy::WRAP:
697             os << "WRAP";
698             break;
699         case ConvertPolicy::SATURATE:
700             os << "SATURATE";
701             break;
702         default:
703             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
704     }
705
706     return os;
707 }
708
709 inline std::string to_string(const ConvertPolicy &policy)
710 {
711     std::stringstream str;
712     str << policy;
713     return str.str();
714 }
715
716 /** Formatted output of the Reduction Operations. */
717 inline ::std::ostream &operator<<(::std::ostream &os, const ReductionOperation &op)
718 {
719     switch(op)
720     {
721         case ReductionOperation::SUM_SQUARE:
722             os << "SUM_SQUARE";
723             break;
724         default:
725             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
726     }
727
728     return os;
729 }
730
731 inline std::string to_string(const ReductionOperation &op)
732 {
733     std::stringstream str;
734     str << op;
735     return str.str();
736 }
737
738 inline std::string to_string(const NormType &type)
739 {
740     std::stringstream str;
741     str << type;
742     return str.str();
743 }
744
745 inline std::string to_string(const PoolingType &type)
746 {
747     std::stringstream str;
748     str << type;
749     return str.str();
750 }
751
752 inline std::string to_string(const PoolingLayerInfo &info)
753 {
754     std::stringstream str;
755     str << "{Type=" << info.pool_type() << ","
756         << "IsGlobalPooling=" << info.is_global_pooling();
757     if(!info.is_global_pooling())
758     {
759         str << ","
760             << "PoolSize=" << info.pool_size() << ","
761             << "PadStride=" << info.pad_stride_info();
762     }
763     str << "}";
764     return str.str();
765 }
766
767 /** Formatted output of the KeyPoint type. */
768 inline ::std::ostream &operator<<(::std::ostream &os, const KeyPoint &point)
769 {
770     os << "{x=" << point.x << ","
771        << "y=" << point.y << ","
772        << "strength=" << point.strength << ","
773        << "scale=" << point.scale << ","
774        << "orientation=" << point.orientation << ","
775        << "tracking_status=" << point.tracking_status << ","
776        << "error=" << point.error << "}";
777
778     return os;
779 }
780
781 /** Formatted output of the PhaseType type. */
782 inline ::std::ostream &operator<<(::std::ostream &os, const PhaseType &phase_type)
783 {
784     switch(phase_type)
785     {
786         case PhaseType::SIGNED:
787             os << "SIGNED";
788             break;
789         case PhaseType::UNSIGNED:
790             os << "UNSIGNED";
791             break;
792         default:
793             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
794     }
795
796     return os;
797 }
798
799 inline std::string to_string(const arm_compute::PhaseType &type)
800 {
801     std::stringstream str;
802     str << type;
803     return str.str();
804 }
805
806 /** Formatted output of the MagnitudeType type. */
807 inline ::std::ostream &operator<<(::std::ostream &os, const MagnitudeType &magnitude_type)
808 {
809     switch(magnitude_type)
810     {
811         case MagnitudeType::L1NORM:
812             os << "L1NORM";
813             break;
814         case MagnitudeType::L2NORM:
815             os << "L2NORM";
816             break;
817         default:
818             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
819     }
820
821     return os;
822 }
823
824 inline std::string to_string(const arm_compute::MagnitudeType &type)
825 {
826     std::stringstream str;
827     str << type;
828     return str.str();
829 }
830
831 /** Formatted output of the GradientDimension type. */
832 inline ::std::ostream &operator<<(::std::ostream &os, const GradientDimension &dim)
833 {
834     switch(dim)
835     {
836         case GradientDimension::GRAD_X:
837             os << "GRAD_X";
838             break;
839         case GradientDimension::GRAD_Y:
840             os << "GRAD_Y";
841             break;
842         case GradientDimension::GRAD_XY:
843             os << "GRAD_XY";
844             break;
845         default:
846             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
847     }
848
849     return os;
850 }
851
852 inline std::string to_string(const arm_compute::GradientDimension &type)
853 {
854     std::stringstream str;
855     str << type;
856     return str.str();
857 }
858 } // namespace arm_compute
859 #endif /* __ARM_COMPUTE_TEST_TYPE_PRINTER_H__ */