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 GetConvnetFilterFormatAttrString() {
     29   return "filter_format: { 'HWIO', 'OIHW' } = 'HWIO' ";
     30 }
     31 
     32 string GetConvnet3dFilterFormatAttrString() {
     33   return "filter_format: { 'DHWIO', 'OIDHW' } = 'DHWIO' ";
     34 }
     35 
     36 string ToString(TensorFormat format) {
     37   switch (format) {
     38     case FORMAT_NHWC:
     39       return "NHWC";
     40     case FORMAT_NCHW:
     41       return "NCHW";
     42     case FORMAT_NCHW_VECT_C:
     43       return "NCHW_VECT_C";
     44     default:
     45       LOG(FATAL) << "Invalid Format: " << static_cast<int32>(format);
     46       return "INVALID_FORMAT";
     47   }
     48 }
     49 
     50 string ToString(FilterTensorFormat format) {
     51   switch (format) {
     52     case FORMAT_HWIO:
     53       return "HWIO";
     54     case FORMAT_OIHW:
     55       return "OIHW";
     56     case FORMAT_OIHW_VECT_I:
     57       return "OIHW_VECT_I";
     58     default:
     59       LOG(FATAL) << "Invalid Filter Format: " << static_cast<int32>(format);
     60       return "INVALID_FORMAT";
     61   }
     62 }
     63 
     64 bool FormatFromString(const string& format_str, TensorFormat* format) {
     65   if (format_str == "NHWC" || format_str == "NDHWC") {
     66     *format = FORMAT_NHWC;
     67     return true;
     68   }
     69   if (format_str == "NCHW" || format_str == "NCDHW") {
     70     *format = FORMAT_NCHW;
     71     return true;
     72   }
     73   if (format_str == "NCHW_VECT_C") {
     74     *format = FORMAT_NCHW_VECT_C;
     75     return true;
     76   }
     77   return false;
     78 }
     79 
     80 bool FilterFormatFromString(const string& format_str,
     81                             FilterTensorFormat* format) {
     82   if (format_str == "HWIO" || format_str == "DHWIO") {
     83     *format = FORMAT_HWIO;
     84     return true;
     85   }
     86   if (format_str == "OIHW" || format_str == "OIDHW") {
     87     *format = FORMAT_OIHW;
     88     return true;
     89   }
     90   if (format_str == "OIHW_VECT_I") {
     91     *format = FORMAT_OIHW_VECT_I;
     92     return true;
     93   }
     94   return false;
     95 }
     96 
     97 }  // namespace tensorflow
     98