Read about our upcoming Code of Conduct on this issue

This instance will be upgraded to Heptapod 0.28.1 on 2022-01-26 at 16:00 UTC+1 (a few minutes of down time)

Commit 7064f6ce authored by Pierre Augier's avatar Pierre Augier
Browse files

Filter code for annotations

parent 86791d7060b9
......@@ -8,14 +8,14 @@ from pprint import pprint
from capturex import CaptureX, make_code_external
from transonic.analyses.util import get_annotations, print_ast, print_dump
from transonic.analyses.util import get_annotations, print_ast, print_dump, filter_code_typevars
path_examples = Path("examples")
files = sorted(path_examples.glob("*.py"))
with open(files[2]) as file:
with open(files[1]) as file:
code = file.read()
print("ast.parse")
......@@ -96,16 +96,8 @@ for boosted in boosteds.values():
for definition in boosted.values():
objects.append(definition)
capturex_annotations = CaptureX(
objects,
module,
defuse_chains=duc,
usedef_chains=udc,
ancestors=ancestors,
consider_annotations="only",
)
code_dependance_annotations = make_code_external(capturex_annotations.external)
code_dependance_annotations = filter_code_typevars(module, duc, ancestors)
capturex = CaptureX(
objects,
......@@ -130,4 +122,4 @@ for kind, boosted in boosteds.items():
for key, definition in boosted.items():
annotations[kind][key] = get_annotations(definition, namespace)
pprint(annotations)
\ No newline at end of file
pprint(annotations)
import numpy as np
import transonic as ts
from transonic import Type, NDim, Array, Union
import numpy as np
import skimage
T = Type(int, np.complex128)
N = NDim(1, 3)
dim = 2
dim += 1
N = NDim(1, dim)
A = Array[T, N]
A1 = Array[np.float32, N + 1]
......@@ -14,8 +21,11 @@ N1 = NDim(4, 5)
T = Type(int, np.complex128)
a_type_var = "hello"
myconst = 0
cdict = skimage.color.color_dict
@ts.boost
def compute(a: A, b: A, c: T, d: Union[A, A1], e: str):
print(e)
......
......@@ -10,7 +10,7 @@ class MyClass:
self.a = a
self.b = b
def compute(self, n):
def compute(self, n: int):
a = self.a
b = self.b
......@@ -18,18 +18,13 @@ class MyClass:
if ts.is_transpiled:
result = ts.use_block("block0")
else:
# transonic block (
# transonic signature (
# float[][] a, b;
# int n
# ) -> result
# blabla
# )
# blibli
# transonic block (
# transonic signature (
# float[][][] a, b;
# int n
# ) -> result
# )
result = np.zeros_like(a)
for _ in range(n):
result += a ** 2 + b ** 3
......@@ -39,18 +34,13 @@ class MyClass:
if ts.is_transpiled:
result = ts.use_block("block1")
else:
# transonic block (
# transonic signature (
# float[][] a, b;
# int n
# ) -> (result)
# blabla
# blibli
# )
# transonic block (
# transonic signature (
# float[][][] a, b;
# int n
# ) -> (result)
# )
result = np.zeros_like(a)
for _ in range(n):
result += a ** 2 + b ** 3
......
......@@ -26,15 +26,15 @@ class MyClass:
if ts.is_transpiled:
result = ts.use_block("block0")
else:
# transonic block (
# transonic signature (
# A a; A1 b;
# int n
# ) -> result
# float n
# )
# transonic block (
# transonic signature (
# int[:] a, b;
# float n
# ) -> result
# )
result = a ** 2 + b.mean() ** 3 + n
......
......@@ -4,6 +4,8 @@ See https://bitbucket.org/fluiddyn/fluidpythran/issues/29/
"""
from transonic import with_blocks, block
T = int
def non_pythranizable(arg):
"""represent a function that can not be compiled by Pythran"""
......@@ -13,10 +15,20 @@ def non_pythranizable(arg):
@with_blocks
def func0(arg: int):
a = non_pythranizable(arg)
a = b = non_pythranizable(arg)
c: float = 1.2
with block():
# transonic signature(
# T a, b
# )
# transonic signature(
a: int
b: float
# )
with block(a=float):
return a + arg
return a + b + c + arg
@with_blocks
......@@ -24,7 +36,8 @@ def func1(arg: int):
a = non_pythranizable(arg)
with block(a=float):
with block():
a: int
tmp = a + arg
return non_pythranizable(tmp)
from pathlib import Path
import gast as ast
import beniget
from transonic.analyses.util import (
get_annotations,
print_ast,
print_dump,
print_unparsed,
filter_code_typevars
)
path_examples = Path("examples")
files = sorted(path_examples.glob("*.py"))
with open(files[1]) as file:
code = file.read()
print("ast.parse")
module = ast.parse(code)
print("compute DefUseChains")
duc = beniget.DefUseChains()
duc.visit(module)
print("compute Ancestors")
ancestors = beniget.Ancestors()
ancestors.visit(module)
code_filtered = filter_code_typevars(module, duc, ancestors)
print(code_filtered)
\ No newline at end of file
......@@ -12,6 +12,10 @@ def print_dump(node):
print(astunparse.dump(node))
def print_unparsed(node):
print(astunparse.unparse(node))
def _fill_ast_annotations_function(function_def, ast_annotations):
dict_node = ast_annotations.value
......@@ -65,3 +69,36 @@ def get_annotations(object_def, namespace):
pass
exec(source, namespace)
return namespace["annotations"]
def filter_code_typevars(module, duc, ancestors):
module_filtered = ast.Module()
kept = module_filtered.body = []
suppressed = set()
def fill_suppressed(def_):
for user in def_.users():
parent_in_body = ancestors.parents(user.node)[1]
suppressed.add(parent_in_body)
fill_suppressed(user)
for node in module.body:
if node in suppressed:
continue
if isinstance(node, ast.Import):
if node.names[0].name in ["transonic", "numpy"]:
kept.append(node)
else:
def_ = duc.chains[node.names[0]]
fill_suppressed(def_)
# suppressed.add()
elif isinstance(node, ast.ImportFrom):
if node.module in ["transonic", "numpy"]:
kept.append(node)
elif isinstance(node, (ast.Assign, ast.AugAssign)):
kept.append(node)
return astunparse.unparse(module_filtered)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment