pytorch/torch/csrc/jit/python
Rodrigo Kumpera b4e491798c Avoid temporary buffers for tensors with torch.save. (#80404)
Fix torch.save _open_zipfile_writer optimization that uses a c++ stream when `f` is a os.PathLike.
This fastpath requires that we don't `open()` in python if possible, so don't do it unconditionally.

Fix PyTorchStreamWriter construction binding that takes a buffer object.
Use py::memoryview instead of py::bytes as the former doesn't copy the data.

Validated with a trivial benchmark that calls torch.save in a loop 20x with a 10M elements float32 tensor
either on cpu or cuda. Saved to /dev/null.

Tried two variants 'str' and 'open'
    In 'str' we pass the string "/dev/null" to torch.save.
    In 'open' we pass `open("/dev/null", "wb")` to torch.save.

Timing in seconds.

Before this patch:
str-cpu :: 0.757
open-cpu :: 0.757
str-cuda :: 1.367
open-cuda :: 1.366

After this patch:
str-cpu :: 0.256
open-cpu :: 0.251
str-cuda :: 0.896
open-cuda :: 0.834

Fixes #ISSUE_NUMBER

Pull Request resolved: https://github.com/pytorch/pytorch/pull/80404
Approved by: https://github.com/jamesr66a
2022-06-30 00:19:42 +00:00
..
init.cpp Avoid temporary buffers for tensors with torch.save. (#80404) 2022-06-30 00:19:42 +00:00
init.h
module_python.h
pybind_utils.cpp Revert "Revert "parse pysymints to IValues (#80066)"" (#80419) 2022-06-28 17:21:34 +00:00
pybind_utils.h Wconstab/reland pysymint (#79795) 2022-06-20 22:55:06 +00:00
pybind.h
python_arg_flatten.cpp [ONNX] Support optional type (#68793) (#73284) 2022-05-04 20:24:30 +00:00
python_arg_flatten.h
python_custom_class.cpp
python_custom_class.h
python_dict.cpp
python_dict.h
python_interpreter.cpp
python_ir.cpp canonicalize includes of form <aten/src/ATen/...> 2022-06-16 17:46:45 +00:00
python_ir.h
python_ivalue.h
python_list.cpp
python_list.h Fix sign-compare violations in python_list.h 2022-04-01 19:15:51 +00:00
python_sugared_value.cpp turn on -Werror=unused-variable in our Bazel CPU build 2022-06-11 02:46:34 +00:00
python_sugared_value.h [JIT] Support for ParameterDict getattr 2022-05-13 01:03:25 +00:00
python_tracer.cpp
python_tracer.h
python_tree_views.cpp Reland "Make debug_pkl smaller by only emitting unique traces." (#73368) 2022-04-18 22:34:21 +00:00
python_tree_views.h
script_init.cpp Expose _export_data from C++ to Python (#79207) 2022-06-10 00:41:33 +00:00
script_init.h
update_graph_executor_opt.cpp
update_graph_executor_opt.h