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#include <tvm/tirx/type.h>
31
32#include <string>
33#include <utility>
34#include <vector>
35
36namespace tvm {
37namespace topi {
38namespace detail {
39
40using namespace tvm::te;
41
47using FExtern = std::function<PrimExpr(ffi::Array<TensorVar>, ffi::Array<TensorVar>)>;
48
66inline ffi::Array<Tensor> make_extern(const ffi::Array<ffi::Array<PrimExpr>>& out_shapes,
67 const std::vector<PrimType>& out_types,
68 const ffi::Array<Tensor>& inputs, FExtern fextern,
69 std::string name, std::string tag,
70 ::tvm::ffi::Map<ffi::String, ffi::Any> attrs) {
71 TVM_FFI_ICHECK_EQ(out_shapes.size(), out_types.size())
72 << "make_extern: out_shapes and out_types must have equal size";
73
74 ffi::Array<TensorVar> input_placeholders;
75 for (auto t : inputs) {
76 input_placeholders.push_back(
77 tvm::tirx::TensorVar(t->op.value()->name, tvm::tirx::TensorType(t->shape, t->dtype)));
78 }
79 ffi::Array<TensorVar> output_placeholders;
80 for (size_t i = 0; i < out_shapes.size(); ++i) {
81 output_placeholders.push_back(
82 tvm::tirx::TensorVar(name, tvm::tirx::TensorType(out_shapes[i], out_types[i])));
83 }
84
85 auto body = fextern(input_placeholders, output_placeholders);
86 auto body_stmt = tvm::Evaluate(body);
87
88 auto op = ExternOp(name, tag, attrs, inputs, input_placeholders, output_placeholders, body_stmt);
89
90 ffi::Array<Tensor> outputs;
91 for (size_t i = 0; i < output_placeholders.size(); ++i) {
92 outputs.push_back(op.output(i));
93 }
94 return outputs;
95}
96
105inline Expr pack_buffer(TensorVar buf) {
106 using namespace tvm::prim;
107 TVM_FFI_ICHECK_GT(buf->shape.size(), 0) << "buf shape must have at least one element";
109 Expr strides;
110 if (tvm::tirx::MatchShapeStridePattern(buf.ty()).value().get<1>().size() > 0) {
112 tvm::tirx::MatchShapeStridePattern(buf.ty()).value().get<1>());
113 } else {
114 strides = PrimExpr(0);
115 }
116 ffi::Array<Expr> pack_args{buf->data,
117 shape,
118 strides,
119 IntImm::Int32(static_cast<int64_t>(buf->shape.size())),
120 MakeConst(PrimType(buf->dtype), 0),
121 tvm::tirx::MatchShapeStridePattern(buf.ty()).value().get<2>()};
123}
124
134inline PrimExpr call_packed(ffi::Array<Expr> args) {
135 return Call(PrimType::Int(32), tvm::tirx::call_packed_op(), args).as_or_throw<PrimExpr>();
136}
137
138} // namespace detail
139} // namespace topi
140} // namespace tvm
141#endif // TVM_TOPI_DETAIL_EXTERN_H_
Shared primitive construction and constant helpers.
Types specific to TIRX.
Abi operations for TIRx.
Managed reference to CallObj.
Definition expr.h:825
Managed reference to EvaluateObj.
Definition stmt.h:386
Managed reference to ExprObj.
Definition expr.h:101
static IntImm Int32(ffi::BigInt value, Location loc=UnknownLoc())
Construct a scalar int32 constant.
Definition expr.h:360
Typed reference/view over any Expr whose ExprObj::ty is PrimType.
Definition expr.h:187
Definition type.h:182
static TVM_TIRX_DLL PrimType Int(int bits, int lanes=1)
Construct a signed integer type with fixed lanes.
Definition type.h:456
static TVM_TIRX_DLL PtrType VoidPtrType(runtime::MemoryScope scope={})
Construct an opaque pointer with void element type.
Managed reference to ExternOpObj.
Definition operation.h:402
Managed reference to TensorTypeObj.
Definition type.h:129
Checked zero-state view over an ordinary VarObj with TensorType.
Definition expr.h:48
Expr data() const
Project the physical pointer established by the definition site.
Definition expr.h:69
const ffi::Array< PrimExpr > & shape() const
Return the shape of the tensor.
Definition expr.h:63
TensorType ty() const
Return the tensor type carried by the ordinary variable.
Definition expr.h:60
PrimType dtype() const
Return the primitive element type.
Definition expr.h:66
Definition reduction.h:29
PrimExpr MakeConst(PrimType dtype, ValueType value, Location loc=UnknownLoc())
Make a const value with certain data type.
Definition op.h:814
Tensor expression language DSL.
Definition extracted_task.h:33
ffi::Optional< ffi::Tuple< ffi::Array< PrimExpr >, ffi::Array< PrimExpr >, PrimExpr > > MatchShapeStridePattern(const TensorType &tensor)
Match a tensor's shape-aligned strided memory representation.
Definition type.h:156
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 analysis.h:41
Operation node can generate one or multiple Tensors.