Home | History | Annotate | Download | only in costs
      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/core/grappler/costs/cost_estimator.h"
     17 
     18 namespace tensorflow {
     19 namespace grappler {
     20 
     21 Costs CombineCosts(const Costs& left, const Costs& right) {
     22   CHECK_NE(left.max_memory, kMemoryUnknown);
     23   CHECK_NE(left.max_per_op_buffers, kMemoryUnknown);
     24   CHECK_NE(left.max_per_op_streaming, kMemoryUnknown);
     25 
     26   Costs result = left;
     27   result.execution_time += right.execution_time;
     28   result.compute_time += right.compute_time;
     29   result.memory_time += right.memory_time;
     30   result.intermediate_memory_time += right.intermediate_memory_time;
     31   result.intermediate_memory_read_time += right.intermediate_memory_read_time;
     32   result.intermediate_memory_write_time += right.intermediate_memory_write_time;
     33 
     34   if (right.max_per_op_buffers != kMemoryUnknown) {
     35     result.max_per_op_buffers =
     36         std::max(left.max_per_op_buffers, right.max_per_op_buffers);
     37   }
     38   if (right.max_per_op_streaming != kMemoryUnknown) {
     39     result.max_per_op_streaming =
     40         std::max(left.max_per_op_streaming, right.max_per_op_streaming);
     41   }
     42 
     43   result.num_ops_total += right.num_ops_total;
     44   if (right.inaccurate) {
     45     result.inaccurate = true;
     46   }
     47   result.num_ops_with_unknown_shapes += right.num_ops_with_unknown_shapes;
     48   if (right.max_memory != kMemoryUnknown) {
     49     result.max_memory += right.max_memory;
     50   }
     51 
     52   return result;
     53 }
     54 
     55 // Multiplies Costs by a scalar.
     56 // Equivalent to applying CombineCosts "multiplier" times.
     57 // Note the field regarding num_ops are not multiplied.
     58 Costs MultiplyCosts(const Costs& costs, int multiplier) {
     59   CHECK_GE(multiplier, 0);
     60   if (multiplier == 0) {
     61     return Costs::ZeroCosts();
     62   }
     63   if (multiplier == 1) {
     64     return costs;
     65   }
     66 
     67   Costs result = costs;
     68   result.execution_time *= multiplier;
     69   result.compute_time *= multiplier;
     70   result.memory_time *= multiplier;
     71   result.intermediate_memory_time *= multiplier;
     72   result.intermediate_memory_read_time *= multiplier;
     73   result.intermediate_memory_write_time *= multiplier;
     74   if (result.max_memory != kMemoryUnknown) {
     75     result.max_memory *= multiplier;
     76   }
     77   return result;
     78 }
     79 
     80 }  // end namespace grappler
     81 }  // end namespace tensorflow
     82