Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

label-based Tree model for vfl #528

Merged
merged 6 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
bugfix: do not add self-loop in comm_manager.neighbors
  • Loading branch information
xieyxclack committed Feb 21, 2023
commit 4ca2aac25e04c065271b821076219c2786e8eebb
11 changes: 1 addition & 10 deletions federatedscope/vertical_fl/xgb_base/worker/XGBClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ def __init__(self,
self.feature_importance = [0] * self.num_of_feature

self._init_data_related_var()
# Add self-loop
if self._cfg.federate.mode == 'distributed':
self.comm_manager.add_neighbors(neighbor_id=self.ID,
address={
'host': self.comm_manager.host,
'port': self.comm_manager.port
})
else:
self.comm_manager.add_neighbors(neighbor_id=self.ID, address=None)

self.register_handlers('model_para', self.callback_func_for_model_para)
self.register_handlers('data_sample',
Expand Down Expand Up @@ -126,7 +117,7 @@ def start_a_new_training_round(self,
self.state = tree_num
receiver = [
each for each in list(self.comm_manager.neighbors.keys())
if each not in [self.ID, self.server_id]
if each != self.server_id
]
self.comm_manager.send(
Message(msg_type='data_sample',
Expand Down
35 changes: 22 additions & 13 deletions federatedscope/vertical_fl/xgb_base/worker/evaluation_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _feedback_eval_metrics(self):
receiver=[
each
for each in list(self.comm_manager.neighbors.keys())
if each not in [self.server_id, self.ID]
if each != self.server_id
],
content='None'))

Expand All @@ -87,12 +87,17 @@ def _test_for_node(self, tree_num, node_num):
self._test_for_node(tree_num, node_num + 1)
# Other client owns the weight, need to communicate
elif self.model[tree_num][node_num].member:
self.comm_manager.send(
Message(msg_type='split_request',
sender=self.ID,
state=self.state,
receiver=[self.model[tree_num][node_num].member],
content=(tree_num, node_num)))
send_message = Message(
msg_type='split_request',
sender=self.ID,
state=self.state,
receiver=[self.model[tree_num][node_num].member],
content=(tree_num, node_num))
if self.model[tree_num][node_num].member == self.ID:
self.callback_func_for_split_request(send_message)
else:
self.comm_manager.send(send_message)

else:
self._test_for_node(tree_num, node_num + 1)

Expand All @@ -105,12 +110,16 @@ def callback_func_for_split_request(self, message: Message):
feature_value = self.model[tree_num][node_num].feature_value
left_child, right_child = self.model[tree_num].split_childern(
self.test_x[:, feature_idx], feature_value)
self.comm_manager.send(
Message(msg_type='split_result',
sender=self.ID,
state=self.state,
receiver=[sender],
content=(tree_num, node_num, left_child, right_child)))
send_message = Message(msg_type='split_result',
sender=self.ID,
state=self.state,
receiver=[sender],
content=(tree_num, node_num, left_child,
right_child))
if sender == self.ID:
self.callback_func_for_split_result(send_message)
else:
self.comm_manager.send(send_message)

def callback_func_for_split_result(self, message: Message):
tree_num, node_num, left_child, right_child = message.content
Expand Down
91 changes: 53 additions & 38 deletions federatedscope/vertical_fl/xgb_base/worker/train_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ def train(self, tree_num, node_num=None, training_info=None):
self._find_and_send_split(split_ref, tree_num, node_num)
elif train_flag == 'call_for_local_gain':
g, h, tree_num, node_num = results
self.comm_manager.send(
Message(
msg_type='grad_and_hess',
sender=self.ID,
state=self.state,
receiver=[
each
for each in list(self.comm_manager.neighbors.keys())
if each != self.server_id
],
content=(tree_num, node_num, g, h)))
send_message = Message(
msg_type='grad_and_hess',
sender=self.ID,
state=self.state,
receiver=[
each for each in list(self.comm_manager.neighbors.keys())
if each != self.server_id
],
content=(tree_num, node_num, g, h))
self.comm_manager.send(send_message)
# imitate send this message to itself
self.callback_funcs_for_grad_and_hess(send_message)
else:
raise ValueError(f'The handler of {train_flag} is not defined.')

Expand All @@ -60,13 +61,17 @@ def _find_and_send_split(self, split_ref, tree_num, node_num):

split_ref['feature_idx'] -= accum_dim
split_child = False
self.comm_manager.send(
Message(msg_type='split',
sender=self.ID,
state=self.state,
receiver=[client_id],
content=(tree_num, node_num, split_ref,
split_child)))
send_message = Message(msg_type='split',
sender=self.ID,
state=self.state,
receiver=[client_id],
content=(tree_num, node_num, split_ref,
split_child))
if client_id == self.ID:
self.callback_func_for_split(send_message)
else:
self.comm_manager.send(send_message)

break
else:
accum_dim += dim
Expand Down Expand Up @@ -105,12 +110,15 @@ def callback_func_for_split(self, message: Message):
else:
content = (tree_num, node_num)

self.comm_manager.send(
Message(msg_type='continue_training',
sender=self.ID,
state=self.state,
receiver=[sender],
content=content))
send_message = Message(msg_type='continue_training',
sender=self.ID,
state=self.state,
receiver=[sender],
content=content)
if sender == self.ID:
self.callback_funcs_for_continue_training(send_message)
else:
self.comm_manager.send(send_message)

def callback_funcs_for_continue_training(self, message: Message):
if len(message.content) == 4:
Expand All @@ -128,13 +136,17 @@ def callback_funcs_for_grad_and_hess(self, message: Message):
tree_num, node_num, g, h)
if 'feature_idx' in split_info and 'value_idx' in split_info:
self.trainer.split_ref = split_info
self.comm_manager.send(
Message(msg_type='local_best_gain',
sender=self.ID,
state=self.state,
receiver=[message.sender],
content=(tree_num, node_num, best_gain, split_info,
improved_flag)))

send_message = Message(msg_type='local_best_gain',
sender=self.ID,
state=self.state,
receiver=[message.sender],
content=(tree_num, node_num, best_gain,
split_info, improved_flag))
if message.sender == self.ID:
self.callback_funcs_for_local_best_gain(send_message)
else:
self.comm_manager.send(send_message)

def callback_funcs_for_local_best_gain(self, message: Message):
tree_num, node_num, local_best_gain, split_info, improved_flag = \
Expand All @@ -150,13 +162,16 @@ def callback_funcs_for_local_best_gain(self, message: Message):
if max_gain is not None:
self.model[tree_num][node_num].member = split_client_id
split_child = True
self.comm_manager.send(
Message(msg_type='split',
sender=self.ID,
state=self.state,
receiver=[split_client_id],
content=(tree_num, node_num, split_ref,
split_child)))
send_message = Message(msg_type='split',
sender=self.ID,
state=self.state,
receiver=[split_client_id],
content=(tree_num, node_num, split_ref,
split_child))
if split_client_id == self.ID:
self.callback_func_for_split(send_message)
else:
self.comm_manager.send(send_message)
else:
self.trainer._set_weight_and_status(tree_num, node_num)

Expand Down