Skip to content
This repository was archived by the owner on Aug 14, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions controller/controller/create_pod.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: MIT
import os
import random
import secrets
import string

from kubernetes import client as k8s_client
Expand Down Expand Up @@ -55,7 +55,7 @@ def get_run_pod_env_vars(run_context):


def random_run_name():
return "run-" + "".join(random.choice(RUN_RANDOM_PART_CHARS) for _ in range(RUN_RANDOM_PART_LENGTH))
return "run-" + "".join(secrets.SystemRandom().choice(RUN_RANDOM_PART_CHARS) for _ in range(RUN_RANDOM_PART_LENGTH))


def create_run_pod(k8s_settings, run_context):
Expand Down
18 changes: 9 additions & 9 deletions integration_test/test_xgboost_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: MIT
import copy
import os
import random
import secrets

import pytest

Expand Down Expand Up @@ -33,14 +33,14 @@ class TestXGBoostExperiment:

def _generate_randomized_search_space(self):
search_space = copy.deepcopy(SEARCH_SPACES)
if random.randint(0, 1) == 1: # add bounds and type to eta randomly
if secrets.SystemRandom().randint(0, 1) == 1: # add bounds and type to eta randomly
search_space[0]["type"] = "double"
search_space[0]["bounds"] = {"min": 0.1, "max": 0.5}
if random.randint(0, 1) == 1: # add bounds and type to min_child_weight randomly
if secrets.SystemRandom().randint(0, 1) == 1: # add bounds and type to min_child_weight randomly
search_space[1]["type"] = "double"
search_space[1]["bounds"] = {"min": 0.0, "max": 0.3}
random_subset_size = random.randint(1, len(search_space))
search_space = random.sample(search_space, random_subset_size)
random_subset_size = secrets.SystemRandom().randint(1, len(search_space))
search_space = secrets.SystemRandom().sample(search_space, random_subset_size)
if not any(p["name"] in ["eta", "min_child_weight"] for p in search_space):
search_space.append(SEARCH_SPACES[0])
return search_space
Expand All @@ -49,9 +49,9 @@ def _form_random_experiment_config(self, task):
experiment_params = _form_random_run_params(task)
is_classification = bool(task in ("binary", "multiclass"))
if is_classification:
metric_to_optimize = random.choice(CLASSIFICATION_METRIC_CHOICES)
metric_to_optimize = secrets.SystemRandom().choice(CLASSIFICATION_METRIC_CHOICES)
else:
metric_to_optimize = random.choice(REGRESSION_METRIC_CHOICES)
metric_to_optimize = secrets.SystemRandom().choice(REGRESSION_METRIC_CHOICES)
search_space = self._generate_randomized_search_space()

for param in search_space:
Expand All @@ -65,9 +65,9 @@ def _form_random_experiment_config(self, task):
"parameters": search_space,
"metrics": [{"name": metric_to_optimize, "strategy": "optimize", "objective": "maximize"}],
"parallel_bandwidth": 1,
"budget": random.randint(1, 3),
"budget": secrets.SystemRandom().randint(1, 3),
}
if random.randint(0, 1) == 0:
if secrets.SystemRandom().randint(0, 1) == 0:
del experiment_config["metrics"]

experiment_params["experiment_config"] = experiment_config
Expand Down
50 changes: 25 additions & 25 deletions integration_test/test_xgboost_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import math
import os
import platform
import random
import secrets
from inspect import signature

import pytest
Expand All @@ -32,20 +32,20 @@


POSSIBLE_PARAMETERS = {
"eta": 10 ** random.uniform(-4, 1),
"gamma": random.uniform(0, 4),
"max_depth": random.randint(1, 5),
"min_child_weight": random.uniform(0, 3),
"lambda": random.uniform(1, 3),
"alpha": 10 ** random.uniform(-4, 0),
"tree_method": random.choice(["hist", "exact", "approx", "auto"]),
"eta": 10 ** secrets.SystemRandom().uniform(-4, 1),
"gamma": secrets.SystemRandom().uniform(0, 4),
"max_depth": secrets.SystemRandom().randint(1, 5),
"min_child_weight": secrets.SystemRandom().uniform(0, 3),
"lambda": secrets.SystemRandom().uniform(1, 3),
"alpha": 10 ** secrets.SystemRandom().uniform(-4, 0),
"tree_method": secrets.SystemRandom().choice(["hist", "exact", "approx", "auto"]),
}


def _create_random_dataset(task="binary"):
if task == "binary":
n_samples = random.randint(180, 300)
n_features = random.randint(5, 25)
n_samples = secrets.SystemRandom().randint(180, 300)
n_features = secrets.SystemRandom().randint(5, 25)
n_classes = 2

return datasets.make_classification(
Expand All @@ -54,10 +54,10 @@ def _create_random_dataset(task="binary"):
n_classes=n_classes,
)
elif task == "multiclass":
n_samples = random.randint(180, 300)
n_classes = random.randint(3, 8)
n_informative = random.randint(2 * n_classes, 20)
n_features = random.randint(n_informative + 2, 40)
n_samples = secrets.SystemRandom().randint(180, 300)
n_classes = secrets.SystemRandom().randint(3, 8)
n_informative = secrets.SystemRandom().randint(2 * n_classes, 20)
n_features = secrets.SystemRandom().randint(n_informative + 2, 40)

return datasets.make_classification(
n_samples=n_samples,
Expand All @@ -66,9 +66,9 @@ def _create_random_dataset(task="binary"):
n_classes=n_classes,
)
else:
n_samples = random.randint(200, 500)
n_features = random.randint(50, 100)
n_informative = random.randint(10, n_features - 2)
n_samples = secrets.SystemRandom().randint(200, 500)
n_features = secrets.SystemRandom().randint(50, 100)
n_informative = secrets.SystemRandom().randint(10, n_features - 2)

return datasets.make_regression(
n_samples=n_samples,
Expand All @@ -81,17 +81,17 @@ def _create_random_dataset(task="binary"):
def _create_random_metric_objective(task="binary"):
if task == "binary":
return {
"objective": random.choice(["binary:logistic", "binary:hinge", "binary:logitraw"]),
"objective": secrets.SystemRandom().choice(["binary:logistic", "binary:hinge", "binary:logitraw"]),
"eval_metric": ["logloss", "aucpr", "error"],
}
elif task == "multiclass":
return {
"objective": random.choice(["multi:softmax", "multi:softprob"]),
"objective": secrets.SystemRandom().choice(["multi:softmax", "multi:softprob"]),
"eval_metric": ["mlogloss", "merror"],
}
else:
return {
"objective": random.choice(["reg:squarederror", "reg:pseudohubererror"]),
"objective": secrets.SystemRandom().choice(["reg:squarederror", "reg:pseudohubererror"]),
"eval_metric": ["rmse", "mae", "mape"],
}

Expand All @@ -103,8 +103,8 @@ def _form_random_run_params(task):
D_test = xgb.DMatrix(X_test, label=Y_test)

possible_params = POSSIBLE_PARAMETERS
random_subset_size = random.randint(1, len(possible_params))
subset_keys = random.sample(possible_params.keys(), random_subset_size)
random_subset_size = secrets.SystemRandom().randint(1, len(possible_params))
subset_keys = secrets.SystemRandom().sample(possible_params.keys(), random_subset_size)
subset_params = {k: possible_params[k] for k in subset_keys}
subset_params.update(_create_random_metric_objective(task))
if task == "multiclass":
Expand All @@ -115,9 +115,9 @@ def _form_random_run_params(task):
return dict(
params=subset_params,
dtrain=D_train,
evals=[(D_test, f"test{n}") for n in range(random.randint(1, 3))],
num_boost_round=random.randint(3, 15),
verbose_eval=random.choice([True, False]),
evals=[(D_test, f"test{n}") for n in range(secrets.SystemRandom().randint(1, 3))],
num_boost_round=secrets.SystemRandom().randint(3, 15),
verbose_eval=secrets.SystemRandom().choice([True, False]),
run_options=run_options,
)

Expand Down
4 changes: 2 additions & 2 deletions sigopt/orchestrate/common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright © 2022 Intel Corporation
#
# SPDX-License-Identifier: MIT
import random
import secrets
import sys
import time
from enum import Enum
Expand Down Expand Up @@ -44,7 +44,7 @@ def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
time.sleep(2**i + random.random()) # nosec
time.sleep(2**i + secrets.SystemRandom().random()) # nosec
if i == NUM_RETRIES:
raise e

Expand Down
5 changes: 3 additions & 2 deletions sigopt/orchestrate/docker/service.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Copyright © 2022 Intel Corporation
#
# SPDX-License-Identifier: MIT
import random
import re
import secrets
import string
import sys
from collections import namedtuple
Expand Down Expand Up @@ -124,7 +124,8 @@ def build(
dockerfile = dockerfile_name
try:
tag = tag or (
"sigopt-temp:" + "".join(random.choice(string.ascii_lowercase + string.digits) for _ in range(8)) # nosec
"sigopt-temp:"
+ "".join(secrets.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(8)) # nosec
)
if quiet:
self.client.images.build(
Expand Down
4 changes: 2 additions & 2 deletions sigopt/orchestrate/job_runner/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: MIT
import json
import os
import random
import secrets
import string

from ..docker.service import DockerService
Expand Down Expand Up @@ -51,7 +51,7 @@ def format_resources(self, resource_options):
limits["nvidia.com/gpu"] = resource_options.pop("gpus")

def random_id_string(self):
return "".join(random.choice(string.ascii_lowercase) for _ in range(8)) # nosec
return "".join(secrets.SystemRandom().choice(string.ascii_lowercase) for _ in range(8)) # nosec

def create_sigopt_experiment(self, optimization_options, project_id):
data = optimization_options.copy()
Expand Down
8 changes: 4 additions & 4 deletions sigopt/orchestrate/kubernetes/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import errno
import json
import os
import random
import secrets
import shutil
import tempfile
import time
Expand Down Expand Up @@ -230,7 +230,7 @@ def wait_until_nodes_are_ready(self, retries=20):
if try_number >= retries:
raise
else:
time.sleep(random.uniform(20, 40)) # nosec
time.sleep(secrets.SystemRandom().uniform(20, 40)) # nosec

def check_nodes_are_ready(self):
nodes = self.get_nodes().items
Expand Down Expand Up @@ -312,7 +312,7 @@ def create_docker_tls_certs(self):
ca_key.generate_key(crypto.TYPE_RSA, 4096)
ca_cert = crypto.X509()
ca_cert.get_subject().CN = "sigopt:docker ca"
ca_cert.set_serial_number(random.getrandbits(64))
ca_cert.set_serial_number(secrets.SystemRandom().getrandbits(64))
ca_cert.set_issuer(ca_cert.get_subject())
ca_cert.set_pubkey(ca_key)
ca_cert.gmtime_adj_notBefore(0)
Expand All @@ -334,7 +334,7 @@ def create_docker_tls_certs(self):
server_req.set_pubkey(server_key)
server_req.sign(ca_key, "sha256")
server_cert = crypto.X509()
server_cert.set_serial_number(random.getrandbits(64))
server_cert.set_serial_number(secrets.SystemRandom().getrandbits(64))
server_cert.gmtime_adj_notBefore(0)
server_cert.gmtime_adj_notAfter(ten_years)
server_cert.set_issuer(ca_cert.get_subject())
Expand Down
4 changes: 2 additions & 2 deletions sigopt/ratelimit.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright © 2022 Intel Corporation
#
# SPDX-License-Identifier: MIT
import random
import secrets
import threading
import time

Expand All @@ -18,7 +18,7 @@ def increment_and_check(self):
multiples_over = self.count // self.limit
if multiples_over:
quadratic_backoff = multiples_over**2
jitter = random.random() * 2
jitter = secrets.SystemRandom().random() * 2
time.sleep(quadratic_backoff + jitter)

def clear(self):
Expand Down