You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by Pratyush Das via Apache TVM Discuss <no...@discuss.tvm.ai> on 2025/06/05 12:09:32 UTC
[Apache TVM Discuss] [Questions] Provide external functions as TVMScript?
I have a simple TVMScript program:
```
import tvm
from tvm.ir.module import IRModule
from tvm.script import tir as T
@T.prim_func
def main(A: T.Buffer[(128,), "float32"],
B: T.Buffer[(128,), "float32"],
C: T.Buffer[(128,), "float32"]):
T.func_attr({"global_symbol": "main", "tir.noalias": True})
for i in range(128):
with T.block("C"):
vi = T.axis.spatial(128, i)
C[vi] = A[vi] + B[vi]
mod = tvm.IRModule.from_expr(main)
print(mod["main"].script())
```
Can I provide an external definition fo the kernel:
```
import tvm
from tvm.ir.module import IRModule
from tvm.script import tir as T
def foo(A, B, C):
# Convert A,B,C to numpy.ndarray or something?
__ my_random_kernel(A, B, C)
@T.prim_func
def main(A: T.Buffer[(128,), "float32"],
B: T.Buffer[(128,), "float32"],
C: T.Buffer[(128,), "float32"]):
T.func_attr({"global_symbol": "main", "tir.noalias": True})
foo(A, B, C)
```
It seems like external definitions can only be provided at the Tensor Expression level https://tvm.apache.org/docs/v0.9.0/how_to/work_with_schedules/extern_op.html.
Is there a way to provide external function definitions at the TVMScript level?
---
[Visit Topic](https://discuss.tvm.apache.org/t/provide-external-functions-as-tvmscript/18371/1) to respond.
You are receiving this because you enabled mailing list mode.
To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/643ee2f91e0923d019cfcd474cbb7775dd300fc8aa9cebc183c89d04c3438152).
[Apache TVM Discuss] [Questions] Provide external functions as TVMScript?
Posted by tqchen via Apache TVM Discuss <no...@discuss.tvm.ai>.
The easies way to do this is via ffi call or Packed Func call, where you can call from both relax and tir.
see https://tvm.apache.org/docs/how_to/tutorials/customize_opt.html#library-dispatch for relax dispatch. For TIR functions you can use intrinsic call_packed to achieve the goal
---
[Visit Topic](https://discuss.tvm.apache.org/t/provide-external-functions-as-tvmscript/18371/2) to respond.
You are receiving this because you enabled mailing list mode.
To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/3d56f4118d05d0ddc97d667d0226fddb7c37be1a9f258828203ddc3e04bc8662).
[Apache TVM Discuss] [Questions] Provide external functions as TVMScript?
Posted by Ruihang Lai via Apache TVM Discuss <no...@discuss.tvm.ai>.
[quote="reikdas, post:5, topic:18371"]
`out = R.call_dps_packed("foo", (a), out_sinfo=R.Tensor((n,), dtype="float32"))`
[/quote]
Hi @reikdas, your `foo` doesn't make any effective change to `out`, since the line of `out = A.numpy()` is pointing the variable `out` to the object of `A.numpy()`, rather than in-place mutating the input NDArray `out`.
There are two ways to make it properly work. The first way is to use `out.copyfrom(A)`, in which way there will be a cuda memory copy from `A` to `out`:
```python
@tvm.register_func("foo", override=True)
def foo(A: tvm.nd.NDArray, out: tvm.nd.NDArray):
out.copyfrom(A)
print(out)
```
The second approach is to use `call_pure_packed`, in which way your customized `foo` needs to handle memory allocation for output. (Or like the example below, `foo` just returns `A` instead of allocating new NDArray, where the return value shares the same memory as `A`).
```python
import numpy as np
import tvm
from tvm.script import ir as I
from tvm.script import relax as R
from tvm.script import tir as T
@tvm.register_func("foo", override=True)
def foo(A):
return A
@I.ir_module
class Module:
@R.function
def main(a: R.Tensor(("n",), dtype="float32")):
with R.dataflow():
n = T.int64()
out = R.call_pure_packed("foo", a, sinfo_args=R.Tensor((n,), dtype="float32"))
R.output(out)
return out
if __name__ == "__main__":
ex = tvm.relax.build(Module, target="llvm")
vm = tvm.relax.VirtualMachine(ex, tvm.cpu())
a = tvm.nd.array(np.array([1, 2, 3, 4, 5], dtype=np.float32), device=tvm.cpu())
out = vm["main"](a)
print(out)
```
Let me know if this makes sense to you.
---
[Visit Topic](https://discuss.tvm.apache.org/t/provide-external-functions-as-tvmscript/18371/6) to respond.
You are receiving this because you enabled mailing list mode.
To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/1e10e374b6ac25d649dc13c02c73ac25fda4c8984c3bf835ddaa9211280ae320).
[Apache TVM Discuss] [Questions] Provide external functions as TVMScript?
Posted by Pratyush Das via Apache TVM Discuss <no...@discuss.tvm.ai>.
I was able to invoke an external function using Relax:
```
import tvm
from tvm.script import relax as R
from tvm.script import ir as I
import numpy as np
@tvm.register_func("foo", override=True)
def foo(A, out):
out = A.numpy()
print(out)
@I.ir_module
class Module:
@R.function
def main(a: R.Tensor(("n",), dtype="float32")):
with R.dataflow():
out = R.call_dps_packed("foo", (a), out_sinfo=R.Tensor((n,), dtype="float32"))
R.output(out)
return out
if __name__ == "__main__":
ex = tvm.relax.build(Module, target="llvm")
vm = tvm.relax.VirtualMachine(ex, tvm.cpu())
a = tvm.nd.array(np.array([1, 2, 3, 4, 5], dtype=np.float32), device=tvm.cpu())
out = vm["main"](a)
print(out)
```
However, the `print()` from inside `foo` prints `[1, 2, 3, 4, 5]` (as expected), whereas the `print()` from `__main__` prints something along the lines of `[8.9683102e-44 0.0000000e+00 6.7262326e-44 0.0000000e+00 4.8746746e-35]`.
It feels like I'm somehow not handling the datatype packing correctly.
---
[Visit Topic](https://discuss.tvm.apache.org/t/provide-external-functions-as-tvmscript/18371/5) to respond.
You are receiving this because you enabled mailing list mode.
To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/10909c497f5d854d7299a8b4bb0b8fc37cc5e15b3b3fa9beb36d6b0c317a8a9a).
[Apache TVM Discuss] [Questions] Provide external functions as TVMScript?
Posted by tqchen via Apache TVM Discuss <no...@discuss.tvm.ai>.
we can not support call_packed in this case because foo is a python function, packed function needs to be registered as global where you can invoke as string
https://mlc.ai/chapter_end_to_end/index.html#integrate-existing-libraries-in-the-environment contains some examples on how global functions are registered
---
[Visit Topic](https://discuss.tvm.apache.org/t/provide-external-functions-as-tvmscript/18371/4) to respond.
You are receiving this because you enabled mailing list mode.
To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/47c2db63477a9a26bae9b825b91e5bdcb90dd59020cdc3215543412139c77168).
[Apache TVM Discuss] [Questions] Provide external functions as TVMScript?
Posted by Pratyush Das via Apache TVM Discuss <no...@discuss.tvm.ai>.
I tried a simple program:
```
import tvm
from tvm.ir.module import IRModule
from tvm.script import tir as T
import numpy as np
def foo(A):
A[0] = 2.0
@T.prim_func
def main(a: T.handle):
T.func_attr({"global_symbol": "main", "tir.noalias": True})
A = T.match_buffer(a, (5,), "float32")
tvm.tir.call_packed("foo", A)
if __name__ == "__main__":
mod = IRModule.from_expr(main)
f = tvm.build(mod, target="llvm")
a = tvm.nd.array(np.array([1, 2, 3, 4, 5], dtype=np.float32), device=tvm.cpu())
f(a)
print(a.numpy())
```
But got the error:
```
error: Internal Error: function must be of type Intrin, ScopeHandler or SpecialStmt, but it is function
|
13 | tvm.tir.call_packed("foo", A)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
Inlining `foo` manually instead of using `tvm.tir.call_packed` works.
Am I using `call_packed` incorrectly? Do I need to convert `A` to some other type before passing it?
---
[Visit Topic](https://discuss.tvm.apache.org/t/provide-external-functions-as-tvmscript/18371/3) to respond.
You are receiving this because you enabled mailing list mode.
To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/ef3372504af23b97c49a90d4bef95947d34e1e8ea827d89d307338a4dbba98bf).