-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RUNTIME] Refactor object python FFI to new protocol. (#4128)
* [RUNTIME] Refactor object python FFI to new protocol. This is a pre-req to bring the Node system under object protocol. Most of the code reflects the current code in the Node system. - Use new instead of init so subclass can define their own constructors - Allow register via name, besides type idnex - Introduce necessary runtime C API functions - Refactored Tensor and Datatype to directly use constructor. * address review comments
- Loading branch information
Showing
23 changed files
with
440 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# pylint: disable=invalid-name | ||
"""Runtime Object api""" | ||
from __future__ import absolute_import | ||
|
||
import ctypes | ||
from ..base import _LIB, check_call | ||
from .types import TypeCode, RETURN_SWITCH, C_TO_PY_ARG_SWITCH, _wrap_arg_func | ||
|
||
|
||
ObjectHandle = ctypes.c_void_p | ||
__init_by_constructor__ = None | ||
|
||
"""Maps object type to its constructor""" | ||
OBJECT_TYPE = {} | ||
|
||
def _register_object(index, cls): | ||
"""register object class""" | ||
OBJECT_TYPE[index] = cls | ||
|
||
|
||
def _return_object(x): | ||
handle = x.v_handle | ||
if not isinstance(handle, ObjectHandle): | ||
handle = ObjectHandle(handle) | ||
tindex = ctypes.c_uint() | ||
check_call(_LIB.TVMObjectGetTypeIndex(handle, ctypes.byref(tindex))) | ||
cls = OBJECT_TYPE.get(tindex.value, ObjectBase) | ||
# Avoid calling __init__ of cls, instead directly call __new__ | ||
# This allows child class to implement their own __init__ | ||
obj = cls.__new__(cls) | ||
obj.handle = handle | ||
return obj | ||
|
||
RETURN_SWITCH[TypeCode.OBJECT_HANDLE] = _return_object | ||
C_TO_PY_ARG_SWITCH[TypeCode.OBJECT_HANDLE] = _wrap_arg_func( | ||
_return_object, TypeCode.OBJECT_HANDLE) | ||
|
||
|
||
class ObjectBase(object): | ||
"""Base object for all object types""" | ||
__slots__ = ["handle"] | ||
|
||
def __del__(self): | ||
if _LIB is not None: | ||
check_call(_LIB.TVMObjectFree(self.handle)) | ||
|
||
def __init_handle_by_constructor__(self, fconstructor, *args): | ||
"""Initialize the handle by calling constructor function. | ||
Parameters | ||
---------- | ||
fconstructor : Function | ||
Constructor function. | ||
args: list of objects | ||
The arguments to the constructor | ||
Note | ||
---- | ||
We have a special calling convention to call constructor functions. | ||
So the return handle is directly set into the Node object | ||
instead of creating a new Node. | ||
""" | ||
# assign handle first to avoid error raising | ||
self.handle = None | ||
handle = __init_by_constructor__(fconstructor, args) | ||
if not isinstance(handle, ObjectHandle): | ||
handle = ObjectHandle(handle) | ||
self.handle = handle |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.