Home | History | Annotate | Download | only in framework
      1 /* Copyright 2015 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 #ifndef TENSORFLOW_CORE_FRAMEWORK_FUNCTION_TESTLIB_H_
     17 #define TENSORFLOW_CORE_FRAMEWORK_FUNCTION_TESTLIB_H_
     18 
     19 #include <string>
     20 
     21 #include "tensorflow/core/framework/attr_value_util.h"
     22 #include "tensorflow/core/framework/function.h"
     23 #include "tensorflow/core/framework/function.pb.h"
     24 #include "tensorflow/core/framework/graph.pb.h"
     25 #include "tensorflow/core/framework/node_def.pb.h"
     26 #include "tensorflow/core/lib/gtl/array_slice.h"
     27 #include "tensorflow/core/platform/types.h"
     28 
     29 namespace tensorflow {
     30 namespace test {
     31 namespace function {
     32 
     33 // A helper class to make AttrSlice from initializer lists
     34 class Attrs {
     35  public:
     36   Attrs(const std::initializer_list<  // NOLINT(runtime/explicit)
     37         std::pair<string, FunctionDefHelper::AttrValueWrapper>>& attrs) {
     38     for (const auto& aval : attrs) {
     39       map_.insert({aval.first, aval.second.proto});
     40     }
     41   }
     42 
     43   operator AttrSlice() { return AttrSlice(&map_); }  // NOLINT(runtime/explicit)
     44 
     45  private:
     46   AttrValueMap map_;
     47 };
     48 
     49 // Helper to construct a NodeDef.
     50 NodeDef NDef(
     51     StringPiece name, StringPiece op, gtl::ArraySlice<string> inputs,
     52     gtl::ArraySlice<std::pair<string, FunctionDefHelper::AttrValueWrapper>>
     53         attrs = {},
     54     const string& device = "");
     55 
     56 // Helper to construct a GraphDef proto.
     57 GraphDef GDef(gtl::ArraySlice<NodeDef> nodes,
     58               gtl::ArraySlice<FunctionDef> funcs = {});
     59 
     60 // For testing convenience, we provide a few simple functions that can
     61 // be easily executed and tested.
     62 
     63 // x:T -> x * 2.
     64 FunctionDef XTimesTwo();
     65 
     66 // x:T -> cpu(x * 2) + cpu(x * 3).
     67 FunctionDef TwoDeviceTimesFive();
     68 
     69 // x:T -> cpu(x * 2), gpu(x * 3).
     70 FunctionDef TwoDeviceMult();
     71 
     72 // cpu(x):T, gpu(y):T -> cpu(x * 2), gpu(y * 3).
     73 FunctionDef TwoDeviceInputOutput();
     74 
     75 // Function taking a list of Tensors as input.
     76 FunctionDef FuncWithListInput();
     77 
     78 // Function returning a list of Tensors as output.
     79 FunctionDef FuncWithListOutput();
     80 
     81 // x:T -> x + x.
     82 FunctionDef XAddX();
     83 
     84 // x:T -> x * 2, where x is int32.
     85 FunctionDef XTimesTwoInt32();
     86 
     87 // x:T -> (x * 2) * 2.
     88 FunctionDef XTimesFour();
     89 
     90 // x:T -> ((x * 2) * 2) * 2.
     91 FunctionDef XTimes16();
     92 
     93 // w:T, x:T, b:T -> MatMul(w, x) + b
     94 FunctionDef WXPlusB();
     95 
     96 // x:T -> x:T, T is a type which we automatically converts to a bool.
     97 FunctionDef NonZero();
     98 
     99 // x: T -> bool.
    100 FunctionDef IsZero();
    101 
    102 // x: T -> int64
    103 FunctionDef RandomUniform();
    104 
    105 // x:T, y:T -> y:T, x:T
    106 FunctionDef Swap();
    107 
    108 // x:T, y:T -> y:T, x:T, the body has no nodes.
    109 FunctionDef EmptyBodySwap();
    110 
    111 // x:float, y:resource -> y:resource, 2*x:float.
    112 FunctionDef ResourceOutput();
    113 
    114 // x:resource -> y:float.
    115 FunctionDef ReadResourceVariable();
    116 
    117 // Contains malformed control flow which can't be run by the executor.
    118 FunctionDef InvalidControlFlow();
    119 
    120 // x:T -> x <= N.
    121 FunctionDef LessThanOrEqualToN(int64 N);
    122 
    123 // x:T, y:T -> x+1, x*y
    124 FunctionDef XPlusOneXTimesY();
    125 
    126 // x:T, y:T -> x <= N
    127 FunctionDef XYXLessThanOrEqualToN(int64 N);
    128 
    129 void FunctionTestSchedClosure(std::function<void()> fn);
    130 
    131 }  // end namespace function
    132 }  // end namespace test
    133 }  // end namespace tensorflow
    134 
    135 #endif  // TENSORFLOW_CORE_FRAMEWORK_FUNCTION_TESTLIB_H_
    136