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

Dont recompute masks #163

Merged
merged 23 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6811a82
added ising example
josephdviviano Feb 20, 2024
89027dd
function to stack a list of states
josephdviviano Feb 22, 2024
2afb00e
added notes for bug
josephdviviano Feb 22, 2024
be2fee1
NOT WORKING: this commit contains trajectories_states_b which is the …
josephdviviano Feb 22, 2024
7b536a2
using stack_states to prevent recomputation of masks
josephdviviano Feb 24, 2024
77e7e1b
stack_states now ignores masks for non-discrete states, and fixed bug…
josephdviviano Feb 24, 2024
4e364d3
black
josephdviviano Feb 24, 2024
26dda4b
isort
josephdviviano Feb 24, 2024
1a6e768
removed comment
josephdviviano Feb 24, 2024
45d9893
black
josephdviviano Feb 24, 2024
1e72273
default value reduced for grid size
josephdviviano Feb 24, 2024
c8cf89c
typo
josephdviviano Feb 24, 2024
687136c
black
josephdviviano Feb 24, 2024
1846da1
black upgrade
josephdviviano Feb 24, 2024
552e010
upgrade black
josephdviviano Feb 24, 2024
21b845d
black
josephdviviano Feb 24, 2024
6e2daee
Merge branch 'train_ising.py' of github.com:GFNOrg/torchgfn into dont…
josephdviviano Feb 24, 2024
1a54615
black upgrade
josephdviviano Feb 24, 2024
6aa1659
black formatting update
josephdviviano Feb 24, 2024
f1a5c7f
extended excludes
josephdviviano Feb 24, 2024
ccfa959
Merge branch 'train_ising.py' of github.com:GFNOrg/torchgfn into dont…
josephdviviano Feb 24, 2024
1a5ad2c
checks whether user-defined function returns the expected type
josephdviviano Feb 24, 2024
7996b37
Merge pull request #165 from GFNOrg/step_type_checking
saleml Feb 25, 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
4 changes: 2 additions & 2 deletions src/gfn/gflownet/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ def get_pfs_and_pbs(
if self.off_policy:
# We re-use the values calculated in .sample_trajectories().
if trajectories.estimator_outputs is not None:
estimator_outputs = trajectories.estimator_outputs[
~trajectories.actions.is_dummy
estimator_outputs = trajectories.estimator_outputs[ # TODO: This contains `inf` when we use the new `stack_states` method in `samplers.py`!
~trajectories.actions.is_dummy # And this causes later failures (p_f is not finite).
]
else:
raise Exception(
Expand Down
17 changes: 14 additions & 3 deletions src/gfn/samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from gfn.containers import Trajectories
from gfn.env import Env
from gfn.modules import GFNModule
from gfn.states import States
from gfn.states import States, stack_states


class Sampler:
Expand Down Expand Up @@ -140,6 +140,8 @@ def sample_trajectories(
else states.is_sink_state
)

trajectories_states_b: List[States] = [states]

trajectories_states: List[TT["n_trajectories", "state_shape", torch.float]] = [
states.tensor
]
Expand Down Expand Up @@ -220,9 +222,18 @@ def sample_trajectories(
dones = dones | new_dones

trajectories_states += [states.tensor]
trajectories_states_b += [states]

# New Method
trajectories_states_b = stack_states(trajectories_states_b)

# Old Method
trajectories_states = env.states_from_tensor(
torch.stack(trajectories_states, dim=0))

assert (trajectories_states_b.tensor == trajectories_states.tensor).sum() == trajectories_states.tensor.numel()
assert (trajectories_states_b.forward_masks == trajectories_states.forward_masks).sum() == trajectories_states.forward_masks.numel()

trajectories_states = torch.stack(trajectories_states, dim=0)
trajectories_states = env.states_from_tensor(trajectories_states)
trajectories_actions = env.Actions.stack(trajectories_actions)
trajectories_logprobs = torch.stack(trajectories_logprobs, dim=0)

Expand Down
19 changes: 18 additions & 1 deletion src/gfn/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from abc import ABC, abstractmethod
from copy import deepcopy
from math import prod
from typing import Callable, ClassVar, Optional, Sequence, cast
from typing import Callable, ClassVar, Optional, Sequence, List, cast

import torch
from torchtyping import TensorType as TT
Expand Down Expand Up @@ -446,3 +446,20 @@ def init_forward_masks(self, set_ones: bool = True):
self.forward_masks = torch.ones(shape).bool()
else:
self.forward_masks = torch.zeros(shape).bool()


def stack_states(states: List[States]):
"""Given a list of states, stacks them along a new dimension (0)."""
state_example = states[0] # We assume all elems of `states` are the same.

stacked_states = state_example.from_batch_shape((0, 0)) # Empty.
stacked_states.tensor = torch.stack([s.tensor for s in states], dim=0)
if state_example._log_rewards:
stacked_states._log_rewards = torch.stack([s._log_rewards for s in states], dim=0)
stacked_states.forward_masks = torch.stack([s.forward_masks for s in states], dim=0)
stacked_states.backward_masks = torch.stack([s.backward_masks for s in states], dim=0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

this should only be implemented for DiscreteStates, not all States


# Adds the trajectory dimension.
stacked_states.batch_shape = (stacked_states.tensor.shape[0],) + state_example.batch_shape

return stacked_states
Loading