Skip to content

Add rewrite for softplus(log(x)) -> log1p(x) #1452

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pytensor/tensor/rewriting/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def local_exp_log(fgraph, node):


@register_specialize
@node_rewriter([exp, expm1])
@node_rewriter([exp, expm1, softplus])
def local_exp_log_nan_switch(fgraph, node):
# Rewrites of the kind exp(log...(x)) that require a `nan` switch
x = node.inputs[0]
Expand Down Expand Up @@ -453,6 +453,13 @@ def local_exp_log_nan_switch(fgraph, node):
new_out = switch(le(x, 0), neg(exp(x)), np.asarray(np.nan, old_out.dtype))
return [new_out]

# Case for softplus(log(x)) -> log1p(x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick I prefer to refer to it by log1pexp, which we have as an alias to softplus:

log1pexp = softplus

Also we can add a similar case for log1mexp?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this (with the code below) and it works fine in its domain [0, 1]. I will add it too.

if isinstance(prev_op, ps.Log) and isinstance(node_op, ps_math.Softplus):
x = x.owner.inputs[0]
old_out = node.outputs[0]
new_out = switch(ge(x, 0), log1p(x), np.asarray(np.nan, old_out.dtype))
return [new_out]


@register_canonicalize
@register_specialize
Expand Down
21 changes: 21 additions & 0 deletions tests/tensor/rewriting/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,27 @@ def test_exp_softplus(self, exp_op):
decimal=6,
)

def test_softplus_log(self):
# softplus(log(x)) -> log1p(x)
data_valid = np.random.random((4, 3)).astype("float32") * 2
data_valid[0, 0] = 0 # edge case
data_invalid = data_valid - 2

x = fmatrix()
f = function([x], softplus(log(x)), mode=self.mode)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you want you can check against the expected graph directly, something like

assert equal_computations(f.maker.fgraph.outputs, [pt.switch(x > 0, pt.log1p(x), np.asarray([[np.nan]], dtype="float32")])

Or something like that. This is not a request!

graph = f.maker.fgraph.toposort()
ops_graph = [
node
for node in graph
if isinstance(node.op, Elemwise)
and isinstance(node.op.scalar_op, ps.Log | ps.Exp | ps.Softplus)
]
assert len(ops_graph) == 0

expected = np.log1p(data_valid)
np.testing.assert_almost_equal(f(data_valid), expected)
assert np.all(np.isnan(f(data_invalid)))

@pytest.mark.parametrize(
["nested_expression", "expected_switches"],
[
Expand Down