-
Notifications
You must be signed in to change notification settings - Fork 137
Fix bug in gradient of Blockwise'd Scan #1482
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
Merged
AlexAndorra
merged 3 commits into
pymc-devs:main
from
ricardoV94:fix_blockwise_scan_bug
Jun 16, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -344,81 +344,66 @@ def connection_pattern(self, node): | |
|
||
return [[True for _ in node.outputs] for _ in node.inputs] | ||
|
||
def _bgrad(self, inputs, outputs, ograds): | ||
# Grad, with respect to broadcasted versions of inputs | ||
|
||
def as_core(t, core_t): | ||
# Inputs could be NullType or DisconnectedType | ||
if isinstance(t.type, NullType | DisconnectedType): | ||
return t | ||
return core_t.type() | ||
def L_op(self, inputs, outputs, output_gradients): | ||
batch_ndim = self.batch_ndim(outputs[0].owner) | ||
|
||
# Obtain core_op gradients | ||
with config.change_flags(compute_test_value="off"): | ||
safe_inputs = [ | ||
tensor(dtype=inp.type.dtype, shape=(None,) * len(sig)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line was the problematic one: |
||
for inp, sig in zip(inputs, self.inputs_sig, strict=True) | ||
] | ||
core_node = self._create_dummy_core_node(safe_inputs) | ||
|
||
core_inputs = [ | ||
as_core(inp, core_inp) | ||
for inp, core_inp in zip(inputs, core_node.inputs, strict=True) | ||
] | ||
core_ograds = [ | ||
as_core(ograd, core_ograd) | ||
for ograd, core_ograd in zip(ograds, core_node.outputs, strict=True) | ||
tensor( | ||
dtype=inp.type.dtype, | ||
shape=inp.type.shape[batch_ndim:], | ||
) | ||
for inp in inputs | ||
] | ||
# FIXME: These core_outputs do not depend on core_inputs, not pretty | ||
# It's not neccessarily a problem because if they are referenced by the gradient, | ||
# they get replaced later in vectorize. But if the Op was to make any decision | ||
# by introspecting the dependencies of output on inputs it would fail badly! | ||
Comment on lines
-371
to
-374
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also fixed |
||
core_outputs = core_node.outputs | ||
|
||
core_igrads = self.core_op.L_op(core_inputs, core_outputs, core_ograds) | ||
|
||
igrads = vectorize_graph( | ||
[core_igrad for core_igrad in core_igrads if core_igrad is not None], | ||
replace=dict( | ||
zip( | ||
core_inputs + core_outputs + core_ograds, | ||
inputs + outputs + ograds, | ||
strict=True, | ||
core_outputs = self._create_dummy_core_node(core_inputs).outputs | ||
|
||
# Define core output_gradients, but keep original disconnected/null output_gradients (if any) | ||
core_output_gradients = [ | ||
output_grad | ||
if isinstance(output_grad.type, NullType | DisconnectedType) | ||
else core_output.type() | ||
for output_grad, core_output in zip( | ||
output_gradients, core_outputs, strict=True | ||
) | ||
), | ||
) | ||
|
||
igrads_iter = iter(igrads) | ||
return [ | ||
None if core_igrad is None else next(igrads_iter) | ||
for core_igrad in core_igrads | ||
] | ||
] | ||
|
||
def L_op(self, inputs, outs, ograds): | ||
from pytensor.tensor.math import sum as pt_sum | ||
core_input_gradients = self.core_op.L_op( | ||
core_inputs, core_outputs, core_output_gradients | ||
) | ||
|
||
# Compute grad with respect to broadcasted input | ||
rval = self._bgrad(inputs, outs, ograds) | ||
# Vectorize core gradients to original inputs | ||
input_gradients = list( | ||
vectorize_graph( | ||
core_input_gradients, | ||
replace=dict( | ||
zip( | ||
core_inputs + core_outputs + core_output_gradients, | ||
inputs + outputs + output_gradients, | ||
strict=True, | ||
) | ||
), | ||
) | ||
) | ||
|
||
# Sum out the broadcasted dimensions | ||
batch_ndims = self.batch_ndim(outs[0].owner) | ||
batch_shape = outs[0].type.shape[:batch_ndims] | ||
# Sum out the broadcasted batch dimensions | ||
batch_shape = outputs[0].type.shape[:batch_ndim] | ||
for i, (inp, sig) in enumerate(zip(inputs, self.inputs_sig, strict=True)): | ||
if isinstance(rval[i].type, NullType | DisconnectedType): | ||
if isinstance(input_gradients[i].type, NullType | DisconnectedType): | ||
continue | ||
|
||
assert inp.type.ndim == batch_ndims + len(sig) | ||
assert inp.type.ndim == batch_ndim + len(sig) | ||
|
||
to_sum = [ | ||
if to_sum := [ | ||
j | ||
for j, (inp_s, out_s) in enumerate( | ||
zip(inp.type.shape, batch_shape, strict=False) | ||
) | ||
if inp_s == 1 and out_s != 1 | ||
] | ||
if to_sum: | ||
rval[i] = pt_sum(rval[i], axis=to_sum, keepdims=True) | ||
]: | ||
input_gradients[i] = input_gradients[i].sum(axis=to_sum, keepdims=True) | ||
|
||
return rval | ||
return input_gradients | ||
|
||
def _create_node_gufunc(self, node: Apply, impl) -> Callable: | ||
"""Define (or retrieve) the node gufunc used in `perform`. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.