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

Get the country of each session #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class HFTrainerArguments(TrainingArguments):

output_dir: str = "outputs"

run_country: str = ""

@property
def batch_size_per_step(self):
"""Compute the number of training sequences contributed by each .step() from this peer"""
Expand Down
17 changes: 17 additions & 0 deletions lib/training/hf_trainer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""A catch-all module for the dirty hacks required to make HF Trainer work with collaborative training"""
import json
import urllib

import torch
from torch import nn
from torch.utils.data import DataLoader
Expand All @@ -10,6 +13,7 @@
use_hivemind_log_handler("in_root_logger")
logger = get_logger()
LRSchedulerBase = getattr(torch.optim.lr_scheduler, '_LRScheduler', None)
URL_IP_INFO = "http://ipinfo.io/json"


class CollaborativeHFTrainer(Trainer):
Expand All @@ -21,6 +25,9 @@ class CollaborativeHFTrainer(Trainer):
def __init__(self, *, data_seed: int, collaborative_optimizer: CollaborativeOptimizer, **kwargs):
self.data_seed = data_seed
self.collaborative_optimizer = collaborative_optimizer

args = kwargs["args"]
setattr(args, "run_country", self.get_country_info())
super().__init__(optimizers=(collaborative_optimizer, NoOpScheduler(collaborative_optimizer)), **kwargs)

if self.fp16_backend is not None:
Expand All @@ -37,6 +44,16 @@ def _wrap_model(self, model, training=True):
return IgnoreGradManipulations(super()._wrap_model(model, training=training),
override_zero_grad=self.collaborative_optimizer.grad_averager.reuse_grad_buffers)

def get_country_info(self):
# As this method is only a nice to have, if ever the command fails for any reason we move on to something else
try:
response = urllib.request.urlopen(URL_IP_INFO)
data = json.load(response)
country = data["country"]
except Exception:
country = ""
return country

Comment on lines +49 to +56
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that bare except is not a good practice, nevertheless I thought here that it was useless to risk to have an error at this level for this information which is only a "nice to have"


class NoOpScheduler(LRSchedulerBase):
"""Dummy scheduler for transformers.Trainer. The real scheduler is defined in CollaborativeOptimizer.scheduler"""
Expand Down