Skip to content

fuse

Fuse

Bases: NumpyOp

Run a sequence of NumpyOps as a single Op.

Parameters:

Name Type Description Default
ops Union[NumpyOp, List[NumpyOp]]

A sequence of NumpyOps to run. They must all share the same mode. It also doesn't support scheduled ops at the moment, though the Fuse itself may be scheduled.

required

Raises:

Type Description
ValueError

If repeat or ops are invalid.

Source code in fastestimator\fastestimator\op\numpyop\meta\fuse.py
@traceable()
class Fuse(NumpyOp):
    """Run a sequence of NumpyOps as a single Op.

    Args:
        ops: A sequence of NumpyOps to run. They must all share the same mode. It also doesn't support scheduled ops at
            the moment, though the Fuse itself may be scheduled.

    Raises:
        ValueError: If `repeat` or `ops` are invalid.
    """
    def __init__(self, ops: Union[NumpyOp, List[NumpyOp]]) -> None:
        ops = to_list(ops)
        if len(ops) < 1:
            raise ValueError("Fuse requires at least one op")
        inputs = []
        outputs = []
        mode = ops[0].mode
        for op in ops:
            if op.mode != mode:
                raise ValueError(f"All Fuse ops must share the same mode, but got {mode} and {op.mode}")
            for inp in op.inputs:
                if inp not in inputs and inp not in outputs:
                    inputs.append(inp)
            for out in op.outputs:
                if out not in outputs:
                    outputs.append(out)
        super().__init__(inputs=inputs, outputs=outputs, mode=mode)
        self.ops = ops

    def __getstate__(self) -> Dict[str, List[Dict[Any, Any]]]:
        return {'ops': [elem.__getstate__() if hasattr(elem, '__getstate__') else {} for elem in self.ops]}

    def forward(self, data: List[np.ndarray], state: Dict[str, Any]) -> List[np.ndarray]:
        data = {key: elem for key, elem in zip(self.inputs, data)}
        forward_numpyop(self.ops, data, state["mode"])
        return [data[key] for key in self.outputs]