From df475aa1dc4310abc273cf26b14b6ac1cdb7dfa4 Mon Sep 17 00:00:00 2001 From: Stephen Jia Date: Tue, 5 Oct 2021 07:58:30 -0700 Subject: [PATCH] Update Vulkan runner in benchmark binary to handle non-tensor inputs (#66123) Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/66123 Some models may take in a list of tensors as inputs, thus the bundled inputs will contain `IValues` that are of the type `c10::List`. For Vulkan models, every tensor in the `IValue` list has to be converted to a vulkan tensor first, and this case is not currently handled by the Vulkan model wrapper in the benchmark binary. This diff introduces `IValue` type checking to the input processor of the Vulkan model wrapper, and adds support for Tensor and List types. Test Plan: ``` # Build the binary cd ~/fbsource buck build -c ndk.custom_libcxx=false -c pt.enable_qpl=0 //xplat/caffe2:ptmobile_compareAndroid\#android-arm64 --show-output # Push it to the device adb push buck-out/gen/xplat/caffe2/ptmobile_compareAndroid\#android-arm64 /data/local/tmp/compare_models # Run the benchmark binary BENCH_CMD="/data/local/tmp/compare_models" BENCH_CMD+=" --model=$PATH_TO_MODEL" BENCH_CMD+=" --refmodel=$PATH_TO_REFERENCE_MODEL" BENCH_CMD+=" --input_type=float --input_dims=$MODEL_INPUT_SIZE" BENCH_CMD+=" --iter=100" BENCH_CMD+=" --tolerance 1e-5" ``` Reviewed By: beback4u Differential Revision: D31276862 fbshipit-source-id: 1d9abf958963da6ecad641202f0458402bee5ced --- binaries/speed_benchmark_torch.cc | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/binaries/speed_benchmark_torch.cc b/binaries/speed_benchmark_torch.cc index e317d74ca24..bb160d6adb6 100644 --- a/binaries/speed_benchmark_torch.cc +++ b/binaries/speed_benchmark_torch.cc @@ -184,7 +184,27 @@ class vkRunner final : public Runner { inputs_.clear(); inputs_.reserve(inputs.size()); for (const auto& input : inputs) { - inputs_.emplace_back(input.toTensor().vulkan()); + if (input.isTensor()) { + inputs_.emplace_back(input.toTensor().vulkan()); + } + else if (input.isList()) { + const c10::List input_as_list = input.toList(); + c10::List input_vk_list; + input_vk_list.reserve(input_as_list.size()); + for (int i=0; i < input_as_list.size(); ++i) { + const c10::IValue element = input_as_list.get(i); + if (element.isTensor()) { + input_vk_list.emplace_back(element.toTensor().vulkan()); + } + else { + CAFFE_THROW("Input of type c10::List must only contain Tensors!"); + } + } + inputs_.emplace_back(c10::IValue(input_vk_list)); + } + else { + CAFFE_THROW("Inputs must only contain IValues of type c10::Tensor or c10::List!"); + } } // Run, and download the output tensor to system memory.