Replies: 1 comment
-
The main difference between # Example 1 - return and hcl.return_ have the same results
# In this example, we use hcl.return_ in hcl.compute
# with Python return
def f(arg):
return arg+1
A = hcl.compute((10,), lambda x: f(x))
# with hcl.return_
def f(arg):
hcl.return_(arg+1)
A = hcl.compute((10,), lambda x: f(x))
# Equivalent Python code
for x in range(0, 10):
A[x] = x+1 In the above example, we can see that with Python # Example 2 - return and hcl.return_ work differently
# In this example, we still use hcl.compute API.
# However, since Python cannot recognize HeteroCL API, it doesn't understand the conditional statement.
# with Python return
def f(arg):
with hcl.if_(arg > 5):
return arg+2
with hcl.else_():
return arg+1
A = hcl.compute((10,), lambda x: f(x))
# Equivalent Python code - The Python program returns when it hits the first return statement
# Thus it never visits the else branch. Also, the if branch is omitted because Python doesn't understand it
for x in range(0, 10):
A[x] = x+2
# with hcl.return_
def f(arg):
with hcl.if_(arg > 5):
hcl.return_(arg+2)
with hcl.else_():
hcl.return_(arg+1)
A = hcl.compute((10,), lambda x: f(x))
# Equivalent Python code - This one acts as expected
for x in range(0, 10):
if x > 5:
A[x] = x+2
else:
A[x] = x+1 As you can see, the Python # Example 3: Differences between using def+reutrn and hcl.def_+hcl.return_
# with Python return + def
def f(arg):
return arg+1
A = hcl.compute((10,), lambda x: f(x))
# Equivalent Python code - f is inlined
for x in range(0, 10):
A[x] = x+1
# with hcl.return_ + hcl.def_
@hcl.def_([()])
def f(arg):
hcl.return_(arg+1)
A = hcl.compute((10,), lambda x: f(x))
# Equivalent Python code - f is not inlined
def f(arg):
return arg+1
for x in range(0, 10):
A[x] = f(x) In this example, the behavior of Python |
Beta Was this translation helpful? Give feedback.
-
What is the difference between
return
andhcl.return_
? When to use what?Beta Was this translation helpful? Give feedback.
All reactions