Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VirtualMachine] new method allowing to set one input tensor by its index or name #10293

Merged
merged 15 commits into from
Feb 25, 2022

Conversation

vvchernov
Copy link
Contributor

@vvchernov vvchernov commented Feb 17, 2022

Implementation of set_one_input method in VirtualMachine (VM) class. It provides more convenient way to work with multiple input models on native side. Now you can use index or name to set one input tensor to VM.
Examples:
set_one_input("main", "data", tensor0)
set_one_input("main", 1, tensor1)
It was required due to GraphExecutor (GE) and VM have different signature for set_input method. set_input from VM is not so convenient for work with multiple tensor input. Implemented set_one_input method in VM is repeated signature from set_input in GE.
I think it will be good if we have the same API for both GraphExecutor and VirtualMachine. But I do not see simple way for it.
My current solution seems solid but not final.

Possible improvements: I can insert functionality of set_one_input method to base method set_input. But I do not think that it is good idea to mix them. Other possible extension is to additionally reload set_input with args: func_name, index1(name1), tensor1, index2(name2), tensor2, …, indexN(nameN), tensorN, where N is number of all input tensors. But it is still not perfect solution for me.

@vvchernov vvchernov force-pushed the vc/vm-set-input-patch branch 4 times, most recently from 9311b72 to de3e0ed Compare February 21, 2022 12:32
@vvchernov vvchernov changed the title WIP: [VirtualMachine] new method allowing to set one input tensor by its index WIP: [VirtualMachine] new method allowing to set one input tensor by its index or name Feb 21, 2022
@vvchernov vvchernov changed the title WIP: [VirtualMachine] new method allowing to set one input tensor by its index or name [VirtualMachine] new method allowing to set one input tensor by its index or name Feb 21, 2022
Copy link
Contributor

@tmoreau89 tmoreau89 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good overall, thank you @vvchernov ! I'd like to see some new unit tests in here: https://github.com/apache/tvm/blob/main/tests/python/relay/test_vm.py to at least demonstrate how this new method is intended to be used.

Copy link
Contributor

@mbs-octoml mbs-octoml left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Just some nits and a q about the corresponding change in runtime/vm.py.

include/tvm/runtime/vm/vm.h Outdated Show resolved Hide resolved
include/tvm/runtime/vm/vm.h Outdated Show resolved Hide resolved
include/tvm/runtime/vm/vm.h Outdated Show resolved Hide resolved
@@ -221,6 +211,9 @@ PackedFunc VirtualMachine::GetFunction(const std::string& name,
} else if (name == "set_input") {
return PackedFunc(
[sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { SetInput(args[0], args, 1); });
} else if (name == "set_one_input") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: suggest a unit test exercising this new method from vm.py (though I'm assuming you need to change that anyway, perhaps in a follow up PR?) Maybe you are just using the runtime module directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I use runtime module directly on native side. It is a reason why I try to use set_input methods instead of direct using of invoke. The latter requires to pack all input tensors to TvmArgs in certain sequence on native side. For me it is more controlable and clear to input one by one tensor with its tag (index or name). It is simultaneously the answer to your below question. Nevertheless I agree that my patch was not full and I've extended python API of VirtualMachine by set_one_input method. I observed that there is no even set_input unit test. Unit tests are in progress.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added pytests.

@mbs-octoml
Copy link
Contributor

mbs-octoml commented Feb 23, 2022

One more question (sorry to double dip!): I agree a uniform parameter passing convention for all executors would be a Fine Thing. What I don't understand is why we separate binding and invocation in the first place? Surely the uniform API would be invoke(func, *args)?

Copy link
Contributor

@mbs-octoml mbs-octoml left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the unit tests.

I can see there's a potential performance advantage to being able to selectively bind function parameters, eg to pay the device copy / format change cost just once for weights or other args which do not vary frequently between calls.

However, I can't think of a reason it needs to be bound up with the VM, and could imagine a separate 'argument builder' interface to manage that. However that's technically a refactor so would need a design review. So perhaps leave a TODO in this PR to that effect (sorry if that means another 6 hours of CI!) so you can make progress. And if you'd be at all interested in taking on that design & refactor please do so!

@vvchernov
Copy link
Contributor Author

vvchernov commented Feb 24, 2022

Hello @mbs-octoml, thanks for your comment. I'm not sure that I fully caught your idea, I think it is related to design improvement of an executor, but for this the more qualified ingeneer is needed who knows functionality and design of GE, VM and possible future executor to evaluate profits and risk related to back compatability of the code. My aim was to solve my local task only and try to do it so that somebody also can use my tool. @tmoreau89 what do you think about it?

@tmoreau89
Copy link
Contributor

It sounds like there are more deeply rooted issues with the VM design choice to do explicit parameter binding etc. This seems indeed like a bigger discussion that warrants its own RFC, tracking issue etc; not sure a TODO here would be the best way to track the need to redesign. Due to time difference and latency in getting the PR changed, then approved, we could merge this without the TODO change. WDYT @mbs-octoml ?

@mbs-octoml
Copy link
Contributor

I filed CORE-142, though obviously that's not visible to the wider community. @jroesch expressed concern with bringing GE APIs over to the VM since the one-param-at-a-time API is fragile.

Copy link
Contributor

@tmoreau89 tmoreau89 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @vvchernov for adding the unit tests, LGTM

@masahi masahi merged commit d62a364 into apache:main Feb 25, 2022
pfk-beta pushed a commit to pfk-beta/tvm that referenced this pull request Apr 11, 2022
…ndex or name (apache#10293)

* set_input_with_index was implemented for VM

* clean code

* add getInputIndexFromName. add function descriptions. lint fix

* fix lint

* transfer comparison of parameter names number and assigned devices number to VMFunction constructor

* add GetVMFunctionWithName to Executable API

* clean code

* add SetInputWithName (set_input_with_name) to VM API

* join SetInputWithIndex and SetInputWithName to SetOneInputTensor (set_one_input) to VM API, the joined methods were removed

* fix lint

* some fixes after review

* add set_one_input method to python API of VirtualMachine

* pytests for set_input and set_one_input methods of VirtualMachine were implemented and checked

* CI restart

* construct simple model for pytests by relay instead of onnx tools (need for correct CI)

Co-authored-by: Valery Chernov <valery.chernov@deelvin.com>
@vvchernov vvchernov deleted the vc/vm-set-input-patch branch February 28, 2023 06:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants