Skip to content

Commit cda95af

Browse files
MET-46: Cleanup Confidence Implementation (#16)
(FIXES MET-46) I have cleaned up the confidence implementation with the folowing: 1. Removed GT testing (specifically a reference eq check) entirely from cpp (only in Python). CPP testing is kept for smoke testing and checking bounds for `confidence` and `sigmoid`. 2. Removed a couple unnecessary imports 3. Removed `nan` check in CPP sigmoid, nan-checking in python still passes.
1 parent 3e45d8e commit cda95af

File tree

5 files changed

+4
-58
lines changed

5 files changed

+4
-58
lines changed

genmetaballs/src/cuda/bindings.cu

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
#include <nanobind/nanobind.h>
33
#include <nanobind/operators.h>
44
#include <nanobind/stl/vector.h>
5-
#include <stdexcept>
65

76
#include "core/confidence.cuh"
87
#include "core/geometry.cuh"
98
#include "core/utils.cuh"
109

11-
constexpr uint32_t GRID_DIM = 4096;
12-
constexpr uint32_t BLOCK_DIM = 1024;
13-
1410
namespace nb = nanobind;
1511

1612
NB_MODULE(_genmetaballs_bindings, m) {

genmetaballs/src/cuda/core/confidence.cuh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <cmath>
44
#include <cuda_runtime.h>
5-
#include <vector>
65

76
#include "utils.cuh"
87

genmetaballs/src/cuda/core/utils.cuh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
void cuda_check(cudaError_t code, const char* file, int line);
1616

1717
CUDA_CALLABLE __forceinline__ float sigmoid(float x) {
18-
if (isnan(x)) {
19-
return x;
20-
}
2118
return 1.0f / (1.0f + expf(-x));
2219
}
2320

tests/cpp_tests/test_confidence.cu

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@
1010

1111
#include "core/confidence.cuh"
1212

13-
// Helper: Python ground truth, as in test_confidence.py
14-
inline float ground_truth_expit(float x) {
15-
return 1.0F / (1.0F + std::exp(-x));
16-
}
17-
float ground_truth_two_parameter_confidence(float beta4, float beta5, float sumexpd) {
18-
return ground_truth_expit((beta4 * sumexpd) + beta5);
19-
}
20-
float ground_truth_zero_parameter_confidence(float sumexpd) {
21-
return 1.0F - std::exp(-sumexpd);
22-
}
23-
2413
template <typename Confidence>
2514
__global__ void confidence_kernel(const float* sumexpd, float* confidences, uint32_t n,
2615
Confidence confidence) {
@@ -69,7 +58,6 @@ static std::vector<int> confidence_test_sizes() {
6958
return sizes;
7059
}
7160

72-
// Define simple struct to match python CONFIDENCE_TEST_CASES
7361
struct ConfidenceCase {
7462
std::string name;
7563
float beta4 = 0.0F;
@@ -112,16 +100,6 @@ TEST(GpuConfidenceTest, ConfidenceMultipleValuesGPU_AllTypes) {
112100
for (int i = 0; i < N; ++i)
113101
sumexpd_vec[i] = dist(rng);
114102

115-
std::vector<float> expected(N);
116-
if (conf_case.is_two_param) {
117-
for (int i = 0; i < N; ++i)
118-
expected[i] = ground_truth_two_parameter_confidence(
119-
conf_case.beta4, conf_case.beta5, sumexpd_vec[i]);
120-
} else {
121-
for (int i = 0; i < N; ++i)
122-
expected[i] = ground_truth_zero_parameter_confidence(sumexpd_vec[i]);
123-
}
124-
125103
std::vector<float> actual;
126104
if (conf_case.is_two_param) {
127105
TwoParameterConfidence conf(conf_case.beta4, conf_case.beta5);
@@ -131,12 +109,6 @@ TEST(GpuConfidenceTest, ConfidenceMultipleValuesGPU_AllTypes) {
131109
actual = gpu_get_confidence(sumexpd_vec, conf);
132110
}
133111

134-
ASSERT_EQ(actual.size(), expected.size());
135-
for (int i = 0; i < N; ++i) {
136-
ASSERT_NEAR(actual[i], expected[i], 1e-6F)
137-
<< "at idx=" << i << " N=" << N << " conf_type=" << conf_case.name
138-
<< " exp=" << expected[i] << " act=" << actual[i];
139-
}
140112
// Ensure all actual values are in [0, 1]
141113
ASSERT_TRUE(std::all_of(actual.begin(), actual.end(),
142114
[](float v) { return v >= 0.0F && v <= 1.0F; }))

tests/cpp_tests/test_utils.cu

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313

1414
namespace test_utils_gpu {
1515

16-
// CUDA kernel for computing sigmoid element-wise (relies on __device__ sigmoid in utils.cuh)
16+
// CUDA kernel for computing sigmoid element-wise
1717
__global__ void sigmoid_kernel(const float* x, float* result, uint32_t n) {
1818
uint32_t i = threadIdx.x + blockIdx.x * blockDim.x;
1919
if (i < n) {
2020
result[i] = sigmoid(x[i]);
2121
}
2222
}
2323

24-
// GPU function to compute sigmoid for a vector (float only)
24+
// GPU function to compute sigmoid for a vector
2525
template <uint32_t grid_dim, uint32_t block_dim>
2626
std::vector<float> gpu_sigmoid(const std::vector<float>& x_vec) {
2727
uint32_t n = x_vec.size();
@@ -45,11 +45,6 @@ std::vector<float> gpu_sigmoid(const std::vector<float>& x_vec) {
4545
return result;
4646
}
4747

48-
// Host sigmoid for reference
49-
inline float host_sigmoid(float x) {
50-
return 1.0f / (1.0f + std::exp(-x));
51-
}
52-
5348
} // namespace test_utils_gpu
5449

5550
// Parameters matching the removed Python test
@@ -65,7 +60,7 @@ static std::vector<int> sigmoid_test_sizes() {
6560
return sizes;
6661
}
6762

68-
TEST(GpuSigmoidTest, SigmoidVectorCorrectness) {
63+
TEST(GpuSigmoidTest, SigmoidGPUWithinBounds) {
6964
// Generate seeds
7065
std::mt19937 master_gen(SEED_MASTER);
7166
std::uniform_int_distribution<uint32_t> seed_dist(0, std::numeric_limits<uint32_t>::max());
@@ -85,23 +80,10 @@ TEST(GpuSigmoidTest, SigmoidVectorCorrectness) {
8580
for (int i = 0; i < N; ++i)
8681
x_vec[i] = dist(rng);
8782

88-
// Compute expected (host)
89-
std::vector<float> expected(N);
90-
for (int i = 0; i < N; ++i)
91-
expected[i] = test_utils_gpu::host_sigmoid(x_vec[i]);
92-
93-
// Compute actual (GPU)
83+
// Run on GPU
9484
constexpr uint32_t block_dim = 256;
95-
uint32_t grid_dim = (N + block_dim - 1) / block_dim;
9685
std::vector<float> actual = test_utils_gpu::gpu_sigmoid<1024, block_dim>(x_vec);
9786

98-
// Compare
99-
ASSERT_EQ(actual.size(), expected.size());
100-
for (int i = 0; i < N; ++i) {
101-
ASSERT_NEAR(actual[i], expected[i], 1e-5)
102-
<< "at idx=" << i << " for N=" << N << " seed=" << seed;
103-
}
104-
10587
// Check [0, 1] bounds
10688
ASSERT_TRUE(std::all_of(actual.begin(), actual.end(),
10789
[](float v) { return v >= 0.0f && v <= 1.0f; }))

0 commit comments

Comments
 (0)