Skip to content

feature penalty not supported in CUDA #7054

@gnahz04

Description

@gnahz04

Description

It appears that feature_penalty parameter is not activated under CUDA.

Reproducible example

In the following example, I expected CUDA version, similar to the CPU version, to also output feature importance to be "0 600" when feature penalty is 0 on the first feature.

import numpy as np
import lightgbm as lgb

def run_me(cpu_or_cuda='cpu'):
    # 1. Create a synthetic binary classification dataset
    np.random.seed(42)
    N = 1000
    X = np.zeros((N, 2))
    y = np.zeros(N)

    # Feature 0 is a strong predictor (we'll make it correlate  strongly with y)
    # Feature 1 will be a weak predictor (mostly noise)
    X[:, 0] = np.random.randint(0, 2, size=N)        # binary feature
    X[:, 1] = np.random.normal(size=N)               # continuous noise feature
    y = X[:, 0].copy()                               # make label equal to feature 0 (perfect predictor)
    # Add a little randomness to labels to avoid perfect separability
    y[:50] = 1 - y[:50]  # flip first 50 labels to introduce some noise

    # Convert to LightGBM Dataset
    dtrain = lgb.Dataset(X, label=y)

    # 2. Train a baseline model with no feature penalty (using CPU for demo; use device_type='cuda' if available)
    params_no_penalty = {
        "objective": "binary",
        "metric": "auc",
        "device_type": cpu_or_cuda,        # would be "cuda" for GPU with CUDA support
        "verbose": -1,
        "seed": 42
    }
    model_no_penalty = lgb.train(params_no_penalty, dtrain, num_boost_round=20)

    # 3. Train a model with feature penalty to down-weight feature 0
    # We set feature0's penalty to 0 (no gain) and feature1's penalty to 1 (no penalty) for demonstration
    params_with_penalty = {
        "objective": "binary",
        "metric": "auc",
        "device_type": cpu_or_cuda,        # use "cuda" for actual GPU run
        "feature_penalty": [0.0, 1.0],  # penalize feature 0 heavily, feature 1 normal
        "verbose": -1,
        "seed": 42
    }
    model_with_penalty = lgb.train(params_with_penalty, dtrain, num_boost_round=20)

    # 4. Compare feature importances
    print(f"{cpu_or_cuda=}")
    print("Feature importances (no penalty):", model_no_penalty.feature_importance(importance_type="split"))
    print("Feature importances (with penalty):", model_with_penalty.feature_importance(importance_type="split"))

run_me('cpu')
run_me('cuda')

--->

cpu_or_cuda='cpu'
Feature importances (no penalty): [ 20 580]
Feature importances (with penalty): [  0 600]
cpu_or_cuda='cuda'
Feature importances (no penalty): [ 20 580]
Feature importances (with penalty): [ 20 580]

Environment info

LightGBM version or commit hash:
lightgbm 4.6.0.99
Command(s) you used to install LightGBM

pip install -U lightgbm

Additional Comments

Metadata

Metadata

Assignees

No one assigned

    Labels

    gpu (CUDA)Issue is related to the CUDA GPU variant.question

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions