Best Python code snippet using yandex-tank
test_subgraph_rewriter.py
Source:test_subgraph_rewriter.py  
...27        # Replace `pattern` with the same pattern (shouldn't change28        # the underlying logic)29        subgraph_rewriter.replace_pattern(traced, pattern, pattern)30        traced.graph.lint()31        ref_output = comparison_fn(x)32        test_output = traced.forward(x)33        self.assertEqual(ref_output, test_output)34    def test_subgraph_rewriter_with_oneliner_pattern(self):35        class M(torch.nn.Module):36            def forward(self, x):37                val = torch.neg(x)38                return torch.add(val, val)39        def pattern(x):40            return torch.neg(x)41        def replacement(x):42            return torch.relu(x)43        def comparison(x):44            val = torch.relu(x)45            return torch.add(val, val)46        traced = symbolic_trace(M())47        comparison_fn = symbolic_trace(comparison)48        x = torch.rand(1, 3)49        subgraph_rewriter.replace_pattern(traced, pattern, replacement)50        traced.graph.lint()51        ref_output = comparison_fn(x)52        test_output = traced.forward(x)53        self.assertEqual(ref_output, test_output)54    def test_subgraph_rewriter_single_pattern_match(self):55        class M(torch.nn.Module):56            def forward(self, x):57                val = torch.neg(x) + torch.relu(x)58                return torch.add(val, val)59        def pattern(x):60            return torch.neg(x) + torch.relu(x)61        def replacement(x):62            return torch.relu(x)63        def comparison(x):64            val = torch.relu(x)65            return torch.add(val, val)66        traced = symbolic_trace(M())67        comparison_fn = symbolic_trace(comparison)68        x = torch.rand(1, 3)69        subgraph_rewriter.replace_pattern(traced, pattern, replacement)70        traced.graph.lint()71        ref_output = comparison_fn(x)72        test_output = traced.forward(x)73        self.assertEqual(ref_output, test_output)74    def test_subgraph_rewriter_multiple_pattern_match(self):75        class M(torch.nn.Module):76            def forward(self, x, w1, w2):77                m1 = torch.cat([w1, w2]).sum()78                m2 = torch.cat([w1, w2]).sum()79                return x + torch.max(m1) + torch.max(m2)80        def pattern(w1, w2):81            return torch.cat([w1, w2]).sum()82        def replacement(w1, w2):83            return torch.stack([w1, w2])84        def comparison(x, w1, w2):85            m1 = torch.stack([w1, w2])86            m2 = torch.stack([w1, w2])87            return x + torch.max(m1) + torch.max(m2)88        traced = symbolic_trace(M())89        comparison_fn = symbolic_trace(comparison)90        x = torch.rand(1, 3)91        w1 = torch.rand(1, 3)92        w2 = torch.rand(1, 3)93        subgraph_rewriter.replace_pattern(traced, pattern, replacement)94        traced.graph.lint()95        ref_outs = comparison_fn(x, w1, w2)96        test_outs = traced.forward(x, w1, w2)97        self.assertEqual(ref_outs, test_outs)98    def test_subgraph_rewriter_graph_argument_order(self):99        class M(torch.nn.Module):100            def forward(self, x, y):101                return torch.mm(x, y)102        def pattern(x, y):103            return torch.mm(x, y)104        def comparison(x, y):105            return torch.mm(x, y)106        traced = symbolic_trace(M())107        comparison_fn = symbolic_trace(comparison)108        x = torch.randn(3, 4)109        y = torch.randn(4, 5)110        subgraph_rewriter.replace_pattern(traced, pattern, pattern)111        traced.graph.lint()112        ref_outs = comparison_fn(x, y)113        test_outs = traced.forward(x, y)114        self.assertEqual(ref_outs, test_outs)115    def test_subgraph_rewriter_correct_output_replacement(self):116        class M(torch.nn.Module):117            def forward(self, x, y):118                val = torch.neg(y) + torch.relu(x)119                return torch.add(val, val)120        def pattern(x):121            return torch.relu(x)122        def replacement(x):123            return torch.neg(x)124        def comparison(x, y):125            val = torch.neg(y) + torch.neg(x)126            return torch.add(val, val)127        traced = symbolic_trace(M())128        comparison_fn = symbolic_trace(comparison)129        x = torch.randn(4, 4)130        y = torch.randn(4, 4)131        subgraph_rewriter.replace_pattern(traced, pattern, replacement)132        traced.graph.lint()133        ref_outs = comparison_fn(x, y)134        test_outs = traced.forward(x, y)135        self.assertEqual(ref_outs, test_outs)136    def test_subgraph_rewriter_traced_as_callable(self):137        class M(torch.nn.Module):138            def forward(self, x):139                val = torch.neg(x) + torch.relu(x)140                return torch.add(val, val)141        class Pattern(torch.nn.Module):142            def forward(self, x):143                return torch.neg(x) + torch.relu(x)144        class Replacement(torch.nn.Module):145            def forward(self, x):146                return torch.sigmoid(x)147        def comparison(x):148            val = torch.sigmoid(x)149            return torch.add(val, val)150        traced = symbolic_trace(M())151        traced_pattern = symbolic_trace(Pattern())152        traced_replacement = symbolic_trace(Replacement())153        comparison_fn = symbolic_trace(comparison)154        x = torch.randn(3, 4)155        subgraph_rewriter.replace_pattern(traced, traced_pattern, traced_replacement)156        traced.graph.lint()157        ref_outs = comparison_fn(x)158        test_outs = traced.forward(x)159        self.assertEqual(ref_outs, test_outs)160    def test_subgraph_rewriter_pattern_is_entire_graph(self):161        class M(torch.nn.Module):162            def forward(self, x):163                a = torch.neg(x)164                return torch.add(a, a)165        def pattern(x):166            a = torch.neg(x)167            return torch.add(a, a)168        def replacement(x):169            a = torch.sigmoid(x)170            return torch.cat([a, a])171        traced = symbolic_trace(M())172        comparison_fn = symbolic_trace(replacement)173        x = torch.randn(3, 4)174        subgraph_rewriter.replace_pattern(traced, pattern, replacement)175        traced.graph.lint()176        ref_outs = comparison_fn(x)177        test_outs = traced.forward(x)178        self.assertEqual(ref_outs, test_outs)179    def test_subgraph_rewriter_pattern_output_pattern_node_can_have_users_that_are_not_matched(self):180        class M(torch.nn.Module):181            def forward(self, x):182                y = torch.relu(x)183                return torch.neg(y) - y184        def pattern(x):185            return torch.relu(x)186        def replacement(x):187            return torch.sigmoid(x)188        def comparison(x):189            y = torch.sigmoid(x)190            return torch.neg(y) - y191        traced = symbolic_trace(M())192        comparison_fn = symbolic_trace(comparison)193        x = torch.randn(3, 4)194        subgraph_rewriter.replace_pattern(traced, pattern, replacement)195        traced.graph.lint()196        ref_outs = comparison_fn(x)197        test_outs = traced.forward(x)198        self.assertEqual(ref_outs, test_outs)199    def test_subgraph_rewriter_internal_pattern_nodes_cannot_have_users_that_are_not_matched(self):200        class M(torch.nn.Module):201            def forward(self, x, w1, w2, b1, b2):202                m0 = torch.cat([w1, w2])203                m1 = torch.cat([w1, w2])204                m2 = torch.cat([x, b2])205                t0 = torch.addmm(b1, m1, m2.t())206                t1 = torch.sum(w1, 1)207                t2 = torch.addmm(b1, m1, m2.t())208                return torch.sum(t1), torch.sum(t2)209        def pattern(x, w1, w2, b1, b2):210            m1 = torch.cat([w1, w2])...ls.py
Source:ls.py  
...179180def legal_neighbors(state,neighbors,comparison_fn):181	legal_neighbors = []182	for neighbor in neighbors:183		if comparison_fn(state,neighbor):184			legal_neighbors.append(neighbor)185	return legal_neighbors186187def all_neighbors(state,neighbors):188	comparison_fn = no_checking189	return legal_neighbors(state,neighbors,comparison_fn)190191def strictly_decreasing(state,neighbors):192	comparison_fn = less_than193	return legal_neighbors(state,neighbors,comparison_fn)194195def strictly_increasing(state,neighbors):196	comparison_fn = greater_than197	return legal_neighbors(state,neighbors,comparison_fn)
...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
