Skip to content
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
11 changes: 11 additions & 0 deletions cirkit/backend/torch/parameters/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,17 @@ def forward(self, x: Tensor) -> Tensor:
return torch.clamp(x, min=self.vmin, max=self.vmax)


class TorchSoftplusParameter(TorchEntrywiseParameterOp):
"""Softmax reparameterization.

Range: (0, + inf), 0 available if input is masked.
Constraints: Positive.
"""

def forward(self, x: Tensor) -> Tensor:
return torch.nn.functional.softplus(x)


class TorchConjugateParameter(TorchEntrywiseParameterOp):
"""Conjugate parameterization."""

Expand Down
10 changes: 10 additions & 0 deletions cirkit/backend/torch/rules/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
TorchScaledSigmoidParameter,
TorchSigmoidParameter,
TorchSoftmaxParameter,
TorchSoftplusParameter,
TorchSquareParameter,
TorchSumParameter,
TorchTensorParameter,
Expand Down Expand Up @@ -61,6 +62,7 @@
ScaledSigmoidParameter,
SigmoidParameter,
SoftmaxParameter,
SoftplusParameter,
SquareParameter,
SumParameter,
TensorParameter,
Expand Down Expand Up @@ -188,6 +190,13 @@ def compile_clamp_parameter(compiler: "TorchCompiler", p: ClampParameter) -> Tor
return TorchClampParameter(in_shape, vmin=p.vmin, vmax=p.vmax)


def compile_softplus_parameter(
compiler: "TorchCompiler", p: SoftplusParameter
) -> TorchSoftplusParameter:
(in_shape,) = p.in_shapes
return TorchSoftplusParameter(in_shape)


def compile_conjugate_parameter(
compiler: "TorchCompiler", p: ClampParameter
) -> TorchConjugateParameter:
Expand Down Expand Up @@ -285,6 +294,7 @@ def compile_polynomial_differential(
SigmoidParameter: compile_sigmoid_parameter,
ScaledSigmoidParameter: compile_scaled_sigmoid_parameter,
ClampParameter: compile_clamp_parameter,
SoftplusParameter: compile_softplus_parameter,
ConjugateParameter: compile_conjugate_parameter,
ReduceSumParameter: compile_reduce_sum_parameter,
ReduceProductParameter: compile_reduce_product_parameter,
Expand Down
3 changes: 3 additions & 0 deletions cirkit/templates/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
ParameterFactory,
SigmoidParameter,
SoftmaxParameter,
SoftplusParameter,
TensorParameter,
UnaryParameterOp,
)
Expand Down Expand Up @@ -189,6 +190,8 @@ def name_to_parameter_activation(
if "vmin" not in kwargs:
kwargs["vmin"] = 1e-18
return functools.partial(ClampParameter, **kwargs)
case "softplus":
return functools.partial(SoftplusParameter, **kwargs)
case _:
raise ValueError

Expand Down
Loading