Skip to content

Add distance tests #270

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 19 commits into
base: main
Choose a base branch
from
Open

Add distance tests #270

wants to merge 19 commits into from

Conversation

bob80905
Copy link
Contributor

@bob80905 bob80905 commented Jun 21, 2025

This PR adds tests for the distance function. Adds float16 and float32 test files.
Fixes #175

@bob80905
Copy link
Contributor Author

Should I add a test case for NaNs? Or other non-normal floats?

Copy link
Collaborator

@spall spall left a comment

Choose a reason for hiding this comment

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

Yeah adding tests for odd values like NaN and Inf would be good.

@bob80905 bob80905 force-pushed the add_distance_tests branch from 5cb1a01 to 913c363 Compare June 23, 2025 17:50
float R3 = distance(X[0].xyzw, Y[0].xyzw);
Result[3] = R3;

// test denormal cases
Copy link

Choose a reason for hiding this comment

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

These aren't denormal (aka. subnormal) cases, which would also be good to test. That would be very small positive or negative values, below the smallest normalized value in the given float representation.

Elsewhere we use the terminology "floating point specials" to refer to INF or NaN non-numeric values.

Copy link
Collaborator

Choose a reason for hiding this comment

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

https://github.com/llvm/offload-test-suite/pull/268/files
you can look here for examples of 16 and 32 bit float values which should be denorm according to c++

@bob80905 bob80905 force-pushed the add_distance_tests branch from be926bc to ae5988a Compare July 3, 2025 00:53
@bob80905 bob80905 force-pushed the add_distance_tests branch from 638892e to 8b75db9 Compare July 7, 2025 23:33
void main() {
// test special cases
// distance(inf, 1.0) = inf
Result[0] = distance(X[0].x, Y[0].x);
Copy link

Choose a reason for hiding this comment

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

The degenerate scalar case is not that interesting for distance, since it just translates to fsub. Vector distance translates to fsub, fmul+fadd per component, sqrt.

So these scalar cases are really only testing the behavior of fsub.

...
#--- end

# UNSUPPORTED: Clang-Vulkan
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is Clang-Vulkan unsupported? Sorry if this has been explained before and I forgot or missed it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

# | Validation Error: [ VUID-VkShaderModuleCreateInfo-pCode-08742 ]
# | vkCreateShaderModule(): SPIR-V Extension SPV_KHR_float_controls2 was declared, but one of the following requirements is required (1.4.0 (0x00404000) or VK_KHR_shader_float_controls2).

is what I get when running it without that line.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you leave a comment about that in the tests?

@bob80905 bob80905 force-pushed the add_distance_tests branch from 15dded5 to 977c5fa Compare July 9, 2025 20:32
- Name: X
Format: Float32
Stride: 16
Data: [ inf, NaN, -inf, 0 ]

Choose a reason for hiding this comment

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

Would -0 also an interesting value to test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would, looks like it didn't affect any of the expected results however.

Copy link

@tex3d tex3d left a comment

Choose a reason for hiding this comment

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

I feel like there needs to be an adjustment in approach due to the intended purpose of these tests.

  • I don't think we need NaN/Inf/Denorm tests
  • Values used should test full operation/expansion within reasonable ranges and precisions for confidence that the expansion used is correct, but not hyper-sensitive to precision
  • Don't just test degenerate edge cases that could pass with an incomplete expansion
  • ULP tolerance should be higher to allow for valid variances in operation expansions
  • Need constant (literal) input cases to test constant folding

Copy link

Choose a reason for hiding this comment

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

I'm not sure this particular test is necessary, since the purpose of this test suite is to make sure Clang produces the same key DXIL / SPIR-V code as DXC to implement the operation.

The important aspects to test would be the forms of HLSL that generate different operations/dimensions, and a variety of reasonable inputs to verify that the operations performed are the same.

I thought edge cases like these aren't what we care about for this test suite.

Additionally, the denorm tests (after which this file is apparently named) aren't even interesting, as their results would be identical if those denorms were replaced with 0's.

// distance({0.0, 0.0, 0.0}, {3.0, 4.0, 12.0}) = 13.0
float R2 = distance(X[0].xyz, Y[0].xyz);
Result[2] = R2;
// distance({0.0, 0.0, 0.0, 0.0}, {3.0, 4.0, 12.0, 84.0}) = 85.0
Copy link

Choose a reason for hiding this comment

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

Always using 0's for the first argument is an edge case that doesn't even require the full operation expansion (initial subtraction could be omitted, making it the same as length). It would be better to test some random values in reasonable ranges on both sides with a reasonable ULP tolerance for comparison of expected results. That should be sufficient to show that the correct operations are used for each compiler scenario.

Testing the exact precision of implementations isn't the point of this test either, so having a higher tolerance than necessary would probably not be a problem.

Copy link

Choose a reason for hiding this comment

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

Come to think of it, one special case that SHOULD be tested is when inputs are constant. In that case, the compiler (likely) constant-folds operations, which is a separate code path.

Constant inputs should be tested for most/all math ops.

Results:
- Result: CheckResult
Rule: BufferFloatULP
ULPT: 1
Copy link

Choose a reason for hiding this comment

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

ULP of 1 seems pretty tight. Since the expansion could use dot or fma with varying ordering, and uses sqrt, it seems possible you could exceed the tolerance on a perfectly valid implementation.

Comment on lines 46 to 49
- Name: X
Format: Float32
Stride: 16
Data: [ 1.125, 2.5, 4.75, 6.625 ]
Copy link
Contributor

@kmpeng kmpeng Jul 18, 2025

Choose a reason for hiding this comment

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

Would it be interesting to test some negative values? (same for the half test)

Result[5] = R2_constant;

// distance({1.125, 2.5, 4.75, 6.625}, {2.375, 5.25, 8.375, 5.30}) = 4.90115
float R3 = distance(X[0].xyzw, Y[0].xyzw);
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT: you don't need to do X[0].xyzw and Y[0].xyzw, you can just do distance(X[0], Y[0]) (same for the half test)

Comment on lines 49 to 50
Data: [ 0x3c80, 0x4100, 0x44c0, 0x46a0 ]
- Name: Y
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be useful to add comments under the Data lines listing these values, even though you have them commented in main so you don't have to scroll up to look for/match them

Data: [ 0x3c80, 0x4100, 0x44c0, 0x46a0 ]
# 1.125, 2.5, 4.75, 6.625

@@ -0,0 +1,100 @@
#--- source.hlsl
Copy link
Contributor

Choose a reason for hiding this comment

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

I remember for ldexp, @spall said to just name my files <intrinsic>.32.test and <intrinsic>.16.test to make it clear there isn't a missing int16.test file. Probably want her input for this though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add test for distance
5 participants