Files
NexQuant/test/utils/test_env.py
T

76 lines
3.3 KiB
Python
Raw Normal View History

2024-07-03 04:54:24 +08:00
import os
2024-07-05 17:42:00 +08:00
import sys
2024-07-03 04:54:24 +08:00
import unittest
from pathlib import Path
2024-07-15 08:31:27 +00:00
2024-07-03 04:54:24 +08:00
sys.path.append(str(Path(__file__).resolve().parent.parent))
import shutil
2024-09-21 21:31:56 +08:00
from rdagent.utils.env import LocalConf, LocalEnv, QlibDockerConf, QTDockerEnv
2024-07-03 04:54:24 +08:00
DIRNAME = Path(__file__).absolute().resolve().parent
class EnvUtils(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
# NOTE: For a docker file, the output are generated with root permission.
2024-07-05 17:42:00 +08:00
# mlrun_p = DIRNAME / "env_tpl" / "mlruns"
2024-07-03 04:54:24 +08:00
# if mlrun_p.exists():
# shutil.rmtree(mlrun_p)
...
2024-07-09 12:45:32 +08:00
# NOTE: Since I don't know the exact environment in which it will be used, here's just an example.
# NOTE: Because you need to download the data during the prepare process. So you need to have pyqlib in your environment.
2024-07-11 08:49:37 +00:00
def test_local(self):
local_conf = LocalConf(
py_bin="/home/v-linlanglv/miniconda3/envs/RD-Agent-310/bin",
default_entry="qrun conf.yaml",
)
qle = LocalEnv(conf=local_conf)
qle.prepare()
2024-07-15 08:31:27 +00:00
conf_path = str(DIRNAME / "env_tpl" / "conf.yaml")
2024-07-11 08:49:37 +00:00
qle.run(entry="qrun " + conf_path)
2024-07-15 08:31:27 +00:00
mlrun_p = DIRNAME / "env_tpl" / "mlruns"
2024-07-11 08:49:37 +00:00
self.assertTrue(mlrun_p.exists(), f"Expected output file {mlrun_p} not found")
2024-07-09 12:45:32 +08:00
2024-07-03 04:54:24 +08:00
def test_docker(self):
2024-09-21 21:31:56 +08:00
"""We will mount `env_tpl` into the docker image.
2024-07-03 04:54:24 +08:00
And run the docker image with `qrun conf.yaml`
"""
qtde = QTDockerEnv()
qtde.prepare() # you can prepare for multiple times. It is expected to handle it correctly
2024-07-19 16:20:07 +08:00
# qtde.run("nvidia-smi") # NOTE: you can check your GPU with this command
2024-07-03 04:54:24 +08:00
# the stdout are returned as result
result = qtde.run(local_path=str(DIRNAME / "env_tpl"), entry="qrun conf.yaml")
2024-07-15 08:31:27 +00:00
mlrun_p = DIRNAME / "env_tpl" / "mlruns"
2024-07-03 04:54:24 +08:00
self.assertTrue(mlrun_p.exists(), f"Expected output file {mlrun_p} not found")
2024-07-09 20:49:51 +08:00
# read experiment
2024-07-11 08:49:37 +00:00
result = qtde.run(local_path=str(DIRNAME / "env_tpl"), entry="python read_exp_res.py")
2024-07-09 20:49:51 +08:00
print(result)
2024-09-21 21:31:56 +08:00
def test_docker_mem(self):
cmd = 'python -c \'print("start"); import numpy as np; size_mb = 500; size = size_mb * 1024 * 1024 // 8; array = np.random.randn(size).astype(np.float64); print("success")\''
qtde = QTDockerEnv(QlibDockerConf(mem_limit="10m"))
qtde.prepare()
result = qtde.run(local_path=str(DIRNAME / "env_tpl"), entry=cmd)
self.assertTrue(not result.strip().endswith("success"))
qtde = QTDockerEnv(QlibDockerConf(mem_limit="1g"))
qtde.prepare()
result = qtde.run(local_path=str(DIRNAME / "env_tpl"), entry=cmd)
self.assertTrue(result.strip().endswith("success"))
# The above command equals to the follow commands with dockr cli.sh
# docker run --memory=10m -it --rm local_qlib:latest python -c 'import numpy as np; print(123); size_mb = 1; size = size_mb * 1024 * 1024 // 8; array = np.random.randn(size).astype(np.float64); array[0], array[-1] = 1.0, 1.0; print(321)'
# docker run --memory=10g -it --rm local_qlib:latest python -c 'import numpy as np; print(123); size_mb = 1; size = size_mb * 1024 * 1024 // 8; array = np.random.randn(size).astype(np.float64); array[0], array[-1] = 1.0, 1.0; print(321)'
2024-07-03 04:54:24 +08:00
if __name__ == "__main__":
unittest.main()