2727#define TVM_LOG_DEBUG 0
2828#define TVM_LOG_CUSTOMIZE 1
2929#define TVM_FFI_USE_LIBBACKTRACE 0
30+ #define TVM_FFI_ALWAYS_LOG_BEFORE_THROW 1
3031#define DMLC_USE_LOGGING_LIBRARY <tvm/runtime/logging.h>
3132
32- #include < tvm/runtime/c_runtime_api.h>
3333#include < tvm/runtime/logging.h>
3434
3535#include " src/runtime/c_runtime_api.cc"
@@ -107,45 +107,24 @@ void LogMessageImpl(const std::string& file, int lineno, int level, const std::s
107107
108108} // namespace detail
109109
110- TVM_REGISTER_GLOBAL (" testing.echo" ).set_body_packed([](ffi::PackedArgs args, ffi::Any* ret) {
111- *ret = args[0 ];
112- });
113-
114- TVM_REGISTER_GLOBAL (" testing.call" ).set_body_packed([](ffi::PackedArgs args, ffi::Any* ret) {
115- (args[0 ].cast <ffi::Function>()).CallPacked (args.Slice (1 ), ret);
116- });
117-
118- TVM_REGISTER_GLOBAL (" testing.ret_string" ).set_body_packed([](ffi::PackedArgs args, ffi::Any* ret) {
119- *ret = args[0 ].cast <String>();
120- });
121-
122- TVM_REGISTER_GLOBAL (" testing.log_info_str" )
110+ TVM_FFI_REGISTER_GLOBAL (" tvmjs.testing.call" )
123111 .set_body_packed([](ffi::PackedArgs args, ffi::Any* ret) {
124- LOG (INFO) << args[0 ].cast <String>( );
112+ ( args[0 ].cast <ffi::Function>()). CallPacked (args. Slice ( 1 ), ret );
125113 });
126114
127- TVM_REGISTER_GLOBAL ( " testing.log_fatal_str " )
115+ TVM_FFI_REGISTER_GLOBAL ( " tvmjs. testing.log_info_str " )
128116 .set_body_packed([](ffi::PackedArgs args, ffi::Any* ret) {
129- LOG (FATAL ) << args[0 ].cast <String>();
117+ LOG (INFO ) << args[0 ].cast <String>();
130118 });
131119
132- TVM_REGISTER_GLOBAL ( " testing.add_one" ).set_body_typed([](int x) { return x + 1 ; });
120+ TVM_FFI_REGISTER_GLOBAL ( " tvmjs. testing.add_one" ).set_body_typed([](int x) { return x + 1 ; });
133121
134- TVM_REGISTER_GLOBAL ( " testing.wrap_callback" )
122+ TVM_FFI_REGISTER_GLOBAL ( " tvmjs. testing.wrap_callback" )
135123 .set_body_packed([](ffi::PackedArgs args, ffi::Any* ret) {
136124 ffi::Function pf = args[0 ].cast <ffi::Function>();
137125 *ret = ffi::TypedFunction<void ()>([pf]() { pf (); });
138126 });
139127
140- // internal function used for debug and testing purposes
141- TVM_REGISTER_GLOBAL (" testing.object_use_count" )
142- .set_body_packed([](ffi::PackedArgs args, ffi::Any* ret) {
143- auto obj = args[0 ].cast <ffi::ObjectRef>();
144- // subtract the current one because we always copy
145- // and get another value.
146- *ret = (obj.use_count () - 1 );
147- });
148-
149128void ArrayDecodeStorage (NDArray cpu_arr, std::string bytes, std::string format, std::string dtype) {
150129 if (format == " f32-to-bf16" && dtype == " float32" ) {
151130 std::vector<uint16_t > buffer (bytes.length () / 2 );
@@ -167,10 +146,10 @@ void ArrayDecodeStorage(NDArray cpu_arr, std::string bytes, std::string format,
167146 }
168147}
169148
170- TVM_REGISTER_GLOBAL (" tvmjs.array.decode_storage" ).set_body_typed(ArrayDecodeStorage);
149+ TVM_FFI_REGISTER_GLOBAL (" tvmjs.array.decode_storage" ).set_body_typed(ArrayDecodeStorage);
171150
172151// Concatenate n TVMArrays
173- TVM_REGISTER_GLOBAL (" tvmjs.runtime.ArrayConcat" )
152+ TVM_FFI_REGISTER_GLOBAL (" tvmjs.runtime.ArrayConcat" )
174153 .set_body_packed([](ffi::PackedArgs args, ffi::Any* ret) {
175154 std::vector<Any> data;
176155 for (int i = 0 ; i < args.size (); ++i) {
@@ -220,7 +199,7 @@ NDArray ConcatEmbeddings(const std::vector<NDArray>& embeddings) {
220199}
221200
222201// Concatenate n NDArrays
223- TVM_REGISTER_GLOBAL (" tvmjs.runtime.ConcatEmbeddings" )
202+ TVM_FFI_REGISTER_GLOBAL (" tvmjs.runtime.ConcatEmbeddings" )
224203 .set_body_packed([](ffi::PackedArgs args, ffi::Any* ret) {
225204 std::vector<NDArray> embeddings;
226205 for (int i = 0 ; i < args.size (); ++i) {
@@ -230,5 +209,19 @@ TVM_REGISTER_GLOBAL("tvmjs.runtime.ConcatEmbeddings")
230209 *ret = result;
231210 });
232211
212+ TVM_FFI_REGISTER_GLOBAL (" tvmjs.runtime.NDArrayCopyFromBytes" )
213+ .set_body_typed([](NDArray nd, TVMFFIByteArray* bytes) {
214+ nd.CopyFromBytes (bytes->data , bytes->size );
215+ });
216+
217+ TVM_FFI_REGISTER_GLOBAL (" tvmjs.runtime.NDArrayCopyToBytes" )
218+ .set_body_typed([](NDArray nd) -> ffi::Bytes {
219+ size_t size = GetDataSize (*(nd.operator ->()));
220+ std::string bytes;
221+ bytes.resize (size);
222+ nd.CopyToBytes (bytes.data (), size);
223+ return ffi::Bytes (bytes);
224+ });
225+
233226} // namespace runtime
234227} // namespace tvm
0 commit comments