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 | // See docs in ../ops/parsing_ops.cc. |
17 | |
18 | #include "tensorflow/core/framework/op_kernel.h" |
19 | #include "tensorflow/core/framework/register_types.h" |
20 | #include "tensorflow/core/framework/tensor.h" |
21 | #include "tensorflow/core/framework/tensor.pb.h" |
22 | #include "tensorflow/core/framework/tensor_shape.h" |
23 | #include "tensorflow/core/framework/types.h" |
24 | #include "tensorflow/core/lib/core/errors.h" |
25 | |
26 | namespace tensorflow { |
27 | |
28 | class ParseTensorOp : public OpKernel { |
29 | public: |
30 | explicit ParseTensorOp(OpKernelConstruction* context) : OpKernel(context) { |
31 | OP_REQUIRES_OK(context, context->GetAttr("out_type" , &out_type_)); |
32 | } |
33 | |
34 | void Compute(OpKernelContext* ctx) override { |
35 | const Tensor& serialized = ctx->input(0); |
36 | |
37 | OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(serialized.shape()), |
38 | errors::InvalidArgument( |
39 | "Expected `serialized` to be a scalar, got shape: " , |
40 | serialized.shape().DebugString())); |
41 | |
42 | auto serialized_t = serialized.scalar<tstring>(); |
43 | |
44 | TensorProto proto; |
45 | OP_REQUIRES(ctx, ParseProtoUnlimited(&proto, serialized_t()), |
46 | errors::InvalidArgument( |
47 | "Could not parse `serialized` as TensorProto: '" , |
48 | serialized_t(), "'" )); |
49 | |
50 | Tensor output; |
51 | OP_REQUIRES_OK(ctx, ctx->device()->MakeTensorFromProto( |
52 | proto, ctx->output_alloc_attr(0), &output)); |
53 | |
54 | OP_REQUIRES( |
55 | ctx, out_type_ == output.dtype(), |
56 | errors::InvalidArgument("Type mismatch between parsed tensor (" , |
57 | DataTypeString(output.dtype()), ") and dtype (" , |
58 | DataTypeString(out_type_), ")" )); |
59 | |
60 | ctx->set_output(0, output); |
61 | } |
62 | |
63 | private: |
64 | DataType out_type_; |
65 | }; |
66 | |
67 | REGISTER_KERNEL_BUILDER(Name("ParseTensor" ).Device(DEVICE_CPU), ParseTensorOp); |
68 | |
69 | template <typename T> |
70 | class SerializeTensorOp : public OpKernel { |
71 | public: |
72 | using OpKernel::OpKernel; |
73 | |
74 | void Compute(OpKernelContext* context) override { |
75 | const Tensor& tensor = context->input(0); |
76 | TensorProto proto; |
77 | if (tensor.dtype() == DT_STRING) { |
78 | tensor.AsProtoField(&proto); |
79 | } else { |
80 | tensor.AsProtoTensorContent(&proto); |
81 | } |
82 | Tensor* proto_string = nullptr; |
83 | OP_REQUIRES_OK(context, |
84 | context->allocate_output(0, TensorShape({}), &proto_string)); |
85 | CHECK(SerializeToTString(proto, &proto_string->scalar<tstring>()())); |
86 | } |
87 | }; |
88 | |
89 | #define REGISTER(T) \ |
90 | REGISTER_KERNEL_BUILDER( \ |
91 | Name("SerializeTensor").Device(DEVICE_CPU).TypeConstraint<T>("T"), \ |
92 | SerializeTensorOp<T>); |
93 | TF_CALL_ALL_TYPES(REGISTER) |
94 | #undef REGISTER |
95 | |
96 | } // namespace tensorflow |
97 | |