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/op.h>
28#include <tvm/te/operation.h>
29#include <tvm/tirx/op/abi.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<TensorVar>, ffi::Array<TensorVar>)>;
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) {
70 TVM_FFI_ICHECK_EQ(out_shapes.size(), out_types.size())
71 << "make_extern: out_shapes and out_types must have equal size";
72
73 ffi::Array<TensorVar> input_placeholders;
74 for (auto t : inputs) {
75 input_placeholders.push_back(tvm::tirx::decl_tensor(t->shape, t->dtype, t->op.value()->name));
76 }
77 ffi::Array<TensorVar> output_placeholders;
78 for (size_t i = 0; i < out_shapes.size(); ++i) {
79 output_placeholders.push_back(tvm::tirx::decl_tensor(out_shapes[i], out_types[i], name));
80 }
81
82 auto body = fextern(input_placeholders, output_placeholders);
83 auto body_stmt = tvm::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(TensorVar buf) {
103 using namespace tvm::prim;
104 TVM_FFI_ICHECK_GT(buf->shape.size(), 0) << "buf shape must have at least one element";
106 Expr strides;
107 if (buf->strides.size() > 0) {
109 } else {
110 strides = PrimExpr(0);
111 }
112 ffi::Array<Expr> pack_args{buf->data,
113 shape,
114 strides,
115 IntImm::Int32(static_cast<int64_t>(buf->shape.size())),
116 MakeConst(PrimType(buf->dtype), 0),
117 buf->elem_offset};
119}
120
130inline PrimExpr call_packed(ffi::Array<Expr> args) {
131 return Call(PrimType::Int(32), tvm::tirx::call_packed_op(), args).as_or_throw<PrimExpr>();
132}
133
134} // namespace detail
135} // namespace topi
136} // namespace tvm
137#endif // TVM_TOPI_DETAIL_EXTERN_H_
Abi operations for TIRx.
Managed reference to CallNode.
Definition expr.h:972
Managed reference to EvaluateNode.
Definition stmt.h:372
Managed reference to ExprNode.
Definition base_expr.h:359
static IntImm Int32(ffi::BigInt value, Location loc=UnknownLoc())
Construct a scalar int32 constant.
Definition expr.h:181
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:447
Definition base_expr.h:161
static TVM_TIRX_DLL PrimType Int(int bits, int lanes=1)
Construct a signed integer type with fixed lanes.
Definition type.h:127
static TVM_TIRX_DLL PtrType VoidPtrType(ffi::String storage_scope="")
Construct an opaque pointer with void element type.
Managed reference to ExternOpNode.
Definition operation.h:402
PrimExpr elem_offset
The offset in terms of number of dtype elements (including lanes)
Definition type.h:87
ffi::Array< PrimExpr > shape
The type of the tensor prior to flattening.
Definition type.h:80
PrimType dtype
dtype in the content of the tensor
Definition type.h:71
ffi::Array< PrimExpr > strides
The strides of each dimension This can be an empty array, indicating array is contiguous.
Definition type.h:85
Checked zero-state view over an ordinary VarNode with TensorType.
Definition expr.h:50
Shared primitive construction and constant helpers.
Definition expr.h:47
PrimExpr MakeConst(PrimType dtype, ValueType value, Location loc=UnknownLoc())
Make a const value with certain data type.
Definition op.h:553
Tensor expression language DSL.
Definition extracted_task.h:33
TVM_TIRX_DLL TensorVar decl_tensor(ffi::Array< PrimExpr > shape, PrimType dtype=PrimType::Float(32), ffi::String name="tensor", ffi::String storage_scope="", Location loc=UnknownLoc())
Construct a new tensor given shape, and dtype.
TVM_TIRX_DLL const Op & stack_make_dltensor_op()
Construct a stack-allocated DLTensor.
TVM_TIRX_DLL const Op & stack_make_shape_op()
Construct a stack-allocated shape tuple.
TVM_TIRX_DLL const Op & call_packed_op()
Invoke a runtime packed function.
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:2016
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
Operation node can generate one or multiple Tensors.