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

[JIT] Optimize FunctionSchema::checkArg for the Tensor case. #48034

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions aten/src/ATen/core/ivalue.h
Expand Up @@ -685,6 +685,10 @@ struct CAFFE2_API IValue final {
return "InvalidTag(" + c10::guts::to_string(static_cast<int>(tag)) + ")";
}

uint32_t tagAsInt() const {
return static_cast<uint32_t>(tag);
}

// generic v.to<at::Tensor>() implementations
// that can be used in special functions like pop/push
// that use template meta-programming.
Expand Down
25 changes: 24 additions & 1 deletion torch/csrc/jit/api/function_impl.cpp
Expand Up @@ -45,10 +45,33 @@ c10::intrusive_ptr<c10::ivalue::Future> GraphFunction::runAsync(
return get_executor().runAsync(stack, std::move(taskLauncher));
}

size_t GraphFunction::computeInputTypesHash(
const std::vector<IValue>& stack) const {
// Use an algorithm similar to boost::hash_combine to compute the vector hash
size_t r = 0;
const size_t magic_number = 0x9e3779b9;
for (const IValue& iv : stack) {
r ^= std::hash<uint32_t>{}(iv.tagAsInt()) + magic_number + (r << 6) +
(r >> 2);
}
return r;
}

IValue GraphFunction::operator()(
std::vector<IValue> stack,
const Kwargs& kwargs) {
getSchema().checkAndNormalizeInputs(stack, kwargs);
bool need_schema_check = true;
if (!kwargs.size()) { // Fast path
size_t input_types_hash = computeInputTypesHash(stack);
if (!schema_checks_cache_.count(input_types_hash)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If the hash collides this check produces wrong results. In the fast path (a hit), one would need to check the equality of the types, which would require more computation.

getSchema().checkAndNormalizeInputs(stack, kwargs);
Copy link
Contributor

Choose a reason for hiding this comment

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

If the schema has default arguments, or other things in the 'NormalizeInputs' bucket, then caching this is invalid because these actions need to be applied to each invocation.

schema_checks_cache_.insert(input_types_hash);
Copy link
Contributor

Choose a reason for hiding this comment

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

mutating the GraphFunction data structure requires holding a lock because it is invoked from multiple threads.

}
need_schema_check = false;
}
if (need_schema_check) {
getSchema().checkAndNormalizeInputs(stack, kwargs);
}
run(stack);
return stack.front();
}
Expand Down
8 changes: 8 additions & 0 deletions torch/csrc/jit/api/function_impl.h
Expand Up @@ -141,6 +141,14 @@ struct TORCH_API GraphFunction : public Function {
// mutable because getSchema caches the default schema if one is requested
// before a call to setSchema
mutable std::unique_ptr<FunctionSchema> schema_;

// If we're seeing inputs of the same types over and over again (a frequent
// use case when a model is run in inference), and all of the inputs are
// positional, we can shortcut expensive schema checks. We do this by
// computing a hash of types of the inputs and if that combination of types
// has been seen before, we skip the schema check.
mutable std::unordered_set<size_t> schema_checks_cache_;
size_t computeInputTypesHash(const std::vector<IValue>& stack) const;
};
} // namespace jit
} // namespace torch