Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/runtime/container.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ TVM_REGISTER_GLOBAL("runtime.Array").set_body([](TVMArgs args, TVMRetValue* ret)
*ret = Array<ObjectRef>(data);
});

// Concatenate n TVMArrays
TVM_REGISTER_GLOBAL("runtime.ArrayConcat").set_body([](TVMArgs args, TVMRetValue* ret) {
Comment thread
CharlieFRuan marked this conversation as resolved.
Outdated
std::vector<ObjectRef> data;
for (int i = 0; i < args.size(); ++i) {
// Get i-th TVMArray
ICHECK_EQ(args[i].type_code(), kTVMObjectHandle);
Object* ptr = static_cast<Object*>(args[i].value().v_handle);
ICHECK(ptr->IsInstance<ArrayNode>());
auto* arr_i = static_cast<const ArrayNode*>(ptr);
for (int j = 0; j < arr_i->size(); j++) {
// Push back each j-th element of the i-th array
data.push_back(arr_i->at(j));
}
}
*ret = Array<ObjectRef>(data);
});

TVM_REGISTER_GLOBAL("runtime.ArrayGetItem").set_body([](TVMArgs args, TVMRetValue* ret) {
int64_t i = args[1];
ICHECK_EQ(args[0].type_code(), kTVMObjectHandle);
Expand Down
4 changes: 2 additions & 2 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 26 additions & 6 deletions web/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class RuntimeContext implements Disposable {
arrayGetItem: PackedFunc;
arrayGetSize: PackedFunc;
arrayMake: PackedFunc;
arrayConcat: PackedFunc;
stringMake: PackedFunc;
getFFIString: PackedFunc;
getSysLib: PackedFunc;
Expand All @@ -180,6 +181,7 @@ class RuntimeContext implements Disposable {
this.arrayGetItem = getGlobalFunc("runtime.ArrayGetItem");
this.arrayGetSize = getGlobalFunc("runtime.ArraySize");
this.arrayMake = getGlobalFunc("runtime.Array");
this.arrayConcat = getGlobalFunc("runtime.ArrayConcat");
this.stringMake = getGlobalFunc("runtime.String");
this.getFFIString = getGlobalFunc("runtime.GetFFIString");
this.getSysLib = getGlobalFunc("runtime.SystemLib");
Expand All @@ -205,6 +207,7 @@ class RuntimeContext implements Disposable {
this.arrayGetItem.dispose();
this.arrayGetSize.dispose();
this.arrayMake.dispose();
this.arrayConcat.dispose();
this.stringMake.dispose();
this.getFFIString.dispose();
this.arrayCacheGet.dispose();
Expand Down Expand Up @@ -1382,11 +1385,7 @@ export class Instance implements Disposable {
* @returns Parameters read.
*/
getParamsFromCacheByName(paramNames: Array<string>): TVMObject {
// Convert Array<string> to Array<TVMString>
const paramNamesTVM: TVMString[] = [];
paramNames.forEach(paramName => { paramNamesTVM.push(this.makeString(paramName)) });
return (this.ctx.paramModuleFromCacheByName(
this.makeTVMArray(paramNamesTVM)) as Module).getFunction("get_params")();
return (this.ctx.paramModuleFromCacheByName(paramNames) as Module).getFunction("get_params")();
}

/**
Expand Down Expand Up @@ -1873,7 +1872,20 @@ export class Instance implements Disposable {
makeTVMArray(
inputs: Array<TVMObjectBase>
): TVMArray {
return this.ctx.arrayMake(...inputs) as TVMArray;
const CALL_STACK_LIMIT = 30000;
const inputsLength = inputs.length;
if (inputsLength <= CALL_STACK_LIMIT) {
return this.ctx.arrayMake(...inputs) as TVMArray;
}
// If too many elements, TypeScript would complain `Maximum call stack size exceeded`
// So we make several arrays and concatenate them
const listOfArrays: Array<TVMArray> = [];
for (let begin = 0; begin < inputsLength; begin += CALL_STACK_LIMIT) {
const end = Math.min(inputsLength, begin + CALL_STACK_LIMIT);
const chunk: Array<TVMObjectBase> = inputs.slice(begin, end);
listOfArrays.push(this.ctx.arrayMake(...chunk) as TVMArray);
}
return this.ctx.arrayConcat(...listOfArrays) as TVMArray;
}

/**
Expand Down Expand Up @@ -2230,6 +2242,14 @@ export class Instance implements Disposable {
const tp = typeof val;
const valueOffset = argsValue + i * SizeOf.TVMValue;
const codeOffset = argsCode + i * SizeOf.I32;

// Convert string[] to a TVMArray of TVMString, hence treated as a TVMObject
if (val instanceof Array && val.every(e => typeof e === "string")) {
const tvmStringArray: TVMString[] = [];
val.forEach(e => { tvmStringArray.push(this.makeString(e)) });
val = this.makeTVMArray(tvmStringArray);
}

if (val instanceof NDArray) {
if (!val.isView) {
stack.storePtr(valueOffset, val.getHandle());
Expand Down