Home | History | Annotate | Download | only in common_runtime
      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 #include "tensorflow/core/common_runtime/function_testlib.h"
     16 
     17 #include "tensorflow/core/common_runtime/device.h"
     18 #include "tensorflow/core/framework/node_def.pb.h"
     19 #include "tensorflow/core/framework/node_def_builder.h"
     20 #include "tensorflow/core/framework/op_kernel.h"
     21 
     22 namespace tensorflow {
     23 namespace test {
     24 namespace function {
     25 
     26 typedef FunctionDefHelper FDH;
     27 
     28 class FindDeviceOpKernel : public OpKernel {
     29  public:
     30   explicit FindDeviceOpKernel(OpKernelConstruction* ctx) : OpKernel(ctx) {}
     31   void Compute(OpKernelContext* ctx) override {
     32     Tensor* device_tensor = nullptr;
     33     OP_REQUIRES_OK(ctx, ctx->allocate_output("device_name", TensorShape{},
     34                                              &device_tensor));
     35     device_tensor->scalar<string>()() =
     36         ctx->function_library()->device()->name();
     37   }
     38 };
     39 
     40 REGISTER_KERNEL_BUILDER(Name("FindDeviceOp").Device(tensorflow::DEVICE_CPU),
     41                         FindDeviceOpKernel);
     42 REGISTER_OP("FindDeviceOp").Output("device_name: string");
     43 
     44 FunctionDef FindDevice() {
     45   return FDH::Define(
     46       // Name
     47       "FindDevice",
     48       // Args
     49       {},
     50       // Return values
     51       {"device_name: string"},
     52       // Attr def
     53       {},
     54       // Nodes
     55       {{{"device_name"}, "FindDeviceOp", {}, {}}});
     56 }
     57 
     58 // TODO(phawkins): replace with C++ API for calling functions, when that exists.
     59 Output Call(Scope* scope, const string& op_name, const string& fn_name,
     60             gtl::ArraySlice<Input> inputs) {
     61   NodeDef def;
     62   NodeDefBuilder builder(op_name, fn_name, scope->graph()->op_registry());
     63   for (const Input& input : inputs) {
     64     builder.Input(input.node()->name(), input.index(),
     65                   input.node()->output_type(input.index()));
     66   }
     67   TF_CHECK_OK(builder.Finalize(&def));
     68   Status status;
     69   Node* n = scope->graph()->AddNode(def, &status);
     70   TF_CHECK_OK(status);
     71   TF_CHECK_OK(scope->DoShapeInference(n));
     72   for (int i = 0; i < inputs.size(); ++i) {
     73     scope->graph()->AddEdge(inputs[i].node(), inputs[i].index(), n, i);
     74   }
     75   return Output(n);
     76 }
     77 
     78 }  // namespace function
     79 }  // namespace test
     80 }  // namespace tensorflow
     81