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

Method Compile

tensorflow/compiler/tf2xla/kernels/reduction_ops_common.cc:47–123  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45}
46
47void XlaReductionOp::Compile(XlaOpKernelContext* ctx) {
48 const TensorShape data_shape = ctx->InputShape(0);
49 const TensorShape axes_tensor_shape = ctx->InputShape(1);
50 VLOG(1) << "ReductionOp: " << ctx->op_kernel().name();
51
52 if (axes_tensor_shape.num_elements() == 0) {
53 // The reduction axes is an empty vector, which means there are no
54 // axes to reduce so just pass the input directly through to the
55 // output.
56 ctx->SetOutput(0, ctx->Input(0));
57 return;
58 }
59
60 OP_REQUIRES(ctx, axes_tensor_shape.dims() <= 1,
61 errors::InvalidArgument(
62 "Expected scalar or vector as index argument, got ",
63 axes_tensor_shape.DebugString()));
64
65 // Evaluate the constant, reshaping to a 1-vector if it is a scalar.
66 std::vector<int64> axes;
67 xla::Literal axes_literal;
68 OP_REQUIRES_OK(ctx, ctx->ConstantInputReshapedToIntVector(1, &axes));
69
70 VLOG(1) << "data shape: " << data_shape.DebugString();
71 VLOG(1) << "axes : " << absl::StrJoin(axes, ",");
72
73 absl::InlinedVector<bool, 4> bitmap(data_shape.dims(), false);
74 std::vector<int64> xla_axes;
75 for (int64 i = 0; i < axes_tensor_shape.num_elements(); ++i) {
76 int64 index = axes[i];
77 OP_REQUIRES(ctx,
78 !(index < -data_shape.dims() || index >= data_shape.dims()),
79 errors::InvalidArgument("Invalid reduction dimension (", index,
80 " for input with ", data_shape.dims(),
81 " dimension(s)"));
82 index = (index + data_shape.dims()) % data_shape.dims();
83 bitmap[index] = true;
84 xla_axes.push_back(index);
85 }
86
87 std::vector<int64> final_shape;
88 for (int i = 0; i < data_shape.dims(); ++i) {
89 if (!bitmap[i]) {
90 // If we are not reducing along dimension i.
91 int64 dim = data_shape.dim_size(i);
92 final_shape.push_back(dim);
93 } else if (keep_dims_) {
94 // We are reducing along dimension i, but we want to keep the
95 // same number of dimensions, so we set the dimension of i to
96 // '1'.
97 final_shape.push_back(1);
98 }
99 }
100
101 string desc = ctx->op_kernel().name();
102
103 xla::XlaBuilder* const b = ctx->builder();
104 // Construct the builder for the reduction lambda.

Callers

nothing calls this directly

Calls 15

InvalidArgumentFunction · 0.85
DataTypeToPrimitiveTypeFunction · 0.85
MakeShapeFunction · 0.85
InputShapeMethod · 0.80
ConsumeValueOrDieMethod · 0.80
nameMethod · 0.65
StrCatFunction · 0.50
ConvertElementTypeFunction · 0.50
ParameterFunction · 0.50
ReduceFunction · 0.50
ReshapeFunction · 0.50

Tested by

no test coverage detected