1/* Copyright 2017 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/op.h"
17#include "tensorflow/core/framework/shape_inference.h"
18
19namespace tensorflow {
20REGISTER_OP("GenerateVocabRemapping")
21 .Input("new_vocab_file: string")
22 .Input("old_vocab_file: string")
23 .Attr("new_vocab_offset: int >= 0")
24 .Attr("num_new_vocab: int >= 0")
25 .Attr("old_vocab_size: int >= -1 = -1")
26 .Output("remapping: int64")
27 .Output("num_present: int32")
28 .SetShapeFn([](shape_inference::InferenceContext* c) {
29 shape_inference::ShapeHandle unused;
30 TF_RETURN_IF_ERROR(c->WithRank(c->input(0), 0, &unused));
31 TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 0, &unused));
32
33 int64_t new_vocab_offset;
34 TF_RETURN_IF_ERROR(c->GetAttr("new_vocab_offset", &new_vocab_offset));
35 int64_t num_new_vocab;
36 TF_RETURN_IF_ERROR(c->GetAttr("num_new_vocab", &num_new_vocab));
37
38 c->set_output(0, c->Vector(num_new_vocab));
39 c->set_output(1, c->Scalar());
40 return OkStatus();
41 });
42
43REGISTER_OP("LoadAndRemapMatrix")
44 .Input("ckpt_path: string")
45 .Input("old_tensor_name: string")
46 .Input("row_remapping: int64")
47 .Input("col_remapping: int64")
48 .Input("initializing_values: float")
49 .Attr("num_rows: int >= 0")
50 .Attr("num_cols: int >= 1")
51 .Attr("max_rows_in_memory: int = -1")
52 .Output("output_matrix: float")
53 // TODO(b/30502450): Setting the op as being stateful prevents it from being
54 // executed more often than expected (possibly due to stateful ops not being
55 // subject to constant folding?). This op is usually slow and may require
56 // multiple disk reads, so we want to minimize the number of times it's
57 // executed redundantly.
58 .SetIsStateful()
59 .SetShapeFn([](shape_inference::InferenceContext* c) {
60 shape_inference::ShapeHandle unused;
61 TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 0, &unused));
62
63 int64_t num_rows;
64 TF_RETURN_IF_ERROR(c->GetAttr("num_rows", &num_rows));
65 int64_t num_cols;
66 TF_RETURN_IF_ERROR(c->GetAttr("num_cols", &num_cols));
67
68 c->set_output(0, c->Matrix(num_rows, num_cols));
69 return OkStatus();
70 });
71} // namespace tensorflow
72