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

[AutoScheduler] Add custom build function #7185

Merged
merged 6 commits into from
Jan 5, 2021
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions python/tvm/auto_scheduler/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@
MAX_FLOAT = 1e10


class BuildFunc:
"""store build_func name and callable to class variable.
name: str = "default"
The name of registered build function.
build_func: callable = tar.tar
The callable of registered build function.
"""

name = "default"
build_func = tar.tar


@tvm._ffi.register_object("auto_scheduler.MeasureCallback")
class MeasureCallback(Object):
""" The base class of measurement callback functions. """
Expand Down Expand Up @@ -303,12 +315,28 @@ class LocalBuilder(ProgramBuilder):
This is used in a wrapper of the multiprocessing.Process.join().
n_parallel : int = multiprocessing.cpu_count()
Number of threads used to build in parallel.
build_func : str = 'default'
The name of registered build function.
build_func: callable or str = "default"
If is 'default', use default build function
If is 'ndk', use function for android ndk
If is callable, use it as custom build function, expect lib_format field.
"""

def __init__(self, timeout=15, n_parallel=multiprocessing.cpu_count(), build_func="default"):
self.__init_handle_by_constructor__(_ffi_api.LocalBuilder, timeout, n_parallel, build_func)
if build_func == "default":
Copy link
Contributor

Choose a reason for hiding this comment

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

So this is designed to use the class static member?
Why not just use a self.build_func.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

when auto_scheduler.local_builder.build is called, the context can't get LocalBuilder class instance

Copy link
Member

Choose a reason for hiding this comment

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

He should keep the state information of build_func between functions, so just self.build_func can not meet the requirement because we will create one class instance.

Copy link
Contributor

Choose a reason for hiding this comment

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

@FrozenGene @leowang1225
Since we pass the BuildFunc.name to the constructor, how about just register the custom build function by @tvm._ffi.register_func and pass the fuction name to it?
In this way, it can be serialized in args in the local_build_worker below.

BuildFunc.name = "default"
BuildFunc.build_func = tar.tar
elif build_func == "ndk":
BuildFunc.name = "ndk"
BuildFunc.build_func = ndk.create_shared
elif not isinstance(build_func, str):
BuildFunc.name = "custom"
BuildFunc.build_func = build_func
leowang1225 marked this conversation as resolved.
Show resolved Hide resolved
else:
raise ValueError("Invalid build_func" + build_func)

self.__init_handle_by_constructor__(
_ffi_api.LocalBuilder, timeout, n_parallel, BuildFunc.name
)


@tvm._ffi.register_object("auto_scheduler.LocalRunner")
Expand Down Expand Up @@ -624,12 +652,10 @@ def local_build_worker(args):
The build result of this Builder thread.
"""
inp, build_func, timeout, verbose = args
if build_func == "default":
build_func = tar.tar
elif build_func == "ndk":
build_func = ndk.create_shared
else:
raise ValueError("Invalid build_func" + build_func)
FrozenGene marked this conversation as resolved.
Show resolved Hide resolved
assert build_func == BuildFunc.name, (
"BuildFunc.name: " + BuildFunc.name + ", but args is: " + build_func
)
build_func = BuildFunc.build_func
leowang1225 marked this conversation as resolved.
Show resolved Hide resolved

res = call_func_with_timeout(timeout, _timed_func, args=(inp, build_func, verbose))
if isinstance(res, TimeoutError):
Expand Down