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/tir/builtin.h>
29 
30 #include <string>
31 #include <vector>
32 
33 namespace tvm {
34 namespace topi {
35 namespace detail {
36 
37 using namespace tvm::te;
38 
44 using FExtern = std::function<PrimExpr(Array<Buffer>, Array<Buffer>)>;
45 
63 inline Array<Tensor> make_extern(const Array<Array<PrimExpr>>& out_shapes,
64  const std::vector<DataType>& out_types,
65  const Array<Tensor>& inputs, FExtern fextern, std::string name,
66  std::string tag, ::tvm::Map<String, ObjectRef> attrs) {
67  ICHECK_EQ(out_shapes.size(), out_types.size())
68  << "make_extern: out_shapes and out_types must have equal size";
69 
70  Array<Buffer> input_placeholders;
71  for (auto t : inputs) {
72  input_placeholders.push_back(tvm::tir::decl_buffer(t->shape, t->dtype, t->op->name));
73  }
74  Array<Buffer> output_placeholders;
75  for (size_t i = 0; i < out_shapes.size(); ++i) {
76  output_placeholders.push_back(tvm::tir::decl_buffer(out_shapes[i], out_types[i], name));
77  }
78 
79  auto body = fextern(input_placeholders, output_placeholders);
80  auto body_stmt = tvm::tir::Evaluate(body);
81 
82  auto op = ExternOp(name, tag, attrs, inputs, input_placeholders, output_placeholders, body_stmt);
83 
84  Array<Tensor> outputs;
85  for (size_t i = 0; i < output_placeholders.size(); ++i) {
86  outputs.push_back(op.output(i));
87  }
88  return outputs;
89 }
90 
99 inline PrimExpr pack_buffer(Buffer buf) {
100  ICHECK_GT(buf->shape.size(), 0) << "buf shape must have at least one element";
101  auto shape =
103  PrimExpr strides;
104  if (buf->strides.size() > 0) {
105  strides =
107  } else {
108  strides = 0;
109  }
110  Array<PrimExpr> pack_args{buf->data,
111  shape,
112  strides,
113  make_const(DataType::Int(32), static_cast<int64_t>(buf->shape.size())),
114  make_const(buf->dtype, 0),
115  buf->elem_offset};
117 }
118 
128 inline PrimExpr call_packed(Array<PrimExpr> args) {
130 }
131 
132 } // namespace detail
133 } // namespace topi
134 } // namespace tvm
135 #endif // TVM_TOPI_DETAIL_EXTERN_H_
static DataType Int(int bits, int lanes=1)
Construct an int type.
Definition: data_type.h:219
static DataType Handle(int bits=64, int lanes=1)
Construct a handle type.
Definition: data_type.h:271
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
Managed reference to ExternOpNode.
Definition: operation.h:460
Buffer is a symbolic n-darray structure. It is a composition of primitive symbolic types,...
Definition: buffer.h:174
Managed reference to CallNode.
Definition: expr.h:918
void Evaluate(PrimExpr value)
Evaluate the input expression.
Tensor expression language DSL.
Definition: extracted_task.h:33
const Op & tvm_stack_make_array()
Allocate a NDArray(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.
PrimExpr make_const(DataType t, ValueType value, Span span=Span())
Make a const value with certain data type.
Definition: op.h:962
Buffer decl_buffer(Array< PrimExpr > shape, DataType dtype=DataType::Float(32), String name="buffer", String storage_scope="", Array< IntImm > axis_separators={}, Span span=Span())
Construct a new buffer given shape, and dtype.
Tensor shape(const Tensor &src, DataType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:1913
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Operation node can generate one or multiple Tensors.
TIR builtin intrinsics.