Skip to content

Documentation for Node Module

main(config) async

Main function to start the NEBULA node.

This function initiates the NEBULA core component deployed on each federation participant. It configures the node using the provided configuration object, setting up dataset partitions, selecting and initializing the appropriate model and data handler, and establishing training mechanisms. Additionally, it adjusts specific node parameters (such as indices and timing intervals) based on the participant's configuration, and deploys the node's network communications for federated learning.

Parameters:

Name Type Description Default
config Config

Configuration object containing settings for: - scenario (including federation and deployment parameters), - model selection and its corresponding hyperparameters, - dataset and data partitioning, - training strategy and related arguments, - device roles and security flags.

required

Raises:

Type Description
ValueError

If an unsupported model, dataset, or device role is specified.

NotImplementedError

If an unsupported training strategy (e.g., "scikit") is requested.

Returns:

Type Description

Coroutine that initializes and starts the NEBULA node.

Source code in nebula/node.py
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
async def main(config):
    """
    Main function to start the NEBULA node.

    This function initiates the NEBULA core component deployed on each federation participant.
    It configures the node using the provided configuration object, setting up dataset partitions,
    selecting and initializing the appropriate model and data handler, and establishing training
    mechanisms. Additionally, it adjusts specific node parameters (such as indices and timing intervals)
    based on the participant's configuration, and deploys the node's network communications for
    federated learning.

    Parameters:
        config (Config): Configuration object containing settings for:
            - scenario (including federation and deployment parameters),
            - model selection and its corresponding hyperparameters,
            - dataset and data partitioning,
            - training strategy and related arguments,
            - device roles and security flags.

    Raises:
        ValueError: If an unsupported model, dataset, or device role is specified.
        NotImplementedError: If an unsupported training strategy (e.g., "scikit") is requested.

    Returns:
        Coroutine that initializes and starts the NEBULA node.
    """
    n_nodes = config.participant["scenario_args"]["n_nodes"]
    model_name = config.participant["model_args"]["model"]
    idx = config.participant["device_args"]["idx"]

    additional_node_status = config.participant["mobility_args"]["additional_node"]["status"]
    additional_node_round = config.participant["mobility_args"]["additional_node"]["round_start"]

    # Adjust the total number of nodes and the index of the current node for CFL, as it doesn't require a specific partition for the server (not used for training)
    if config.participant["scenario_args"]["federation"] == "CFL":
        n_nodes -= 1
        if idx > 0:
            idx -= 1

    dataset = None
    dataset_name = config.participant["data_args"]["dataset"]
    handler = None
    batch_size = None
    num_workers = config.participant["data_args"]["num_workers"]
    model = None

    if dataset_name == "MNIST":
        batch_size = 32
        handler = MNISTPartitionHandler
        if model_name == "MLP":
            model = MNISTModelMLP()
        elif model_name == "CNN":
            model = MNISTModelCNN()
        else:
            raise ValueError(f"Model {model} not supported for dataset {dataset_name}")
    elif dataset_name == "FashionMNIST":
        batch_size = 32
        handler = FashionMNISTPartitionHandler
        if model_name == "MLP":
            model = FashionMNISTModelMLP()
        elif model_name == "CNN":
            model = FashionMNISTModelCNN()
        else:
            raise ValueError(f"Model {model} not supported for dataset {dataset_name}")
    elif dataset_name == "EMNIST":
        batch_size = 32
        handler = EMNISTPartitionHandler
        if model_name == "MLP":
            model = EMNISTModelMLP()
        elif model_name == "CNN":
            model = EMNISTModelCNN()
        else:
            raise ValueError(f"Model {model} not supported for dataset {dataset_name}")
    elif dataset_name == "CIFAR10":
        batch_size = 32
        handler = CIFAR10PartitionHandler
        if model_name == "ResNet9":
            model = CIFAR10ModelResNet(classifier="resnet9")
        elif model_name == "fastermobilenet":
            model = FasterMobileNet()
        elif model_name == "simplemobilenet":
            model = SimpleMobileNetV1()
        elif model_name == "CNN":
            model = CIFAR10ModelCNN()
        elif model_name == "CNNv2":
            model = CIFAR10ModelCNN_V2()
        elif model_name == "CNNv3":
            model = CIFAR10ModelCNN_V3()
        else:
            raise ValueError(f"Model {model} not supported for dataset {dataset_name}")
    elif dataset_name == "CIFAR100":
        batch_size = 128
        handler = CIFAR100PartitionHandler
        if model_name == "CNN":
            model = CIFAR100ModelCNN()
        else:
            raise ValueError(f"Model {model} not supported for dataset {dataset_name}")
    else:
        raise ValueError(f"Dataset {dataset_name} not supported")

    dataset = NebulaPartition(handler=handler, config=config)
    dataset.load_partition()
    dataset.log_partition()

    datamodule = DataModule(
        train_set=dataset.train_set,
        train_set_indices=dataset.train_indices,
        test_set=dataset.test_set,
        test_set_indices=dataset.test_indices,
        local_test_set=dataset.local_test_set,
        local_test_set_indices=dataset.local_test_indices,
        num_workers=num_workers,
        batch_size=batch_size,
    )

    trainer = None
    trainer_str = config.participant["training_args"]["trainer"]
    if trainer_str == "lightning":
        trainer = Lightning
    elif trainer_str == "scikit":
        raise NotImplementedError
    elif trainer_str == "siamese":
        trainer = Siamese
    else:
        raise ValueError(f"Trainer {trainer_str} not supported")

    if config.participant["device_args"]["malicious"]:
        node_cls = MaliciousNode
    else:
        if config.participant["device_args"]["role"] == Role.AGGREGATOR:
            node_cls = AggregatorNode
        elif config.participant["device_args"]["role"] == Role.TRAINER:
            node_cls = TrainerNode
        elif config.participant["device_args"]["role"] == Role.SERVER:
            node_cls = ServerNode
        elif config.participant["device_args"]["role"] == Role.IDLE:
            node_cls = IdleNode
        else:
            raise ValueError(f"Role {config.participant['device_args']['role']} not supported")

    VARIABILITY = 0.5

    def randomize_value(value, variability):
        min_value = max(0, value - variability)
        max_value = value + variability
        return random.uniform(min_value, max_value)

    config_keys = [
        ["reporter_args", "report_frequency"],
        ["discoverer_args", "discovery_frequency"],
        ["health_args", "health_interval"],
        ["health_args", "grace_time_health"],
        ["health_args", "check_alive_interval"],
        ["health_args", "send_alive_interval"],
        ["forwarder_args", "forwarder_interval"],
        ["forwarder_args", "forward_messages_interval"],
    ]

    for keys in config_keys:
        value = config.participant
        for key in keys[:-1]:
            value = value[key]
        value[keys[-1]] = randomize_value(value[keys[-1]], VARIABILITY)

    logging.info(f"Starting node {idx} with model {model_name}, trainer {trainer.__name__}, and as {node_cls.__name__}")

    node = node_cls(
        model=model,
        datamodule=datamodule,
        config=config,
        trainer=trainer,
        security=False,
    )
    await node.start_communications()
    await node.deploy_federation()

    # If it is an additional node, it should wait until additional_node_round to connect to the network
    # In order to do that, it should request the current round to the controller
    if additional_node_status:
        logging.info(f"Waiting for round {additional_node_round} to start")
        logging.info("Waiting time to start finding federation")

        await asyncio.sleep(6000)  # DEBUG purposes

    if node.cm is not None:
        await node.cm.network_wait()