| 1446 | |
| 1447 | |
| 1448 | class Net: |
| 1449 | _net_names_used_counters: Dict[str, int] = {} |
| 1450 | _net_names_used: Set[str] = set() |
| 1451 | operator_registry_ = {} |
| 1452 | |
| 1453 | @staticmethod |
| 1454 | def current_prefix(): |
| 1455 | from caffe2.python.net_builder import NetBuilder |
| 1456 | builder = NetBuilder.current(required=False) |
| 1457 | return builder.name if builder else '' |
| 1458 | |
| 1459 | @staticmethod |
| 1460 | def _reset_used_names() -> None: |
| 1461 | Net._net_names_used_counters = {} |
| 1462 | Net._net_names_used = set() |
| 1463 | |
| 1464 | @staticmethod |
| 1465 | def _get_next_net_name(basename): |
| 1466 | basename = "/".join(x for x in [Net.current_prefix(), basename] if x) |
| 1467 | idx = Net._net_names_used_counters.get(basename, 0) |
| 1468 | while ( |
| 1469 | name := basename if idx == 0 else f"{basename}_{idx}" |
| 1470 | ) in Net._net_names_used: |
| 1471 | idx += 1 |
| 1472 | Net._net_names_used_counters[basename] = idx + 1 |
| 1473 | Net._net_names_used.add(name) |
| 1474 | return name |
| 1475 | |
| 1476 | def __init__(self, name_or_proto, inplace=False): |
| 1477 | """ |
| 1478 | Create a Net. |
| 1479 | Args: |
| 1480 | name_or_proto: If a NetDef is provided, clone it (or take ownership, |
| 1481 | depending on the value of `inplace`). Otherwise, |
| 1482 | create an empty net with the given name. |
| 1483 | inplace: If a NetDef is provided, take ownership when `inplace` is True; |
| 1484 | otherwise, clone it. |
| 1485 | """ |
| 1486 | self._input_record = None |
| 1487 | self._output_record = None |
| 1488 | # Register blobs so that it's guaranteed that different calls to |
| 1489 | # NextBlob/NextScopedBlob always return blobs with different names |
| 1490 | self._registered_blob_names = set() |
| 1491 | self._recreate_lookup_tables = False |
| 1492 | self._op_outputs = set() |
| 1493 | self._external_input_map = set() |
| 1494 | self._attr_dict = defaultdict(list) |
| 1495 | if type(name_or_proto) is caffe2_pb2.NetDef: |
| 1496 | proto = name_or_proto |
| 1497 | # We are initializing a network by a NetDef. In this case, we will |
| 1498 | # initialize our network with the given netdef. |
| 1499 | if inplace: |
| 1500 | self._net = proto |
| 1501 | else: |
| 1502 | self._net = caffe2_pb2.NetDef() |
| 1503 | self._net.CopyFrom(proto) |
| 1504 | |
| 1505 | existing_outputs = [list(op.output) for op in self._net.op] |
no outgoing calls