batch caching decorator
Project description
batche
batche
provides a Python decorator that caches the results of a batch function. It helps in reducing computation by caching the outputs for previously processed input batches. The cache is maintained using a dictionary or an LRU (Least Recently Used) Cache, based on the maxsize parameter.
Installation
pip install batche
Usage
To use this decorator, simply import it and apply it to a function that takes a batch of items (list) as input and returns a list of predictions. The decorator will cache the results of the function for previously processed items.
from batche import cache_batch_variable
# batch_variable_name (str): The name of the argument in your batch function that holds the input batch list. This is a required parameter.
# maxsize (int, optional): The maximum size of the cache (uses an LRU Cache).
# If not provided, the cache used will be a dict.
@cache_batch_variable(batch_variable_name="batch_items", maxsize=100)
def batch_function(batch_items: List[HashableType]) -> List[Any]:
# Your implementation here
return expensive_operation(batch_items)
Example
Here is a complete example using the cache_batch_variable decorator:
from cache_batch_decorator import cache_batch_variable
@cache_batch_variable(batch_variable_name="batch_items", maxsize=100)
def batch_function(batch_items: List[str]) -> List[str]:
# Simulating some heavy computation
result = [item.upper() for item in batch_items]
return result
# Test the batch_function
input_batch = ["hello", "world"]
output_batch = batch_function(input_batch)
print(output_batch)
> ['HELLO', 'WORLD']
# Calling the function again with overlapping examples
# The results for the overlapping examples will be fetched from the cache
# The results for the new examples will be computed
input_batch = ["hello", "again",]
output_batch = batch_function(input_batch)
print(output_batch)
> ['HELLO', 'AGAIN']
When calling the function with the same input batch again, the results will be fetched from the cache, improving the performance. Cache Types
By default, the cache size is unlimited (uses a dictionary). If you want to limit the cache size, you can use the maxsize
parameter. This will create an LRU Cache with the specified size:
Important Notes
-
The decorator checks for correct annotations of the batch function (input and return types should be a list). If annotations are not provided, it skips the validation.
-
The input batch variable must contain hashable objects.
-
The batch function must return a list of predictions with the same length as the input batch.
-
The gains in performance will depend on the size of the input batch and the computation time of the batch function. If the batch function is very fast, the gains will be minimal. This is also only useful if you are calling the batch function with possible duplicate examples multiple times.
Possible Improvements
- Add support for multiple batch variables (e.g. batch_items, batch_labels, batch_ids, etc.)
- Add support for custom cache implementations and hash functions
- Add run-time annotations validation
Contributing
Contributions are welcome! Please open an issue if you have any suggestions or improvements.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file batche-0.1.1.tar.gz
.
File metadata
- Download URL: batche-0.1.1.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.2 CPython/3.11.3 Linux/5.19.0-41-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f770cba857d4115868baff407c6b748dd3ad093a5b9522526a9c2b124ad86780 |
|
MD5 | 0f8ef60185ca0a73d9219bee3de5279e |
|
BLAKE2b-256 | 377c3b17da11a6cbb2ec2582c11a653b3f15e8c281a957dce69dbcdc78043637 |
File details
Details for the file batche-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: batche-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.2 CPython/3.11.3 Linux/5.19.0-41-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8d0e02a50d1b91ea5bab4a09bc103cc8f49b324be0a0f3c4443b209ec26c609 |
|
MD5 | 0c887a2c796f78f348d26997b5903bae |
|
BLAKE2b-256 | 137bc00e23404bcf5b23ac8314076db7d85ba09258412193deca427a1c3bf623 |