MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / Compile

Method Compile

tensorflow/compiler/tf2xla/kernels/reshape_op.cc:36–137  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34 explicit ReshapeOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
35
36 void Compile(XlaOpKernelContext* ctx) override {
37 const TensorShape input_shape = ctx->InputShape(0);
38 const TensorShape sizes_shape = ctx->InputShape(1);
39 // Preliminary validation of sizes.
40 OP_REQUIRES(ctx, TensorShapeUtils::IsVector(sizes_shape),
41 errors::InvalidArgument("sizes input must be 1-D, not shape ",
42 sizes_shape.DebugString()));
43 const int64 num_dims = sizes_shape.num_elements();
44
45 std::vector<int64> shape_input;
46 OP_REQUIRES_OK(ctx, ctx->ConstantInputAsIntVector(1, &shape_input));
47
48 // Compute the output shape. Determine product of specified
49 // dimensions, and find the index of the unspecified one if there
50 // is one.
51 TensorShape shape;
52 int64 product = 1;
53 int unknown_index = -1;
54 bool shape_has_zero_dim = false;
55 for (int d = 0; d < num_dims; ++d) {
56 const int32 size = shape_input[d];
57 if (size == -1) {
58 OP_REQUIRES(
59 ctx, unknown_index == -1,
60 errors::InvalidArgument("only one input size may be -1, not both ",
61 unknown_index, " and ", d));
62 unknown_index = d;
63 shape.AddDim(1);
64 } else if (size == 0) {
65 // We don't include zero-sized dimension in product, so that we can
66 // still calculate number of elements for non-zero-sized dimensions and
67 // therefore infer their shapes.
68 shape.AddDim(size);
69 shape_has_zero_dim = true;
70 } else {
71 OP_REQUIRES(ctx, size >= 0,
72 errors::InvalidArgument(
73 "size ", d, " must be non-negative, not ", size));
74 shape.AddDim(size);
75 product *= size;
76 }
77 }
78 if (unknown_index != -1) {
79 int64 input_num_elements = 1;
80 bool input_has_zero_dim = false;
81 for (int dim = 0; dim < input_shape.dims(); dim++) {
82 // For zero dimension, we don't count it into `input_num_elements`
83 // unless `sizes` has no zero dimension, so we are still able to
84 // infer shapes for other dimensions.
85 if (input_shape.dim_size(dim) > 0 || !shape_has_zero_dim) {
86 input_num_elements *= input_shape.dim_size(dim);
87 } else {
88 input_has_zero_dim = true;
89 }
90 }
91
92 const int64 missing = input_num_elements / product;
93 if (!input_has_zero_dim) {

Callers

nothing calls this directly

Calls 15

InvalidArgumentFunction · 0.85
InputShapeMethod · 0.80
DebugStringMethod · 0.45
num_elementsMethod · 0.45
AddDimMethod · 0.45
dimsMethod · 0.45
dim_sizeMethod · 0.45
set_dimMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected