Copilot commented on code in PR #971:
URL: https://github.com/apache/mahout/pull/971#discussion_r2742201632
##########
qdp/qdp-core/src/gpu/encodings/amplitude.rs:
##########
@@ -474,4 +475,61 @@ impl AmplitudeEncoder {
Ok(inv_norm)
}
+
+ /// Compute inverse L2 norm on GPU for float32 input using the reduction
kernel.
+ ///
+ /// # Arguments
+ /// * `device` - CUDA device reference
+ /// * `input_ptr` - Device pointer to input data (f32 array on GPU)
+ /// * `len` - Number of f32 elements
+ ///
+ /// # Returns
+ /// The inverse L2 norm (1/||x||_2) of the input data as `f32`.
+ ///
+ /// # Safety
+ /// The caller must ensure `input_ptr` points to valid GPU memory
containing
+ /// at least `len` f32 elements on the same device as `device`.
+ #[cfg(target_os = "linux")]
+ pub unsafe fn calculate_inv_norm_gpu_f32(
+ device: &Arc<CudaDevice>,
+ input_ptr: *const f32,
+ len: usize,
+ ) -> Result<f32> {
+ crate::profile_scope!("GPU::NormSingleF32");
+
+ let mut norm_buffer = device.alloc_zeros::<f32>(1).map_err(|e| {
+ MahoutError::MemoryAllocation(format!("Failed to allocate f32 norm
buffer: {:?}", e))
+ })?;
+
+ let ret = unsafe {
+ launch_l2_norm_f32(
+ input_ptr,
+ len,
+ *norm_buffer.device_ptr_mut() as *mut f32,
+ std::ptr::null_mut(), // default stream
+ )
+ };
+
+ if ret != 0 {
+ return Err(MahoutError::KernelLaunch(format!(
+ "Norm kernel f32 failed: {} ({})",
+ ret,
+ cuda_error_to_string(ret)
+ )));
+ }
+
+ let inv_norm_host = device
+ .dtoh_sync_copy(&norm_buffer)
+ .map_err(|e| MahoutError::Cuda(format!("Failed to copy f32 norm to
host: {:?}", e)))?;
Review Comment:
The error text says "Failed to copy f32 norm to host", but
`launch_l2_norm_f32` writes the *inverse* norm. Renaming this message (and
possibly `norm_buffer`) to reflect that it contains `inv_norm` would make
debugging less confusing.
##########
qdp/qdp-core/src/gpu/encodings/amplitude.rs:
##########
@@ -474,4 +475,61 @@ impl AmplitudeEncoder {
Ok(inv_norm)
}
+
+ /// Compute inverse L2 norm on GPU for float32 input using the reduction
kernel.
+ ///
+ /// # Arguments
+ /// * `device` - CUDA device reference
+ /// * `input_ptr` - Device pointer to input data (f32 array on GPU)
+ /// * `len` - Number of f32 elements
+ ///
+ /// # Returns
+ /// The inverse L2 norm (1/||x||_2) of the input data as `f32`.
+ ///
+ /// # Safety
+ /// The caller must ensure `input_ptr` points to valid GPU memory
containing
+ /// at least `len` f32 elements on the same device as `device`.
+ #[cfg(target_os = "linux")]
+ pub unsafe fn calculate_inv_norm_gpu_f32(
+ device: &Arc<CudaDevice>,
+ input_ptr: *const f32,
+ len: usize,
+ ) -> Result<f32> {
Review Comment:
`calculate_inv_norm_gpu_f32` is declared `pub` (and `unsafe`) while the
existing f64 helper `calculate_inv_norm_gpu` is `pub(crate)`. This introduces a
new public, Linux-only API that accepts a raw device pointer, which expands the
public surface area and makes downstream cross-platform use harder. Consider
making this `pub(crate)` like the f64 variant and moving the tests into a
`#[cfg(test)]` module in this file (or alternatively exposing a safe wrapper
that takes a cudarc device slice instead of a raw pointer).
##########
qdp/qdp-core/tests/gpu_norm_f32.rs:
##########
@@ -0,0 +1,85 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+//
+// Tests for GPU-side f32 L2 norm helper in AmplitudeEncoder.
+//
+
+#![cfg(target_os = "linux")]
+
+use approx::assert_relative_eq;
+use cudarc::driver::{CudaDevice, DevicePtr};
+use qdp_core::gpu::encodings::amplitude::AmplitudeEncoder;
+
+#[test]
+fn test_calculate_inv_norm_gpu_f32_basic() {
+ println!("Testing AmplitudeEncoder::calculate_inv_norm_gpu_f32 (basic
case)...");
+
+ let device = match CudaDevice::new(0) {
+ Ok(d) => d,
+ Err(_) => {
+ println!("SKIP: No CUDA device available");
+ return;
+ }
+ };
+
+ // Input: [3.0, 4.0] -> norm = 5.0, inv_norm = 0.2
+ let input: Vec<f32> = vec![3.0, 4.0];
+ let expected_norm = (3.0_f32.powi(2) + 4.0_f32.powi(2)).sqrt();
+ let expected_inv_norm = 1.0_f32 / expected_norm;
+
+ let input_d = device.htod_sync_copy(input.as_slice()).unwrap();
+ let inv = unsafe {
+ AmplitudeEncoder::calculate_inv_norm_gpu_f32(
+ &device,
+ *input_d.device_ptr() as *const f32,
+ input.len(),
+ )
+ .unwrap()
+ };
+
+ assert_relative_eq!(inv, expected_inv_norm, epsilon = 1e-6_f32);
+}
+
+#[test]
+fn test_calculate_inv_norm_gpu_f32_invalid_zero() {
+ println!("Testing AmplitudeEncoder::calculate_inv_norm_gpu_f32 with zero
vector...");
+
+ let device = match CudaDevice::new(0) {
+ Ok(d) => d,
+ Err(_) => {
+ println!("SKIP: No CUDA device available");
+ return;
+ }
+ };
+
+ let input: Vec<f32> = vec![0.0, 0.0, 0.0];
+ let input_d = device.htod_sync_copy(input.as_slice()).unwrap();
+
+ let result = unsafe {
+ AmplitudeEncoder::calculate_inv_norm_gpu_f32(
+ &device,
+ *input_d.device_ptr() as *const f32,
+ input.len(),
+ )
+ };
+
+ assert!(
+ result.is_err(),
+ "Expected error for zero-norm f32 input, got {:?}",
+ result
+ );
Review Comment:
This test only asserts `result.is_err()`, so it will pass for unrelated
failures (e.g., kernel launch / allocation errors) and doesn’t actually
validate the intended error-handling behavior for a zero-norm input. Please
assert the specific error variant (e.g., `MahoutError::InvalidInput`) and, if
appropriate, check that the message matches the expected condition.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]