pytorch
9029d0d7 - Introduce a fluent API to construct tensors from external data. (#54530)

Commit
4 years ago
Introduce a fluent API to construct tensors from external data. (#54530) Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/54530 This diff introduces the following changes and improvements: - Introduces a new fluent API to construct tensors from external data as an alternative to `from_blob` overloads. See below for an example. - Leverages several small-buffer optimizations which result in %50 reduction in tensor construction times. - Exposes a new (lightweight) way to construct tensors by passing a naked `context` and `context_deleter` pair as an alternative to the existing `deleter` parameter. - Updates the existing `from_blob` overloads to internally use the fluent API. ``` // Example 1 at::Tensor tensor = at::for_blob(data, sizes) .strides(strides) .context(context, [](void *ctx) { delete static_cast<Ctx*>(ctx); }) .options(...) .target_device(...) .make_tensor(); // Example 2 at::Tensor tensor = at::for_blob(data, sizes).make_tensor(); // Example 3 at::Tensor tensor = at::for_blob(data, sizes) .deleter(...) .make_tensor(); ``` Test Plan: Below are the folly Benchmark results for the following two equivalent operations: ``` // The fluent API at::Tensor tensor = at::for_blob(data, sizes) .deleter([buffer](void*) mutable { buffer.reset(); }) .options(dtype(c10::ScalarType::Float)) .make_tensor(); // The original `from_blob` overload at::Tensor tensor = at::from_blob( data, sizes, [buffer](void*) mutable { buffer.reset(); }, dtype(c10::ScalarType::Float)); ``` ``` ============================================================================ scripts/balioglu/from_blob_exp/main.cpp relative time/iter iters/s ============================================================================ fluent 298.34ns 3.35M from_blob 55.19% 540.51ns 1.85M ============================================================================ ``` Various similar experiments show an approximate %50 reduction in tensor construction times. Reviewed By: ezyang Differential Revision: D27269344 fbshipit-source-id: e6bd0b78384bf89fd24f22254008180329000363
Author
Parents
Loading