Skip to content

Commit 3cf06ff

Browse files
committed
test: run examples smoke tests in CI
1 parent 93087ad commit 3cf06ff

9 files changed

Lines changed: 72 additions & 8 deletions

File tree

.github/workflows/ci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ on:
55
paths:
66
- "deepctr_torch/**"
77
- "tests/**"
8+
- "examples/**"
89
- "setup.py"
910
- ".github/workflows/**"
1011
pull_request:
1112
paths:
1213
- "deepctr_torch/**"
1314
- "tests/**"
15+
- "examples/**"
1416
- "setup.py"
1517
- ".github/workflows/**"
1618
workflow_dispatch:
@@ -37,6 +39,7 @@ jobs:
3739
torch-version: "2.4.1"
3840
- python-version: "3.10"
3941
torch-version: "2.5.1"
42+
run-examples: "1"
4043
- python-version: "3.11"
4144
torch-version: "2.4.1"
4245
- python-version: "3.11"
@@ -49,6 +52,7 @@ jobs:
4952
env:
5053
TORCH_VERSION: ${{ matrix.torch-version }}
5154
TORCH_INDEX_URL: https://download.pytorch.org/whl/cpu
55+
RUN_EXAMPLES: ${{ matrix.run-examples || '0' }}
5256

5357
steps:
5458
- uses: actions/checkout@v5
@@ -65,6 +69,11 @@ jobs:
6569
timeout-minutes: 180
6670
run: bash tests/ci/test.sh
6771

72+
- name: Run examples smoke tests
73+
if: ${{ env.RUN_EXAMPLES == '1' }}
74+
timeout-minutes: 60
75+
run: bash tests/ci/examples.sh
76+
6877
- name: Upload coverage to Codecov
6978
uses: codecov/codecov-action@v6
7079
with:

examples/run_classification_criteo.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
import os
3+
24
import pandas as pd
35
import torch
46
from sklearn.metrics import log_loss, roc_auc_score
@@ -58,7 +60,8 @@
5860
model.compile("adagrad", "binary_crossentropy",
5961
metrics=["binary_crossentropy", "auc"], )
6062

61-
history = model.fit(train_model_input, train[target].values, batch_size=32, epochs=10, verbose=2,
63+
epochs = int(os.getenv("DEEPCTR_EXAMPLE_EPOCHS", "10"))
64+
history = model.fit(train_model_input, train[target].values, batch_size=32, epochs=epochs, verbose=2,
6265
validation_split=0.2)
6366
pred_ans = model.predict(test_model_input, 256)
6467
print("")

examples/run_dien.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import numpy as np
23
import torch
34

@@ -65,4 +66,5 @@ def get_xy_fd(use_neg=False, hash_flag=False):
6566

6667
model.compile('adam', 'binary_crossentropy',
6768
metrics=['binary_crossentropy', 'auc'])
68-
history = model.fit(x, y, batch_size=2, epochs=10, verbose=1, validation_split=0, shuffle=False)
69+
epochs = int(os.getenv("DEEPCTR_EXAMPLE_EPOCHS", "10"))
70+
history = model.fit(x, y, batch_size=2, epochs=epochs, verbose=1, validation_split=0, shuffle=False)

examples/run_din.py

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

33
sys.path.insert(0, '..')
44

5+
import os
56
import numpy as np
67
import torch
78
from deepctr_torch.inputs import (DenseFeat, SparseFeat, VarLenSparseFeat,
@@ -47,4 +48,5 @@ def get_xy_fd():
4748
model = DIN(feature_columns, behavior_feature_list, device=device, att_weight_normalization=True)
4849
model.compile('adagrad', 'binary_crossentropy',
4950
metrics=['binary_crossentropy'])
50-
history = model.fit(x, y, batch_size=3, epochs=10, verbose=2, validation_split=0.0)
51+
epochs = int(os.getenv("DEEPCTR_EXAMPLE_EPOCHS", "10"))
52+
history = model.fit(x, y, batch_size=3, epochs=epochs, verbose=2, validation_split=0.0)

examples/run_multitask_learning.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
import os
3+
24
import pandas as pd
35
import torch
46
from sklearn.metrics import log_loss, roc_auc_score
@@ -56,7 +58,8 @@
5658
model.compile("adagrad", loss=["binary_crossentropy", "binary_crossentropy"],
5759
metrics=['binary_crossentropy'], )
5860

59-
history = model.fit(train_model_input, train[target].values, batch_size=32, epochs=10, verbose=2)
61+
epochs = int(os.getenv("DEEPCTR_EXAMPLE_EPOCHS", "10"))
62+
history = model.fit(train_model_input, train[target].values, batch_size=32, epochs=epochs, verbose=2)
6063
pred_ans = model.predict(test_model_input, 256)
6164
print("")
6265
for i, target_name in enumerate(target):

examples/run_multivalue_movielens.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
1+
import os
2+
13
import numpy as np
24
import pandas as pd
35
import torch
46
from sklearn.preprocessing import LabelEncoder
5-
from tensorflow.python.keras.preprocessing.sequence import pad_sequences
7+
8+
try:
9+
from tensorflow.keras.preprocessing.sequence import pad_sequences
10+
except Exception:
11+
def pad_sequences(sequences, maxlen=None, dtype='int32', padding='pre', truncating='pre', value=0):
12+
if maxlen is None:
13+
maxlen = max(len(seq) for seq in sequences)
14+
x = np.full((len(sequences), maxlen), value, dtype=dtype)
15+
for idx, seq in enumerate(sequences):
16+
if truncating == 'pre':
17+
trunc = seq[-maxlen:]
18+
else:
19+
trunc = seq[:maxlen]
20+
trunc = np.asarray(trunc, dtype=dtype)
21+
if padding == 'post':
22+
x[idx, :len(trunc)] = trunc
23+
else:
24+
x[idx, -len(trunc):] = trunc
25+
return x
626

727
from deepctr_torch.inputs import SparseFeat, VarLenSparseFeat, get_feature_names
828
from deepctr_torch.models import DeepFM
@@ -64,4 +84,6 @@ def split(x):
6484
model = DeepFM(linear_feature_columns, dnn_feature_columns, task='regression', device=device)
6585

6686
model.compile("adam", "mse", metrics=['mse'], )
67-
history = model.fit(model_input, data[target].values, batch_size=256, epochs=10, verbose=2, validation_split=0.2)
87+
epochs = int(os.getenv("DEEPCTR_EXAMPLE_EPOCHS", "10"))
88+
history = model.fit(model_input, data[target].values, batch_size=256, epochs=epochs, verbose=2,
89+
validation_split=0.2)

examples/run_regression_movielens.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
import pandas as pd
24
import torch
35
from sklearn.metrics import mean_squared_error
@@ -40,7 +42,8 @@
4042
model = DeepFM(linear_feature_columns, dnn_feature_columns, task='regression', device=device)
4143
model.compile("adam", "mse", metrics=['mse'], )
4244

43-
history = model.fit(train_model_input, train[target].values, batch_size=256, epochs=10, verbose=2,
45+
epochs = int(os.getenv("DEEPCTR_EXAMPLE_EPOCHS", "10"))
46+
history = model.fit(train_model_input, train[target].values, batch_size=256, epochs=epochs, verbose=2,
4447
validation_split=0.2)
4548
pred_ans = model.predict(test_model_input, batch_size=256)
4649
print("test MSE", round(mean_squared_error(

tests/ci/examples.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
export DEEPCTR_EXAMPLE_EPOCHS="${DEEPCTR_EXAMPLE_EPOCHS:-1}"
5+
6+
scripts=(
7+
"run_classification_criteo.py"
8+
"run_regression_movielens.py"
9+
"run_multitask_learning.py"
10+
"run_multivalue_movielens.py"
11+
"run_din.py"
12+
"run_dien.py"
13+
)
14+
15+
pushd examples >/dev/null
16+
for script in "${scripts[@]}"; do
17+
echo "Running example smoke test: ${script} (epochs=${DEEPCTR_EXAMPLE_EPOCHS})"
18+
python "${script}"
19+
done
20+
popd >/dev/null

tests/ci/install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ else
1010
python -m pip install -q "torch==${TORCH_VERSION}"
1111
fi
1212

13-
python -m pip install -q requests pytest pytest-cov python-coveralls
13+
python -m pip install -q requests pytest pytest-cov python-coveralls pandas
1414
python -m pip install -e .
1515
python -m pip check

0 commit comments

Comments
 (0)