forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_ops.cc
More file actions
2688 lines (2190 loc) · 85.7 KB
/
array_ops.cc
File metadata and controls
2688 lines (2190 loc) · 85.7 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2015 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/common_shape_fns.h"
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/shape_inference.h"
#include "tensorflow/core/util/mirror_pad_mode.h"
#include "tensorflow/core/util/padding.h"
namespace tensorflow {
using shape_inference::Dimension;
using shape_inference::InferenceContext;
using shape_inference::Shape;
namespace {
Status GetAxisForPackAndUnpack(InferenceContext* c, int32 rank_after_pack,
int32* axis) {
TF_RETURN_IF_ERROR(c->GetAttr("axis", axis));
if (*axis < -1 * rank_after_pack || *axis >= rank_after_pack) {
return errors::InvalidArgument("Invalid axis: ", *axis, "; must be in [",
-1 * rank_after_pack, ",", rank_after_pack,
")");
}
if (*axis < 0) *axis = (rank_after_pack + *axis);
return Status::OK();
}
Status PadShapeFn(InferenceContext* c) {
// Paddings is a matrix of [input_rank, 2].
const Shape* paddings;
TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 2, &paddings));
const Dimension* unused;
TF_RETURN_IF_ERROR(c->WithValue(c->Dim(paddings, 1), 2, &unused));
// n_dim and input.rank are equivalent.
const Shape* input = c->input(0);
const Dimension* n_dim = c->Dim(paddings, 0);
if (c->ValueKnown(n_dim)) {
TF_RETURN_IF_ERROR(c->WithRank(input, c->Value(n_dim), &input));
} else if (c->RankKnown(input)) {
TF_RETURN_IF_ERROR(c->WithValue(n_dim, c->Rank(input), &n_dim));
}
const Tensor* paddings_t = c->input_tensor(1);
// paddings_t is unknown
if (paddings_t == nullptr) {
if (c->ValueKnown(n_dim)) {
// Make output with n_dim unknown dims.
std::vector<const Dimension*> dims;
const auto value = c->Value(n_dim);
for (int i = 0; i < value; ++i) dims.push_back(c->UnknownDim());
c->set_output(0, c->MakeShape(dims));
} else {
c->set_output(0, c->UnknownShape());
}
return Status::OK();
}
// tensor value was provided for paddings_t; doublecheck n_dim value is the
// same.
const auto num_dims = c->Value(n_dim);
DCHECK_EQ(num_dims, paddings_t->shape().dim_size(0));
// paddings_t is known.
auto paddings_data = paddings_t->matrix<int32>();
std::vector<const Dimension*> dims(num_dims);
for (int i = 0; i < num_dims; ++i) {
const int32 pad0 = paddings_data(i, 0);
const int32 pad1 = paddings_data(i, 1);
if (pad0 < 0 || pad1 < 0) {
return errors::InvalidArgument("Paddings must be non-negative");
}
TF_RETURN_IF_ERROR(c->Add(c->Dim(input, i), pad0 + pad1, &dims[i]));
}
c->set_output(0, c->MakeShape(dims));
return Status::OK();
}
} // namespace
REGISTER_OP("Pack")
.Input("values: N * T")
.Output("output: T")
.Attr("N: int >= 1")
.Attr("T: type")
.Attr("axis: int = 0")
.SetShapeFn([](InferenceContext* c) {
// Validate shapes of all inputs are compatible
const Shape* cur = c->input(c->num_inputs() - 1);
for (int i = c->num_inputs() - 2; i >= 0; --i) {
TF_RETURN_WITH_CONTEXT_IF_ERROR(c->Merge(c->input(i), cur, &cur),
"From merging shape ", i,
" with other shapes.");
}
if (!c->RankKnown(cur)) {
c->set_output(0, c->UnknownShape());
return Status::OK();
}
// Determine the axis that will be added, converting from negative
// axes to a positive point per negative indexing rules.
int32 rank = c->Rank(cur);
int32 axis;
TF_RETURN_IF_ERROR(GetAxisForPackAndUnpack(c, rank + 1, &axis));
// Copy all dimensions over, inserting a dimension of value #inputs
// at <axis>.
std::vector<const Dimension*> dims;
int index = 0;
while (index < axis) dims.push_back(c->Dim(cur, index++));
dims.push_back(c->MakeDim(c->num_inputs()));
while (index < rank) dims.push_back(c->Dim(cur, index++));
c->set_output(0, c->MakeShape(dims));
return Status::OK();
})
.Doc(R"doc(
Packs a list of `N` rank-`R` tensors into one rank-`(R+1)` tensor.
Packs the `N` tensors in `values` into a tensor with rank one higher than each
tensor in `values`, by packing them along the `axis` dimension.
Given a list of tensors of shape `(A, B, C)`;
if `axis == 0` then the `output` tensor will have the shape `(N, A, B, C)`.
if `axis == 1` then the `output` tensor will have the shape `(A, N, B, C)`.
Etc.
For example:
```prettyprint
# 'x' is [1, 4]
# 'y' is [2, 5]
# 'z' is [3, 6]
pack([x, y, z]) => [[1, 4], [2, 5], [3, 6]] # Pack along first dim.
pack([x, y, z], axis=1) => [[1, 2, 3], [4, 5, 6]]
```
This is the opposite of `unpack`.
values: Must be of same shape and type.
axis: Dimension along which to pack. Negative values wrap around, so the
valid range is `[-(R+1), R+1)`.
output: The packed tensor.
)doc");
// --------------------------------------------------------------------------
REGISTER_OP("Unpack")
.Input("value: T")
.Output("output: num * T")
.Attr("num: int >= 0")
.Attr("T: type")
.Attr("axis: int = 0")
.SetShapeFn([](InferenceContext* c) {
const Shape* s = c->input(0);
const Shape* out;
if (c->RankKnown(s)) {
// Determine the axis that will be removed, converting from negative
// axes to a positive point per negative indexing rules.
int32 rank = c->Rank(s);
int32 axis;
TF_RETURN_IF_ERROR(GetAxisForPackAndUnpack(c, rank, &axis));
// The axis dim matches the number of outputs.
const Dimension* unused;
TF_RETURN_IF_ERROR(
c->WithValue(c->Dim(s, axis), c->num_outputs(), &unused));
// Copy all dimensions, removing the <axis> dimension.
std::vector<const Dimension*> dims;
for (int i = 0; i < rank; ++i) {
if (i != axis) dims.push_back(c->Dim(s, i));
}
out = c->MakeShape(dims);
} else {
// All outputs are the same shape, but it's not known.
out = c->UnknownShape();
}
for (int i = 0; i < c->num_outputs(); ++i) c->set_output(i, out);
return Status::OK();
})
.Doc(R"doc(
Unpacks a given dimension of a rank-`R` tensor into `num` rank-`(R-1)` tensors.
Unpacks `num` tensors from `value` by chipping it along the `axis` dimension.
For example, given a tensor of shape `(A, B, C, D)`;
If `axis == 0` then the i'th tensor in `output` is the slice `value[i, :, :, :]`
and each tensor in `output` will have shape `(B, C, D)`. (Note that the
dimension unpacked along is gone, unlike `split`).
If `axis == 1` then the i'th tensor in `output` is the slice `value[:, i, :, :]`
and each tensor in `output` will have shape `(A, C, D)`.
Etc.
This is the opposite of `pack`.
value: 1-D or higher, with `axis` dimension size equal to `num`.
axis: Dimension along which to unpack. Negative values wrap around, so the
valid range is `[-R, R)`.
output: The list of tensors unpacked from `value`.
)doc");
// --------------------------------------------------------------------------
// TODO(josh11b): Remove the >= 2 constraint, once we can rewrite the graph
// in the N == 1 case to remove the node.
REGISTER_OP("Concat")
.Input("concat_dim: int32")
.Input("values: N * T")
.Output("output: T")
.Attr("N: int >= 2")
.Attr("T: type")
.SetShapeFn([](InferenceContext* c) {
const Shape* unused;
TF_RETURN_IF_ERROR(c->WithRank(c->input(0), 0, &unused));
const Tensor* concat_dim_t = c->input_tensor(0);
if (concat_dim_t == nullptr) {
// Return an unknown shape with same rank as inputs, or an unknown rank
// if no input's rank is known.
// Find rank.
int32 rank = InferenceContext::kUnknownRank;
for (int i = 1; i < c->num_inputs(); ++i) {
if (rank == InferenceContext::kUnknownRank)
rank = c->Rank(c->input(i));
if (rank != InferenceContext::kUnknownRank) {
TF_RETURN_IF_ERROR(c->WithRank(c->input(i), rank, &unused));
}
}
if (rank == InferenceContext::kUnknownRank) {
c->set_output(0, c->UnknownShape());
return Status::OK();
}
if (rank == 0) {
return errors::InvalidArgument(
"Can't concatenate scalars (use tf.pack instead)");
}
// Build result of <rank> different unknown dims.
std::vector<const Dimension*> dims;
for (int i = 0; i < rank; ++i) dims.push_back(c->UnknownDim());
c->set_output(0, c->MakeShape(dims));
return Status::OK();
}
// Merge all the non-concat dims, and sum the concat dim to make an output
// shape.
const int32 concat_dim = concat_dim_t->scalar<int32>()();
if (concat_dim < 0) {
return errors::InvalidArgument("Expected concat_dim >= 0, but got ",
concat_dim);
}
const Shape* output_before;
const Shape* output_after;
const Shape* input = c->input(c->num_inputs() - 1);
TF_RETURN_IF_ERROR(c->WithRankAtLeast(input, concat_dim + 1, &input));
TF_RETURN_IF_ERROR(c->Subshape(input, 0, concat_dim, &output_before));
const Dimension* output_middle = c->Dim(input, concat_dim);
TF_RETURN_IF_ERROR(c->Subshape(input, concat_dim + 1, &output_after));
for (int i = c->num_inputs() - 2; i > 0; --i) {
const Shape* before;
const Shape* after;
input = c->input(i);
TF_RETURN_IF_ERROR(c->WithRankAtLeast(input, concat_dim + 1, &input));
TF_RETURN_IF_ERROR(c->Subshape(input, 0, concat_dim, &before));
const Dimension* middle = c->Dim(input, concat_dim);
TF_RETURN_IF_ERROR(c->Subshape(input, concat_dim + 1, &after));
TF_RETURN_IF_ERROR(c->Merge(before, output_before, &output_before));
TF_RETURN_IF_ERROR(c->Add(output_middle, middle, &output_middle));
TF_RETURN_IF_ERROR(c->Merge(after, output_after, &output_after));
}
const Shape* s;
TF_RETURN_IF_ERROR(
c->Concatenate(output_before, c->Vector(output_middle), &s));
TF_RETURN_IF_ERROR(c->Concatenate(s, output_after, &s));
c->set_output(0, s);
return Status::OK();
})
.Doc(R"doc(
Concatenates tensors along one dimension.
concat_dim: 0-D. The dimension along which to concatenate. Must be in the
range [0, rank(values)).
values: The `N` Tensors to concatenate. Their ranks and types must match,
and their sizes must match in all dimensions except `concat_dim`.
output: A `Tensor` with the concatenation of values stacked along the
`concat_dim` dimension. This tensor's shape matches that of `values` except
in `concat_dim` where it has the sum of the sizes.
)doc");
REGISTER_OP("ConcatOffset")
.Input("concat_dim: int32")
.Input("shape: N * int32")
.Output("offset: N * int32")
.Attr("N: int >= 2")
.SetShapeFn([](InferenceContext* c) {
for (int i = 1; i < c->num_inputs(); ++i) {
c->set_output(i - 1, c->input(i));
}
return Status::OK();
})
.Doc(R"doc(
Computes offsets of concat inputs within its output.
For example:
```prettyprint
# 'x' is [2, 2, 7]
# 'y' is [2, 3, 7]
# 'z' is [2, 5, 7]
concat_offset(2, [x, y, z]) => [0, 0, 0], [0, 2, 0], [0, 5, 0]
```
concat_dim: The dimension along which to concatenate.
shape: The `N` int32 vectors representing shape of tensors being concatenated.
offset: The `N` int32 vectors representing the starting offset
of input tensors within the concatenated output.
This is typically used by gradient computations for a concat operation.
)doc");
// --------------------------------------------------------------------------
REGISTER_OP("Split")
.Input("split_dim: int32")
.Input("value: T")
.Output("output: num_split * T")
.Attr("num_split: int >= 1")
.Attr("T: type")
.Doc(R"doc(
Splits a tensor into `num_split` tensors along one dimension.
split_dim: 0-D. The dimension along which to split. Must be in the range
`[0, rank(value))`.
num_split: The number of ways to split. Must evenly divide
`value.shape[split_dim]`.
value: The tensor to split.
output: They are identically shaped tensors, whose shape matches that of `value`
except along `split_dim`, where their sizes are
`values.shape[split_dim] / num_split`.
)doc");
// --------------------------------------------------------------------------
REGISTER_OP("Const")
.Output("output: dtype")
.Attr("value: tensor")
.Attr("dtype: type")
.SetShapeFn([](InferenceContext* c) {
const TensorProto* proto = nullptr;
TF_RETURN_IF_ERROR(c->GetAttr("value", &proto));
TF_RETURN_IF_ERROR(TensorShape::IsValidShape(proto->tensor_shape()));
TensorShape shape(proto->tensor_shape());
std::vector<const Dimension*> dims;
for (int i = 0; i < shape.dims(); ++i) {
dims.push_back(c->MakeDim(shape.dim_size(i)));
}
c->set_output(0, c->MakeShape(dims));
return Status::OK();
})
.Doc(R"doc(
Returns a constant tensor.
value: Attr `value` is the tensor to return.
)doc");
// --------------------------------------------------------------------------
// TODO(mgubin): Update the doc when the freeze_graph script supports converting
// into memmapped format.
REGISTER_OP("ImmutableConst")
.Attr("dtype: type")
.Attr("shape: shape")
.Attr("memory_region_name: string")
.Output("tensor: dtype")
.SetShapeFn([](InferenceContext* c) {
TensorShape shape_from_attr;
TF_RETURN_IF_ERROR(c->GetAttr("shape", &shape_from_attr));
TensorShapeProto shape_proto;
shape_from_attr.AsProto(&shape_proto);
const Shape* output_shape;
TF_RETURN_IF_ERROR(
c->MakeShapeFromShapeProto(shape_proto, &output_shape));
c->set_output(0, output_shape);
return Status::OK();
})
.Doc(R"doc(
Returns immutable tensor from memory region.
The current implementation memmaps the tensor from a file.
dtype: Type of the returned tensor.
shape: Shape of the returned tensor.
memory_region_name: Name of readonly memory region used by the tensor, see
NewReadOnlyMemoryRegionFromFile in tensorflow::Env.
)doc");
// --------------------------------------------------------------------------
REGISTER_OP("ZerosLike")
.Input("x: T")
.Output("y: T")
.Attr("T: type")
.SetShapeFn(shape_inference::UnchangedShape)
.Doc(R"doc(
Returns a tensor of zeros with the same shape and type as x.
x: a tensor of type T.
y: a tensor of the same shape and type as x but filled with zeros.
)doc");
// --------------------------------------------------------------------------
REGISTER_OP("Diag")
.Input("diagonal: T")
.Output("output: T")
.Attr("T: {float, double, int32, int64, complex64}")
.SetShapeFn([](InferenceContext* c) {
const Shape* in = c->input(0);
TF_RETURN_IF_ERROR(c->WithRankAtMost(in, 3, &in));
// Output shape is original concatenated with itself.
const Shape* out;
TF_RETURN_IF_ERROR(c->Concatenate(in, in, &out));
c->set_output(0, out);
return Status::OK();
})
.Doc(R"doc(
Returns a diagonal tensor with a given diagonal values.
Given a `diagonal`, this operation returns a tensor with the `diagonal` and
everything else padded with zeros. The diagonal is computed as follows:
Assume `diagonal` has dimensions [D1,..., Dk], then the output is a tensor of
rank 2k with dimensions [D1,..., Dk, D1,..., Dk] where:
`output[i1,..., ik, i1,..., ik] = diagonal[i1, ..., ik]` and 0 everywhere else.
For example:
```prettyprint
# 'diagonal' is [1, 2, 3, 4]
tf.diag(diagonal) ==> [[1, 0, 0, 0]
[0, 2, 0, 0]
[0, 0, 3, 0]
[0, 0, 0, 4]]
```
diagonal: Rank k tensor where k is at most 3.
)doc");
// --------------------------------------------------------------------------
REGISTER_OP("DiagPart")
.Input("input: T")
.Output("diagonal: T")
.Attr("T: {float, double, int32, int64, complex64}")
.SetShapeFn([](InferenceContext* c) {
const Shape* in = c->input(0);
if (!c->RankKnown(in)) {
c->set_output(0, c->UnknownShape());
return Status::OK();
}
// Rank must be even, and result will have rank <rank/2>.
const int32 rank = c->Rank(in);
if ((rank % 2) != 0 || rank > 6) {
return errors::InvalidArgument(
"Input must have even rank <= 6, input rank is ", rank);
}
const int32 mid = rank / 2;
// output dim[i] is the merge of in.dim[i] and in.dim[i+mid].
std::vector<const Dimension*> dims(mid);
for (int i = 0; i < mid; ++i) {
TF_RETURN_IF_ERROR(
c->Merge(c->Dim(in, i), c->Dim(in, i + mid), &dims[i]));
}
c->set_output(0, c->MakeShape(dims));
return Status::OK();
})
.Doc(R"doc(
Returns the diagonal part of the tensor.
This operation returns a tensor with the `diagonal` part
of the `input`. The `diagonal` part is computed as follows:
Assume `input` has dimensions `[D1,..., Dk, D1,..., Dk]`, then the output is a
tensor of rank `k` with dimensions `[D1,..., Dk]` where:
`diagonal[i1,..., ik] = input[i1, ..., ik, i1,..., ik]`.
For example:
```prettyprint
# 'input' is [[1, 0, 0, 0]
[0, 2, 0, 0]
[0, 0, 3, 0]
[0, 0, 0, 4]]
tf.diag_part(input) ==> [1, 2, 3, 4]
```
input: Rank k tensor where k is 2, 4, or 6.
diagonal: The extracted diagonal.
)doc");
// --------------------------------------------------------------------------
REGISTER_OP("BatchMatrixDiag")
.Input("diagonal: T")
.Output("output: T")
.Attr("T: type")
.SetShapeFn([](InferenceContext* c) {
const Shape* in;
TF_RETURN_IF_ERROR(c->WithRankAtLeast(c->input(0), 1, &in));
if (!c->RankKnown(in)) {
c->set_output(0, c->UnknownShape());
return Status::OK();
}
const int32 rank = c->Rank(in);
const Shape* out;
TF_RETURN_IF_ERROR(
c->Concatenate(in, c->Vector(c->Dim(in, rank - 1)), &out));
c->set_output(0, out);
return Status::OK();
})
.Doc(R"doc(
Returns a batched diagonal tensor with a given batched diagonal values.
Given a `diagonal`, this operation returns a tensor with the `diagonal` and
everything else padded with zeros. The diagonal is computed as follows:
Assume `diagonal` has `k` dimensions `[I, J, K, ..., N]`, then the output is a
tensor of rank `k+1` with dimensions [I, J, K, ..., N, N]` where:
`output[i, j, k, ..., m, n] = 1{m=n} * diagonal[i, j, k, ..., n]`.
For example:
```prettyprint
# 'diagonal' is [[1, 2, 3, 4], [5, 6, 7, 8]]
and diagonal.shape = (2, 4)
tf.batch_matrix_diag(diagonal) ==> [[[1, 0, 0, 0]
[0, 2, 0, 0]
[0, 0, 3, 0]
[0, 0, 0, 4]],
[[5, 0, 0, 0]
[0, 6, 0, 0]
[0, 0, 7, 0]
[0, 0, 0, 8]]]
which has shape (2, 4, 4)
```
diagonal: Rank `k`, where `k >= 1`.
output: Rank `k+1`, with `output.shape = diagonal.shape + [diagonal.shape[-1]]`.
)doc");
// --------------------------------------------------------------------------
REGISTER_OP("BatchMatrixSetDiag")
.Input("input: T")
.Input("diagonal: T")
.Output("output: T")
.Attr("T: type")
.SetShapeFn([](InferenceContext* c) {
const Shape* input;
const Shape* diag;
TF_RETURN_IF_ERROR(c->WithRankAtLeast(c->input(0), 2, &input));
TF_RETURN_IF_ERROR(c->WithRankAtLeast(c->input(1), 1, &diag));
const Dimension* square_dim;
TF_RETURN_IF_ERROR(
c->Merge(c->Dim(input, -2), c->Dim(input, -1), &square_dim));
TF_RETURN_IF_ERROR(c->Merge(square_dim, c->Dim(diag, -1), &square_dim));
const Shape* output;
TF_RETURN_IF_ERROR(c->Concatenate(diag, c->Vector(square_dim), &output));
TF_RETURN_IF_ERROR(c->Merge(input, output, &output));
c->set_output(0, output);
return Status::OK();
})
.Doc(R"doc(
Returns a batched matrix tensor with new batched diagonal values.
Given `input` and `diagonal`, this operation returns a tensor with the
same shape and values as `input`, except for the diagonals of the innermost
matrices. These will be overwritten by the values in `diagonal`.
The batched matrices must be square.
The output is computed as follows:
Assume `input` has `k+1` dimensions `[I, J, K, ..., N, N]` and `diagonal` has
`k` dimensions `[I, J, K, ..., N]`. Then the output is a
tensor of rank `k+1` with dimensions [I, J, K, ..., N, N]` where:
* `output[i, j, k, ..., m, n] = diagonal[i, j, k, ..., n]` for `m == n`.
* `output[i, j, k, ..., m, n] = input[i, j, k, ..., m, n]` for `m != n`.
input: Rank `k+1`, where `k >= 1`.
diagonal: Rank `k`, where `k >= 1`.
output: Rank `k+1`, with `output.shape = input.shape`.
)doc");
// --------------------------------------------------------------------------
REGISTER_OP("BatchMatrixDiagPart")
.Input("input: T")
.Output("diagonal: T")
.Attr("T: type")
.SetShapeFn([](InferenceContext* c) {
const Shape* in;
TF_RETURN_IF_ERROR(c->WithRankAtLeast(c->input(0), 2, &in));
if (!c->RankKnown(in)) {
c->set_output(0, c->UnknownShape());
return Status::OK();
}
const int32 rank = c->Rank(in);
// Last two dims must match.
const Dimension* unused;
TF_RETURN_IF_ERROR(
c->Merge(c->Dim(in, rank - 1), c->Dim(in, rank - 2), &unused));
// Output shape has all dims but last of input.
std::vector<const Dimension*> dims;
for (int i = 0; i < rank - 1; ++i) dims.push_back(c->Dim(in, i));
c->set_output(0, c->MakeShape(dims));
return Status::OK();
})
.Doc(R"doc(
Returns the batched diagonal part of a batched tensor.
This operation returns a tensor with the `diagonal` part
of the batched `input`. The `diagonal` part is computed as follows:
Assume `input` has `k` dimensions `[I, J, K, ..., N, N]`, then the output is a
tensor of rank `k - 1` with dimensions `[I, J, K, ..., N]` where:
`diagonal[i, j, k, ..., n] = input[i, j, k, ..., n, n]`.
The input must be at least a matrix.
For example:
```prettyprint
# 'input' is [[[1, 0, 0, 0]
[0, 2, 0, 0]
[0, 0, 3, 0]
[0, 0, 0, 4]],
[[5, 0, 0, 0]
[0, 6, 0, 0]
[0, 0, 7, 0]
[0, 0, 0, 8]]]
and input.shape = (2, 4, 4)
tf.batch_matrix_diag_part(input) ==> [[1, 2, 3, 4], [5, 6, 7, 8]]
which has shape (2, 4)
```
input: Rank `k` tensor where `k >= 2` and the last two dimensions are equal.
diagonal: The extracted diagonal(s) having shape
`diagonal.shape = input.shape[:-1]`.
)doc");
// --------------------------------------------------------------------------
REGISTER_OP("BatchMatrixBandPart")
.Input("input: T")
.Input("num_lower: int64")
.Input("num_upper: int64")
.Output("band: T")
.Attr("T: type")
.SetShapeFn(shape_inference::UnchangedShape)
.Doc(R"doc(
Copy a tensor setting everything outside a central band in each innermost matrix
to zero.
The `band` part is computed as follows:
Assume `input` has `k` dimensions `[I, J, K, ..., M, N]`, then the output is a
tensor with the same shape where
`band[i, j, k, ..., m, n] = in_band(m, n) * input[i, j, k, ..., m, n]`.
The indicator function 'in_band(m, n)` is one if
`(num_lower < 0 || (m-n) <= num_lower)) &&
(num_upper < 0 || (n-m) <= num_upper)`, and zero otherwise.
For example:
```prettyprint
# if 'input' is [[ 0, 1, 2, 3]
[-1, 0, 1, 2]
[-2, -1, 0, 1]
[-3, -2, -1, 0]],
tf.batch_matrix_band_part(input, 1, -1) ==> [[ 0, 1, 2, 3]
[-1, 0, 1, 2]
[ 0, -1, 0, 1]
[ 0, 0, -1, 0]],
tf.batch_matrix_band_part(input, 2, 1) ==> [[ 0, 1, 0, 0]
[-1, 0, 1, 0]
[-2, -1, 0, 1]
[ 0, -2, -1, 0]]
```
Useful special cases:
```prettyprint
tf.batch_matrix_band_part(input, 0, -1) ==> Upper triangular part.
tf.batch_matrix_band_part(input, -1, 0) ==> Lower triangular part.
tf.batch_matrix_band_part(input, 0, 0) ==> Diagonal.
```
input: Rank `k` tensor.
num_lower: 0-D tensor. Number of subdiagonals to keep. If negative, keep entire
lower triangle.
num_upper: 0-D tensor. Number of superdiagonals to keep. If negative, keep
entire upper triangle.
band: Rank `k` tensor of the same shape as input. The extracted banded tensor.
)doc");
// --------------------------------------------------------------------------
REGISTER_OP("Reverse")
.Input("tensor: T")
.Input("dims: bool")
.Output("output: T")
.Attr("T: {uint8, int8, int32, bool, half, float, double, complex64, complex128}")
.SetShapeFn([](InferenceContext* c) {
const Shape* input = c->input(0);
const Shape* dims;
TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 1, &dims));
const Dimension* dims_dim = c->Dim(dims, 0);
if (c->ValueKnown(dims_dim)) {
TF_RETURN_IF_ERROR(c->WithRank(input, c->Value(dims_dim), &input));
}
if (c->Rank(input) > 8) {
return errors::InvalidArgument(
"reverse does not work on tensors with more than 8 dimensions");
}
c->set_output(0, input);
return Status::OK();
})
.Doc(R"Doc(
Reverses specific dimensions of a tensor.
Given a `tensor`, and a `bool` tensor `dims` representing the dimensions
of `tensor`, this operation reverses each dimension i of `tensor` where
`dims[i]` is `True`.
`tensor` can have up to 8 dimensions. The number of dimensions
of `tensor` must equal the number of elements in `dims`. In other words:
`rank(tensor) = size(dims)`
For example:
```prettyprint
# tensor 't' is [[[[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11]],
# [[12, 13, 14, 15],
# [16, 17, 18, 19],
# [20, 21, 22, 23]]]]
# tensor 't' shape is [1, 2, 3, 4]
# 'dims' is [False, False, False, True]
reverse(t, dims) ==> [[[[ 3, 2, 1, 0],
[ 7, 6, 5, 4],
[ 11, 10, 9, 8]],
[[15, 14, 13, 12],
[19, 18, 17, 16],
[23, 22, 21, 20]]]]
# 'dims' is [False, True, False, False]
reverse(t, dims) ==> [[[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]
[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]]]
# 'dims' is [False, False, True, False]
reverse(t, dims) ==> [[[[8, 9, 10, 11],
[4, 5, 6, 7],
[0, 1, 2, 3]]
[[20, 21, 22, 23],
[16, 17, 18, 19],
[12, 13, 14, 15]]]]
```
tensor: Up to 8-D.
dims: 1-D. The dimensions to reverse.
output: The same shape as `tensor`.
)Doc");
// --------------------------------------------------------------------------
REGISTER_OP("EditDistance")
.Input("hypothesis_indices: int64")
.Input("hypothesis_values: T")
.Input("hypothesis_shape: int64")
.Input("truth_indices: int64")
.Input("truth_values: T")
.Input("truth_shape: int64")
.Attr("normalize: bool = true")
.Attr("T: type")
.Output("output: float")
.Doc(R"doc(
Computes the (possibly normalized) Levenshtein Edit Distance.
The inputs are variable-length sequences provided by SparseTensors
(hypothesis_indices, hypothesis_values, hypothesis_shape)
and
(truth_indices, truth_values, truth_shape).
The inputs are:
hypothesis_indices: The indices of the hypothesis list SparseTensor.
This is an N x R int64 matrix.
hypothesis_values: The values of the hypothesis list SparseTensor.
This is an N-length vector.
hypothesis_shape: The shape of the hypothesis list SparseTensor.
This is an R-length vector.
truth_indices: The indices of the truth list SparseTensor.
This is an M x R int64 matrix.
truth_values: The values of the truth list SparseTensor.
This is an M-length vector.
truth_shape: The shape of the truth list SparseTensor.
This is an R-length vector.
truth_shape: truth indices, vector.
normalize: boolean (if true, edit distances are normalized by length of truth).
The output is:
output: A dense float tensor with rank R - 1.
For the example input:
// hypothesis represents a 2x1 matrix with variable-length values:
// (0,0) = ["a"]
// (1,0) = ["b"]
hypothesis_indices = [[0, 0, 0],
[1, 0, 0]]
hypothesis_values = ["a", "b"]
hypothesis_shape = [2, 1, 1]
// truth represents a 2x2 matrix with variable-length values:
// (0,0) = []
// (0,1) = ["a"]
// (1,0) = ["b", "c"]
// (1,1) = ["a"]
truth_indices = [[0, 1, 0],
[1, 0, 0],
[1, 0, 1],
[1, 1, 0]]
truth_values = ["a", "b", "c", "a"]
truth_shape = [2, 2, 2]
normalize = true
The output will be:
// output is a 2x2 matrix with edit distances normalized by truth lengths.
output = [[inf, 1.0], // (0,0): no truth, (0,1): no hypothesis
[0.5, 1.0]] // (1,0): addition, (1,1): no hypothesis
)doc");
// --------------------------------------------------------------------------
REGISTER_OP("Fill")
.Input("dims: int32")
.Input("value: T")
.Output("output: T")
.Attr("T: type")
.SetShapeFn([](InferenceContext* c) {
const Shape* out;
TF_RETURN_IF_ERROR(c->MakeShapeFromShapeTensor(0, &out));
c->set_output(0, out);
return Status::OK();
})
.Doc(R"doc(
Creates a tensor filled with a scalar value.
This operation creates a tensor of shape `dims` and fills it with `value`.
For example:
```prettyprint
# Output tensor has shape [2, 3].
fill([2, 3], 9) ==> [[9, 9, 9]
[9, 9, 9]]
```
dims: 1-D. Represents the shape of the output tensor.
value: 0-D (scalar). Value to fill the returned tensor.
)doc");
// --------------------------------------------------------------------------
REGISTER_OP("Gather")
.Input("params: Tparams")
.Input("indices: Tindices")
.Attr("validate_indices: bool = true")
.Output("output: Tparams")
.Attr("Tparams: type")
.Attr("Tindices: {int32,int64}")
.SetShapeFn([](InferenceContext* c) {
const Shape* unused;
TF_RETURN_IF_ERROR(c->WithRankAtLeast(c->input(0), 1, &unused));
const Shape* params_subshape;
TF_RETURN_IF_ERROR(c->Subshape(c->input(0), 1, ¶ms_subshape));
const Shape* indices_shape = c->input(1);
const Shape* out;
TF_RETURN_IF_ERROR(c->Concatenate(indices_shape, params_subshape, &out));
c->set_output(0, out);
return Status::OK();
})
.Doc(R"doc(
Gather slices from `params` according to `indices`.
`indices` must be an integer tensor of any dimension (usually 0-D or 1-D).
Produces an output tensor with shape `indices.shape + params.shape[1:]` where:
# Scalar indices
output[:, ..., :] = params[indices, :, ... :]
# Vector indices
output[i, :, ..., :] = params[indices[i], :, ... :]
# Higher rank indices
output[i, ..., j, :, ... :] = params[indices[i, ..., j], :, ..., :]
If `indices` is a permutation and `len(indices) == params.shape[0]` then
this operation will permute `params` accordingly.
<div style="width:70%; margin:auto; margin-bottom:10px; margin-top:20px;">
<img style="width:100%" src="../../images/Gather.png" alt>
</div>
)doc");
// --------------------------------------------------------------------------
REGISTER_OP("GatherNd")
.Input("params: Tparams")
.Input("indices: Tindices")
.Output("output: Tparams")
.Attr("Tparams: type")
.Attr("Tindices: {int32,int64}")
.SetShapeFn([](InferenceContext* c) {
const Shape* params = c->input(0);
const Shape* indices;
TF_RETURN_IF_ERROR(c->WithRankAtLeast(c->input(1), 1, &indices));
const Dimension* r_dim = c->Dim(indices, -1);
if (!c->RankKnown(params) || !c->ValueKnown(r_dim)) {
c->set_output(0, c->UnknownShape());
return Status::OK();
}
if (c->Value(r_dim) > c->Rank(params)) {
return errors::InvalidArgument(
"indices.shape[-1] must be <= params.rank, but saw indices shape: ",
c->DebugString(indices), " and params shape: ",
c->DebugString(params));
}
// Remove r_dim from indices to get output.
const Shape* indices_slice;
const Shape* params_slice;
TF_RETURN_IF_ERROR(c->Subshape(indices, 0, -1, &indices_slice));
TF_RETURN_IF_ERROR(c->Subshape(params, c->Value(r_dim), ¶ms_slice));
const Shape* out;
TF_RETURN_IF_ERROR(c->Concatenate(indices_slice, params_slice, &out));
c->set_output(0, out);
return Status::OK();
})
.Doc(R"doc(
Gather values or slices from `params` according to `indices`.
`params` is a Tensor of rank `R` and `indices` is a Tensor of rank `M`.
`indices` must be integer tensor, containing indices into `params`.
It must be shape `[d_0, ..., d_N, R]` where `0 < R <= M`.
The innermost dimension of `indices` (with length `R`) corresponds to
indices into elements (if `R = M`) or slices (if `R < M`) along the `N`th
dimension of `params`.
Produces an output tensor with shape