* Logs an attempt to extract the average gradient, and tries to flush all * TakeGrad attempts. * A TakeGrad attempt is blocked until num_required > counter_, i.e., * sufficient gradients have been accumulated. * * num_required: Number of gradients that needs to be accumulated before the * attempt is unblocked. * ctx: Context in which the op is executed. * callback:
| 62 | * callback: A callback to be executed after the attempt has been completed. |
| 63 | */ |
| 64 | void ConditionalAccumulatorBase::TryTakeGrad(int num_required, |
| 65 | OpKernelContext* ctx, |
| 66 | DoneCallback callback) { |
| 67 | if (num_required <= 0) { |
| 68 | ctx->CtxFailureWithWarning(errors::InvalidArgument( |
| 69 | "Argument num_required must be positive, but was ", num_required)); |
| 70 | callback(); |
| 71 | } else { |
| 72 | CancellationManager* cm = ctx->cancellation_manager(); |
| 73 | CancellationToken token = cm->get_cancellation_token(); |
| 74 | bool already_cancelled; |
| 75 | { |
| 76 | mutex_lock l(mu_); |
| 77 | already_cancelled = !cm->RegisterCallback( |
| 78 | token, [this, cm, token]() { Cancel(cm, token); }); |
| 79 | if (!already_cancelled) { |
| 80 | takegrad_attempts_.emplace_back( |
| 81 | num_required, callback, ctx, cm, token, |
| 82 | [this](Attempt* attempt) EXCLUSIVE_LOCKS_REQUIRED(mu_) { |
| 83 | if (counter_ >= attempt->elements_requested) { |
| 84 | bool successful_take_grad = TakeGradLockedHelper( |
| 85 | attempt->context, attempt->done_callback); |
| 86 | if (successful_take_grad) { |
| 87 | return kComplete; |
| 88 | } else { |
| 89 | // Try again |
| 90 | return kNoProgress; |
| 91 | } |
| 92 | } else { |
| 93 | return kNoProgress; |
| 94 | } |
| 95 | }); |
| 96 | } |
| 97 | } |
| 98 | if (!already_cancelled) { |
| 99 | FlushUnlocked(); |
| 100 | } else { |
| 101 | ctx->SetStatus(errors::Cancelled("TakeGrad operation was cancelled")); |
| 102 | callback(); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Cancellation callback. |
no test coverage detected