Coder Social home page Coder Social logo

bshillingford / python-sharearray Goto Github PK

View Code? Open in Web Editor NEW
46.0 3.0 2.0 16 KB

Share numpy arrays across processes efficiently ideal for large, read-only datasets

License: Apache License 2.0

Python 100.00%
python numpy mmap pytorch machine-learning deep-learning

python-sharearray's Introduction

sharearray

Have you worried about creating large identical numpy arrays across processes due to RAM wastage, e.g. datasets that are big enough to fit in RAM but large enough to cause concern when running multiple jobs using the same data? sharearray efficiently caches numpy arrays in RAM (using shared memory in /dev/shm, no root needed) locally on a machine.

Usage is simple, using the cache function or decorator decorator. A first call saves the result of the call into the built-in RAM disk, and returns a read-only memory-mapped view into it. Since it's in RAM, there's no performance penalty. Any subsequent calls with the same ID will return an identical read-only memory mapped view, even across processes. The IDs are global.

Installation:

pip install git+https://github.com/bshillingford/python-sharearray

or

git clone https://github.com/bshillingford/python-sharearray
python setup.py install

Usage

Using decorator:

@sharearray.decorator('some_unique_id', verbose=False)
def get_training_data():
    # create largeish / expensive-to-generate data
    return my_array # some instance of np.ndarray

# first call, across all processes, creates the array
arr_view = get_training_data()

# all further calls are cached/memoized: we return a view into memory
arr_view_2 = get_training_data()

Using the cache function:

import sharearray
import numpy as np
arr = sharearray.cache('my_global_id', lambda: create_large_array())
# or:
arr = sharearray.cache('my_global_id', lambda: create_large_array())

where, for instance, create_large_array returns a large training set, potentially performing expensive feature transformations or data augmentations first.

By default, the file is at /dev/shm/sharearray_my_global_id.npy, and to avoid concurrency issues when first generating the array, and to avoid duplicated computation,

For futher details, read the docstrings. You may be interested in the timeout, verbose, and log_func arguments (to either cache or decorator).

PyTorch

Since PyTorch does not yet support memmapped files (at time of writing), we can instead just create torch Tensors that point to the memory mapped by numpy:

data_numpy = get_training_data()          # numpy.ndarray
data_torch = torch.from_numpy(data_numpy) # torch.Tensor

Notes

TODO: support returning multiple arrays (e.g. as a tuple or dict) from the callback / decorated function

There exist similar libraries in Python already, but this just makes it easier to do as a memoization-style API. Also, this module is a single file, and does not write anything in C.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.