-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial version of cumprod #6211
Conversation
5f1e541
to
be9b2cd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for contributing the cumprod op, the gradient function can come later, for reference the similar grad def for cumsum is in src/gradients/Cumsum_grad.ts
I agree your suggestion on refactoring the scan ops later, that the struct of scan ops are very similar, the similar approach we had is unary op.
The PR looks good overall, just couple minor nitpick. Thanks!
Reviewable status: 0 of 1 approvals obtained (waiting on @iislucas)
tfjs-backend-cpu/src/kernels/Cumprod.ts, line 3 at r1 (raw file):
/** * @license * Copyright 2020 Google LLC. All Rights Reserved.
2022
tfjs-backend-webgl/src/cumprod_gpu.ts, line 33 at r1 (raw file):
let condition = ''; let idxString = ''; // When exclusive is set, the cumsum op becomes roll op that copies the
cumprod
tfjs-backend-webgl/src/cumprod_gpu.ts, line 71 at r1 (raw file):
return `${name}.x, ${name}.y, ${name}.z, ${name}.w`; } else { throw Error(`Cumulative sum for rank ${rank} is not yet supported`);
prod
tfjs-backend-webgl/src/cumprod_gpu.ts, line 85 at r1 (raw file):
return `${name}.w`; } else { throw Error(`Cumulative sum for rank ${rank} is not yet supported`);
prod
tfjs-backend-webgl/src/kernels/Cumprod.ts, line 63 at r1 (raw file):
backend.disposeIntermediateTensorInfo(prevResult); } // For exclusive cumsum, shift the end result in the direction of sum
update this comment according to the cumprod definition.
tfjs-core/src/ops/cumprod.ts, line 40 at r1 (raw file):
* ``` * * @param x The input tensor to be summed.
cumulative multiplied.
tfjs-core/src/ops/cumprod_test.ts, line 41 at r1 (raw file):
const res = tf.tensor1d([1, 2, 3, 4]).cumprod(0, exclusive); expect(res.shape).toEqual([4]); expectArraysClose(await res.data(), [0, 1, 2, 6]);
1,1,2,6?
tfjs-core/src/ops/cumprod_test.ts, line 49 at r1 (raw file):
const res = tf.tensor1d([1, 2, 3, 4]).cumprod(0, exclusive, reverse); expect(res.shape).toEqual([4]); expectArraysClose(await res.data(), [6, 2, 1, 0]);
6,2,1,1?
tfjs-core/src/ops/cumprod_test.ts, line 52 at r1 (raw file):
}); it('gradient: 1D', async () => {
you might want to register the gradient function similar to cumsumGradConfig in src/gradients/Cumsum_grad.ts
tfjs-core/src/ops/cumprod_test.ts, line 78 at r1 (raw file):
.cumprod(1); expect(res.shape).toEqual([2, 2]); expectArraysClose(await res.data(), [1, 3, 3, 7]);
I think all of these test results need to be updated for cumprod
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking over, tests fixed and did a careful search to catch all missing "sum" -> "product" language changes.
Reviewable status: 0 of 1 approvals obtained (waiting on @pyu10055)
tfjs-backend-cpu/src/kernels/Cumprod.ts, line 3 at r1 (raw file):
Previously, pyu10055 (Ping Yu) wrote…
2022
Done
tfjs-backend-webgl/src/cumprod_gpu.ts, line 33 at r1 (raw file):
Previously, pyu10055 (Ping Yu) wrote…
cumprod
done
tfjs-backend-webgl/src/cumprod_gpu.ts, line 71 at r1 (raw file):
Previously, pyu10055 (Ping Yu) wrote…
prod
done
tfjs-backend-webgl/src/cumprod_gpu.ts, line 85 at r1 (raw file):
Previously, pyu10055 (Ping Yu) wrote…
prod
done
tfjs-backend-webgl/src/kernels/Cumprod.ts, line 63 at r1 (raw file):
Previously, pyu10055 (Ping Yu) wrote…
update this comment according to the cumprod definition.
done
tfjs-core/src/ops/cumprod.ts, line 40 at r1 (raw file):
Previously, pyu10055 (Ping Yu) wrote…
cumulative multiplied.
done
tfjs-core/src/ops/cumprod_test.ts, line 41 at r1 (raw file):
Previously, pyu10055 (Ping Yu) wrote…
1,1,2,6?
done
tfjs-core/src/ops/cumprod_test.ts, line 49 at r1 (raw file):
Previously, pyu10055 (Ping Yu) wrote…
6,2,1,1?
Done.
tfjs-core/src/ops/cumprod_test.ts, line 52 at r1 (raw file):
Previously, pyu10055 (Ping Yu) wrote…
you might want to register the gradient function similar to cumsumGradConfig in src/gradients/Cumsum_grad.ts
I'll do gradients in a followup.
tfjs-core/src/ops/cumprod_test.ts, line 78 at r1 (raw file):
Previously, pyu10055 (Ping Yu) wrote…
I think all of these test results need to be updated for cumprod
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status:
complete! 1 of 1 approvals obtained (waiting on @pyu10055)
Initial implementation of
cumprod
:Addresses: #6210
Includes implementation for GPU/WebGL, WASM, and CPU.
Does not (yet) include gradient computation (that's quite a bit harder).
I did this based on replicating files for
cumsum
. If there are more implementations that use the same scanning structure, it might make sense to abstract away some of the common code. But currently there's only two functions, so probably best to wait for a third to avoid premature generalisation.Tested locally with:
This change is