Skip to content

Commit 3d11bab

Browse files
YuchenJinyongwww
authored andcommitted
VM compiler. (apache#18)
* VM compiler. * Update. * Compile IRmodule; expose Python api * Add dtype contant serialization and type hint. * Address comments. * Add todos and fix lint. * Update * Update.
1 parent 89e0a1f commit 3d11bab

File tree

17 files changed

+602
-42
lines changed

17 files changed

+602
-42
lines changed

include/tvm/relax/attrs/memory.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 tvm/relax/attrs/memory.h
22+
* \brief Attributes for memory operators.
23+
*/
24+
#ifndef TVM_RELAX_ATTRS_MEMORY_H_
25+
#define TVM_RELAX_ATTRS_MEMORY_H_
26+
27+
#include <tvm/ir/attrs.h>
28+
29+
namespace tvm {
30+
namespace relax {
31+
/*!
32+
* \brief Options for allocating storage.
33+
*/
34+
struct AllocStorageAttrs : public tvm::AttrsNode<AllocStorageAttrs> {
35+
DataType dtype;
36+
int device_id;
37+
int device_type;
38+
39+
TVM_DECLARE_ATTRS(AllocStorageAttrs, "relax.attrs.AllocStorageAttrs") {
40+
TVM_ATTR_FIELD(dtype)
41+
.describe("The dtype of the tensor to allocate.")
42+
.set_default(DataType::Float(32, 1));
43+
TVM_ATTR_FIELD(device_id).describe("The device id on which to allocate memory.");
44+
TVM_ATTR_FIELD(device_type).describe("The device type on which to allocate memory.");
45+
}
46+
};
47+
48+
/*!
49+
* \brief Options for allocating tensors.
50+
*/
51+
struct AllocTensorAttrs : public tvm::AttrsNode<AllocTensorAttrs> {
52+
DataType dtype;
53+
54+
TVM_DECLARE_ATTRS(AllocTensorAttrs, "relax.attrs.AllocTensorAttrs") {
55+
TVM_ATTR_FIELD(dtype)
56+
.describe("The dtype of the tensor to allocate.")
57+
.set_default(DataType::Float(32, 1));
58+
}
59+
};
60+
61+
} // namespace relax
62+
} // namespace tvm
63+
#endif // TVM_RELAX_ATTRS_MEMORY_H_

include/tvm/relax/op_attr_types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ using relay::Call;
4343
* \param diag_ctx The diagnostic context for reporting errors.
4444
* \return The inferred output shape expression.
4545
*/
46-
using FInferShape = runtime::TypedPackedFunc<Optional<RelayExpr>(const Call& call, DiagnosticContext diag_ctx)>;
46+
using FInferShape =
47+
runtime::TypedPackedFunc<Optional<RelayExpr>(const Call& call, DiagnosticContext diag_ctx)>;
4748

4849
/*!
4950
* \brief Infer the output type for operators. This function will

include/tvm/relax/vm/exec_builder.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
* \file tvm/relax/vm/exec_builder.h
2222
* \brief
2323
*/
24-
#ifndef TVM_RELAX_EXEC_BUILDER_H_
25-
#define TVM_RELAX_EXEC_BUILDER_H_
24+
#ifndef TVM_RELAX_VM_EXEC_BUILDER_H_
25+
#define TVM_RELAX_VM_EXEC_BUILDER_H_
2626

2727
#include <tvm/ir/expr.h>
2828
#include <tvm/node/reflection.h>
@@ -52,7 +52,7 @@ class ExecBuilderNode : public Object {
5252
* \param func The function name.
5353
* \param num_inputs The number of inputs.
5454
*/
55-
void Function(std::string func, int64_t num_inputs);
55+
void EmitFunction(std::string func, int64_t num_inputs);
5656
/*!
5757
* \brief Emit a call instruction for a packed function.
5858
* \param func The packed function name.
@@ -69,7 +69,7 @@ class ExecBuilderNode : public Object {
6969
* \brief Emit a constant value to the constant pool.
7070
* \return The index that represents the constant.
7171
*/
72-
vm::Index EmitConstant(ObjectRef obj);
72+
vm::Index EmitConstant(TVMRetValue obj);
7373
/*!
7474
* \brief Get the built executable.
7575
* \return The built executable.
@@ -102,4 +102,4 @@ class ExecBuilder : public ObjectRef {
102102
} // namespace relax
103103
} // namespace tvm
104104

105-
#endif // TVM_RELAX_EXEC_BUILDER_H_
105+
#endif // TVM_RELAX_VM_EXEC_BUILDER_H_

include/tvm/relax/vm/executable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class ExecutableNode : public Object {
103103
/*! \brief A map from globals (as strings) to their index in the function map. */
104104
std::unordered_map<std::string, Index> global_map;
105105
/*! \brief The global constant pool. */
106-
std::vector<ObjectRef> constants;
106+
std::vector<TVMRetValue> constants;
107107
/*! \brief The name of packed functions. */
108108
std::vector<std::string> func_names;
109109
/*! \brief A mapping from the packed function (as string) to the index that

python/tvm/relax/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from . import parser
2525
from . import analysis
2626
from . import transform
27+
from . import vm_compiler
2728

2829

2930
# Expr
@@ -61,9 +62,11 @@
6162
ExecBuilder = exec_builder.ExecBuilder
6263
VirtualMachine = vm.VirtualMachine
6364
load_exec_from_file = vm.load_exec_from_file
65+
compile = vm_compiler.compile
6466

6567
# Operator
6668
from .op.base import call_dps
69+
from .op.op_attrs import AllocStorageAttrs, AllocTensorAttrs
6770

6871
# IRBuilder
6972
IRBuilder = ir_builder.IRBuilder

python/tvm/relax/base.py

Lines changed: 0 additions & 4 deletions
This file was deleted.

python/tvm/relax/exec_builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def emit_call(self, name, args=[], dst=None):
8080
dst = SpecialReg.VOID_ARG
8181
args_ = []
8282
for arg in args:
83-
if isinstance(arg, tvm.nd.NDArray):
83+
if isinstance(arg, tvm.nd.NDArray) or isinstance(arg, tvm.DataType):
8484
new_arg = self.emit_constant(arg)
8585
args_.append(new_arg)
8686
else:

python/tvm/relax/op/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@
2020
# Operators
2121
from .base import *
2222
from .tensor import *
23+
from .op_attrs import *

python/tvm/relax/op/op_attrs.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
"""The attributes node used for Relax operators"""
18+
from tvm.ir import Attrs
19+
import tvm._ffi
20+
21+
@tvm._ffi.register_object("relax.attrs.AllocStorageAttrs")
22+
class AllocStorageAttrs(Attrs):
23+
"""Attributes used in alloc_storage operators"""
24+
25+
26+
@tvm._ffi.register_object("relax.attrs.AllocTensorAttrs")
27+
class AllocTensorAttrs(Attrs):
28+
"""Attributes used in alloc_tensor operators"""

python/tvm/relax/parser.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
118
from __future__ import annotations
219

320
import inspect

0 commit comments

Comments
 (0)