tvm
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/te/operation.h>
28 #include <tvm/tirx/builtin.h>
29 
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 namespace tvm {
35 namespace topi {
36 namespace detail {
37 
38 using namespace tvm::te;
39 
45 using FExtern = std::function<PrimExpr(ffi::Array<Buffer>, ffi::Array<Buffer>)>;
46 
64 inline ffi::Array<Tensor> make_extern(const ffi::Array<ffi::Array<PrimExpr>>& out_shapes,
65  const std::vector<PrimType>& out_types,
66  const ffi::Array<Tensor>& inputs, FExtern fextern,
67  std::string name, std::string tag,
68  ::tvm::ffi::Map<ffi::String, ffi::Any> attrs) {
69  TVM_FFI_ICHECK_EQ(out_shapes.size(), out_types.size())
70  << "make_extern: out_shapes and out_types must have equal size";
71 
72  ffi::Array<Buffer> input_placeholders;
73  for (auto t : inputs) {
74  input_placeholders.push_back(tvm::tirx::decl_buffer(t->shape, t->dtype, t->op->name));
75  }
76  ffi::Array<Buffer> output_placeholders;
77  for (size_t i = 0; i < out_shapes.size(); ++i) {
78  output_placeholders.push_back(tvm::tirx::decl_buffer(out_shapes[i], out_types[i], name));
79  }
80 
81  auto body = fextern(input_placeholders, output_placeholders);
82  auto body_stmt = tvm::tirx::Evaluate(body);
83 
84  auto op = ExternOp(name, tag, attrs, inputs, input_placeholders, output_placeholders, body_stmt);
85 
86  ffi::Array<Tensor> outputs;
87  for (size_t i = 0; i < output_placeholders.size(); ++i) {
88  outputs.push_back(op.output(i));
89  }
90  return outputs;
91 }
92 
101 inline Expr pack_buffer(Buffer buf) {
102  TVM_FFI_ICHECK_GT(buf->shape.size(), 0) << "buf shape must have at least one element";
103  Expr shape =
104  Call(PointerType(PrimType::Int(64)), tvm::tirx::builtin::tvm_stack_make_shape(), buf->shape);
105  Expr strides;
106  if (buf->strides.size() > 0) {
107  strides = Call(PointerType(PrimType::Int(64)), tvm::tirx::builtin::tvm_stack_make_shape(),
108  buf->strides);
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 
130 inline PrimExpr call_packed(ffi::Array<Expr> args) {
132  .as_or_throw<PrimExpr>();
133 }
134 
135 } // namespace detail
136 } // namespace topi
137 } // namespace tvm
138 #endif // TVM_TOPI_DETAIL_EXTERN_H_
static IntImm Int32(int64_t value, Span span=Span())
Construct a scalar int32 constant.
Definition: expr.h:402
static PointerType VoidPointerTy(ffi::String storage_scope="")
Construct an opaque pointer with void element type.
static PrimType Int(int bits, int lanes=1)
Construct a signed integer type with fixed lanes.
Managed reference to ExternOpNode.
Definition: operation.h:289
Buffer is a symbolic n-darray structure. It is a composition of primitive symbolic types,...
Definition: buffer.h:187
tvm::Expr Expr
Definition: type.h:41
void Evaluate(Expr value)
Evaluate the input expression.
Tensor expression language DSL.
Definition: extracted_task.h:32
const Op & tvm_stack_make_array()
Allocate a Tensor(DLTensor) on stack, return the handle.
const Op & tvm_call_packed()
See pesudo code.
const Op & tvm_stack_make_shape()
Allocate a shape tuple on stack, return the handle.
Buffer decl_buffer(ffi::Array< PrimExpr > shape, PrimType dtype=PrimType::Float(32), ffi::String name="buffer", ffi::String storage_scope="", ffi::Optional< ffi::Array< IntImm >> axis_separators=std::nullopt, 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:1012
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:2010
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.