jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def test_retry_double_precision_double_precision(
        self, mock_eigh: mock.Mock
    ) -> None:
        A = torch.tensor([[1.0, 0.0], [0.0, 1.0]])
        X, _, _ = _matrix_root_eigen(
            A=A,
            root=2,
            epsilon=0.0,
            make_positive_semidefinite=True,
            inverse=False,
            retry_double_precision=True,
        )
        torch.testing.assert_allclose(X, torch.eye(2))
        mock_eigh.assert_called()
        self.assertEqual(mock_eigh.call_count, 2)
//  [python]
def test_retry_double_precision_double_precision(
        self, mock_eigh: mock.Mock
    ) -> None:
        A = torch.tensor([[1.0, 0.0], [0.0, 1.0]])
        X, _, _ = _matrix_root_eigen(
            A=A,
            root=2,
            epsilon=0.0,
            make_positive_semidefinite=True,
            inverse=False,
            retry_double_precision=True,
        )
        torch.testing.assert_close(X, torch.eye(2))
        mock_eigh.assert_called()
        self.assertEqual(mock_eigh.call_count, 2)
//  [python]
def test_superpos_empty(self):
        stack = RewriteStack.empty(1, 3, None)
        policy = torch.ones(1, 3) / 3
        stack.update(policy, NEW_VEC)
        torch.testing.assert_allclose(stack.tapes.tolist(), [[[1/3, 1/3, 0]]])
//  [python]
def test_superpos_empty(self):
        stack = RewriteStack.empty(1, 3, None)
        policy = torch.ones(1, 3) / 3
        stack.update(policy, NEW_VEC)
        torch.testing.assert_close(stack.tapes.tolist(), [[[1/3, 1/3, 0]]])
//  [python]
def test_complex_update(self):
        batch_size = 16
        stack_dim = 10
        policy_vectors = torch.ones(batch_size, 3)
        push_vectors = torch.ones(batch_size, stack_dim)

        stack = RewriteStack.empty(batch_size, stack_dim)
        stack.update(policy_vectors, push_vectors)

        expected = push_vectors.unsqueeze(dim=1)
        torch.testing.assert_allclose(stack.tapes, expected)
//  [python]
def test_complex_update(self):
        batch_size = 16
        stack_dim = 10
        policy_vectors = torch.ones(batch_size, 3)
        push_vectors = torch.ones(batch_size, stack_dim)

        stack = RewriteStack.empty(batch_size, stack_dim)
        stack.update(policy_vectors, push_vectors)

        expected = push_vectors.unsqueeze(dim=1)
        torch.testing.assert_close(stack.tapes, expected)
//  [python]
def test_reset_parameters(self):
        """Test the reset of the parameters."""
        channels_0 = 10
        channels_1 = 20
        channels_2 = 30

        scn = SCN2Layer(channels_0, channels_1, channels_2)
        scn.reset_parameters()

        for module in scn.modules():
            if isinstance(module, torch.nn.Conv2d):
                torch.testing.assert_allclose(
                    module.weight, torch.zeros_like(module.weight)
                )
                torch.testing.assert_allclose(
                    module.bias, torch.zeros_like(module.bias)
                )
//  [python]
def test_reset_parameters(self):
        """Test the reset of the parameters."""
        channels_0 = 10
        channels_1 = 20
        channels_2 = 30

        scn = SCN2Layer(channels_0, channels_1, channels_2)
        scn.reset_parameters()

        for module in scn.modules():
            if isinstance(module, torch.nn.Conv2d):
                torch.testing.assert_close(
                    module.weight, torch.zeros_like(module.weight)
                )
                torch.testing.assert_close(
                    module.bias, torch.zeros_like(module.bias)
                )
//  [python]
def test_inverse_softplus():
    t = torch.randn(32, 4)
    torch.testing.assert_allclose(t, inverse_softplus(nn.functional.softplus(t)))
//  [python]
def test_inverse_softplus():
    t = torch.randn(32, 4)
    torch.testing.assert_close(t, inverse_softplus(nn.functional.softplus(t)))
//  [python]
def test_zero_bias():
    in_dim = (4,)
    out_dim = (2,)
    layers = [2, 4, 6, 8, 4, 2]
    n = DeterministicNN(in_dim, out_dim, layers)
    zero_bias(n)

    for name, param in n.named_parameters():
        if name.endswith("bias"):
            torch.testing.assert_allclose(param, torch.zeros_like(param.data))
//  [python]
def test_zero_bias():
    in_dim = (4,)
    out_dim = (2,)
    layers = [2, 4, 6, 8, 4, 2]
    n = DeterministicNN(in_dim, out_dim, layers)
    zero_bias(n)

    for name, param in n.named_parameters():
        if name.endswith("bias"):
            torch.testing.assert_close(param, torch.zeros_like(param.data))
//  [python]
def test_init_head():
    in_dim = (4,)
    out_dim = (2,)
    layers = [2, 4, 6, 8, 4, 2]
    net = DeterministicNN(in_dim, out_dim, layers)
    init_head_bias(net, offset=1, delta=0)
    init_head_weight(net)

    for name, param in net.named_parameters():
        if name.endswith("head.bias"):
            torch.testing.assert_allclose(param, torch.ones_like(param.data))
//  [python]
def test_init_head():
    in_dim = (4,)
    out_dim = (2,)
    layers = [2, 4, 6, 8, 4, 2]
    net = DeterministicNN(in_dim, out_dim, layers)
    init_head_bias(net, offset=1, delta=0)
    init_head_weight(net)

    for name, param in net.named_parameters():
        if name.endswith("head.bias"):
            torch.testing.assert_close(param, torch.ones_like(param.data))
//  [python]
def test_correctness(self):
        tensor = torch.tensor([2])
        out_tensor = one_hot_encode(tensor, 4)
        torch.testing.assert_allclose(out_tensor, torch.tensor([[0.0, 0.0, 1.0, 0.0]]))

        tensor = torch.tensor([2, 1])
        out_tensor = one_hot_encode(tensor, 4)
        torch.testing.assert_allclose(
            out_tensor, torch.tensor([[0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 0.0]])
        )
//  [python]
def test_correctness(self):
        tensor = torch.tensor([2])
        out_tensor = one_hot_encode(tensor, 4)
        torch.testing.assert_close(out_tensor, torch.tensor([[0.0, 0.0, 1.0, 0.0]]))

        tensor = torch.tensor([2, 1])
        out_tensor = one_hot_encode(tensor, 4)
        torch.testing.assert_close(
            out_tensor, torch.tensor([[0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 0.0]])
        )
//  [python]
def test_1d(self, one_hot):
        encoding = TileCode([-1.0], [1.0], bins=10, one_hot=one_hot)
        encoding = torch.jit.script(encoding)
        t_in = torch.linspace(-1.0, 1.0, 11).unsqueeze(-1)
        code = encoding(t_in)

        if one_hot:
            assert encoding.extra_dims == 10
            torch.testing.assert_allclose(code, torch.eye(11))
        else:
            assert encoding.extra_dims == 0
            torch.testing.assert_allclose(code, torch.arange(11).long())

        t_in_eps = t_in + 0.2 * (torch.rand_like(t_in) - 0.5)
        code_eps = encoding(t_in_eps)

        torch.testing.assert_allclose(code, code_eps)

        torch.testing.assert_allclose(
            encoding.tiles[..., 0], torch.linspace(-0.9, 0.9, 10)
        )
//  [python]
def test_1d(self, one_hot):
        encoding = TileCode([-1.0], [1.0], bins=10, one_hot=one_hot)
        encoding = torch.jit.script(encoding)
        t_in = torch.linspace(-1.0, 1.0, 11).unsqueeze(-1)
        code = encoding(t_in)

        if one_hot:
            assert encoding.extra_dims == 10
            torch.testing.assert_close(code, torch.eye(11))
        else:
            assert encoding.extra_dims == 0
            torch.testing.assert_close(code, torch.arange(11).long())

        t_in_eps = t_in + 0.2 * (torch.rand_like(t_in) - 0.5)
        code_eps = encoding(t_in_eps)

        torch.testing.assert_close(code, code_eps)

        torch.testing.assert_close(
            encoding.tiles[..., 0], torch.linspace(-0.9, 0.9, 10)
        )
//  [python]
def test_output_sum_one(self, batch_size):
        num_classes = 4
        tensor = torch.randint(4, torch.Size([batch_size] if batch_size else []))
        out_tensor = one_hot_encode(tensor, num_classes)
        if batch_size:
            torch.testing.assert_allclose(
                out_tensor.sum(dim=-1), torch.ones(batch_size)
            )
        else:
            torch.testing.assert_allclose(out_tensor.sum(dim=-1), torch.tensor(1.0))
//  [python]
def test_output_sum_one(self, batch_size):
        num_classes = 4
        tensor = torch.randint(4, torch.Size([batch_size] if batch_size else []))
        out_tensor = one_hot_encode(tensor, num_classes)
        if batch_size:
            torch.testing.assert_close(
                out_tensor.sum(dim=-1), torch.ones(batch_size)
            )
        else:
            torch.testing.assert_close(out_tensor.sum(dim=-1), torch.tensor(1.0))
//  [python]
def test_data_vstack(self):
        for n_samples, n_features in product(range(1, 10), range(1, 10)):
            # numpy arrays
            a, b = np.random.rand(n_samples, n_features), np.random.rand(
                n_samples, n_features)
            np.testing.assert_almost_equal(
                modAL.utils.data.data_vstack((a, b)),
                np.concatenate((a, b))
            )

            # sparse matrices
            for format in ['lil', 'csc', 'csr']:
                a, b = sp.random(n_samples, n_features, format=format), sp.random(
                    n_samples, n_features, format=format)
                self.assertEqual((modAL.utils.data.data_vstack(
                    (a, b)) != sp.vstack((a, b))).sum(), 0)

            # lists
            a, b = np.random.rand(n_samples, n_features).tolist(), np.random.rand(
                n_samples, n_features).tolist()
            np.testing.assert_almost_equal(
                modAL.utils.data.data_vstack((a, b)),
                np.concatenate((a, b))
            )

            # torch.Tensors
            a, b = torch.ones(2, 2), torch.ones(2, 2)
            torch.testing.assert_allclose(
                modAL.utils.data.data_vstack((a, b)),
                torch.cat((a, b))
            )

        # not supported formats
        self.assertRaises(TypeError, modAL.utils.data.data_vstack, (1, 1))
//  [python]
def test_data_vstack(self):
        for n_samples, n_features in product(range(1, 10), range(1, 10)):
            # numpy arrays
            a, b = np.random.rand(n_samples, n_features), np.random.rand(
                n_samples, n_features)
            np.testing.assert_almost_equal(
                modAL.utils.data.data_vstack((a, b)),
                np.concatenate((a, b))
            )

            # sparse matrices
            for format in ['lil', 'csc', 'csr']:
                a, b = sp.random(n_samples, n_features, format=format), sp.random(
                    n_samples, n_features, format=format)
                self.assertEqual((modAL.utils.data.data_vstack(
                    (a, b)) != sp.vstack((a, b))).sum(), 0)

            # lists
            a, b = np.random.rand(n_samples, n_features).tolist(), np.random.rand(
                n_samples, n_features).tolist()
            np.testing.assert_almost_equal(
                modAL.utils.data.data_vstack((a, b)),
                np.concatenate((a, b))
            )

            # torch.Tensors
            a, b = torch.ones(2, 2), torch.ones(2, 2)
            torch.testing.assert_close(
                modAL.utils.data.data_vstack((a, b)),
                torch.cat((a, b))
            )

        # not supported formats
        self.assertRaises(TypeError, modAL.utils.data.data_vstack, (1, 1))
//  [python]
def test_indexing(self, batch_size):
        num_classes = 4
        tensor = torch.randint(4, torch.Size([batch_size] if batch_size else [1]))
        out_tensor = one_hot_encode(tensor, num_classes)
        indexes = out_tensor.gather(-1, tensor.unsqueeze(-1)).long().squeeze(-1)
        torch.testing.assert_allclose(indexes, torch.ones_like(tensor))
//  [python]
def test_indexing(self, batch_size):
        num_classes = 4
        tensor = torch.randint(4, torch.Size([batch_size] if batch_size else [1]))
        out_tensor = one_hot_encode(tensor, num_classes)
        indexes = out_tensor.gather(-1, tensor.unsqueeze(-1)).long().squeeze(-1)
        torch.testing.assert_close(indexes, torch.ones_like(tensor))
//  [python]
def test_2d(self, one_hot):
        encoding = TileCode([-1.0, -2.0], [1.0, 2.0], bins=10, one_hot=one_hot)
        encoding = torch.jit.script(encoding)

        t_in = torch.cartesian_prod(
            torch.linspace(-1.0, 1.0, 11), torch.linspace(-2.0, 2.0, 11)
        )
        code = encoding(t_in)

        if one_hot:
            assert encoding.extra_dims == 11 * 11 - 2
            torch.testing.assert_allclose(code, torch.eye(11 * 11))
        else:
            assert encoding.extra_dims == -1
            torch.testing.assert_allclose(code, torch.arange(11 * 11).long())

        t_in_eps = t_in + (torch.rand_like(t_in) - 0.5) @ torch.diag(
            torch.tensor([0.2, 0.4])
        )
        code_eps = encoding(t_in_eps)

        torch.testing.assert_allclose(code, code_eps)

        torch.testing.assert_allclose(
            encoding.tiles[..., 0], torch.linspace(-0.9, 0.9, 10)
        )
        torch.testing.assert_allclose(
            encoding.tiles[..., 1], torch.linspace(-1.8, 1.8, 10)
        )
//  [python]
def test_2d(self, one_hot):
        encoding = TileCode([-1.0, -2.0], [1.0, 2.0], bins=10, one_hot=one_hot)
        encoding = torch.jit.script(encoding)

        t_in = torch.cartesian_prod(
            torch.linspace(-1.0, 1.0, 11), torch.linspace(-2.0, 2.0, 11)
        )
        code = encoding(t_in)

        if one_hot:
            assert encoding.extra_dims == 11 * 11 - 2
            torch.testing.assert_close(code, torch.eye(11 * 11))
        else:
            assert encoding.extra_dims == -1
            torch.testing.assert_close(code, torch.arange(11 * 11).long())

        t_in_eps = t_in + (torch.rand_like(t_in) - 0.5) @ torch.diag(
            torch.tensor([0.2, 0.4])
        )
        code_eps = encoding(t_in_eps)

        torch.testing.assert_close(code, code_eps)

        torch.testing.assert_close(
            encoding.tiles[..., 0], torch.linspace(-0.9, 0.9, 10)
        )
        torch.testing.assert_close(
            encoding.tiles[..., 1], torch.linspace(-1.8, 1.8, 10)
        )
//  [python]
def test_3d(self, one_hot):
        encoding = TileCode(
            [-1.0, -5.0, -3.0], [1.0, 5.0, 5.0], bins=5, one_hot=one_hot
        )
        encoding = torch.jit.script(encoding)

        t_in = torch.cartesian_prod(
            torch.linspace(-1.0, 1.0, 6),
            torch.linspace(-5.0, 5.0, 6),
            torch.linspace(-3.0, 5.0, 6),
        )
        code = encoding(t_in)

        if one_hot:
            assert encoding.extra_dims == 6 ** 3 - 3
            torch.testing.assert_allclose(code, torch.eye(6 ** 3))
        else:
            assert encoding.extra_dims == -2
            torch.testing.assert_allclose(code, torch.arange(6 ** 3))

        t_in_eps = t_in + (torch.rand_like(t_in) - 0.5) @ torch.diag(
            torch.tensor([0.2, 1.0, 0.8])
        )
        code_eps = encoding(t_in_eps)

        torch.testing.assert_allclose(code, code_eps)

        torch.testing.assert_allclose(
            encoding.tiles[..., 0], torch.linspace(-0.8, 0.8, 5)
        )
        torch.testing.assert_allclose(encoding.tiles[..., 1], torch.linspace(-4, 4, 5))
        torch.testing.assert_allclose(
            encoding.tiles[..., 2], torch.linspace(-2.2, 4.2, 5)
        )
//  [python]
def test_3d(self, one_hot):
        encoding = TileCode(
            [-1.0, -5.0, -3.0], [1.0, 5.0, 5.0], bins=5, one_hot=one_hot
        )
        encoding = torch.jit.script(encoding)

        t_in = torch.cartesian_prod(
            torch.linspace(-1.0, 1.0, 6),
            torch.linspace(-5.0, 5.0, 6),
            torch.linspace(-3.0, 5.0, 6),
        )
        code = encoding(t_in)

        if one_hot:
            assert encoding.extra_dims == 6 ** 3 - 3
            torch.testing.assert_close(code, torch.eye(6 ** 3))
        else:
            assert encoding.extra_dims == -2
            torch.testing.assert_close(code, torch.arange(6 ** 3))

        t_in_eps = t_in + (torch.rand_like(t_in) - 0.5) @ torch.diag(
            torch.tensor([0.2, 1.0, 0.8])
        )
        code_eps = encoding(t_in_eps)

        torch.testing.assert_close(code, code_eps)

        torch.testing.assert_close(
            encoding.tiles[..., 0], torch.linspace(-0.8, 0.8, 5)
        )
        torch.testing.assert_close(encoding.tiles[..., 1], torch.linspace(-4, 4, 5))
        torch.testing.assert_close(
            encoding.tiles[..., 2], torch.linspace(-2.2, 4.2, 5)
        )
//  [python]
def test_reparametrization(
        self,
        stride: int,
        k: int,
    ):
        module = MobileOneBlockUp(
            k=k,
            in_channels=3,
            stride=stride,
        )
        module.eval()

        x = torch.randn(1, 3, 28, 28)

        out1 = module(x)
        rep_module = module.reparametrize()
        rep_module.eval()

        out2 = rep_module(x)

        torch.testing.assert_allclose(out1, out2)
//  [python]
def test_reparametrization(
        self,
        stride: int,
        k: int,
    ):
        module = MobileOneBlockUp(
            k=k,
            in_channels=3,
            stride=stride,
        )
        module.eval()

        x = torch.randn(1, 3, 28, 28)

        out1 = module(x)
        rep_module = module.reparametrize()
        rep_module.eval()

        out2 = rep_module(x)

        torch.testing.assert_close(out1, out2)
//  [python]
def test_network(self, tau, network):
        class_ = network
        in_dim = (16,)
        out_dim = (4,)
        layers = [32, 4]
        if class_ is Ensemble:
            net1 = class_(in_dim, out_dim, num_heads=5, layers=layers)
            net1c = class_(in_dim, out_dim, num_heads=5, layers=layers)
            net2 = class_(in_dim, out_dim, num_heads=5, layers=layers)
            net2c = class_(in_dim, out_dim, num_heads=5, layers=layers)
        elif class_ is FelixNet:
            net1 = class_(in_dim, out_dim)
            net1c = class_(in_dim, out_dim)
            net2 = class_(in_dim, out_dim)
            net2c = class_(in_dim, out_dim)
        else:
            net1 = class_(in_dim, out_dim, layers)
            net1c = class_(in_dim, out_dim, layers)
            net2 = class_(in_dim, out_dim, layers)
            net2c = class_(in_dim, out_dim, layers)

        net1c.load_state_dict(net1.state_dict())
        net2c.load_state_dict(net2.state_dict())

        update_parameters(net1, net2, tau)

        for name, _ in net1.named_parameters():
            param1 = net1.state_dict()[name]
            param1c = net1c.state_dict()[name]

            param2 = net2.state_dict()[name]
            param2c = net2c.state_dict()[name]

            torch.testing.assert_allclose(
                param1.data, tau * param1c.data + (1 - tau) * param2c.data
            )

            torch.testing.assert_allclose(param2.data, param2c.data)

            assert param1 is not param1c
            if tau == 0:
                assert torch.allclose(param1.data, param2c.data)
            elif tau == 1:
                assert torch.allclose(param1.data, param1c.data)
            elif not (
                torch.allclose(param1.data, torch.zeros_like(param1.data))
                or name == "_scale"
            ):
                assert not (torch.allclose(param1.data, param1c.data))
                assert not (torch.allclose(param2.data, param1c.data))
//  [python]
def test_network(self, tau, network):
        class_ = network
        in_dim = (16,)
        out_dim = (4,)
        layers = [32, 4]
        if class_ is Ensemble:
            net1 = class_(in_dim, out_dim, num_heads=5, layers=layers)
            net1c = class_(in_dim, out_dim, num_heads=5, layers=layers)
            net2 = class_(in_dim, out_dim, num_heads=5, layers=layers)
            net2c = class_(in_dim, out_dim, num_heads=5, layers=layers)
        elif class_ is FelixNet:
            net1 = class_(in_dim, out_dim)
            net1c = class_(in_dim, out_dim)
            net2 = class_(in_dim, out_dim)
            net2c = class_(in_dim, out_dim)
        else:
            net1 = class_(in_dim, out_dim, layers)
            net1c = class_(in_dim, out_dim, layers)
            net2 = class_(in_dim, out_dim, layers)
            net2c = class_(in_dim, out_dim, layers)

        net1c.load_state_dict(net1.state_dict())
        net2c.load_state_dict(net2.state_dict())

        update_parameters(net1, net2, tau)

        for name, _ in net1.named_parameters():
            param1 = net1.state_dict()[name]
            param1c = net1c.state_dict()[name]

            param2 = net2.state_dict()[name]
            param2c = net2c.state_dict()[name]

            torch.testing.assert_close(
                param1.data, tau * param1c.data + (1 - tau) * param2c.data
            )

            torch.testing.assert_close(param2.data, param2c.data)

            assert param1 is not param1c
            if tau == 0:
                assert torch.allclose(param1.data, param2c.data)
            elif tau == 1:
                assert torch.allclose(param1.data, param1c.data)
            elif not (
                torch.allclose(param1.data, torch.zeros_like(param1.data))
                or name == "_scale"
            ):
                assert not (torch.allclose(param1.data, param1c.data))
                assert not (torch.allclose(param2.data, param1c.data))
//  [python]
def test_discount_cumusum_10(trajectory_dataset):
    vector = torch.tensor([1, 2, 3], dtype=torch.float32)
    discount = 1.0
    expected = torch.tensor([1, 2, 3], dtype=torch.float32)
    expected[0] = expected.sum()
    expected[1] = expected[1:].sum()
    expected[2] = expected[2:].sum()

    actual = trajectory_dataset.discount_cumsum(vector, discount)
    assert actual.shape == expected.shape
    torch.testing.assert_allclose(torch.tensor(actual), expected)
//  [python]
def test_discount_cumusum_10(trajectory_dataset):
    vector = torch.tensor([1, 2, 3], dtype=torch.float32)
    discount = 1.0
    expected = torch.tensor([1, 2, 3], dtype=torch.float32)
    expected[0] = expected.sum()
    expected[1] = expected[1:].sum()
    expected[2] = expected[2:].sum()

    actual = trajectory_dataset.discount_cumsum(vector, discount)
    assert actual.shape == expected.shape
    torch.testing.assert_close(torch.tensor(actual), expected)
//  [python]
def assert_allclose(actual, expected, rtol=1e-3, atol=1e-2):
    if actual is None:
        assert expected is None
    else:
        torch.testing.assert_allclose(actual, expected, rtol=rtol, atol=atol)
//  [python]
def assert_allclose(actual, expected, rtol=1e-3, atol=1e-2):
    if actual is None:
        assert expected is None
    else:
        torch.testing.assert_close(actual, expected, rtol=rtol, atol=atol)
//  [python]
def test_class_weighting():
    # test for checking the class weighting method
    targets = torch.tensor([1, 1, 1, 2, 2, 3], dtype=torch.long)
    n_classes = 4
    norm_val = 1.0

    weights = compute_class_weights(
        target=targets, n_classes=n_classes, norm_val=norm_val
    )
    weights = torch.round(weights, decimals=2)

    expected_weights = torch.tensor([0.0, 2.47, 3.48, 6.49])

    torch.testing.assert_allclose(actual=weights, expected=expected_weights)
//  [python]
def test_class_weighting():
    # test for checking the class weighting method
    targets = torch.tensor([1, 1, 1, 2, 2, 3], dtype=torch.long)
    n_classes = 4
    norm_val = 1.0

    weights = compute_class_weights(
        target=targets, n_classes=n_classes, norm_val=norm_val
    )
    weights = torch.round(weights, decimals=2)

    expected_weights = torch.tensor([0.0, 2.47, 3.48, 6.49])

    torch.testing.assert_close(actual=weights, expected=expected_weights)
//  [python]
def test_vector_equivariance():
    torch.manual_seed(1234)
    rotate = torch.tensor(
        [
            [0.9886788, -0.1102370, 0.1017945],
            [0.1363630, 0.9431761, -0.3030248],
            [-0.0626055, 0.3134752, 0.9475304],
        ]
    )

    model = create_model(
        load_example_args(
            "equivariant-transformer", prior_model=None, output_model="VectorOutput",
        )
    )
    z = torch.ones(100, dtype=torch.long)
    pos = torch.randn(100, 3)
    batch = torch.arange(50, dtype=torch.long).repeat_interleave(2)

    y = model(z, pos, batch)[0]
    y_rot = model(z, pos @ rotate, batch)[0]
    torch.testing.assert_allclose(y @ rotate, y_rot)
//  [python]
def test_vector_equivariance():
    torch.manual_seed(1234)
    rotate = torch.tensor(
        [
            [0.9886788, -0.1102370, 0.1017945],
            [0.1363630, 0.9431761, -0.3030248],
            [-0.0626055, 0.3134752, 0.9475304],
        ]
    )

    model = create_model(
        load_example_args(
            "equivariant-transformer", prior_model=None, output_model="VectorOutput",
        )
    )
    z = torch.ones(100, dtype=torch.long)
    pos = torch.randn(100, 3)
    batch = torch.arange(50, dtype=torch.long).repeat_interleave(2)

    y = model(z, pos, batch)[0]
    y_rot = model(z, pos @ rotate, batch)[0]
    torch.testing.assert_close(y @ rotate, y_rot)
//  [python]
def test_embedding_byte(self, num_embeddings, embedding_dim):
        quant_op = torch.ops.quantized.embedding_byte
        prepack_op = torch.ops.quantized.embedding_bag_prepack

        weights = torch.from_numpy((np.random.random_sample((
            num_embeddings, embedding_dim)) + 1).astype(np.float32))

        obs = PerChannelMinMaxObserver(dtype=torch.quint8, qscheme=torch.per_channel_affine_float_qparams, ch_axis=0)
        obs(weights)
        # Get the scale and zero point for the weight tensor
        qparams = obs.calculate_qparams()

        # Quantize the weights to 8bits
        qweight = torch.quantize_per_channel(weights, qparams[0], qparams[1], axis=0, dtype=torch.quint8)
        max_segments = 5
        max_segment_length = 20
        num_lengths = np.random.randint(1, max_segments + 1)
        lengths = np.random.randint(1, max_segment_length + 1,
                                    size=num_lengths).astype(np.int32)
        num_indices = np.sum(lengths)
        indices = torch.from_numpy(np.random.randint(
            low=0, high=num_embeddings, size=num_indices, dtype=np.int64))

        packed_weight = prepack_op(qweight)
        qresult = quant_op(packed_weight, indices, pruned_weights=False)

        ref = torch.embedding(weights, indices, padding_idx=-1, scale_grad_by_freq=False, sparse=False)
        torch.testing.assert_allclose(ref, qresult, atol=0.005, rtol=1e-3)
//  [python]
def test_embedding_byte(self, num_embeddings, embedding_dim):
        quant_op = torch.ops.quantized.embedding_byte
        prepack_op = torch.ops.quantized.embedding_bag_prepack

        weights = torch.from_numpy((np.random.random_sample((
            num_embeddings, embedding_dim)) + 1).astype(np.float32))

        obs = PerChannelMinMaxObserver(dtype=torch.quint8, qscheme=torch.per_channel_affine_float_qparams, ch_axis=0)
        obs(weights)
        # Get the scale and zero point for the weight tensor
        qparams = obs.calculate_qparams()

        # Quantize the weights to 8bits
        qweight = torch.quantize_per_channel(weights, qparams[0], qparams[1], axis=0, dtype=torch.quint8)
        max_segments = 5
        max_segment_length = 20
        num_lengths = np.random.randint(1, max_segments + 1)
        lengths = np.random.randint(1, max_segment_length + 1,
                                    size=num_lengths).astype(np.int32)
        num_indices = np.sum(lengths)
        indices = torch.from_numpy(np.random.randint(
            low=0, high=num_embeddings, size=num_indices, dtype=np.int64))

        packed_weight = prepack_op(qweight)
        qresult = quant_op(packed_weight, indices, pruned_weights=False)

        ref = torch.embedding(weights, indices, padding_idx=-1, scale_grad_by_freq=False, sparse=False)
        torch.testing.assert_close(ref, qresult, atol=0.005, rtol=1e-3)
//  [python]
def test_cpu(self):
        support = torch.tensor([[[0, 0, 0], [1, 0, 0], [2, 0, 0]]]).float()
        query = torch.tensor([[[0, 0, 0]]]).float()

        idx, dist = knn(support, query, 3)
        torch.testing.assert_allclose(idx, torch.tensor([[[0, 1, 2]]]))
        torch.testing.assert_allclose(dist, torch.tensor([[[0.0, 1.0, 4.0]]]))

        idx, dist = knn(support, query, 2)
        torch.testing.assert_allclose(idx, torch.tensor([[[0, 1]]]))

        with self.assertRaises(RuntimeError):
            knn(support, query, 5)
//  [python]
def test_cpu(self):
        support = torch.tensor([[[0, 0, 0], [1, 0, 0], [2, 0, 0]]]).float()
        query = torch.tensor([[[0, 0, 0]]]).float()

        idx, dist = knn(support, query, 3)
        torch.testing.assert_close(idx, torch.tensor([[[0, 1, 2]]]))
        torch.testing.assert_close(dist, torch.tensor([[[0.0, 1.0, 4.0]]]))

        idx, dist = knn(support, query, 2)
        torch.testing.assert_close(idx, torch.tensor([[[0, 1]]]))

        with self.assertRaises(RuntimeError):
            knn(support, query, 5)
//  [python]
def test_embedding_2d_indices(self):
        """
        Tests the case where 2D indices are passed into the operator
        In this case the operator computes the correct offsets argument.
        Output shape is dependent on the indices dimension.
        """
        quant_op = torch.ops.quantized.embedding_byte
        prepack_op = torch.ops.quantized.embedding_bag_prepack

        indices = torch.tensor([[9, 6, 5, 7, 8, 8, 9, 2, 8, 6, 6, 9, 1, 6, 8, 8], [3, 2, 3, 6, 3, 6, 5, 7, 0, 8, 4, 6, 5, 8, 2, 3]])
        weights = torch.randn(10, 12, dtype=torch.float32)

        ref = torch.embedding(weights, indices, padding_idx=-1, scale_grad_by_freq=False, sparse=False)
        obs = PerChannelMinMaxObserver(dtype=torch.quint8, qscheme=torch.per_channel_affine_float_qparams, ch_axis=0)
        obs(weights)
        qparams = obs.calculate_qparams()

        qweight = torch.quantize_per_channel(weights, qparams[0], qparams[1], axis=0, dtype=torch.quint8)
        packed_weight = prepack_op(qweight)
        qresult = quant_op(packed_weight, indices, pruned_weights=False)
        torch.testing.assert_allclose(ref, qresult, atol=0.05, rtol=1e-3)
//  [python]
def test_embedding_2d_indices(self):
        """
        Tests the case where 2D indices are passed into the operator
        In this case the operator computes the correct offsets argument.
        Output shape is dependent on the indices dimension.
        """
        quant_op = torch.ops.quantized.embedding_byte
        prepack_op = torch.ops.quantized.embedding_bag_prepack

        indices = torch.tensor([[9, 6, 5, 7, 8, 8, 9, 2, 8, 6, 6, 9, 1, 6, 8, 8], [3, 2, 3, 6, 3, 6, 5, 7, 0, 8, 4, 6, 5, 8, 2, 3]])
        weights = torch.randn(10, 12, dtype=torch.float32)

        ref = torch.embedding(weights, indices, padding_idx=-1, scale_grad_by_freq=False, sparse=False)
        obs = PerChannelMinMaxObserver(dtype=torch.quint8, qscheme=torch.per_channel_affine_float_qparams, ch_axis=0)
        obs(weights)
        qparams = obs.calculate_qparams()

        qweight = torch.quantize_per_channel(weights, qparams[0], qparams[1], axis=0, dtype=torch.quint8)
        packed_weight = prepack_op(qweight)
        qresult = quant_op(packed_weight, indices, pruned_weights=False)
        torch.testing.assert_close(ref, qresult, atol=0.05, rtol=1e-3)
//  [python]
def test_global_mean_loss():
    area = torch.randn(10, 10, device=get_device())
    loss = GlobalMeanLoss(area=area, loss=torch.nn.MSELoss())
    x = torch.zeros(10, 10, 10, 10, 10, device=get_device())
    y = torch.randn(10, 10, 10, 10, 10, device=get_device())
    result = loss(x, y)
    assert result.shape == ()
    assert isinstance(result, torch.Tensor)

    def global_weighted_mean(tensor, area):
        return (tensor * area[None, None, None, :, :]).sum(dim=(-1, -2)) / area.sum()

    mse = torch.nn.MSELoss()
    expected = mse(global_weighted_mean(x, area), global_weighted_mean(y, area))
    torch.testing.assert_allclose(result, expected)
//  [python]
def test_global_mean_loss():
    area = torch.randn(10, 10, device=get_device())
    loss = GlobalMeanLoss(area=area, loss=torch.nn.MSELoss())
    x = torch.zeros(10, 10, 10, 10, 10, device=get_device())
    y = torch.randn(10, 10, 10, 10, 10, device=get_device())
    result = loss(x, y)
    assert result.shape == ()
    assert isinstance(result, torch.Tensor)

    def global_weighted_mean(tensor, area):
        return (tensor * area[None, None, None, :, :]).sum(dim=(-1, -2)) / area.sum()

    mse = torch.nn.MSELoss()
    expected = mse(global_weighted_mean(x, area), global_weighted_mean(y, area))
    torch.testing.assert_close(result, expected)
//  [python]
def test_cpugpu(self):
        b = 20
        n = 1000
        known = torch.randn((b, 2 * n, 3))
        unknown = torch.randn((b, n, 3))

        dist_cpu, cpu_idx = three_nn(unknown, known)
        dist_cuda, cuda_idx = three_nn(unknown.cuda(), known.cuda())

        torch.testing.assert_allclose(dist_cpu, dist_cuda.cpu())
//  [python]
def test_cpugpu(self):
        b = 20
        n = 1000
        known = torch.randn((b, 2 * n, 3))
        unknown = torch.randn((b, n, 3))

        dist_cpu, cpu_idx = three_nn(unknown, known)
        dist_cuda, cuda_idx = three_nn(unknown.cuda(), known.cuda())

        torch.testing.assert_close(dist_cpu, dist_cuda.cpu())
//  [python]
def test_embedding_bag_2d_indices(self):
        """
        Tests the case where 2D indices are passed into the operator
        In this case the operator computes the correct offsets argument.
        """
        indices = torch.tensor([[9, 6, 5, 7, 8, 8, 9, 2, 8, 6, 6, 9, 1, 6, 8, 8], [3, 2, 3, 6, 3, 6, 5, 7, 0, 8, 4, 6, 5, 8, 2, 3]])
        weights = torch.randn(10, 12, dtype=torch.float32)

        embedding_bag = torch.nn.EmbeddingBag(
            num_embeddings=10,
            embedding_dim=12,
            include_last_offset=False, _weight=weights,
            scale_grad_by_freq=False, mode='sum'
        )
        result = embedding_bag(indices)

        pt_op = torch.ops.quantized.embedding_bag_byte_rowwise_offsets
        pt_prepack_op = torch.ops.quantized.embedding_bag_byte_prepack
        q_weights = pt_prepack_op(weights)
        qresult = pt_op(q_weights, indices, mode=0, pruned_weights=False)
        torch.testing.assert_allclose(result, qresult, atol=0.05, rtol=1e-3)

        # Test TorchBind based embedding_bag operator
        obs = PerChannelMinMaxObserver(dtype=torch.quint8, qscheme=torch.per_channel_affine_float_qparams, ch_axis=0)
        obs(weights)
        # Get the scale and zero point for the weight tensor
        qparams = obs.calculate_qparams()

        # Quantize the weights to 8bits
        qweight = torch.quantize_per_channel(weights, qparams[0], qparams[1], axis=0, dtype=torch.quint8)

        packed_weight = torch.ops.quantized.embedding_bag_prepack(qweight)
        qresult = torch.ops.quantized.embedding_bag_byte(packed_weight, indices, mode=0)

        torch.testing.assert_allclose(result, qresult, atol=0.05, rtol=1e-3)
//  [python]
def test_embedding_bag_2d_indices(self):
        """
        Tests the case where 2D indices are passed into the operator
        In this case the operator computes the correct offsets argument.
        """
        indices = torch.tensor([[9, 6, 5, 7, 8, 8, 9, 2, 8, 6, 6, 9, 1, 6, 8, 8], [3, 2, 3, 6, 3, 6, 5, 7, 0, 8, 4, 6, 5, 8, 2, 3]])
        weights = torch.randn(10, 12, dtype=torch.float32)

        embedding_bag = torch.nn.EmbeddingBag(
            num_embeddings=10,
            embedding_dim=12,
            include_last_offset=False, _weight=weights,
            scale_grad_by_freq=False, mode='sum'
        )
        result = embedding_bag(indices)

        pt_op = torch.ops.quantized.embedding_bag_byte_rowwise_offsets
        pt_prepack_op = torch.ops.quantized.embedding_bag_byte_prepack
        q_weights = pt_prepack_op(weights)
        qresult = pt_op(q_weights, indices, mode=0, pruned_weights=False)
        torch.testing.assert_close(result, qresult, atol=0.05, rtol=1e-3)

        # Test TorchBind based embedding_bag operator
        obs = PerChannelMinMaxObserver(dtype=torch.quint8, qscheme=torch.per_channel_affine_float_qparams, ch_axis=0)
        obs(weights)
        # Get the scale and zero point for the weight tensor
        qparams = obs.calculate_qparams()

        # Quantize the weights to 8bits
        qweight = torch.quantize_per_channel(weights, qparams[0], qparams[1], axis=0, dtype=torch.quint8)

        packed_weight = torch.ops.quantized.embedding_bag_prepack(qweight)
        qresult = torch.ops.quantized.embedding_bag_byte(packed_weight, indices, mode=0)

        torch.testing.assert_close(result, qresult, atol=0.05, rtol=1e-3)
//  [python]
def test_expected_returns(self):
        """
        Test the true values of the actual returns.

        Notes
        -------
        Checks the actual values return in the discounted returns without
        standardization and with the simple case of gamma=1
        """
        discounted_return = torch.tensor([15, 14, 12, 9, 5])
        value_function = self.loss.compute_true_value_function(
            rewards=self.rewards, gamma=1, standardize=False
        )
        torch.testing.assert_allclose(discounted_return, value_function)
//  [python]
def test_expected_returns(self):
        """
        Test the true values of the actual returns.

        Notes
        -------
        Checks the actual values return in the discounted returns without
        standardization and with the simple case of gamma=1
        """
        discounted_return = torch.tensor([15, 14, 12, 9, 5])
        value_function = self.loss.compute_true_value_function(
            rewards=self.rewards, gamma=1, standardize=False
        )
        torch.testing.assert_close(discounted_return, value_function)
//  [python]
def test_fftshift(size):
    ndims = len(size)
    x = torch.rand(size)
    x_np = x.numpy()
    for d in range(ndims):
        for axes in itertools.combinations(range(ndims), d + 1):
            y = fftshift(x, axes)
            y_np = np.fft.fftshift(x_np, axes)
            print(axes, size)
            print(x, '\n', x_np)
            print(y, '\n', y_np)
            torch.testing.assert_allclose(y, y_np)
//  [python]
def test_fftshift(size):
    ndims = len(size)
    x = torch.rand(size)
    x_np = x.numpy()
    for d in range(ndims):
        for axes in itertools.combinations(range(ndims), d + 1):
            y = fftshift(x, axes)
            y_np = np.fft.fftshift(x_np, axes)
            print(axes, size)
            print(x, '\n', x_np)
            print(y, '\n', y_np)
            torch.testing.assert_close(y, y_np)
//  [python]
def test_ifftshift(size):
    ndims = len(size)
    x = torch.rand(size)
    x_np = x.numpy()
    for d in range(ndims):
        for axes in itertools.combinations(range(ndims), d + 1):
            y = ifftshift(x, axes)
            y_np = np.fft.ifftshift(x_np, axes)
            torch.testing.assert_allclose(y, y_np)
//  [python]
def test_ifftshift(size):
    ndims = len(size)
    x = torch.rand(size)
    x_np = x.numpy()
    for d in range(ndims):
        for axes in itertools.combinations(range(ndims), d + 1):
            y = ifftshift(x, axes)
            y_np = np.fft.ifftshift(x_np, axes)
            torch.testing.assert_close(y, y_np)
//  [python]
def test_move(seat, action, initial, expected):
    result = apply(seat, action, initial)
    torch.testing.assert_allclose(initial, expected)
    torch.testing.assert_allclose(result, torch.zeros_like(result))
//  [python]
def test_move(seat, action, initial, expected):
    result = apply(seat, action, initial)
    torch.testing.assert_close(initial, expected)
    torch.testing.assert_close(result, torch.zeros_like(result))
//  [python]
def test_NSPDE_inverseDFT1D():

    batch, dim_x, dim_t = 2, 32, 64
    u0 = torch.rand(batch, 1, dim_x, dtype=torch.float32)
    xi = torch.rand(batch, 1, dim_x, dim_t, dtype=torch.float32)

    # create space-time grid of points 
    gridt = torch.tensor(np.linspace(0, 1, dim_t), dtype=torch.float32).reshape(1, 1, dim_t).repeat(batch, dim_x, 1)
    gridx = torch.tensor(np.linspace(0, 1, dim_x+1)[:-1], dtype=torch.float32).reshape(1, dim_x, 1).repeat(batch, 1, dim_t)
    grid = torch.stack([gridx, gridt], dim=-1)

    model = NeuralSPDE(dim=1, in_channels=1, noise_channels=1, hidden_channels=16, n_iter=4, modes1=16, modes2=50).cuda()

    out_physicsinformed = model(u0.cuda(), xi.cuda(), grid.cuda())
    out = model(u0.cuda(), xi.cuda())

    torch.testing.assert_allclose(out_physicsinformed, out, rtol=1e-03, atol=1e-08)
//  [python]
def test_NSPDE_inverseDFT1D():

    batch, dim_x, dim_t = 2, 32, 64
    u0 = torch.rand(batch, 1, dim_x, dtype=torch.float32)
    xi = torch.rand(batch, 1, dim_x, dim_t, dtype=torch.float32)

    # create space-time grid of points 
    gridt = torch.tensor(np.linspace(0, 1, dim_t), dtype=torch.float32).reshape(1, 1, dim_t).repeat(batch, dim_x, 1)
    gridx = torch.tensor(np.linspace(0, 1, dim_x+1)[:-1], dtype=torch.float32).reshape(1, dim_x, 1).repeat(batch, 1, dim_t)
    grid = torch.stack([gridx, gridt], dim=-1)

    model = NeuralSPDE(dim=1, in_channels=1, noise_channels=1, hidden_channels=16, n_iter=4, modes1=16, modes2=50).cuda()

    out_physicsinformed = model(u0.cuda(), xi.cuda(), grid.cuda())
    out = model(u0.cuda(), xi.cuda())

    torch.testing.assert_close(out_physicsinformed, out, rtol=1e-03, atol=1e-08)
//  [python]
def test_inverseDFT1D(dim_phys, modes, dim_fft):
    
    # create space-time grid of points 
    dim_x, dim_t = dim_phys[0], dim_phys[1]
    gridt = torch.tensor(np.linspace(0, 1, dim_t), dtype=torch.float).reshape(1, dim_t).repeat(dim_x, 1)
    gridx = torch.tensor(np.linspace(0, 1, dim_x+1)[:-1], dtype=torch.float).reshape(dim_x, 1).repeat(1, dim_t)
    if dim_fft == [2, 3]:
        grid = torch.stack([gridx, gridt], dim=-1)
    elif dim_fft == [2]:
        grid = gridx[:, 0].unsqueeze(-1)
    elif dim_fft == [3]:
        grid = gridt[0, :].unsqueeze(-1)

    # determine whether we want to recover more points
    if modes is None:
        modes = dim_phys
        s = None
    else:
        s = [dim_phys[i-2] for i in dim_fft]

    # create tensor in spectral domain
    batch, channels = 2, 4
    u_ft = torch.rand(batch, channels, modes[0], modes[1], dtype=torch.complex64)

    # compute the inverse DFT via our implementation and Pytorch's
    ans_torch = torch.fft.ifftn(u_ft, dim=dim_fft, s=s)
    ans_ours = inverseDFTn(u_ft, grid, dim=dim_fft, s=s)

    # check answers
    torch.testing.assert_allclose(ans_torch, ans_ours, rtol=1e-03, atol=1e-08)
//  [python]
def test_inverseDFT1D(dim_phys, modes, dim_fft):
    
    # create space-time grid of points 
    dim_x, dim_t = dim_phys[0], dim_phys[1]
    gridt = torch.tensor(np.linspace(0, 1, dim_t), dtype=torch.float).reshape(1, dim_t).repeat(dim_x, 1)
    gridx = torch.tensor(np.linspace(0, 1, dim_x+1)[:-1], dtype=torch.float).reshape(dim_x, 1).repeat(1, dim_t)
    if dim_fft == [2, 3]:
        grid = torch.stack([gridx, gridt], dim=-1)
    elif dim_fft == [2]:
        grid = gridx[:, 0].unsqueeze(-1)
    elif dim_fft == [3]:
        grid = gridt[0, :].unsqueeze(-1)

    # determine whether we want to recover more points
    if modes is None:
        modes = dim_phys
        s = None
    else:
        s = [dim_phys[i-2] for i in dim_fft]

    # create tensor in spectral domain
    batch, channels = 2, 4
    u_ft = torch.rand(batch, channels, modes[0], modes[1], dtype=torch.complex64)

    # compute the inverse DFT via our implementation and Pytorch's
    ans_torch = torch.fft.ifftn(u_ft, dim=dim_fft, s=s)
    ans_ours = inverseDFTn(u_ft, grid, dim=dim_fft, s=s)

    # check answers
    torch.testing.assert_close(ans_torch, ans_ours, rtol=1e-03, atol=1e-08)
//  [python]
def test_wins():
    # Black win
    board = tokened_board((TC, TOP), (BC, BOT))
    result = apply(B, CC, board)
    torch.testing.assert_allclose(result, torch.tensor([+1., -1.]).cuda())

    # White win
    board = tokened_board((CL, LEFT), (CR, RIGHT))
    result = apply(W, CC, board)
    torch.testing.assert_allclose(result, torch.tensor([-1., +1.]).cuda())
//  [python]
def test_wins():
    # Black win
    board = tokened_board((TC, TOP), (BC, BOT))
    result = apply(B, CC, board)
    torch.testing.assert_close(result, torch.tensor([+1., -1.]).cuda())

    # White win
    board = tokened_board((CL, LEFT), (CR, RIGHT))
    result = apply(W, CC, board)
    torch.testing.assert_close(result, torch.tensor([-1., +1.]).cuda())
//  [python]
def test_NSPDE_inverseDFT2D():
    batch, dim_x, dim_y,  dim_t = 2, 32, 32, 10
    u0 = torch.rand(batch, 1, dim_x, dim_y, dtype=torch.float32)
    xi = torch.rand(batch, 1, dim_x, dim_y, dim_t, dtype=torch.float32)

    # create space-time grid of points 
    gridt = torch.tensor(np.linspace(0, 1, dim_t), dtype=torch.float32).reshape(1, 1, 1, dim_t).repeat(batch, dim_x, dim_y, 1)
    gridx = torch.tensor(np.linspace(0, 1, dim_x+1)[:-1], dtype=torch.float32).reshape(1, dim_x, 1, 1).repeat(batch, 1, dim_y, dim_t)
    gridy = torch.tensor(np.linspace(0, 1, dim_y+1)[:-1], dtype=torch.float32).reshape(1, 1, dim_y, 1).repeat(batch, dim_x, 1, dim_t)
    grid = torch.stack([gridx, gridy, gridt], dim=-1)

    model = NeuralSPDE(dim=2, in_channels=1, noise_channels=1, hidden_channels=8, n_iter=4, modes1=8, modes2=8, modes3=8).cuda()
    
    out_physicsinformed = model(u0.cuda(), xi.cuda(), grid.cuda())
    out = model(u0.cuda(), xi.cuda())

    torch.testing.assert_allclose(out_physicsinformed, out, rtol=1e-03, atol=1e-08)
//  [python]
def test_NSPDE_inverseDFT2D():
    batch, dim_x, dim_y,  dim_t = 2, 32, 32, 10
    u0 = torch.rand(batch, 1, dim_x, dim_y, dtype=torch.float32)
    xi = torch.rand(batch, 1, dim_x, dim_y, dim_t, dtype=torch.float32)

    # create space-time grid of points 
    gridt = torch.tensor(np.linspace(0, 1, dim_t), dtype=torch.float32).reshape(1, 1, 1, dim_t).repeat(batch, dim_x, dim_y, 1)
    gridx = torch.tensor(np.linspace(0, 1, dim_x+1)[:-1], dtype=torch.float32).reshape(1, dim_x, 1, 1).repeat(batch, 1, dim_y, dim_t)
    gridy = torch.tensor(np.linspace(0, 1, dim_y+1)[:-1], dtype=torch.float32).reshape(1, 1, dim_y, 1).repeat(batch, dim_x, 1, dim_t)
    grid = torch.stack([gridx, gridy, gridt], dim=-1)

    model = NeuralSPDE(dim=2, in_channels=1, noise_channels=1, hidden_channels=8, n_iter=4, modes1=8, modes2=8, modes3=8).cuda()
    
    out_physicsinformed = model(u0.cuda(), xi.cuda(), grid.cuda())
    out = model(u0.cuda(), xi.cuda())

    torch.testing.assert_close(out_physicsinformed, out, rtol=1e-03, atol=1e-08)
//  [python]
def test_cutmix_disabled_with_coefficient(self):
        class Model(torch.nn.Module):
            def forward(self, batch):
                return batch

        images = torch.stack(
            [
                torch.ones(1, 10, 10),
                torch.ones(1, 10, 10) * 2,
                torch.ones(1, 10, 10) * 3,
            ]
        ).to(torch.float)

        class args:
            pass

        args = args()
        # Value of 1.0 for lambda disables cutmix.
        args.cutmix_lambda_low = 1.0
        args.cutmix_lambda_high = 1.0
        args.cutmix_disable_prob = 0.0
        args.precision = "32.32"

        model = poptorch.inferenceModel(
            AugmentationModel(
                Model(),
                use_mixup=False,
                use_cutmix=True,
                args=args,
            )
        )
        cutmixed_images, all_coeff = model(images)

        torch.testing.assert_allclose(all_coeff[0], torch.tensor(1.0))
        torch.testing.assert_allclose(cutmixed_images, images)
//  [python]
def test_cutmix_disabled_with_coefficient(self):
        class Model(torch.nn.Module):
            def forward(self, batch):
                return batch

        images = torch.stack(
            [
                torch.ones(1, 10, 10),
                torch.ones(1, 10, 10) * 2,
                torch.ones(1, 10, 10) * 3,
            ]
        ).to(torch.float)

        class args:
            pass

        args = args()
        # Value of 1.0 for lambda disables cutmix.
        args.cutmix_lambda_low = 1.0
        args.cutmix_lambda_high = 1.0
        args.cutmix_disable_prob = 0.0
        args.precision = "32.32"

        model = poptorch.inferenceModel(
            AugmentationModel(
                Model(),
                use_mixup=False,
                use_cutmix=True,
                args=args,
            )
        )
        cutmixed_images, all_coeff = model(images)

        torch.testing.assert_close(all_coeff[0], torch.tensor(1.0))
        torch.testing.assert_close(cutmixed_images, images)