forked from apache/tvm
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[POC] Cython FFI support for tuple, slice, ellipsis
- Loading branch information
Showing
9 changed files
with
249 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import timeit | ||
import tvm | ||
nop = tvm._api_internal._nop | ||
|
||
setup = """ | ||
import tvm | ||
nop = tvm._api_internal._nop | ||
""" | ||
timer = timeit.Timer(setup=setup, | ||
stmt='nop((None,..., slice(0, 100, 2)))') | ||
timer.timeit(1) | ||
num_repeat = 1000 | ||
print("tvm.tuple_slice_ellipsis_combo:", timer.timeit(num_repeat) / num_repeat) | ||
|
||
|
||
setup = """ | ||
import numpy as np | ||
""" | ||
|
||
timer = timeit.Timer(setup=setup, | ||
stmt='np.empty((1,2,1))') | ||
timer.timeit(1) | ||
print("numpy.emmpty:", timer.timeit(num_repeat) / num_repeat) | ||
|
||
|
||
setup = """ | ||
import tvm | ||
nop = tvm._api_internal._nop | ||
""" | ||
timer = timeit.Timer(setup=setup, | ||
stmt='nop("mystr")') | ||
timer.timeit(1) | ||
num_repeat = 1000 | ||
print("tvm.str_arg:", timer.timeit(num_repeat) / num_repeat) |
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,111 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/*! | ||
* \file ffi_helper | ||
* \brief Helper class to support additional objects in FFI. | ||
*/ | ||
#ifndef TVM_RUNTIME_FFI_HELPER_H_ | ||
#define TVM_RUNTIME_FFI_HELPER_H_ | ||
|
||
#include <tvm/runtime/object.h> | ||
#include <tvm/runtime/container.h> | ||
#include <tvm/runtime/memory.h> | ||
#include <limits> | ||
|
||
namespace tvm { | ||
namespace runtime { | ||
|
||
/*! \brief Ellipsis. */ | ||
class EllipsisObj : public Object { | ||
public: | ||
static constexpr const uint32_t _type_index = TypeIndex::kEllipsis; | ||
static constexpr const char* _type_key = "vm.Ellipsis"; | ||
TVM_DECLARE_FINAL_OBJECT_INFO(EllipsisObj, Object); | ||
}; | ||
|
||
inline ObjectRef CreateEllipsis() { | ||
return ObjectRef(make_object<EllipsisObj>()); | ||
} | ||
|
||
/*! \brief Slice. */ | ||
class SliceObj : public Object { | ||
public: | ||
int64_t start; | ||
int64_t stop; | ||
int64_t step; | ||
|
||
static constexpr const uint32_t _type_index = TypeIndex::kSlice; | ||
static constexpr const char* _type_key = "vm.Slice"; | ||
TVM_DECLARE_FINAL_OBJECT_INFO(SliceObj, Object); | ||
}; | ||
|
||
class Slice : public ObjectRef { | ||
public: | ||
explicit Slice(int64_t start, int64_t stop, int64_t step, | ||
ObjectPtr<SliceObj>&& data = make_object<SliceObj>()) { | ||
data->start = start; | ||
data->stop = stop; | ||
data->step = step; | ||
data_ = std::move(data); | ||
} | ||
|
||
explicit Slice(int64_t stop) | ||
: Slice(kNoneValue, stop, kNoneValue) { | ||
} | ||
|
||
// constant to represent None. | ||
static constexpr int64_t kNoneValue = std::numeric_limits<int64_t>::min(); | ||
|
||
TVM_DEFINE_OBJECT_REF_METHODS(Slice, ObjectRef, SliceObj); | ||
}; | ||
|
||
int64_t SliceNoneValue() { | ||
return Slice::kNoneValue; | ||
} | ||
|
||
// Helper functions for fast FFI implementations | ||
/*! | ||
* \brief A builder class that helps to incrementally build ADT. | ||
*/ | ||
class ADTBuilder { | ||
public: | ||
/*! \brief default constructor */ | ||
ADTBuilder() = default; | ||
|
||
explicit ADTBuilder(uint32_t tag, uint32_t size) | ||
: data_(make_inplace_array_object<ADTObj, ObjectRef>(size)) { | ||
} | ||
|
||
template <typename... Args> | ||
void EmplaceInit(size_t idx, Args&&... args) { | ||
data_->EmplaceInit(idx, std::forward<Args>(args)...); | ||
} | ||
|
||
ADT Get() { | ||
return ADT(std::move(data_)); | ||
} | ||
|
||
private: | ||
friend class ADT; | ||
ObjectPtr<ADTObj> data_; | ||
}; | ||
} // namespace runtime | ||
} // namespace tvm | ||
#endif // TVM_RUNTIME_FFI_HELPER_H_ |
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,80 @@ | ||
"""Fast object conversion API in cython.""" | ||
|
||
cdef extern from "tvm/runtime/object.h" namespace "tvm::runtime": | ||
cdef cppclass Object: | ||
pass | ||
|
||
cdef cppclass ObjectPtr[T]: | ||
ObjectPtr() | ||
|
||
cdef cppclass ObjectRef: | ||
ObjectRef() | ||
ObjectRef(ObjectPtr[Object]) | ||
Object* get() | ||
|
||
|
||
cdef ObjectPtr[T] GetObjectPtr[T](T* ptr) | ||
|
||
|
||
cdef extern from "tvm/runtime/container.h" namespace "tvm::runtime": | ||
cdef cppclass ADT(ObjectRef): | ||
ADT() | ||
|
||
cdef extern from "tvm/runtime/ffi_helper.h" namespace "tvm::runtime": | ||
cdef cppclass ADTBuilder: | ||
ADTBuilder() | ||
ADTBuilder(uint32_t tag, uint32_t size) | ||
void EmplaceInit(size_t idx, ObjectRef) | ||
ADT Get() | ||
|
||
cdef ObjectRef CreateEllipsis() | ||
|
||
cdef cppclass Slice(ObjectRef): | ||
Slice() | ||
Slice(int) | ||
Slice(int, int, int) | ||
|
||
cdef int64_t SliceNoneValue() | ||
|
||
|
||
|
||
cdef extern from "tvm/runtime/memory.h" namespace "tvm::runtime": | ||
cdef ObjectPtr[T] make_object[T]() | ||
|
||
|
||
cdef extern from "tvm/expr.h" namespace "tvm": | ||
cdef cppclass Integer(ObjectRef): | ||
Integer(int value); | ||
|
||
|
||
## Implementations | ||
cdef inline ADT convert_tuple(tuple src_tuple) except +: | ||
cdef uint32_t size = len(src_tuple) | ||
cdef ADTBuilder builder = ADTBuilder(0, size); | ||
|
||
for i in range(size): | ||
builder.EmplaceInit(i, convert_object(src_tuple[i])) | ||
|
||
return builder.Get() | ||
|
||
|
||
cdef inline Slice convert_slice(slice slice_obj) except +: | ||
cdef int64_t kNoneValue = SliceNoneValue() | ||
return Slice(<int>(slice_obj.start) if slice_obj.start is not None else kNoneValue, | ||
<int>(slice_obj.stop) if slice_obj.stop is not None else kNoneValue, | ||
<int>(slice_obj.step) if slice_obj.step is not None else kNoneValue) | ||
|
||
|
||
cdef inline ObjectRef convert_object(object src_obj) except +: | ||
if isinstance(src_obj, int): | ||
return Integer(<int>src_obj) | ||
elif isinstance(src_obj, tuple): | ||
return convert_tuple(src_obj) | ||
elif src_obj is Ellipsis: | ||
return CreateEllipsis() | ||
elif isinstance(src_obj, slice): | ||
return convert_slice(src_obj) | ||
elif src_obj is None: | ||
return ObjectRef() | ||
else: | ||
raise TypeError("Don't know how to convert type %s" % type(src_obj)) |
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