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
20/*!
21 * \file src/ir/tensor_type.cc
22 * \brief The type system AST nodes of Relay.
23 */
24#include <tvm/ir/tensor_type.h>
25#include <tvm/runtime/registry.h>
26#include <tvm/tir/op.h>
27
28namespace tvm {
29
30using tvm::ReprPrinter;
31using namespace tvm::runtime;
32
33TensorType::TensorType(Array<PrimExpr> shape, DataType dtype) {
34 ObjectPtr<TensorTypeNode> n = make_object<TensorTypeNode>();
35 n->shape = std::move(shape);
36 n->dtype = std::move(dtype);
37 data_ = std::move(n);
38}
39
40TensorType TensorType::Scalar(DataType dtype) { return TensorType({}, dtype); }
41
42PrimExpr TensorTypeNode::Size() const {
43 if (shape.size() == 0) {
44 return tir::make_const(DataType::Int(64), 1);
45 }
46
47 PrimExpr size = shape[0];
48 for (size_t i = 1; i < shape.size(); ++i) {
49 size *= shape[i];
50 }
51 return size;
52}
53
54TVM_REGISTER_NODE_TYPE(TensorTypeNode);
55
56TVM_REGISTER_GLOBAL("ir.TensorType").set_body_typed([](Array<PrimExpr> shape, DataType dtype) {
57 return TensorType(shape, dtype);
58});
59
60TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
61 .set_dispatch<TensorTypeNode>([](const ObjectRef& ref, ReprPrinter* p) {
62 auto* node = static_cast<const TensorTypeNode*>(ref.get());
63 p->stream << "TensorType(" << node->shape << ", " << node->dtype << ")";
64 });
65
66} // namespace tvm
67