Skip to content

Commit

Permalink
Merge pull request #290 from you-n-g/online_srv
Browse files Browse the repository at this point in the history
init version of online serving and rolling
  • Loading branch information
you-n-g authored May 17, 2021
2 parents 142a9dc + 8c3a08b commit d08146c
Show file tree
Hide file tree
Showing 39 changed files with 3,903 additions and 125 deletions.
Binary file added docs/_static/img/online_serving.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docs/advanced/serial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Serializable Class

``Qlib`` provides a base class ``qlib.utils.serial.Serializable``, whose state can be dumped into or loaded from disk in `pickle` format.
When users dump the state of a ``Serializable`` instance, the attributes of the instance whose name **does not** start with `_` will be saved on the disk.
However, users can use ``config`` method or override ``default_dump_all`` attribute to prevent this feature.

Users can also override ``pickle_backend`` attribute to choose a pickle backend. The supported value is "pickle" (default and common) and "dill" (dump more things such as function, more information in `here <https://pypi.org/project/dill/>`_).

Example
==========================
Expand Down
89 changes: 89 additions & 0 deletions docs/advanced/task_management.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
.. _task_management:

=================================
Task Management
=================================
.. currentmodule:: qlib


Introduction
=============

The `Workflow <../component/introduction.html>`_ part introduces how to run research workflow in a loosely-coupled way. But it can only execute one ``task`` when you use ``qrun``.
To automatically generate and execute different tasks, ``Task Management`` provides a whole process including `Task Generating`_, `Task Storing`_, `Task Training`_ and `Task Collecting`_.
With this module, users can run their ``task`` automatically at different periods, in different losses, or even by different models.

This whole process can be used in `Online Serving <../component/online.html>`_.

An example of the entire process is shown `here <https://github.com/microsoft/qlib/tree/main/examples/model_rolling/task_manager_rolling.py>`_.

Task Generating
===============
A ``task`` consists of `Model`, `Dataset`, `Record`, or anything added by users.
The specific task template can be viewed in
`Task Section <../component/workflow.html#task-section>`_.
Even though the task template is fixed, users can customize their ``TaskGen`` to generate different ``task`` by task template.

Here is the base class of ``TaskGen``:

.. autoclass:: qlib.workflow.task.gen.TaskGen
:members:

``Qlib`` provides a class `RollingGen <https://github.com/microsoft/qlib/tree/main/qlib/workflow/task/gen.py>`_ to generate a list of ``task`` of the dataset in different date segments.
This class allows users to verify the effect of data from different periods on the model in one experiment. More information is `here <../reference/api.html#TaskGen>`_.

Task Storing
===============
To achieve higher efficiency and the possibility of cluster operation, ``Task Manager`` will store all tasks in `MongoDB <https://www.mongodb.com/>`_.
``TaskManager`` can fetch undone tasks automatically and manage the lifecycle of a set of tasks with error handling.
Users **MUST** finish the configuration of `MongoDB <https://www.mongodb.com/>`_ when using this module.

Users need to provide the MongoDB URL and database name for using ``TaskManager`` in `initialization <../start/initialization.html#Parameters>`_ or make a statement like this.

.. code-block:: python
from qlib.config import C
C["mongo"] = {
"task_url" : "mongodb://localhost:27017/", # your MongoDB url
"task_db_name" : "rolling_db" # database name
}
.. autoclass:: qlib.workflow.task.manage.TaskManager
:members:

More information of ``Task Manager`` can be found in `here <../reference/api.html#TaskManager>`_.

Task Training
===============
After generating and storing those ``task``, it's time to run the ``task`` which is in the *WAITING* status.
``Qlib`` provides a method called ``run_task`` to run those ``task`` in task pool, however, users can also customize how tasks are executed.
An easy way to get the ``task_func`` is using ``qlib.model.trainer.task_train`` directly.
It will run the whole workflow defined by ``task``, which includes *Model*, *Dataset*, *Record*.

.. autofunction:: qlib.workflow.task.manage.run_task

Meanwhile, ``Qlib`` provides a module called ``Trainer``.

.. autoclass:: qlib.model.trainer.Trainer
:members:

``Trainer`` will train a list of tasks and return a list of model recorders.
``Qlib`` offer two kinds of Trainer, TrainerR is the simplest way and TrainerRM is based on TaskManager to help manager tasks lifecycle automatically.
If you do not want to use ``Task Manager`` to manage tasks, then use TrainerR to train a list of tasks generated by ``TaskGen`` is enough.
`Here <../reference/api.html#Trainer>`_ are the details about different ``Trainer``.

Task Collecting
===============
To collect the results of ``task`` after training, ``Qlib`` provides `Collector <../reference/api.html#Collector>`_, `Group <../reference/api.html#Group>`_ and `Ensemble <../reference/api.html#Ensemble>`_ to collect the results in a readable, expandable and loosely-coupled way.

`Collector <../reference/api.html#Collector>`_ can collect objects from everywhere and process them such as merging, grouping, averaging and so on. It has 2 step action including ``collect`` (collect anything in a dict) and ``process_collect`` (process collected dict).

`Group <../reference/api.html#Group>`_ also has 2 steps including ``group`` (can group a set of object based on `group_func` and change them to a dict) and ``reduce`` (can make a dict become an ensemble based on some rule).
For example: {(A,B,C1): object, (A,B,C2): object} ---``group``---> {(A,B): {C1: object, C2: object}} ---``reduce``---> {(A,B): object}

`Ensemble <../reference/api.html#Ensemble>`_ can merge the objects in an ensemble.
For example: {C1: object, C2: object} ---``Ensemble``---> object

So the hierarchy is ``Collector``'s second step corresponds to ``Group``. And ``Group``'s second step correspond to ``Ensemble``.

For more information, please see `Collector <../reference/api.html#Collector>`_, `Group <../reference/api.html#Group>`_ and `Ensemble <../reference/api.html#Ensemble>`_, or the `example <https://github.com/microsoft/qlib/tree/main/examples/model_rolling/task_manager_rolling.py>`_.
46 changes: 46 additions & 0 deletions docs/component/online.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
.. _online:

=================================
Online Serving
=================================
.. currentmodule:: qlib


Introduction
=============

.. image:: ../_static/img/online_serving.png
:align: center


In addition to backtesting, one way to test a model is effective is to make predictions in real market conditions or even do real trading based on those predictions.
``Online Serving`` is a set of modules for online models using the latest data,
which including `Online Manager <#Online Manager>`_, `Online Strategy <#Online Strategy>`_, `Online Tool <#Online Tool>`_, `Updater <#Updater>`_.

`Here <https://github.com/microsoft/qlib/tree/main/examples/online_srv>`_ are several examples for reference, which demonstrate different features of ``Online Serving``.
If you have many models or `task` needs to be managed, please consider `Task Management <../advanced/task_management.html>`_.
The `examples <https://github.com/microsoft/qlib/tree/main/examples/online_srv>`_ are based on some components in `Task Management <../advanced/task_management.html>`_ such as ``TrainerRM`` or ``Collector``.

Online Manager
=============

.. automodule:: qlib.workflow.online.manager
:members:

Online Strategy
=============

.. automodule:: qlib.workflow.online.strategy
:members:

Online Tool
=============

.. automodule:: qlib.workflow.online.utils
:members:

Updater
=============

.. automodule:: qlib.workflow.online.update
:members:
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Document Structure
Intraday Trading: Model&Strategy Testing <component/backtest.rst>
Qlib Recorder: Experiment Management <component/recorder.rst>
Analysis: Evaluation & Results Analysis <component/report.rst>
Online Serving: Online Management & Strategy & Tool <component/online.rst>

.. toctree::
:maxdepth: 3
Expand All @@ -50,6 +51,7 @@ Document Structure
Building Formulaic Alphas <advanced/alpha.rst>
Online & Offline mode <advanced/server.rst>
Serialization <advanced/serial.rst>
Task Management <advanced/task_management.rst>

.. toctree::
:maxdepth: 3
Expand Down
69 changes: 68 additions & 1 deletion docs/reference/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,70 @@ Record Template
.. automodule:: qlib.workflow.record_temp
:members:

Task Management
====================


TaskGen
--------------------
.. automodule:: qlib.workflow.task.gen
:members:

TaskManager
--------------------
.. automodule:: qlib.workflow.task.manage
:members:

Trainer
--------------------
.. automodule:: qlib.model.trainer
:members:

Collector
--------------------
.. automodule:: qlib.workflow.task.collect
:members:

Group
--------------------
.. automodule:: qlib.model.ens.group
:members:

Ensemble
--------------------
.. automodule:: qlib.model.ens.ensemble
:members:

Utils
--------------------
.. automodule:: qlib.workflow.task.utils
:members:


Online Serving
====================


Online Manager
--------------------
.. automodule:: qlib.workflow.online.manager
:members:

Online Strategy
--------------------
.. automodule:: qlib.workflow.online.strategy
:members:

Online Tool
--------------------
.. automodule:: qlib.workflow.online.utils
:members:

RecordUpdater
--------------------
.. automodule:: qlib.workflow.online.update
:members:


Utils
====================
Expand All @@ -162,4 +226,7 @@ Serializable
--------------------

.. automodule:: qlib.utils.serial.Serializable
:members:
:members:



11 changes: 11 additions & 0 deletions docs/start/initialization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,14 @@ Besides `provider_uri` and `region`, `qlib.init` has other parameters. The follo
"default_exp_name": "Experiment",
}
})
- `mongo`
Type: dict, optional parameter, the setting of `MongoDB <https://www.mongodb.com/>`_ which will be used in some features such as `Task Management <../advanced/task_management.html>`_, with high performance and clustered processing.
Users need finished `installation <https://www.mongodb.com/try/download/community>`_ firstly, and run it in a fixed URL.

.. code-block:: Python
# For example, you can initialize qlib below
qlib.init(provider_uri=provider_uri, region=REG_CN, mongo={
"task_url": "mongodb://localhost:27017/", # your mongo url
"task_db_name": "rolling_db", # the database name of Task Management
})
Loading

0 comments on commit d08146c

Please sign in to comment.