towhee.functional.mixins.compile.CompileMixin¶
- class towhee.functional.mixins.compile.CompileMixin[source]¶
Bases:
object
Mixin to just-in-time compile the Operator. More information about just-in-time compilation can be found at https://en.wikipedia.org/wiki/Just-in-time_compilation.
Examples:
compile the python function with numba:
>>> import numpy >>> import towhee >>> import time >>> from towhee import register >>> @register(name='inner_distance') ... def inner_distance(query, data): ... dists = [] ... for vec in data: ... dist = 0 ... for i in range(len(vec)): ... dist += vec[i] * query[i] ... dists.append(dist) ... return dists >>> data = [numpy.random.random((10000, 128)) for _ in range(10)] >>> query = numpy.random.random(128)
>>> t1 = time.time() >>> dc1 = ( ... towhee.dc['a'](data) ... .runas_op['a', 'b'](func=lambda _: query) ... .inner_distance[('b', 'a'), 'c']() ... ) >>> t2 = time.time() >>> dc2 = ( ... towhee.dc['a'](data) ... .config(jit='numba') ... .runas_op['a', 'b'](func=lambda _: query) ... .inner_distance[('b', 'a'), 'c']() ... ) >>> t3 = time.time() >>> assert(t3-t2 < t2-t1)
compile the models with towhee.compiler:
Note
It will take some time to compile in the first time.
More information about towhee.compiler refer to https://github.com/towhee-io/towhee-compiler.
>>> import towhee >>> dc1 = (towhee.dc(['hello world']) ... .set_jit('towhee') ... .text_embedding.transformers(model_name='distilbert-base-cased') ... )
Methods
jit_resolve
set_jit