tvm
Loading...
Searching...
No Matches
extern.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
24#ifndef TVM_TOPI_DETAIL_EXTERN_H_
25#define TVM_TOPI_DETAIL_EXTERN_H_
26
27#include <tvm/ir/prim/builtin.h>
28#include <tvm/te/operation.h>
29#include <tvm/tirx/builtin.h>
30
31#include <string>
32#include <utility>
33#include <vector>
34
35namespace tvm {
36namespace topi {
37namespace detail {
38
39using namespace tvm::te;
40
46using FExtern = std::function<PrimExpr(ffi::Array<BufferVar>, ffi::Array<BufferVar>)>;
47
65inline ffi::Array<Tensor> make_extern(const ffi::Array<ffi::Array<PrimExpr>>& out_shapes,
66 const std::vector<PrimType>& out_types,
67 const ffi::Array<Tensor>& inputs, FExtern fextern,
68 std::string name, std::string tag,
69 ::tvm::ffi::Map<ffi::String, ffi::Any> attrs) {
71 << "make_extern: out_shapes and out_types must have equal size";
72
73 ffi::Array<BufferVar> input_placeholders;
74 for (auto t : inputs) {
75 input_placeholders.push_back(tvm::tirx::decl_buffer(t->shape, t->dtype, t->op->name));
76 }
77 ffi::Array<BufferVar> output_placeholders;
78 for (size_t i = 0; i < out_shapes.size(); ++i) {
79 output_placeholders.push_back(tvm::tirx::decl_buffer(out_shapes[i], out_types[i], name));
80 }
81
82 auto body = fextern(input_placeholders, output_placeholders);
83 auto body_stmt = tvm::tirx::Evaluate(body);
84
85 auto op = ExternOp(name, tag, attrs, inputs, input_placeholders, output_placeholders, body_stmt);
86
87 ffi::Array<Tensor> outputs;
88 for (size_t i = 0; i < output_placeholders.size(); ++i) {
89 outputs.push_back(op.output(i));
90 }
91 return outputs;
92}
93
102inline Expr pack_buffer(BufferVar buf) {
103 TVM_FFI_ICHECK_GT(buf->shape.size(), 0) << "buf shape must have at least one element";
104 Expr shape =
106 Expr strides;
107 if (buf->strides.size() > 0) {
109 buf->strides);
110 } else {
111 strides = PrimExpr(0);
112 }
113 ffi::Array<Expr> pack_args{buf->data,
114 shape,
115 strides,
116 IntImm::Int32(static_cast<int64_t>(buf->shape.size())),
117 MakeConst(PrimType(buf->dtype), 0),
118 buf->elem_offset};
120}
121
131inline PrimExpr call_packed(ffi::Array<Expr> args) {
133 .as_or_throw<PrimExpr>();
134}
135
136} // namespace detail
137} // namespace topi
138} // namespace tvm
139#endif // TVM_TOPI_DETAIL_EXTERN_H_
Managed reference to CallNode.
Definition expr.h:474
Managed reference to ExprNode.
Definition base_expr.h:335
static IntImm Int32(int64_t value, Span span=Span())
Construct a scalar int32 constant.
Definition expr.h:528
Definition type.h:71
static PointerType VoidPointerTy(ffi::String storage_scope="")
Construct an opaque pointer with void element type.
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:401
Definition base_expr.h:137
static PrimType Int(int bits, int lanes=1)
Construct a signed integer type with fixed lanes.
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Managed reference to ExternOpNode.
Definition operation.h:388
Checked zero-state view over an ordinary VarNode with BufferType.
Definition buffer.h:179
Managed reference to EvaluateNode.
Definition stmt.h:356
Tensor expression language DSL.
Definition extracted_task.h:33
const Op & tvm_stack_make_shape()
Allocate a shape tuple on stack, return the handle.
const Op & tvm_call_packed()
See pesudo code.
const Op & tvm_stack_make_array()
Allocate a Tensor(DLTensor) on stack, return the handle.
BufferVar decl_buffer(ffi::Array< PrimExpr > shape, PrimType dtype=PrimType::Float(32), ffi::String name="buffer", ffi::String storage_scope="", Span span=Span())
Construct a new buffer given shape, and dtype.
PrimExpr MakeConst(PrimType dtype, ValueType value, Span span=Span())
Make a const value with certain data type.
Definition op.h:1002
Tensor shape(const Tensor &src, PrimType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition transform.h:2009
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
Operation node can generate one or multiple Tensors.
TIR builtin intrinsics.