You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Should clamp be only defined for low <= high? #2557
Currently all of the versions of clamp ( for u32, i32, f32) are defined with language along these lines:
clamp(e: T , low: T, high: T) -> T
Returns min(max(e, low), high). Component-wise when T is a vector.
with no restrictions on low and high.
All of the definitions of clamp I have looked at (C++, MSL, SPIR-V, GLSL, HLSL) either explicitly state that it only applies to low <= high, and high > low is undefined behaviour, or have language talking about the parameters being the lower and upper bounds, thus implicitly implying those restrictions.
This means that any implementation of WebGPU will have to do a polyfill when translating down to the native backend shader language, to flip low and high if needed, to avoid causing undefined behaviour, instead of just directly calling the native implementation of clamp.
Additionally the current WGSL definition of clamp has unexpected/unusual behaviour when high < low. For the common case, low <= high the result is as expected:
If we are viewing clamp as a general function that ensures a value is on the range defined by arbitrary a and b I wouldn't expect it to clamp in this case, since 1 falls between 2 and 0.
It is actually going to return whatever the third param is, since first max operation is going to guarantee that larger of the two values is passed to the min operation, and that will always be greater that the third param when high < low.
I am proposing adding language to the spec to restrict clamp so that it is only defined if low <= high, otherwise the behaviour is undefined.
I don't think we'd want undefined behaviour, but instead an undefined value. I know gpus have hardware instructions for clamps and it is a very common operation so I'm inclined to not polyfill it.
The same rule should apply to smoothStep as well.
I remember from experience that having code not respecting this property is a recipe for portability disaster. I don't think we really want that for WGSL to happen. The prior decisions on handling x/-1 and friends would also be more consistent with handling "low > high" case here.
We prefer to default to well-defined (even if maybe unintuitive) unless we have a decently strong reason to be undefined.
How much worse would it be to keep it defined-as min(max(a,v),b), versus the native clamp ops?
We prefer to default to well-defined (even if maybe unintuitive) unless we have a decently strong reason to be undefined.
How much worse would it be to keep it defined-as min(max(a,v),b), versus the native clamp ops?
I think that can't be cost-free:
AMD GCN implements clamp as median-of-3. So in the clamp(1,2,0) case it gives 1, which is contrary to min(max(2,1),0).
If we must have a polyfill, we can either define it as median-of-3 (which may look weird) or as
min(max(v, min(low,high)), max(low,high))
which I believe is the same thing. We can massage it editorially:
clamp(v, bound0, bound1) = min(max(v, low), high)
where low = min(bound0, bound1)
and high = max(bound0, bound1)
I kind of feel we should just add both behaviors to the standard library, as different functions. clamp() would return min(max(e, low), high) and median() would return sorted(args)[1].
I think having both functions wouldn't be a great outcome. Clamping is too common. If anything, I'd suggest allowing both versions of the implementation to be correct (i.e. describe both in clamp). It is non-portable, but this seems like a place to favour performance.
If anything, I'd suggest allowing both versions of the implementation to be correct (i.e. describe both in clamp). It is non-portable, but this seems like a place to favour performance.
In the 2022-03-01 meeting@litherum suggested data showing a significant slowdown from clamp or min/max would be convincing.
Issue gfx-rs/naga#1202 is "Out-of-bounds clamping brings big perf regression on TFJS ML demos. " Intel reported that Chrome's index clamping significantly affects performance. This is still open, and Google expects to ship WebGPU in production without making a significant fix in this space. See @ben-clayton's comment.
I don't think the bounds checking discussion is very relevant here. The way we are injecting clamping is very different from the way users use clamp() in their code, and our injection is not constrained by whatever choice is made in this issue.
DN: Our position is that we don’t want to be force to polyfill, and as spec’d we would need to.
MM: Agree that clamp needs to be fast. But we’re not talking about the claiming that browsers do, which is different from the problem we’re talking about for user code to normalize clamping behavior.
MM: We’re therefore talking about maybe-adding an extra instruction 1->2, where we don’t have compelling evidence the perf in user clamps is a problem, because we don’t know the frequencies here.
DN: In the user-clamping case, flipped low/high is a misuse of the API.
MM: I don’t know if I agree that it’s misuse.
GR: I would not call it fully-standardized on hlsl, but rather that today hlsl happens to ensure standardized behavior here.
MM: Hard to know what the trade-offs are here, so it’s hard to decide.
AB: I feel like we’ve made decisions before based on commonalities in underlying apis/hardware, and that that prior art is pretty strong pressure.
GR: Worth noting that maybe HLSL’s experience shows that it’s probably not a perf problem for users, since they haven’t complained, and (to my knowledge, no ihv tries to replace min(max()) with clamp().
DN: From another direction, one alternative is to just remove the builtin entirely. If you want min(max()) use min(max())
DN: No native APIs specify it as anything in particular.
MM: HLSL does polyfill this anyway on all devices.
Oh, I forgot to mention: the table I listed above is for floating point clamp(). For integral clamp(), the behavior is consistent with the min()max() polyfill on all devices.
Edit: "all devices" means "the same devices described in #2557 (comment)"
I’ve previously described a desire for integral operations to be a corner of the language where everything is well-defined. We should spec the polyfill behavior at least for integral values.
Activity
[-]Should `clamp` be only defined for `low` < `high`?[/-][+]Should `clamp` be only defined for `low` <= `high`?[/+]Implement `clamp` tests
alan-baker commentedon Feb 2, 2022
I don't think we'd want undefined behaviour, but instead an undefined value. I know gpus have hardware instructions for clamps and it is a very common operation so I'm inclined to not polyfill it.
kvark commentedon Feb 4, 2022
The same rule should apply to
smoothStepas well.I remember from experience that having code not respecting this property is a recipe for portability disaster. I don't think we really want that for WGSL to happen. The prior decisions on handling
x/-1and friends would also be more consistent with handling "low > high" case here.kdashg commentedon Feb 7, 2022
We prefer to default to well-defined (even if maybe unintuitive) unless we have a decently strong reason to be undefined.
How much worse would it be to keep it defined-as min(max(a,v),b), versus the native clamp ops?
kdashg commentedon Feb 7, 2022
(which I believe is the same as
clamp(v, a, max(a,b)), if that helps at all)Add `clamp` tests (#975)
dneto0 commentedon Feb 24, 2022
I think that can't be cost-free:
AMD GCN implements clamp as median-of-3. So in the clamp(1,2,0) case it gives 1, which is contrary to min(max(2,1),0).
If we must have a polyfill, we can either define it as median-of-3 (which may look weird) or as
which I believe is the same thing. We can massage it editorially:
litherum commentedon Mar 1, 2022
clamp(1.f, 4.f, 2.f)clamp(3.f, 4.f, 2.f)clamp(5.f, 4.f, 2.f)222234222222litherum commentedon Mar 1, 2022
I kind of feel we should just add both behaviors to the standard library, as different functions.
clamp()would returnmin(max(e, low), high)andmedian()would returnsorted(args)[1].alan-baker commentedon Mar 1, 2022
I think having both functions wouldn't be a great outcome. Clamping is too common. If anything, I'd suggest allowing both versions of the implementation to be correct (i.e. describe both in clamp). It is non-portable, but this seems like a place to favour performance.
pow2clk commentedon Mar 1, 2022
min(max(a,v),b)is the result that both fxc and dxc producedneto0 commentedon Mar 2, 2022
DXC's SPIR-V backend produces a GLSL.std.450 FClamp instruction.
Edit: min(max(x, minVal), maxVal). Result is undefined if minVal > maxVal.
dneto0 commentedon Mar 2, 2022
This sounds good to me.
dneto0 commentedon Mar 2, 2022
In the 2022-03-01 meeting @litherum suggested data showing a significant slowdown from clamp or min/max would be convincing.
Issue gfx-rs/naga#1202 is "Out-of-bounds clamping brings big perf regression on TFJS ML demos. " Intel reported that Chrome's index clamping significantly affects performance. This is still open, and Google expects to ship WebGPU in production without making a significant fix in this space. See @ben-clayton's comment.
dneto0 commentedon Mar 2, 2022
Also, @kvark filed gfx-rs/wgpu#4360 to track this for Naga. It's still open.
kdashg commentedon Mar 2, 2022
WGSL meeting minutes 2022-03-01
kvark commentedon Mar 4, 2022
I don't think the bounds checking discussion is very relevant here. The way we are injecting clamping is very different from the way users use
clamp()in their code, and our injection is not constrained by whatever choice is made in this issue.kdashg commentedon Mar 9, 2022
WGSL meeting minutes 2022-03-08
litherum commentedon Mar 15, 2022
Oh, I forgot to mention: the table I listed above is for floating point clamp(). For integral clamp(), the behavior is consistent with the min()max() polyfill on all devices.
Edit: "all devices" means "the same devices described in #2557 (comment)"
I’ve previously described a desire for integral operations to be a corner of the language where everything is well-defined. We should spec the polyfill behavior at least for integral values.
kdashg commentedon Mar 15, 2022
WGSL meeting minutes 2022-03-15
Floating clamp can be median-of-three
Floating clamp can be median-of-three (#2681)
kdashg commentedon Mar 23, 2022
WGSL meeting minutes 2022-03-22
Explicitly state the accuracy for clamp
Explicitly state the accuracy for clamp (#2918)
Explicitly state the accuracy for clamp (gpuweb#2918)