Skip to content

Commit 94e9402

Browse files
authored
update moments for normalize bug (NVIDIA-Merlin#444)
1 parent 7e11a86 commit 94e9402

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

nvtabular/ops/moments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def _chunkwise_moments(df):
125125
df2[col] = df[col].astype("float64").pow(2)
126126
vals = {
127127
"df-count": df.count().to_frame().transpose(),
128-
"df-sum": df.sum().to_frame().transpose(),
128+
"df-sum": df.sum().astype("float64").to_frame().transpose(),
129129
"df2-sum": df2.sum().to_frame().transpose(),
130130
}
131131
# NOTE: Perhaps we should convert to pandas here

tests/unit/test_ops.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,38 @@ def test_normalize(tmpdir, df, dataset, gpu_memory_frac, engine, op_columns):
436436
assert new_gdf["x"].equals(df["x"])
437437

438438

439+
@pytest.mark.parametrize("gpu_memory_frac", [0.1])
440+
@pytest.mark.parametrize("engine", ["parquet"])
441+
@pytest.mark.parametrize("op_columns", [["x"], None])
442+
def test_normalize_upcastfloat64(tmpdir, dataset, gpu_memory_frac, engine, op_columns):
443+
df = cudf.DataFrame(
444+
{"x": [1.9e10, 2.3e16, 3.4e18, 1.6e19], "label": [1, 0, 1, 0]}, dtype="float32"
445+
)
446+
447+
cat_names = []
448+
cont_names = ["x"]
449+
label_name = ["label"]
450+
451+
config = nvt.workflow.get_new_config()
452+
config["PP"]["continuous"] = [ops.Moments(columns=op_columns)]
453+
454+
processor = nvtabular.Workflow(
455+
cat_names=cat_names, cont_names=cont_names, label_name=label_name, config=config
456+
)
457+
458+
processor.update_stats(dataset)
459+
460+
op = ops.Normalize()
461+
462+
columns_ctx = {}
463+
columns_ctx["continuous"] = {}
464+
columns_ctx["continuous"]["base"] = op_columns or cont_names
465+
466+
new_gdf = op.apply_op(df, columns_ctx, "continuous", stats_context=processor.stats)
467+
df["x"] = (df["x"] - processor.stats["means"]["x"]) / processor.stats["stds"]["x"]
468+
assert new_gdf["x"].equals(df["x"])
469+
470+
439471
@pytest.mark.parametrize("gpu_memory_frac", [0.01, 0.1])
440472
@pytest.mark.parametrize("engine", ["parquet", "csv", "csv-no-header"])
441473
@pytest.mark.parametrize("op_columns", [["x"], None])

0 commit comments

Comments
 (0)