Home | History | Annotate | Download | only in kernels
      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/framework/op_kernel.h"
     17 #include "tensorflow/core/framework/register_types.h"
     18 #include "tensorflow/core/framework/resource_mgr.h"
     19 #include "tensorflow/core/framework/summary.pb.h"
     20 #include "tensorflow/core/framework/tensor.pb.h"
     21 #include "tensorflow/core/lib/core/errors.h"
     22 #include "tensorflow/core/platform/logging.h"
     23 #include "tensorflow/core/platform/protobuf.h"
     24 
     25 namespace tensorflow {
     26 
     27 template <typename T>
     28 class SummaryTensorOpV2 : public OpKernel {
     29  public:
     30   explicit SummaryTensorOpV2(OpKernelConstruction* context)
     31       : OpKernel(context) {}
     32 
     33   void Compute(OpKernelContext* c) override {
     34     const Tensor& tag = c->input(0);
     35     OP_REQUIRES(c, IsLegacyScalar(tag.shape()),
     36                 errors::InvalidArgument("tag must be scalar"));
     37     const Tensor& tensor = c->input(1);
     38     const Tensor& serialized_summary_metadata_tensor = c->input(2);
     39 
     40     Summary s;
     41     Summary::Value* v = s.add_value();
     42     v->set_tag(tag.scalar<string>()());
     43 
     44     if (tensor.dtype() == DT_STRING) {
     45       // tensor_util.makeNdarray doesn't work for strings in tensor_content
     46       tensor.AsProtoField(v->mutable_tensor());
     47     } else {
     48       tensor.AsProtoTensorContent(v->mutable_tensor());
     49     }
     50 
     51     v->mutable_metadata()->ParseFromString(
     52         serialized_summary_metadata_tensor.scalar<string>()());
     53 
     54     Tensor* summary_tensor = nullptr;
     55     OP_REQUIRES_OK(c, c->allocate_output(0, TensorShape({}), &summary_tensor));
     56     CHECK(s.SerializeToString(&summary_tensor->scalar<string>()()));
     57   }
     58 };
     59 
     60 #define REGISTER(T)                                                      \
     61   REGISTER_KERNEL_BUILDER(                                               \
     62       Name("TensorSummaryV2").Device(DEVICE_CPU).TypeConstraint<T>("T"), \
     63       SummaryTensorOpV2<T>);
     64 
     65 TF_CALL_ALL_TYPES(REGISTER)
     66 
     67 #undef REGISTER
     68 
     69 // NOTE(chizeng): We are phasing out the use of SummaryTensorOp in favor of
     70 // SummaryTensorOpV2. This is because SummaryTensorOpV2 allows the callers to
     71 // pass a tag (more consistent with other summaries) as well as serialized
     72 // summary metadata used by plugins (which lets TensorBoard determine which
     73 // events are relevant to which plugins).
     74 template <typename T>
     75 class SummaryTensorOp : public OpKernel {
     76  public:
     77   explicit SummaryTensorOp(OpKernelConstruction* context) : OpKernel(context) {}
     78 
     79   void Compute(OpKernelContext* c) override {
     80     const Tensor& tensor = c->input(0);
     81 
     82     Summary s;
     83     Summary::Value* v = s.add_value();
     84     v->set_node_name(c->op_kernel().name());
     85 
     86     if (tensor.dtype() == DT_STRING) {
     87       // tensor_util.makeNdarray doesn't work for strings in tensor_content
     88       tensor.AsProtoField(v->mutable_tensor());
     89     } else {
     90       tensor.AsProtoTensorContent(v->mutable_tensor());
     91     }
     92 
     93     Tensor* summary_tensor = nullptr;
     94     OP_REQUIRES_OK(c, c->allocate_output(0, TensorShape({}), &summary_tensor));
     95     CHECK(s.SerializeToString(&summary_tensor->scalar<string>()()));
     96   }
     97 };
     98 
     99 #define REGISTER(T)                                                    \
    100   REGISTER_KERNEL_BUILDER(                                             \
    101       Name("TensorSummary").Device(DEVICE_CPU).TypeConstraint<T>("T"), \
    102       SummaryTensorOp<T>);
    103 
    104 TF_CALL_ALL_TYPES(REGISTER)
    105 
    106 #undef REGISTER
    107 
    108 }  // namespace tensorflow
    109