Skip to content

Commit

Permalink
Adding back a check for empty library (apache#49)
Browse files Browse the repository at this point in the history
* Adding back a check for empty library

* Adding test for empty Module compilation
  • Loading branch information
mnuyens authored and yongwww committed Oct 22, 2019
1 parent e4fd1d3 commit ea3f8a2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions python/tvm/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ def export_library(self,
if isinstance(file_name, Path):
file_name = str(file_name)

if self.is_empty():
logging.info("The lib generated by the NNVM compiler does not contain optimized "
"functions for any operators. This usually happens when an external "
"accelerator, e.g. TensorRT, is employed along with TVM to compile "
"the model, and all the operators in the model are supported by the "
"external accelerator at runtime. Therefore, "
"the NNVM compiler skipped optimizing them at the compile time.")
if os.path.isfile(file_name):
logging.warning("Lib file %s exists, and will be overwritten by the newly created"
" lib with the same name.", file_name)
open(file_name, 'w').close()
return

if self.type_key == "stackvm":
if not file_name.endswith(".stackvm"):
raise ValueError("Module[%s]: can only be saved as stackvm format."
Expand Down
35 changes: 35 additions & 0 deletions tests/python/unittest/test_tensorrt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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.
import os
from unittest import mock
import tempfile

import tvm

def test_empty_library_export():
with tempfile.TemporaryDirectory() as temp_dir:
temp_file_path = os.path.join(temp_dir, "tmp_lib")
print(temp_file_path)
with mock.patch.object(tvm.module.Module, "is_empty") as is_empty_mock:
is_empty_mock.return_value = True
module = tvm.module.Module(None)
module.export_library(temp_file_path)
assert(os.path.isfile(temp_file_path))


if __name__ == "__main__":
test_empty_library_export()

0 comments on commit ea3f8a2

Please sign in to comment.