LeetKernels
ProblemsStart solving
Problems

33. Ordinary Least Squares medium

Solve the Ordinary Least Squares (OLS) regression problem on a GPU. Given a feature matrix X of size n_samples×n_features and a target vector y of size n_samples, compute the coefficient vector β that minimizes the sum of squared residuals: βmin​∣∣Xβ−y∣∣2 The closed-form solution to OLS is: β=(XTX)−1XTy

Implementation Requirements

  • External libraries are not permitted.
  • The solve function signature must remain unchanged.
  • The final coefficients must be stored in the beta vector.
  • Assume that the feature matrix X is full rank (i.e., XTX is invertible).

Example:

Input:
X (samples × features): ​−0.230.77−0.14−1.91−0.46​−0.23−0.470.65−1.72−0.47​1.521.580.50.240.54​​ y: ​83.0193.447.33−62.2213.06​​ Output:
β: ​13.9729.1261.05​​

Constraints

  • 1 ≤ n_samples ≤ 100,000
  • 1 ≤ n_features ≤ 1,000
  • n_samples ≥ n_features
  • -1000.0 ≤ values in X and y ≤ 1000.0
  • Solutions are tested with absolute tolerance of 1e-2 and relative tolerance of 1e-2
  • Performance is measured with n_features = 32, n_samples = 32

Starter code (CUDA)

#include <cuda_runtime.h>

// X, y, beta are device pointers
extern "C" void solve(const float* X, const float* y, float* beta, int n_samples, int n_features) {}

Solve it on your GPU

Previous: INT8 Quantized MatMulNext: Logistic Regression

From LeetGPU, CC BY-NC-ND 4.0.