| 1208 | REGISTER_GRADIENT_OP("Select", SelectGrad); |
| 1209 | |
| 1210 | Status SelectV2Grad(const Scope& scope, const Operation& op, |
| 1211 | const std::vector<Output>& grad_inputs, |
| 1212 | std::vector<Output>* grad_outputs) { |
| 1213 | if (op.num_inputs() != 3) { |
| 1214 | return errors::InvalidArgument("Select requires 3 arguments"); |
| 1215 | } |
| 1216 | |
| 1217 | if (grad_inputs.size() != 1) { |
| 1218 | return errors::InvalidArgument("Select grad requires 1 grad input"); |
| 1219 | } |
| 1220 | |
| 1221 | auto c = op.input(0); |
| 1222 | auto x = op.input(1); |
| 1223 | auto y = op.input(2); |
| 1224 | |
| 1225 | auto zeros = ZerosLike(scope, grad_inputs[0]); |
| 1226 | auto gx = SelectV2(scope, c, grad_inputs[0], zeros); |
| 1227 | auto x_shape = Shape(scope, x); |
| 1228 | auto output_shape = Shape(scope, op.output(0)); |
| 1229 | |
| 1230 | // Reduce away broadcasted leading dims. |
| 1231 | auto reduce_x = internal::BroadcastGradientArgs(scope, x_shape, output_shape); |
| 1232 | auto gx_sum = |
| 1233 | ReduceSum(scope, gx, /*axis=*/reduce_x.r0, ReduceSum::KeepDims(true)); |
| 1234 | auto gx_sum_reshape = Reshape(scope, gx_sum, x_shape); |
| 1235 | |
| 1236 | auto gy = SelectV2(scope, c, zeros, grad_inputs[0]); |
| 1237 | auto y_shape = Shape(scope, y); |
| 1238 | |
| 1239 | // Reduce away broadcasted leading dims. |
| 1240 | auto reduce_y = internal::BroadcastGradientArgs(scope, y_shape, output_shape); |
| 1241 | auto gy_sum = |
| 1242 | ReduceSum(scope, gy, /*axis=*/reduce_y.r0, ReduceSum::KeepDims(true)); |
| 1243 | auto gy_sum_reshape = Reshape(scope, gy_sum, y_shape); |
| 1244 | |
| 1245 | grad_outputs->push_back(NoGradient()); // Condition |
| 1246 | grad_outputs->push_back(gx_sum_reshape); |
| 1247 | grad_outputs->push_back(gy_sum_reshape); |
| 1248 | return scope.status(); |
| 1249 | } |
| 1250 | |
| 1251 | REGISTER_GRADIENT_OP("SelectV2", SelectV2Grad); |
| 1252 |
nothing calls this directly
no test coverage detected