cpprb

Fast Flexible Replay Buffer Library (Mirror repository of https://gitlab.com/ymd_h/cpprb)

https://github.com/ymd-h/cpprb

Science Score: 67.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
    Found 3 DOI reference(s) in README
  • Academic publication links
    Links to: arxiv.org, sciencedirect.com, springer.com
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (12.3%) to scientific vocabulary

Keywords

cython machine-learning python3 reinforcement-learning
Last synced: 4 months ago · JSON representation ·

Repository

Fast Flexible Replay Buffer Library (Mirror repository of https://gitlab.com/ymd_h/cpprb)

Basic Info
  • Host: GitHub
  • Owner: ymd-h
  • License: mit
  • Language: Python
  • Default Branch: master
  • Homepage:
  • Size: 2.36 MB
Statistics
  • Stars: 72
  • Watchers: 5
  • Forks: 10
  • Open Issues: 0
  • Releases: 0
Topics
cython machine-learning python3 reinforcement-learning
Created over 6 years ago · Last pushed about 1 year ago
Metadata Files
Readme Changelog License Citation

README.org

#+OPTIONS: ':nil *:t -:t ::t <:t H:3 \n:nil ^:t arch:headline
#+OPTIONS: author:t broken-links:nil c:nil creator:nil
#+OPTIONS: d:(not "LOGBOOK") date:t e:t email:nil f:t inline:t num:t
#+OPTIONS: p:nil pri:nil prop:nil stat:t tags:t tasks:t tex:t
#+OPTIONS: timestamp:t title:t toc:nil todo:nil |:t
#+TITLE: cpprb
#+DATE: <2019-01-29 Tue>
#+AUTHOR: Hiroyuki Yamada
#+LANGUAGE: en
#+SELECT_TAGS: export
#+EXCLUDE_TAGS: noexport
#+CREATOR: Emacs 26.1 (Org mode 9.1.14)

#+HUGO_FRONT_MATTER_FORMAT: toml
#+HUGO_CUSTOM_FRONT_MATTER: :disableToc true
#+HUGO_LEVEL_OFFSET: 1
#+HUGO_PRESERVE_FILLING:
#+HUGO_DELETE_TRAILING_WS:
#+HUGO_SECTION: .
#+HUGO_BASE_DIR: ./site
#+HUGO_DATE_FORMAT: %Y-%m-%dT%T+09:00
#+HUGO_WEIGHT: auto

#+STARTUP: showall logdone

[[https://img.shields.io/gitlab/pipeline/ymd_h/cpprb.svg]]
[[https://img.shields.io/pypi/v/cpprb.svg]]
[[https://img.shields.io/pypi/l/cpprb.svg]]
[[https://img.shields.io/pypi/status/cpprb.svg]]
[[https://ymd_h.gitlab.io/cpprb/coverage/][https://gitlab.com/ymd_h/cpprb/badges/master/coverage.svg]]

[[https://pypi.org/project/cpprb/][https://img.shields.io/pypi/dd/cpprb.svg]]
[[https://pypi.org/project/cpprb/][https://img.shields.io/pypi/dw/cpprb.svg]]
[[https://pypi.org/project/cpprb/][https://img.shields.io/pypi/dm/cpprb.svg]]


#+attr_html: :width 100px
[[https://ymd_h.gitlab.io/cpprb/images/favicon.png]]

* Overview

cpprb is a python ([[https://github.com/python/cpython/tree/master/Python][CPython]]) module providing replay buffer classes for
reinforcement learning.

Major target users are researchers and library developers.

You can build your own reinforcement learning algorithms together with
your favorite deep learning library (e.g. [[https://www.tensorflow.org/][TensorFlow]], [[https://pytorch.org/][PyTorch]]).

cpprb forcuses speed, flexibility, and memory efficiency.

By utilizing [[https://cython.org/][Cython]], complicated calculations (e.g. segment tree for
prioritized experience replay) are offloaded onto C++.
(The name cpprb comes from "C++ Replay Buffer".)

In terms of API, initially cpprb referred to [[https://github.com/openai/baselines][OpenAI Baselines]]'
implementation. The current version of cpprb has much more
flexibility. Any [[https://numpy.org/][NumPy]] compatible types of any numbers of values can
be stored (as long as memory capacity is sufficient). For example, you
can store the next action and the next next observation, too.


* DONE Installation
CLOSED: [2020-01-17 Fri 23:08]
:PROPERTIES:
:EXPORT_HUGO_SECTION: installation
:EXPORT_FILE_NAME: _index
:EXPORT_HUGO_WEIGHT: 100
:END:

cpprb requires following softwares before installation.

- C++17 compiler (for installation from source)
  - [[https://gcc.gnu.org/][GCC]] (maybe 7.2 and newer)
  - [[https://visualstudio.microsoft.com/][Visual Studio]] (2017 Enterprise is fine)
- Python 3
- pip

Additionally, here are user's good feedbacks for installation at [[https://gitlab.com/ymd_h/cpprb/issues/73][Ubuntu]].
(Thanks!)


** Install from [[https://pypi.org/][PyPI]] (Recommended)

The following command installs cpprb together with other dependencies.

#+BEGIN_SRC shell
pip install cpprb
#+END_SRC

Depending on your environment, you might need =sudo= or =--user= flag
for installation.

On supported platflorms (Linux x86-64, Windows amd64, and macOS
x86_64), binary packages hosted on PyPI can be used, so that you don't
need C++ compiler. On the other platforms, such as 32bit or
arm-architectured Linux and Windows, you cannot install from binary,
and you need to compile by yourself. Please be patient, we plan to
support wider platforms in future.


If you have any troubles to install from binary, you can fall back to
source installation by passing ~--no-binary~ option to the above pip
command. (In order to avoid NumPy source installation, it is better to
install NumPy beforehand.)

#+begin_src shell
pip install numpy
pip install --no-binary cpprb
#+end_src

** Install from source code

First, download source code manually or clone the repository;

#+begin_src shell
git clone https://gitlab.com/ymd_h/cpprb.git
#+end_src

Then you can install in the same way;

#+begin_src shell
cd cpprb
pip install .
#+end_src


For this installation, you need to convert extended Python (.pyx) to
C++ (.cpp) during installation, it takes longer time than installation
from PyPI.


* DONE Usage
CLOSED: [2020-01-17 Fri 23:09]
:PROPERTIES:
:EXPORT_HUGO_SECTION: features
:EXPORT_FILE_NAME: simple_usage
:END:

** Basic Usage
Basic usage is following step;
1. Create replay buffer (~ReplayBuffer.__init__~)
2. Add transitions (~ReplayBuffer.add~)
   1. Reset at episode end (~ReplayBuffer.on_episode_end~)
3. Sample transitions (~ReplayBuffer.sample~)


** Example Code
Here is a simple example for storing standard environment (aka. ~obs~,
~act~, ~rew~, ~next_obs~, and ~done~).

#+BEGIN_SRC python
from cpprb import ReplayBuffer

buffer_size = 256
obs_shape = 3
act_dim = 1
rb = ReplayBuffer(buffer_size,
                  env_dict ={"obs": {"shape": obs_shape},
                             "act": {"shape": act_dim},
                             "rew": {},
                             "next_obs": {"shape": obs_shape},
                             "done": {}})

obs = np.ones(shape=(obs_shape))
act = np.ones(shape=(act_dim))
rew = 0
next_obs = np.ones(shape=(obs_shape))
done = 0

for i in range(500):
    rb.add(obs=obs,act=act,rew=rew,next_obs=next_obs,done=done)

    if done:
        # Together with resetting environment, call ReplayBuffer.on_episode_end()
        rb.on_episode_end()

batch_size = 32
sample = rb.sample(batch_size)
# sample is a dictionary whose keys are 'obs', 'act', 'rew', 'next_obs', and 'done'
#+END_SRC

** Construction Parameters
(See also [[https://ymd_h.gitlab.io/cpprb/api/api/cpprb.ReplayBuffer.html][API reference]])

| Name             | Type                         | Optional           | Discription                       |
|------------------+------------------------------+--------------------+-----------------------------------|
| ~size~           | ~int~                        | No                 | Buffer size                       |
| ~env_dict~       | ~dict~                       | Yes (but unusable) | Environment definition (See [[https://ymd_h.gitlab.io/cpprb/features/flexible_environment/][here]]) |
| ~next_of~        | ~str~ or array-like of ~str~ | Yes                | Memory compression (See [[https://ymd_h.gitlab.io/cpprb/features/memory_compression/][here]])     |
| ~stack_compress~ | ~str~ or array-like of ~str~ | Yes                | Memory compression (See [[https://ymd_h.gitlab.io/cpprb/features/memory_compression/][here]])     |
| ~default_dtype~  | ~numpy.dtype~                | Yes                | Fall back data type               |
| ~Nstep~          | ~dict~                       | Yes                | Nstep configuration (See [[https://ymd_h.gitlab.io/cpprb/features/nstep/][here]])    |
| ~mmap_prefix~    | ~str~                        | Yes                | mmap file prefix (See [[https://ymd_h.gitlab.io/cpprb/features/mmap/][here]])       |


** Notes
Flexible environment values are defined by =env_dict= when buffer
creation. The detail is described at [[https://ymd_h.gitlab.io/cpprb/features/flexible_environment/][document]].

Since stored values have flexible name, you have to pass to
=ReplayBuffer.add= member by keyword.


* DONE Features
CLOSED: [2020-01-17 Fri 23:09]
:PROPERTIES:
:EXPORT_HUGO_SECTION: features
:EXPORT_FILE_NAME: _index
:EXPORT_HUGO_WEIGHT: 200
:END:

cpprb provides buffer classes for building following algorithms.

| Algorithms                            | cpprb class                                  | Paper                  |
|---------------------------------------+----------------------------------------------+------------------------|
| Experience Replay                     | =ReplayBuffer=                               | [[https://link.springer.com/article/10.1007/BF00992699][L. J. Lin]]              |
| [[https://ymd_h.gitlab.io/cpprb/features/per/][Prioritized Experience Replay]]         | =PrioritizedReplayBuffer=                    | [[https://arxiv.org/abs/1511.05952][T. Schaul et. al.]]      |
| [[https://ymd_h.gitlab.io/cpprb/features/nstep/][Multi-step (Nstep) Learning]]           | =ReplayBuffer=, =PrioritizedReplayBuffer=    |                        |
| [[https://ymd_h.gitlab.io/cpprb/features/ape-x/][Multiprocess Learning (Ape-X)]]         | =MPReplayBuffer= =MPPrioritizedReplayBuffer= | [[https://arxiv.org/abs/1803.00933][D. Horgan et. al.]]      |
| [[https://ymd_h.gitlab.io/cpprb/features/laber/][Large Batch Experience Replay (LaBER)]] | =LaBERmean=, =LaBERlazy=, =LaBERmax=         | [[https://dblp.org/db/journals/corr/corr2110.html#journals/corr/abs-2110-01528][T. Lahire et al.]]       |
| [[https://ymd_h.gitlab.io/cpprb/features/rer/][Reverse Experience Replay (RER)]]       | =ReverseReplayBuffer=                        | [[https://arxiv.org/abs/1910.08780][E. Rotinov]]             |
| [[https://ymd_h.gitlab.io/cpprb/features/her/][Hindsight Experience Replay (HER)]]     | =HindsightReplayBuffer=                      | [[https://arxiv.org/abs/1707.01495][M. Andrychowicz et al.]] |




cpprb features and its usage are described at following pages:
- [[https://ymd_h.gitlab.io/cpprb/features/flexible_environment/][Flexible Environment]]
- [[https://ymd_h.gitlab.io/cpprb/features/multistep_add/][Multi-step add]]
- [[https://ymd_h.gitlab.io/cpprb/features/per/][Prioritized Experience Replay]]
- [[https://ymd_h.gitlab.io/cpprb/features/nstep/][Nstep Experience Replay]]
- [[https://ymd_h.gitlab.io/cpprb/features/memory_compression/][Memory Compression]]
- [[https://ymd_h.gitlab.io/cpprb/features/mmap/][Map Large Data on File]]
- [[https://ymd_h.gitlab.io/cpprb/features/ape-x/][Multiprocess Learning (Ape-X)]]
- [[https://ymd_h.gitlab.io/cpprb/features/save_load_transitions/][Save/Load Transitions]]

* DONE Design
CLOSED: [2021-09-16 Thu 19:09]
:PROPERTIES:
:EXPORT_FILE_NAME: _index
:EXPORT_HUGO_SECTION: design
:EXPORT_HUGO_WEIGHT: 250
:END:

** Column-oriented and Flexible
One of the most distinctive design of cpprb is column-oriented
flexibly defined transitions. As far as we know, other replay buffer
implementations adopt row-oriented flexible transitions (aka. array of
transition class) or column-oriented non-flexible transitions.

In deep reinforcement learning, sampled batch is divided into
variables (i.e. =obs=, =act=, etc.). If the sampled batch is
row-oriented, users (or library) need to convert it into
column-oriented one. (See [[https://ymd_h.gitlab.io/cpprb/features/flexible_environment/][doc]], too)

** Batch Insertion
cpprb can accept addition of multiple transitions simultaneously. This
design is convenient when batch transitions are moved from local
buffers to a global buffer. Moreover it is more efficient because of
not only removing pure-Python =for= loop but also suppressing
unnecessary priority updates for PER. (See [[https://ymd_h.gitlab.io/cpprb/features/multistep_add/][doc]], too)

** Minimum Dependency
We try to minimize dependency. Only NumPy is required during its
execution. Small dependency is always preferable to avoid dependency
hell.


* DONE Contributing to cpprb
CLOSED: [2020-01-17 Fri 23:09]
:PROPERTIES:
:EXPORT_HUGO_SECTION: contributing
:EXPORT_FILE_NAME: _index
:EXPORT_HUGO_WEIGHT: 300
:END:

Any contribution are very welcome!

** Making Community Larger
Bigger commumity makes development more active and improve cpprb.

- Star [[https://gitlab.com/ymd_h/cpprb][GitLab repository]] (and/or [[https://github.com/ymd-h/cpprb][GitHub Mirror]])
- Publish your code using cpprb
- Share this repository to your friend and/or followers.


** Q & A at Forum
When you have any problems or requests, you can check [[https://github.com/ymd-h/cpprb/discussions][Discussions on
GitHub.com]]. If you still cannot find any information, you can post
your own.

We keep [[https://gitlab.com/ymd_h/cpprb/issues][issues on GitLab.com]] and users are still allowed to open
issues, however, we mainly use the place as development issue tracker.

** Merge Request (Pull Request)

cpprb follows local rules:
- Branch Name
  - "HotFix_***" for bug fix
  - "Feature_***" for new feature implementation
- docstring
  - Must for external API
  - [[https://numpydoc.readthedocs.io/en/latest/format.html][Numpy Style]]
- Unit Test
  - Put test code under "test/" directory
  - Can test by =python -m unittest = command
  - Continuous Integration on GitLab CI configured by ~.gitlab-ci.yaml~
- Open an issue and associate it to Merge Request

Step by step instruction for beginners is described at [[https://ymd_h.gitlab.io/cpprb/contributing/merge_request][here]].

* DONE Links
CLOSED: [2020-01-17 Fri 23:09]
:PROPERTIES:
:EXPORT_HUGO_SECTION: misc
:EXPORT_FILE_NAME: links
:END:

** cpprb sites
- [[https://ymd_h.gitlab.io/cpprb/][Project Site]]
  - [[https://ymd_h.gitlab.io/cpprb/api/][Class Reference]]
  - [[https://ymd_h.gitlab.io/cpprb/coverage/][Unit Test Coverage]]
- [[https://gitlab.com/ymd_h/cpprb][Main Repository]]
- [[https://github.com/ymd-h/cpprb][GitHub Mirror]]
- [[https://pypi.org/project/cpprb/][cpprb on PyPI]]

** cpprb users' repositories
- [[https://github.com/keiohta/tf2rl][keiohta/TF2RL]] :: TensorFlow2.x Reinforcement Learning

** Example usage at Kaggle competition
- [[https://www.kaggle.com/ymdhryk/ape-x-dqn-lap-safeguard-rewardredesign][Ape-X DQN-LAP: SafeGuard & RewardRedesign]] | [[https://www.kaggle.com/c/hungry-geese][Hungry Geese]]


** Japanese Documents
- [[https://qiita.com/ymd_h/items/505c607c40cf3e42d080][【強化学習】cpprb で Experience Replay を簡単に!| Qiita]]
- [[https://qiita.com/ymd_h/items/ac9e3f1315d56a1b2718][【強化学習】Ape-X の高速な実装を簡単に!| Qiita]]
- [[https://qiita.com/ymd_h/items/21071d7778cfb3cd596a][【強化学習】自作ライブラリでDQN | Qiita]]
- [[https://zenn.dev/ymd_h/articles/03edcaa47a3b1c][【強化学習】Ape-Xの高速化を実現 | Zenn]]
- [[https://zenn.dev/ymd_h/articles/e65fed3b7991c9][【強化学習】cpprb に遷移のファイル保存機能を追加 | Zenn]]

* DONE License
CLOSED: [2020-01-17 Fri 23:09]
:PROPERTIES:
:EXPORT_HUGO_SECTION: misc
:EXPORT_FILE_NAME: license
:END:
cpprb is available under MIT license.

#+INCLUDE: "./LICENSE" src plaintext


* DONE Citation
CLOSED: [2021-09-09 Thu 18:05]
:PROPERTIES:
:EXPORT_HUGO_SECTION: misc
:EXPORT_FILE_NAME: citation
:END:

We would be very happy if you cite cpprb in your papers.

#+begin_src bibtex
@misc{Yamada_cpprb_2019,
author = {Yamada, Hiroyuki},
month = {1},
title = {{cpprb}},
url = {https://gitlab.com/ymd_h/cpprb},
year = {2019}
}
#+end_src


- 3rd Party Papers citing cpprb
  - [[https://www.sciencedirect.com/science/article/pii/S187705092100990X][E. Aitygulov and A. I. Panov, "Transfer Learning with Demonstration Forgetting for Robotic Manipulator", Proc. Comp. Sci. 186 (2021), 374-380, https://doi.org/10.1016/j.procs.2021.04.159]]
  - [[https://nips.cc/Conferences/2021/Schedule?showEvent=21848][T. Kitamura and R. Yonetani, "ShinRL: A Library for Evaluating RL Algorithms from Theoretical and Practical Perspectives", NeurIPS Deep RL Workshop (2021)]] ([[https://arxiv.org/abs/2112.04123][arXiv]], [[https://github.com/omron-sinicx/ShinRL][code]])

Owner

  • Name: H.Yamada
  • Login: ymd-h
  • Kind: user
  • Location: Japan

Citation (CITATION.cff)

cff-version: 1.2.0
message: "If you use this software, please cite it as below."
authors:
  - family-names: Yamada
    given-names: Hiroyuki
title: "cpprb"
license: MIT
repository-code: "https://gitlab.com/ymd_h/cpprb"
type: software
url: "https://ymd_h.gitlab.io/cpprb/"
date-released: "2019-01-12"

GitHub Events

Total
  • Watch event: 1
  • Delete event: 4
  • Push event: 29
  • Create event: 4
Last Year
  • Watch event: 1
  • Delete event: 4
  • Push event: 29
  • Create event: 4

Committers

Last synced: 8 months ago

All Time
  • Total Commits: 3,890
  • Total Committers: 6
  • Avg Commits per committer: 648.333
  • Development Distribution Score (DDS): 0.358
Past Year
  • Commits: 116
  • Committers: 1
  • Avg Commits per committer: 116.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Yamada 2,498
Yamada y****h@o****p 1,383
R107333 j****h@r****m 5
Kei Ohta O****i@d****p 2
ohtake_i d****i@g****m 1
Yamada Hiroyuki 3****_@u****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 0
  • Total pull requests: 2
  • Average time to close issues: N/A
  • Average time to close pull requests: 4 days
  • Total issue authors: 0
  • Total pull request authors: 1
  • Average comments per issue: 0
  • Average comments per pull request: 7.5
  • Merged pull requests: 2
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
  • jamartinh (2)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 3,676 last-month
  • Total docker downloads: 78
  • Total dependent packages: 0
  • Total dependent repositories: 21
  • Total versions: 108
  • Total maintainers: 1
pypi.org: cpprb

ReplayBuffer for Reinforcement Learning written by C++ and Cython

  • Versions: 108
  • Dependent Packages: 0
  • Dependent Repositories: 21
  • Downloads: 3,676 Last month
  • Docker Downloads: 78
Rankings
Docker downloads count: 3.1%
Dependent repos count: 3.2%
Downloads: 5.3%
Average: 9.6%
Dependent packages count: 10.1%
Stargazers count: 16.6%
Forks count: 19.2%
Maintainers (1)
Last synced: 5 months ago

Dependencies

.github/workflows/build.yml actions
  • actions/cache v3 composite
  • actions/checkout v3 composite
  • actions/checkout master composite
  • actions/download-artifact v3 composite
  • actions/setup-python v4 composite
  • actions/upload-artifact v3 composite
  • docker/build-push-action v4 composite
  • docker/setup-buildx-action v2 composite
  • docker/setup-qemu-action v2 composite
Dockerfile docker
  • python 3.8 build
pyproject.toml pypi
setup.py pypi