| 580 | |
| 581 | |
| 582 | class _RunIf(_RunOnce): |
| 583 | def __init__(self, cond_blob=None, name=None, _already_ran=None): |
| 584 | _RunOnce.__init__(self, name) |
| 585 | assert cond_blob or _already_ran |
| 586 | self._is_else = cond_blob is None |
| 587 | if _already_ran is None: |
| 588 | self._else_blob = ops.Not(cond_blob) |
| 589 | self._already_ran = ops.Const(False) |
| 590 | else: |
| 591 | self._already_ran = _already_ran |
| 592 | self._else_blob = _already_ran if cond_blob is None else ( |
| 593 | ops.Or([_already_ran, ops.Not(cond_blob)])) |
| 594 | |
| 595 | def __enter__(self): |
| 596 | r = _RunOnce.__enter__(self) |
| 597 | ops.stop_if(self._else_blob) |
| 598 | ops.Const(True, blob_out=self._already_ran) |
| 599 | return r |
| 600 | |
| 601 | def Elif(self, cond, name=None): |
| 602 | assert not self._is_else, 'Else not allowed for an Else.' |
| 603 | return NetBuilder.current().add(_RunIf( |
| 604 | cond, name=name or self.name, _already_ran=self._already_ran)) |
| 605 | |
| 606 | def Else(self, name=None): |
| 607 | assert not self._is_else, 'Elif not allowed for an Else.' |
| 608 | return NetBuilder.current().add( |
| 609 | _RunIf(name=name or self.name, _already_ran=self._already_ran)) |
| 610 | |
| 611 | |
| 612 | class _RunIfNet(NetBuilder): |