Home | History | Annotate | Download | only in util
      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_OIHW_VECT_I:
     67       return "OIHW_VECT_I";
     68     default:
     69       LOG(FATAL) << "Invalid Filter Format: " << static_cast<int32>(format);
     70       return "INVALID_FORMAT";
     71   }
     72 }
     73 
     74 bool FormatFromString(const string& format_str, TensorFormat* format) {
     75   if (format_str == "NHWC" || format_str == "NDHWC") {
     76     *format = FORMAT_NHWC;
     77     return true;
     78   }
     79   if (format_str == "NCHW" || format_str == "NCDHW") {
     80     *format = FORMAT_NCHW;
     81     return true;
     82   }
     83   if (format_str == "NCHW_VECT_C") {
     84     *format = FORMAT_NCHW_VECT_C;
     85     return true;
     86   }
     87   if (format_str == "NHWC_VECT_W") {
     88     *format = FORMAT_NHWC_VECT_W;
     89     return true;
     90   }
     91   if (format_str == "HWNC") {
     92     *format = FORMAT_HWNC;
     93     return true;
     94   }
     95   if (format_str == "HWCN") {
     96     *format = FORMAT_HWCN;
     97     return true;
     98   }
     99   return false;
    100 }
    101 
    102 bool FilterFormatFromString(const string& format_str,
    103                             FilterTensorFormat* format) {
    104   if (format_str == "HWIO" || format_str == "DHWIO") {
    105     *format = FORMAT_HWIO;
    106     return true;
    107   }
    108   if (format_str == "OIHW" || format_str == "OIDHW") {
    109     *format = FORMAT_OIHW;
    110     return true;
    111   }
    112   if (format_str == "OIHW_VECT_I") {
    113     *format = FORMAT_OIHW_VECT_I;
    114     return true;
    115   }
    116   return false;
    117 }
    118 
    119 }  // namespace tensorflow
    120