add support for custom function for reducing the batch size (#3071)
* add support for custom function for reducing the batch size
* fix scoping
* Apply style fixes
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>