-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helper functions to retrieve node mars/flows
- Loading branch information
Showing
7 changed files
with
154 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import pyjuice as juice | ||
import torch | ||
import numpy as np | ||
|
||
import pyjuice.nodes.distributions as dists | ||
from pyjuice.utils import BitSet | ||
from pyjuice.nodes import multiply, summate, inputs | ||
from pyjuice.model import TensorCircuit | ||
|
||
import pytest | ||
|
||
|
||
def test_tensorcircuit_fns(): | ||
|
||
ni0 = inputs(0, num_node_blocks = 2, dist = dists.Categorical(num_cats = 2)) | ||
ni1 = inputs(1, num_node_blocks = 2, dist = dists.Categorical(num_cats = 2)) | ||
ni2 = inputs(2, num_node_blocks = 2, dist = dists.Categorical(num_cats = 2)) | ||
ni3 = inputs(3, num_node_blocks = 2, dist = dists.Categorical(num_cats = 2)) | ||
|
||
m1 = multiply(ni0, ni1, edge_ids = torch.tensor([[0, 0], [0, 1], [1, 0], [1, 1]], dtype = torch.long)) | ||
n1 = summate(m1, edge_ids = torch.tensor([[0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 2, 3, 0, 1, 2, 3]], dtype = torch.long)) | ||
|
||
m2 = multiply(ni2, ni3, edge_ids = torch.tensor([[0, 0], [1, 1]], dtype = torch.long)) | ||
n2 = summate(m2, edge_ids = torch.tensor([[0, 0, 1, 1], [0, 1, 0, 1]], dtype = torch.long)) | ||
|
||
m = multiply(n1, n2, edge_ids = torch.tensor([[0, 0], [1, 1]], dtype = torch.long)) | ||
n = summate(m, edge_ids = torch.tensor([[0, 0], [0, 1]], dtype = torch.long)) | ||
|
||
pc = TensorCircuit(n) | ||
|
||
device = torch.device("cuda:0") | ||
pc.to(device) | ||
|
||
data = torch.randint(0, 2, [16, 4]).to(device) | ||
|
||
lls = pc(data) | ||
pc.backward(data.permute(1, 0), allow_modify_flows = False) | ||
|
||
nsid, neid = n2._output_ind_range | ||
n2_mars = pc.get_node_mars(n2) | ||
assert n2_mars.size(0) == neid - nsid | ||
assert n2_mars.size(1) == 16 | ||
assert (n2_mars == pc.node_mars[nsid:neid,:]).all() | ||
|
||
n2_flows = pc.get_node_flows(n2) | ||
assert n2_flows.size(0) == neid - nsid | ||
assert n2_flows.size(1) == 16 | ||
assert (n2_flows == pc.node_flows[nsid:neid,:]).all() | ||
|
||
nsid, neid = m2._output_ind_range | ||
m2_mars = pc.get_node_mars(m2) | ||
assert m2_mars.size(0) == neid - nsid | ||
assert m2_mars.size(1) == 16 | ||
assert (m2_mars == pc.element_mars[nsid:neid,:]).all() | ||
|
||
m2_flows = pc.get_node_flows(m2) | ||
assert m2_flows.size(0) == neid - nsid | ||
assert m2_flows.size(1) == 16 | ||
assert (m2_flows == pc.element_flows[nsid:neid,:]).all() | ||
|
||
|
||
if __name__ == "__main__": | ||
test_tensorcircuit_fns() |