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

Implementation of random variables with PyTorch backend #1075

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

twaclaw
Copy link
Contributor

@twaclaw twaclaw commented Nov 10, 2024

Description

Related Issue

  • Closes #
  • Related to #

Checklist

Type of change

  • New feature / enhancement
  • Bug fix
  • Documentation
  • Maintenance
  • Other (please specify):

📚 Documentation preview 📚: https://pytensor--1075.org.readthedocs.build/en/1075/

Copy link

codecov bot commented Nov 10, 2024

Codecov Report

Attention: Patch coverage is 82.45614% with 10 lines in your changes missing coverage. Please review.

Project coverage is 82.11%. Comparing base (07bd48d) to head (6176479).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
pytensor/link/pytorch/dispatch/random.py 82.60% 8 Missing ⚠️
pytensor/link/pytorch/dispatch/basic.py 50.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1075   +/-   ##
=======================================
  Coverage   82.10%   82.11%           
=======================================
  Files         185      186    +1     
  Lines       48089    48184   +95     
  Branches     8659     8673   +14     
=======================================
+ Hits        39485    39564   +79     
- Misses       6439     6452   +13     
- Partials     2165     2168    +3     
Files with missing lines Coverage Δ
pytensor/link/pytorch/dispatch/__init__.py 100.00% <100.00%> (ø)
pytensor/link/pytorch/linker.py 100.00% <100.00%> (ø)
pytensor/link/pytorch/dispatch/basic.py 93.69% <50.00%> (-0.81%) ⬇️
pytensor/link/pytorch/dispatch/random.py 82.60% <82.60%> (ø)

... and 3 files with indirect coverage changes

static_shape = rv.type.shape
batch_ndim = op.batch_ndim(node)

# Try to pass static size directly to JAX
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: pytorch

# XXX replace
state_ = rng["pytorch_state"]
gen = torch.Generator().set_state(state_)
sample = torch.bernoulli(torch.expand_copy(p, size), generator=gen)
Copy link
Contributor

Choose a reason for hiding this comment

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

I actually don't mind this approach! Torch has a lot of wrapping and abstraction on top of it's random generation, so if we just keep a little bit of state around it feels a bit simpler.

thunk_inputs = []
for n in self.fgraph.inputs:
sinput = storage_map[n]
if isinstance(sinput[0], RandomState | Generator):
new_value = pytorch_typify(
sinput[0], dtype=getattr(sinput[0], "dtype", None)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed?

static_shape = rv.type.shape
batch_ndim = op.batch_ndim(node)

# Try to pass static size directly to JAX
Copy link
Member

Choose a reason for hiding this comment

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

This static size is a JAX limitation that shouldn't exist in PyTorch

# XXX replace
state_ = rng["pytorch_state"]
gen = torch.Generator().set_state(state_)
sample = torch.bernoulli(torch.expand_copy(p, size), generator=gen)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't it jut broadcast?, why copy?

Suggested change
sample = torch.bernoulli(torch.expand_copy(p, size), generator=gen)
sample = torch.bernoulli(torch.expand_copy(p, size), generator=gen)

@twaclaw twaclaw force-pushed the implement_random_vars_pytorch_poc branch from 85d6080 to 1c8dc80 Compare December 8, 2024 12:17
Comment on lines 27 to 33
def pytorch_typify(data, dtype=None, **kwargs):
if dtype is None:
return data
else:
return torch.tensor(data, dtype=dtype)
Copy link
Member

Choose a reason for hiding this comment

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

We change this approach. You need to dispatch on the RNG type and decide what to do with it. The base-cass is to raise

# XXX: Check if there is a better way.
# Numpy uses PCG64 while Torch uses Mersenne-Twister (https://github.com/pytorch/pytorch/blob/main/aten/src/ATen/CPUGeneratorImpl.cpp)
state = rng.__getstate__()
seed = torch.from_numpy(rng.integers([2**32]))
Copy link
Member

Choose a reason for hiding this comment

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

You have to copy the rng before calling rng.integers we don't want to modify the original one

Comment on lines 26 to 33
def sample_fn(rng, size, *parameters):
return pytorch_sample_fn(op, node=node)(rng, shape, out_dtype, *parameters)

return sample_fn
Copy link
Member

Choose a reason for hiding this comment

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

call pytorch_sample_fn outside of sample_fn.

Comment on lines 54 to 63
sample = torch.binomial(
torch.broadcast_to(n.to(p.dtype), size),
torch.broadcast_to(p, size),
generator=gen,
)
return (gen, sample)
Copy link
Member

Choose a reason for hiding this comment

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

size may be none, in which case you should do: n, p = torch.broacast_arrays(n, p) or whatever it's called

@@ -84,9 +86,16 @@ def fn(*inputs, inner_fn=inner_fn):
return fn

def create_thunk_inputs(self, storage_map):
from pytensor.link.pytorch.dispatch import pytorch_typify
Copy link
Member

Choose a reason for hiding this comment

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

You'll need to copy the logic with SharedVariables in JAX to emmit a warning and use different variables. You can refactor the logic so it's not duplicated

4,
),
10,
0.5,
Copy link
Member

Choose a reason for hiding this comment

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

If you take some of these trailing commas, pre-commit won't force it to be multi-line, which is very unreadable here

],
)
def test_binomial(n, p, size):
rng = shared(np.random.default_rng(123))
Copy link
Member

Choose a reason for hiding this comment

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

We need tests that confirm the original rng was not affected

rng = shared(np.random.default_rng(123))
g = pt.random.binomial(n, p, size=size, rng=rng)
g_fn = function([], g, mode=pytorch_mode)
samples = g_fn()
Copy link
Member

Choose a reason for hiding this comment

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

You should call twice. In this case, because you did not set updates you should get the same draws back. See https://pytensor.readthedocs.io/en/latest/tutorial/prng.html for details

You should also test with updates separately

Copy link
Contributor

Choose a reason for hiding this comment

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

I updated this to include a test without the update, but I'm not getting the same draws. I'll read through the article and see if I can see why

@twiecki twiecki marked this pull request as draft December 9, 2024 16:30
@twiecki twiecki changed the title Started implementation of random variables with PyTorch backend [WIP] Implementation of random variables with PyTorch backend Dec 9, 2024
@Ch0ronomato Ch0ronomato force-pushed the implement_random_vars_pytorch_poc branch from 6176479 to 1f863f5 Compare March 11, 2025 05:25
rng_copy = np.random.default_rng()
rng_copy.bit_generator.state = rng.bit_generator.state
seed = torch.from_numpy(rng_copy.integers([2**32]))
state["pytorch_gen"] = torch.manual_seed(seed)
Copy link
Member

@ricardoV94 ricardoV94 Mar 18, 2025

Choose a reason for hiding this comment

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

I don't think we need this monkeypatching on the original state anymore, just work directly with the torch.manual_seed. It's not any better to pretend this is still a valid numpy generator

Copy link
Contributor

Choose a reason for hiding this comment

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

i guess at this point in the pipeline, it doesn't matter if it is still a torch generator, since we're calling typify

def torch_funcify_RandomVariable(op: ptr.RandomVariable, node, **kwargs):
rv = node.outputs[1]
out_dtype = rv.type.dtype
shape = rv.type.shape
Copy link
Member

Choose a reason for hiding this comment

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

shape is not guaranteed to be static. Use the size argument passed at runtime? Or add an if/else if this was an optimization

@pytorch_sample_fn.register(ptr.BernoulliRV)
def pytorch_sample_fn_bernoulli(op, node):
def sample_fn(rng, size, dtype, p):
gen = rng["pytorch_gen"]
Copy link
Member

Choose a reason for hiding this comment

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

yeah let's not do this indirectaion just work with rng directly, not with the rng stored inside the dictionary

rv_sample = pytorch_sample_fn(op, node=node)

def sample_fn(rng, size, *args):
_rng = deepcopy(rng)
Copy link
Member

Choose a reason for hiding this comment

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

you shouldn't always deepcopy, only when op.inplace=False

@ricardoV94
Copy link
Member

Looking nearly ready!

@Ch0ronomato Ch0ronomato force-pushed the implement_random_vars_pytorch_poc branch from 9f1416a to 4b4f8d0 Compare March 19, 2025 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants