1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/core/framework/lookup_interface.h"
17
18#include "tensorflow/core/framework/tensor_shape.h"
19#include "tensorflow/core/lib/core/errors.h"
20
21namespace tensorflow {
22namespace lookup {
23
24Status LookupInterface::CheckKeyShape(const TensorShape& shape) {
25 if (!TensorShapeUtils::EndsWith(shape, key_shape())) {
26 return errors::InvalidArgument("Input key shape ", shape.DebugString(),
27 " must end with the table's key shape ",
28 key_shape().DebugString());
29 }
30 return OkStatus();
31}
32
33Status LookupInterface::CheckKeyAndValueTypes(const Tensor& keys,
34 const Tensor& values) {
35 if (keys.dtype() != key_dtype()) {
36 return errors::InvalidArgument("Key must be type ", key_dtype(),
37 " but got ", keys.dtype());
38 }
39 if (values.dtype() != value_dtype()) {
40 return errors::InvalidArgument("Value must be type ", value_dtype(),
41 " but got ", values.dtype());
42 }
43 return OkStatus();
44}
45
46Status LookupInterface::CheckKeyAndValueTensorsHelper(const Tensor& keys,
47 const Tensor& values) {
48 TF_RETURN_IF_ERROR(CheckKeyAndValueTypes(keys, values));
49 TF_RETURN_IF_ERROR(CheckKeyShape(keys.shape()));
50
51 TensorShape expected_value_shape = keys.shape();
52 for (int i = 0; i < key_shape().dims(); ++i) {
53 expected_value_shape.RemoveDim(expected_value_shape.dims() - 1);
54 }
55 expected_value_shape.AppendShape(value_shape());
56 if (values.shape() != expected_value_shape) {
57 return errors::InvalidArgument(
58 "Expected shape ", expected_value_shape.DebugString(),
59 " for value, got ", values.shape().DebugString());
60 }
61 return OkStatus();
62}
63
64Status LookupInterface::CheckKeyAndValueTensorsForInsert(const Tensor& keys,
65 const Tensor& values) {
66 return CheckKeyAndValueTensorsHelper(keys, values);
67}
68
69Status LookupInterface::CheckKeyAndValueTensorsForImport(const Tensor& keys,
70 const Tensor& values) {
71 return CheckKeyAndValueTensorsHelper(keys, values);
72}
73
74Status LookupInterface::CheckKeyTensorForRemove(const Tensor& keys) {
75 if (keys.dtype() != key_dtype()) {
76 return errors::InvalidArgument("Key must be type ", key_dtype(),
77 " but got ", keys.dtype());
78 }
79 return CheckKeyShape(keys.shape());
80}
81
82Status LookupInterface::CheckFindArguments(const Tensor& key,
83 const Tensor& default_value) {
84 TF_RETURN_IF_ERROR(CheckKeyAndValueTypes(key, default_value));
85 TF_RETURN_IF_ERROR(CheckKeyShape(key.shape()));
86 TensorShape fullsize_value_shape = key.shape();
87 for (int i = 0; i < key_shape().dims(); ++i) {
88 fullsize_value_shape.RemoveDim(fullsize_value_shape.dims() - 1);
89 }
90 fullsize_value_shape.AppendShape(value_shape());
91 if (default_value.shape() != value_shape() &&
92 default_value.shape() != fullsize_value_shape) {
93 return errors::InvalidArgument(
94 "Expected shape ", value_shape().DebugString(), " or ",
95 fullsize_value_shape.DebugString(), " for default value, got ",
96 default_value.shape().DebugString());
97 }
98 return OkStatus();
99}
100
101} // namespace lookup
102} // namespace tensorflow
103