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

Twin RHO Model Step 2: split the training set and train the twin model #552

Merged
merged 26 commits into from
Jul 1, 2024
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3274c16
first commit
XianzheMa Jun 24, 2024
7f2b802
change interface
XianzheMa Jun 24, 2024
209830e
Merge branch 'main' into XianzheMa/feature/twin-rho-model
XianzheMa Jun 24, 2024
972cbf7
fix ci
XianzheMa Jun 24, 2024
cff027d
fix a logit
XianzheMa Jun 24, 2024
dd5e2f3
Merge branch 'main' into XianzheMa/feature/twin-rho-model
XianzheMa Jun 25, 2024
ea1d6de
modify
XianzheMa Jun 25, 2024
b0a0b8a
add notimplemented exception
XianzheMa Jun 25, 2024
f1163a3
Merge branch 'main' into XianzheMa/feature/twin-rho-model
XianzheMa Jun 25, 2024
7857c6e
add unit tests
XianzheMa Jun 25, 2024
00301ac
add test
XianzheMa Jun 25, 2024
1971af1
linter
XianzheMa Jun 25, 2024
6b6e52f
add tests
XianzheMa Jun 26, 2024
a402d99
add type
XianzheMa Jun 27, 2024
2992df2
Merge branch 'main' into XianzheMa/feature/twin-rho-model
XianzheMa Jun 27, 2024
7cec8c2
add many unit tests
XianzheMa Jun 27, 2024
f2c1894
Merge branch 'main' into XianzheMa/feature/split-training-set
XianzheMa Jun 27, 2024
23ada1d
Merge branch 'XianzheMa/feature/twin-rho-model' into XianzheMa/featur…
XianzheMa Jun 27, 2024
a429e53
add more tests
XianzheMa Jun 27, 2024
eb0398e
make full pipeline run work
XianzheMa Jun 28, 2024
3e1e6fd
add del
XianzheMa Jul 1, 2024
cddc9ea
yearbook
XianzheMa Jul 1, 2024
3a465ff
add unit test
XianzheMa Jul 1, 2024
2a9908d
Merge branch 'XianzheMa/feature/twin-rho-model' into XianzheMa/featur…
XianzheMa Jul 1, 2024
08d7630
Merge branch 'main' into XianzheMa/feature/split-training-set
XianzheMa Jul 1, 2024
3e9250a
add ratio max
XianzheMa Jul 1, 2024
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
modify
  • Loading branch information
XianzheMa committed Jun 25, 2024
commit ea1d6de8df4ef4296ca2e31874bd1c3f3163f7d2
54 changes: 24 additions & 30 deletions modyn/models/rho_loss_twin_model/rho_loss_twin_model.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,66 @@
import logging
from typing import Any, Optional

import torch
from modyn.utils import dynamic_module_import
from torch import nn

logger = logging.getLogger(__name__)



class RHOLOSSTwinModel:

def __init__(self, model_configuration: dict[str, Any], device: str, amp: bool) -> None:
self.model = RHOLOSSTwinModelModyn(model_configuration, device, amp)
self.model.to(device)

Check warning on line 16 in modyn/models/rho_loss_twin_model/rho_loss_twin_model.py

View check run for this annotation

Codecov / codecov/patch

modyn/models/rho_loss_twin_model/rho_loss_twin_model.py#L15-L16

Added lines #L15 - L16 were not covered by tests


class RHOLOSSTwinModelModyn(nn.Module):

def __init__(self, model_configuration: dict[str, Any], device: str, amp: bool) -> None:
super().__init__()
self.device = device
model_module = dynamic_module_import("modyn.models")
rho_model_class = model_configuration["rho_real_model_class"]
rho_model_config = model_configuration["rho_real_model_config"]
model_handler = getattr(model_module, rho_model_class)

Check warning on line 27 in modyn/models/rho_loss_twin_model/rho_loss_twin_model.py

View check run for this annotation

Codecov / codecov/patch

modyn/models/rho_loss_twin_model/rho_loss_twin_model.py#L22-L27

Added lines #L22 - L27 were not covered by tests
# we only need the inner model, not the wrapper
self.model0 = model_handler(rho_model_config, device, amp).model
self.model1 = model_handler(rho_model_config, device, amp).model
self.model0_seen_ids: set[int] = set()
self.model1_seen_ids: set[int] = set()
self.models = [

Check warning on line 29 in modyn/models/rho_loss_twin_model/rho_loss_twin_model.py

View check run for this annotation

Codecov / codecov/patch

modyn/models/rho_loss_twin_model/rho_loss_twin_model.py#L29

Added line #L29 was not covered by tests
model_handler(rho_model_config, device, amp).model,
model_handler(rho_model_config, device, amp).model
]
self.models_seen_ids = [

Check warning on line 33 in modyn/models/rho_loss_twin_model/rho_loss_twin_model.py

View check run for this annotation

Codecov / codecov/patch

modyn/models/rho_loss_twin_model/rho_loss_twin_model.py#L33

Added line #L33 was not covered by tests
set(),
set()
]
self.current_model = 0

Check warning on line 37 in modyn/models/rho_loss_twin_model/rho_loss_twin_model.py

View check run for this annotation

Codecov / codecov/patch

modyn/models/rho_loss_twin_model/rho_loss_twin_model.py#L37

Added line #L37 was not covered by tests

def get_extra_state(self) -> Any:
return {

Check warning on line 40 in modyn/models/rho_loss_twin_model/rho_loss_twin_model.py

View check run for this annotation

Codecov / codecov/patch

modyn/models/rho_loss_twin_model/rho_loss_twin_model.py#L40

Added line #L40 was not covered by tests
"model0_seen_ids": self.model0_seen_ids,
"model1_seen_ids": self.model1_seen_ids,
"models_seen_ids": self.models_seen_ids,
}

def set_extra_state(self, state: dict) -> None:
self.model0_seen_ids = state["model0_seen_ids"]
self.model1_seen_ids = state["model1_seen_ids"]
self.models_seen_ids = state["models_seen_ids"]
self.current_model = 1

Check warning on line 46 in modyn/models/rho_loss_twin_model/rho_loss_twin_model.py

View check run for this annotation

Codecov / codecov/patch

modyn/models/rho_loss_twin_model/rho_loss_twin_model.py#L45-L46

Added lines #L45 - L46 were not covered by tests

def forward(self, data: torch.Tensor, sample_ids: Optional[list[int]] = None) -> torch.Tensor:
assert sample_ids is not None
if self.training:
output_tensor = self._training_forward(sample_ids, data)

Check warning on line 51 in modyn/models/rho_loss_twin_model/rho_loss_twin_model.py

View check run for this annotation

Codecov / codecov/patch

modyn/models/rho_loss_twin_model/rho_loss_twin_model.py#L49-L51

Added lines #L49 - L51 were not covered by tests
else:
output_tensor = self._eval_forward(sample_ids, data)
return output_tensor

Check warning on line 54 in modyn/models/rho_loss_twin_model/rho_loss_twin_model.py

View check run for this annotation

Codecov / codecov/patch

modyn/models/rho_loss_twin_model/rho_loss_twin_model.py#L53-L54

Added lines #L53 - L54 were not covered by tests

def _training_forward(self, sample_ids: list[int], data: torch.Tensor) -> torch.Tensor:
is_seen_by_model0 = any(sample_id in self.model0_seen_ids for sample_id in sample_ids)
is_seen_by_model1 = any(sample_id in self.model1_seen_ids for sample_id in sample_ids)
if is_seen_by_model0 and is_seen_by_model1:
raise ValueError("Sample ID is seen by both models; This shouldn't happen with IL model.")

if is_seen_by_model0 or is_seen_by_model1:
if is_seen_by_model0:
return self.model0(data)
return self.model1(data)

train_by_model0 = len(self.model0_seen_ids) <= len(self.model1_seen_ids)
if train_by_model0:
self.model0_seen_ids.update(sample_ids)
return self.model0(data)

self.model1_seen_ids.update(sample_ids)
return self.model1(data)
self.models_seen_ids[self.current_model].update(sample_ids)
return self.models[self.current_model](data)

Check warning on line 58 in modyn/models/rho_loss_twin_model/rho_loss_twin_model.py

View check run for this annotation

Codecov / codecov/patch

modyn/models/rho_loss_twin_model/rho_loss_twin_model.py#L57-L58

Added lines #L57 - L58 were not covered by tests

def _eval_forward(self, sample_ids: list[int], data: torch.Tensor) -> torch.Tensor:
is_seen_by_model1 = any(sample_id in self.model1_seen_ids for sample_id in sample_ids)
if is_seen_by_model1:
return self.model0(data)
seen_by_model0 = torch.BoolTensor([sample_id in self.models_seen_ids[0] for sample_id in sample_ids], device=self.device)
seen_by_model1 = torch.BoolTensor([sample_id in self.models_seen_ids[1] for sample_id in sample_ids], device=self.device)

Check warning on line 62 in modyn/models/rho_loss_twin_model/rho_loss_twin_model.py

View check run for this annotation

Codecov / codecov/patch

modyn/models/rho_loss_twin_model/rho_loss_twin_model.py#L61-L62

Added lines #L61 - L62 were not covered by tests

# when a sample is not seen by any model, we default to model0
return self.model0(data)
# assert that a sample is seen by at least one model
assert (seen_by_model0 | seen_by_model1).all()
return torch.where(seen_by_model1, self.models[0](data), self.models[1](data))

Check warning on line 66 in modyn/models/rho_loss_twin_model/rho_loss_twin_model.py

View check run for this annotation

Codecov / codecov/patch

modyn/models/rho_loss_twin_model/rho_loss_twin_model.py#L65-L66

Added lines #L65 - L66 were not covered by tests
Loading