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

Method Compile

tensorflow/compiler/tf2xla/kernels/select_op.cc:35–74  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

33 explicit SelectOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
34
35 void Compile(XlaOpKernelContext* ctx) override {
36 const TensorShape cond_shape = ctx->InputShape(0);
37 const TensorShape then_shape = ctx->InputShape(1);
38 const TensorShape else_shape = ctx->InputShape(2);
39
40 OP_REQUIRES(
41 ctx, then_shape.IsSameSize(else_shape),
42 errors::InvalidArgument(
43 "'then' and 'else' must have the same size. but received: ",
44 then_shape.DebugString(), " vs. ", else_shape.DebugString()));
45
46 auto cond_handle = ctx->Input(0);
47 auto then_handle = ctx->Input(1);
48 auto else_handle = ctx->Input(2);
49
50 bool broadcasting = !cond_shape.IsSameSize(then_shape);
51 bool cond_is_scalar = TensorShapeUtils::IsScalar(cond_shape);
52 if (broadcasting && !cond_is_scalar) {
53 OP_REQUIRES(ctx, TensorShapeUtils::IsVector(cond_shape),
54 errors::InvalidArgument(
55 "'cond' must be a scalar or a vector, but saw shape: ",
56 cond_shape.DebugString()));
57 OP_REQUIRES(ctx, TensorShapeUtils::IsVectorOrHigher(then_shape),
58 errors::InvalidArgument(
59 "'then' must be at least a vector, but saw shape: ",
60 then_shape.DebugString()));
61 OP_REQUIRES(ctx, then_shape.dim_size(0) == cond_shape.num_elements(),
62 errors::InvalidArgument("Number of batches of 'then' must "
63 "match size of 'cond', but saw: ",
64 then_shape.dim_size(0), " vs. ",
65 cond_shape.num_elements()));
66
67 // Broadcast into the dimensions on the right.
68 std::vector<int64> broadcast_dimensions(cond_shape.dims());
69 absl::c_iota(broadcast_dimensions, 0);
70 cond_handle = xla::BroadcastInDim(cond_handle, then_shape.dim_sizes(),
71 broadcast_dimensions);
72 }
73 ctx->SetOutput(0, xla::Select(cond_handle, then_handle, else_handle));
74 }
75
76 private:
77 TF_DISALLOW_COPY_AND_ASSIGN(SelectOp);

Callers

nothing calls this directly

Calls 13

InvalidArgumentFunction · 0.85
BroadcastInDimFunction · 0.85
InputShapeMethod · 0.80
IsScalarFunction · 0.50
SelectFunction · 0.50
IsSameSizeMethod · 0.45
DebugStringMethod · 0.45
InputMethod · 0.45
dim_sizeMethod · 0.45
num_elementsMethod · 0.45
dimsMethod · 0.45
dim_sizesMethod · 0.45

Tested by

no test coverage detected