yr.create_function_group#
- yr.create_function_group(wrapper: FunctionProxy | InstanceCreator, args: tuple, group_size: int, options: FunctionGroupOptions) List[yr.ObjectRef] | FunctionGroupHandler[source]#
Create function group instance.
This function is used to create function group instances, and supports the invocation of function proxies or class instance creators.
Note
A single group can create up to 256 instances.
The number of instances of a single group must be divisible by the number of instances of a single bundle.
During the process deployment, the heterogeneous resource collection function needs to be enabled (enabled by default). Different modes need to be selected to collect parameters of different ranges. The parameter npu_collection_mode can be set.
- Parameters:
wrapper (Union[function_proxy.FunctionProxy, instance_proxy.InstanceCreator]) – The decorated function proxy or the creator of the decorated class.
args (tuple) – Function parameters or positional parameters of a class constructor.
group_size (int) – Number of instances in the function group.
options (FunctionGroupOptions) – Options for creating function groups.
- Returns:
- Returns a list of references to data objects or a function group handle.
Data type is Union[List[ObjectRef], FunctionGroupHandler].
- Raises:
ValueError – If the FunctionGroupOptions or group_size parameter is incorrectly filled, this exception will be thrown.
RuntimeError – If function is not wrapped by @yr.invoke or @yr.instance, this exception will be thrown.
Examples
- stateless function invoke example:
>>> import yr >>> >>> yr.init() >>> >>> @yr.invoke ... def demo_func(name): ... return name >>> >>> opts = yr.FunctionGroupOptions( ... cpu=1000, ... memory=1000, ... resources={ ... "NPU/Ascend910B4/count": 1, ... }, ... scheduling_affinity_each_bundle_size=2, ... ) >>> name = "function_demo" >>> objs = yr.fcc.create_function_group(demo_func, args=(name,), group_size=8, options=opts) >>> rets = yr.get(objs) >>> assert rets == [name for i in range(1, 9)] >>> yr.finalize()
- class invoke example:
>>> import yr >>> >>> @yr.instance ... class Demo(object): ... name = "" >>> >>> def __init__(self, name): ... self.name = name >>> >>> def return_name(self): ... return self.name >>> >>> opts = yr.FunctionGroupOptions( ... cpu=1000, ... memory=1000, ... resources={ ... "NPU/Ascend910B4/count": 1, ... }, ... scheduling_affinity_each_bundle_size=2, ... ) >>> name = "class_demo" >>> function_group_handler = yr.fcc.create_function_group(Demo, args=(name, ), group_size=8, options=opts) >>> objs = function_group_handler.return_name.invoke() >>> results = yr.get(objs) >>> assert results == [name for i in range(1, 9)] >>> function_group_handler.terminate() >>> >>> yr.finalize()