Ejemplo n.º 1
0
    def _process_deployment(self, task_declaration: TaskDeclaration):
        assert task_declaration.state == TaskDeclaration.State.DEPLOYMENT

        with async_commit():
            save = False
            for ta in task_declaration.get_task_assignments(states=(TaskAssignment.State.READY,)):
                if self._is_task_assignment_allowed(task_declaration, ta):
                    ta.state = TaskAssignment.State.ACCEPTED
                    ta.save()

                    task_declaration.workers_needed -= 1
                    save = True
                else:
                    ta.state = TaskAssignment.State.REJECTED
                    ta.save()

            for va in task_declaration.get_verification_assignments(states=(VerificationAssignment.State.READY,)):
                if self._is_verification_assignment_allowed(task_declaration, va):
                    va.state = VerificationAssignment.State.ACCEPTED
                    va.save()

                    task_declaration.verifiers_needed -= 1
                    save = True
                else:
                    va.state = VerificationAssignment.State.REJECTED
                    va.save()

            # save if were changes
            if save:
                task_declaration.save()

        ready_to_start = task_declaration.workers_needed == 0 and task_declaration.verifiers_needed == 0
        logger.info('{} ready: {} workers_needed: {} verifiers_needed: {}'.format(
            task_declaration, ready_to_start, task_declaration.workers_needed, task_declaration.verifiers_needed))

        if ready_to_start:
            self._assign_initial_train_data(task_declaration)
            return

        if not save:
            # recheck how many workers and verifiers really accepted
            accepted_workers_count = len(task_declaration.get_task_assignments(
                states=(TaskAssignment.State.ACCEPTED,)))

            accepted_verifiers_count = len(task_declaration.get_verification_assignments(
                states=(VerificationAssignment.State.ACCEPTED,)))

            if accepted_workers_count == task_declaration.workers_requested \
                    and accepted_verifiers_count == task_declaration.verifiers_requested:
                logger.info('All performers are accepted, start train')
                task_declaration.workers_needed = 0
                task_declaration.verifiers_needed = 0
                self._assign_initial_train_data(task_declaration)
Ejemplo n.º 2
0
    def _process_epoch_in_progress(self, task_declaration: TaskDeclaration):
        assert task_declaration.state == TaskDeclaration.State.EPOCH_IN_PROGRESS
        task_assignments = task_declaration.get_task_assignments(
            states=(
                TaskAssignment.State.TRAINING,
                TaskAssignment.State.FINISHED,
            )
        )

        failed = False
        finished_task_assignments = []
        count_timeout = 0
        with async_commit():
            for ta in task_assignments:
                if ta.state == TaskAssignment.State.TRAINING:
                    if ta.iteration_is_finished:
                        ta.state = TaskAssignment.State.FINISHED
                        ta.save()

                if ta.state == TaskAssignment.State.FINISHED:
                    if ta.train_result.error:
                        failed = True
                    else:
                        finished_task_assignments.append(ta)
                    continue

                train_timeout = settings.WAIT_TRAIN_TIMEOUT
                now = datetime.datetime.utcnow().replace(tzinfo=ta.train_result.modified_at.tzinfo)
                if (now - ta.train_result.modified_at).total_seconds() > train_timeout:
                    ta.state = TaskAssignment.State.TIMEOUT
                    ta.save()

                    logger.info('Timeout of waiting for {}'.format(ta))
                    count_timeout += 1

        if failed:
            logger.info('{} is failed'.format(task_declaration))
            task_declaration.state = TaskDeclaration.State.FAILED
            task_declaration.save()
            return

        if count_timeout:
            task_declaration.workers_needed += count_timeout
            self._republish_for_train(task_declaration)
            return

        if len(finished_task_assignments) < task_declaration.workers_requested:
            logger.info('Wait for finish of training for {} iteration {}'.format(
                task_declaration, task_declaration.current_iteration))
            return

        if task_declaration.current_iteration > 1:
            self._save_loss_and_accuracy(task_declaration, finished_task_assignments)

        self._assign_verification_data(task_declaration, finished_task_assignments)
Ejemplo n.º 3
0
    def _reassign_train_data(self, task_declaration: TaskDeclaration):
        assert task_declaration.state == TaskDeclaration.State.DEPLOYMENT_TRAIN

        task_assignments = task_declaration.get_task_assignments(
            states=(
                TaskAssignment.State.ACCEPTED,
                TaskAssignment.State.TIMEOUT,
                TaskAssignment.State.FAKE_RESULTS
            )
        )

        # split by state
        accepted_task_assignment = []
        failed_task_assignments = []
        for ta in task_assignments:
            if ta.state == TaskAssignment.State.ACCEPTED:
                accepted_task_assignment.append(ta)
                continue

            if ta.state == TaskAssignment.State.TIMEOUT:
                failed_task_assignments.append(ta)
                continue

            if ta.state == TaskAssignment.State.FAKE_RESULTS:
                failed_task_assignments.append(ta)
                continue

            assert False and 'Check query!'

        assert len(failed_task_assignments) == len(accepted_task_assignment)

        # assign data to new accepted task_assignments
        for index, ta in enumerate(accepted_task_assignment):
            failed_ta = failed_task_assignments[index]
            # reassign train data
            # retrieve data which producer is able to encrypt
            train_data = TrainData.get_with_initial_data(
                asset_id=failed_ta.train_data_id,
                db=self.db,
                encryption=self.encryption
            )
            train_data.task_assignment_id = ta.asset_id
            # share data with new worker
            train_data.set_encryption_key(ta.worker.enc_key)
            train_data.save()

            ta.train_data_id = train_data.asset_id
            ta.state = TaskAssignment.State.TRAINING
            ta.save()

            failed_ta.state = TaskAssignment.State.FORGOTTEN
            failed_ta.save()

        task_declaration.state = TaskDeclaration.State.EPOCH_IN_PROGRESS
        task_declaration.save()
Ejemplo n.º 4
0
    def _reassign_verification_data(self, task_declaration: TaskDeclaration):
        verification_assignments = task_declaration.get_verification_assignments(
            states=(VerificationAssignment.State.ACCEPTED, VerificationAssignment.State.TIMEOUT)
        )

        # split accepted and overdue
        accepted_verification_assignments = []
        timeout_verification_assignments = []
        for va in verification_assignments:
            if va.state == VerificationAssignment.State.ACCEPTED:
                accepted_verification_assignments.append(va)
                continue

            if va.state == VerificationAssignment.State.TIMEOUT:
                timeout_verification_assignments.append(va)
                continue

            assert False and 'Check query!'

        assert len(accepted_verification_assignments) == len(timeout_verification_assignments)

        train_results = [
            {
                'worker_id': ta.worker_id,
                'result': ta.train_result.weights_ipfs
            }
            for ta in task_declaration.get_task_assignments(states=(TaskAssignment.State.FINISHED,))
        ]

        for index, va in enumerate(accepted_verification_assignments):
            assert va.verification_data_id is None
            verification_data = VerificationData.create(
                verification_assignment_id=va.asset_id,
                # share data with verifier
                public_key=va.verifier.enc_key,
                test_dir_ipfs=task_declaration.dataset.test_dir_ipfs,
                model_code_ipfs=task_declaration.train_model.code_ipfs,
                train_results=train_results,
                db=self.db,
                encryption=self.encryption
            )

            va.verification_data_id = verification_data.asset_id
            va.state = VerificationAssignment.State.VERIFYING
            va.save()

            failed_va = timeout_verification_assignments[index]
            failed_va.state = VerificationAssignment.State.FORGOTTEN
            failed_va.save()

        task_declaration.state = TaskDeclaration.State.VERIFY_IN_PROGRESS
        task_declaration.save()
Ejemplo n.º 5
0
    def _republish_for_train(self, task_declaration: TaskDeclaration):
        assert task_declaration.workers_needed > 0

        task_declaration.state = TaskDeclaration.State.DEPLOYMENT_TRAIN
        task_declaration.current_iteration_retry += 1
        task_declaration.save()

        task_assignments = task_declaration.get_task_assignments(
            states=(TaskAssignment.State.REJECTED,)
        )

        for ta in task_assignments:
            ta.state = TaskAssignment.State.REASSIGN
            # return back ownership
            ta.save(recipients=ta.worker.address)
Ejemplo n.º 6
0
    def _update_train_data_for_next_iteration(self, task_declaration: TaskDeclaration):
        assert task_declaration.state == TaskDeclaration.State.VERIFY_IN_PROGRESS

        task_declaration.current_iteration += 1
        task_declaration.current_iteration_retry = 0

        task_declaration.progress = (
                task_declaration.current_iteration * task_declaration.epochs_in_iteration * 100
                / task_declaration.epochs)

        count_ta = 0
        for ta in task_declaration.get_task_assignments(states=(TaskAssignment.State.FINISHED,)):
            train_data = ta.train_data
            # share data to worker
            train_data.set_encryption_key(ta.worker.enc_key)
            train_data.save()

            ta.state = TaskAssignment.State.TRAINING
            ta.save()
            count_ta += 1

        assert task_declaration.workers_requested == count_ta
        task_declaration.state = TaskDeclaration.State.EPOCH_IN_PROGRESS
        task_declaration.save()
Ejemplo n.º 7
0
    def _assign_initial_train_data(self, task_declaration: TaskDeclaration):
        assert task_declaration.state == TaskDeclaration.State.DEPLOYMENT
        # start of train
        task_declaration.current_iteration += 1
        task_declaration.current_iteration_retry = 0

        accepted_task_assignment = task_declaration.get_task_assignments(states=(TaskAssignment.State.ACCEPTED,))

        count_ta = 0

        train_dirs_ipfs, files = Directory(multihash=task_declaration.dataset.train_dir_ipfs).ls()
        test_dirs_ipfs, files = Directory(multihash=task_declaration.dataset.test_dir_ipfs).ls()

        all_train_chunks_ipfs = self._chunk_it(
            iterable=[x.multihash for x in train_dirs_ipfs],
            count=task_declaration.workers_requested
        )

        assert len(all_train_chunks_ipfs) == task_declaration.workers_requested

        all_test_chunks_ipfs = self._chunk_it(
            iterable=[x.multihash for x in test_dirs_ipfs],
            count=task_declaration.workers_requested
        )

        assert len(all_test_chunks_ipfs) == task_declaration.workers_requested

        list_td_ta = []
        with async_commit():
            # create TrainData
            for index, task_assignment in enumerate(accepted_task_assignment):
                train_chunks_ipfs = all_train_chunks_ipfs[index]
                test_chunks_ipfs = all_test_chunks_ipfs[index]

                train_data = TrainData.create(
                    model_code_ipfs=task_declaration.train_model.code_ipfs,
                    train_chunks_ipfs=train_chunks_ipfs,
                    test_chunks_ipfs=test_chunks_ipfs,
                    data_index=index,
                    db=self.db,
                    encryption=self.encryption
                )

                list_td_ta.append((train_data, task_assignment))
                logger.debug('Created {}, train chunks: {}, count:{}, test chunks: {}, count:{}'.format(
                    train_data, train_chunks_ipfs, len(train_chunks_ipfs), test_chunks_ipfs, len(test_chunks_ipfs)))
                count_ta += 1

        assert task_declaration.workers_requested == count_ta

        with async_commit():
            # share to worker
            for train_data, task_assignment in list_td_ta:
                train_data.task_assignment_id = task_assignment.asset_id
                train_data.set_encryption_key(task_assignment.worker.enc_key)
                train_data.save()

                task_assignment.train_data_id = train_data.asset_id
                task_assignment.state = TaskAssignment.State.TRAINING
                task_assignment.save()

            task_declaration.state = TaskDeclaration.State.EPOCH_IN_PROGRESS
            task_declaration.save()