1 | /* Copyright 2016 The TensorFlow Authors. All Rights Reserved. |
2 | |
3 | Licensed under the Apache License, Version 2.0 (the "License"); |
4 | you may not use this file except in compliance with the License. |
5 | You may obtain a copy of the License at |
6 | |
7 | http://www.apache.org/licenses/LICENSE-2.0 |
8 | |
9 | Unless required by applicable law or agreed to in writing, software |
10 | distributed under the License is distributed on an "AS IS" BASIS, |
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | See the License for the specific language governing permissions and |
13 | limitations under the License. |
14 | ==============================================================================*/ |
15 | |
16 | #include "tensorflow/core/util/tensor_format.h" |
17 | |
18 | namespace tensorflow { |
19 | |
20 | string GetConvnetDataFormatAttrString() { |
21 | return "data_format: { 'NHWC', 'NCHW' } = 'NHWC' " ; |
22 | } |
23 | |
24 | string GetConvnet3dDataFormatAttrString() { |
25 | return "data_format: { 'NDHWC', 'NCDHW' } = 'NDHWC' " ; |
26 | } |
27 | |
28 | string GetConvnetDataFormat2D3DAttrString() { |
29 | return "data_format: { 'NHWC', 'NCHW', 'NDHWC', 'NCDHW' } = 'NHWC' " ; |
30 | } |
31 | |
32 | string GetConvnetFilterFormatAttrString() { |
33 | return "filter_format: { 'HWIO', 'OIHW' } = 'HWIO' " ; |
34 | } |
35 | |
36 | string GetConvnet3dFilterFormatAttrString() { |
37 | return "filter_format: { 'DHWIO', 'OIDHW' } = 'DHWIO' " ; |
38 | } |
39 | |
40 | string ToString(TensorFormat format) { |
41 | switch (format) { |
42 | case FORMAT_NHWC: |
43 | return "NHWC" ; |
44 | case FORMAT_NCHW: |
45 | return "NCHW" ; |
46 | case FORMAT_NCHW_VECT_C: |
47 | return "NCHW_VECT_C" ; |
48 | case FORMAT_NHWC_VECT_W: |
49 | return "NHWC_VECT_W" ; |
50 | case FORMAT_HWNC: |
51 | return "HWNC" ; |
52 | case FORMAT_HWCN: |
53 | return "HWCN" ; |
54 | default: |
55 | LOG(FATAL) << "Invalid Format: " << static_cast<int32>(format); |
56 | return "INVALID_FORMAT" ; |
57 | } |
58 | } |
59 | |
60 | string ToString(FilterTensorFormat format) { |
61 | switch (format) { |
62 | case FORMAT_HWIO: |
63 | return "HWIO" ; |
64 | case FORMAT_OIHW: |
65 | return "OIHW" ; |
66 | case FORMAT_OHWI: |
67 | return "OHWI" ; |
68 | case FORMAT_OIHW_VECT_I: |
69 | return "OIHW_VECT_I" ; |
70 | default: |
71 | LOG(FATAL) << "Invalid Filter Format: " << static_cast<int32>(format); |
72 | return "INVALID_FORMAT" ; |
73 | } |
74 | } |
75 | |
76 | bool FormatFromString(absl::string_view format_str, TensorFormat* format) { |
77 | if (format_str == "NHWC" || format_str == "NDHWC" ) { |
78 | *format = FORMAT_NHWC; |
79 | return true; |
80 | } |
81 | if (format_str == "NCHW" || format_str == "NCDHW" ) { |
82 | *format = FORMAT_NCHW; |
83 | return true; |
84 | } |
85 | if (format_str == "NCHW_VECT_C" ) { |
86 | *format = FORMAT_NCHW_VECT_C; |
87 | return true; |
88 | } |
89 | if (format_str == "NHWC_VECT_W" ) { |
90 | *format = FORMAT_NHWC_VECT_W; |
91 | return true; |
92 | } |
93 | if (format_str == "HWNC" ) { |
94 | *format = FORMAT_HWNC; |
95 | return true; |
96 | } |
97 | if (format_str == "HWCN" ) { |
98 | *format = FORMAT_HWCN; |
99 | return true; |
100 | } |
101 | return false; |
102 | } |
103 | |
104 | bool FilterFormatFromString(absl::string_view format_str, |
105 | FilterTensorFormat* format) { |
106 | if (format_str == "HWIO" || format_str == "DHWIO" ) { |
107 | *format = FORMAT_HWIO; |
108 | return true; |
109 | } |
110 | if (format_str == "OIHW" || format_str == "OIDHW" ) { |
111 | *format = FORMAT_OIHW; |
112 | return true; |
113 | } |
114 | if (format_str == "OIHW_VECT_I" ) { |
115 | *format = FORMAT_OIHW_VECT_I; |
116 | return true; |
117 | } |
118 | return false; |
119 | } |
120 | |
121 | } // namespace tensorflow |
122 | |