Home | History | Annotate | Download | only in grappler
      1 /* Copyright 2017 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/python/grappler/model_analyzer.h"
     17 
     18 #include <iomanip>
     19 #include "tensorflow/core/framework/op.h"
     20 #include "tensorflow/core/framework/tensor_shape.pb.h"
     21 #include "tensorflow/core/grappler/costs/graph_properties.h"
     22 #include "tensorflow/core/grappler/grappler_item.h"
     23 
     24 namespace tensorflow {
     25 namespace grappler {
     26 
     27 ModelAnalyzer::ModelAnalyzer(const GrapplerItem& item) : item_(item) {}
     28 
     29 Status ModelAnalyzer::GenerateReport(bool debug, std::ostream& os) {
     30   GraphProperties properties(item_);
     31   TF_RETURN_IF_ERROR(properties.InferStatically(false));
     32 
     33   for (const auto& node : item_.MainOpsFanin()) {
     34     PrintNodeInfo(node, properties, debug, os);
     35   }
     36   for (const auto& node : item_.EnqueueOpsFanin()) {
     37     PrintNodeInfo(node, properties, debug, os);
     38   }
     39 
     40   return Status::OK();
     41 }
     42 
     43 void ModelAnalyzer::PrintNodeInfo(const NodeDef* node,
     44                                   const GraphProperties& properties, bool debug,
     45                                   std::ostream& os) const {
     46   os << node->name() << " [" << node->op() << "]" << std::endl;
     47   if (properties.HasOutputProperties(node->name())) {
     48     const std::vector<OpInfo::TensorProperties>& props =
     49         properties.GetOutputProperties(node->name());
     50     for (int i = 0; i < props.size(); ++i) {
     51       const OpInfo::TensorProperties& prop = props[i];
     52       os << "\t"
     53          << "output " << i << " (" << DataTypeString(prop.dtype())
     54          << ") has shape ";
     55       if (prop.shape().unknown_rank()) {
     56         os << "?";
     57       } else {
     58         os << "[";
     59         for (int i = 0; i < prop.shape().dim_size(); ++i) {
     60           if (i > 0) {
     61             os << ", ";
     62           }
     63           if (prop.shape().dim(i).size() >= 0) {
     64             // Print the actual dimension.
     65             os << prop.shape().dim(i).size();
     66           } else if (prop.shape().dim(i).size() == -1) {
     67             // We don't know anything about the dimension.
     68             os << "?";
     69           } else {
     70             // Symbolic dimension.
     71             os << "x" << -prop.shape().dim(i).size();
     72           }
     73         }
     74         os << "]";
     75       }
     76       os << std::endl;
     77     }
     78   }
     79 
     80   if (debug) {
     81     const OpRegistrationData* op_reg_data;
     82     Status status = OpRegistry::Global()->LookUp(node->op(), &op_reg_data);
     83     if (!status.ok()) {
     84       os << "\tCouldn't find op registration for " << node->op() << std::endl;
     85     } else if (!op_reg_data->shape_inference_fn) {
     86       os << "\tCouldn't find shape function for op " << node->op() << std::endl;
     87     } else if (properties.HasInputProperties(node->name())) {
     88       const std::vector<OpInfo::TensorProperties>& props =
     89           properties.GetInputProperties(node->name());
     90       for (int i = 0; i < props.size(); ++i) {
     91         const OpInfo::TensorProperties& prop = props[i];
     92         if (prop.has_value()) {
     93           os << "\t"
     94              << "input " << i << " (" << DataTypeString(prop.dtype())
     95              << ") has known value" << std::endl;
     96         }
     97       }
     98     }
     99   }
    100 }
    101 
    102 }  // end namespace grappler
    103 }  // end namespace tensorflow
    104