MCPcopy Create free account
hub / github.com/apple/ml-sharp / backward

Method backward

src/sharp/utils/math.py:128–143  ·  view source on GitHub ↗

Compute gradient of clamp with pushback.

(  # type: ignore[override] # Deal with buggy torch annotations.
        ctx: Any, grad_in: torch.Tensor
    )

Source from the content-addressed store, hash-verified

126
127 @staticmethod
128 def backward( # type: ignore[override] # Deal with buggy torch annotations.
129 ctx: Any, grad_in: torch.Tensor
130 ) -> tuple[torch.Tensor, None, None, None]:
131 """Compute gradient of clamp with pushback."""
132 grad_out = grad_in.clone()
133 (tensor,) = ctx.saved_tensors
134
135 if ctx.min is not None:
136 mask_min = tensor < ctx.min
137 grad_out[mask_min] = -ctx.pushback
138
139 if ctx.max is not None:
140 mask_max = tensor > ctx.max
141 grad_out[mask_max] = ctx.pushback
142
143 return grad_out, None, None, None
144
145
146def clamp_with_pushback(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected