aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/framework/variant.cc
blob: d43e3c72ece410aa1b9f58def0a09eff60a34756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/core/framework/variant.h"
#include "tensorflow/core/framework/tensor.pb.h"
#include "tensorflow/core/framework/variant_encode_decode.h"
#include "tensorflow/core/framework/variant_op_registry.h"
#include "tensorflow/core/framework/variant_tensor_data.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/gtl/map_util.h"

namespace tensorflow {

bool Variant::Decode(VariantTensorData data) {
  if (!is_empty()) {
    return value_->Decode(std::move(data));
  }
  return true;
}

template <>
void* Variant::get() {
  if (is_empty()) {
    return nullptr;
  }
  return value_->RawPtr();
}

template <>
const void* Variant::get() const {
  if (is_empty()) {
    return nullptr;
  }
  return value_->RawPtr();
}

template <>
string TypeNameVariant(const VariantTensorDataProto& value) {
  return value.type_name();
}

template <>
void EncodeVariant(const VariantTensorDataProto& value,
                   VariantTensorData* data) {
  data->FromConstProto(value);
}

template <>
bool DecodeVariant(VariantTensorData* data, VariantTensorDataProto* value) {
  data->ToProto(value);
  return true;
}

template <>
void EncodeVariant(const VariantTensorDataProto& value, string* buf) {
  value.SerializeToString(buf);
}

template <>
bool DecodeVariant(string* buf, VariantTensorDataProto* value) {
  return value->ParseFromString(*buf);
}

void EncodeVariantList(const Variant* variant_array, int64 n,
                       std::unique_ptr<port::StringListEncoder> e) {
  for (int i = 0; i < n; ++i) {
    string s;
    variant_array[i].Encode(&s);
    e->Append(s);
  }
  e->Finalize();
}

bool DecodeVariantList(std::unique_ptr<port::StringListDecoder> d,
                       Variant* variant_array, int64 n) {
  std::vector<uint32> sizes(n);
  if (!d->ReadSizes(&sizes)) return false;

  for (int i = 0; i < n; ++i) {
    if (variant_array[i].is_empty()) {
      variant_array[i] = VariantTensorDataProto();
    }
    // TODO(ebrevdo): Replace with StringPiece?  Any way to make this a
    // zero-copy operation that keeps a reference to the data in d?
    string str(d->Data(sizes[i]), sizes[i]);
    if (!variant_array[i].Decode(std::move(str))) return false;
    if (!DecodeUnaryVariant(&variant_array[i])) {
      LOG(ERROR) << "Could not decode variant with type_name: \""
                 << variant_array[i].TypeName()
                 << "\".  Perhaps you forgot to register a "
                    "decoder via REGISTER_UNARY_VARIANT_DECODE_FUNCTION?";
      return false;
    }
  }
  return true;
}

}  // end namespace tensorflow