diff --git a/CMakeLists.txt b/CMakeLists.txt index cc025fe165..d87024b00c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,10 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) PROJECT(singa) +IF(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + SET(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE) +ENDIF() + LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Thirdparty) #include(GetGitRevisionDescription) @@ -47,10 +51,10 @@ message(STATUS "SINGA Version ${SINGA_VERSION}") # Flags IF(UNIX) - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g -O2 -fPIC -Wall -pthread") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fPIC -Wall -pthread") ENDIF() IF(APPLE) - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++ -g -O2 -fPIC -Wall -pthread") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++ -fPIC -Wall -pthread") SET(CMAKE_MACOSX_RPATH 1) ENDIF() @@ -61,7 +65,7 @@ IF(WIN32) #MESSAGE(STATUS "Using msvc compiler ${CMAKE_CXX_FLAGS}") ENDIF() ENDIF() -IF(CMAKE_BUILD_TYPE=Debug) +IF(CMAKE_BUILD_TYPE STREQUAL "Debug") SET(NVCC_FLAG "${NVCC_FLAG} -g -G ") ENDIF() #message(STATUS "${CMAKE_CXX_FLAGS}") diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake index 60c4327291..a9b5264ed3 100644 --- a/cmake/Dependencies.cmake +++ b/cmake/Dependencies.cmake @@ -52,6 +52,7 @@ IF(USE_MODULES) #ENDIF() ELSE() FIND_PACKAGE( Protobuf 3.0 REQUIRED ) + INCLUDE_DIRECTORIES(${Protobuf_INCLUDE_DIRS}) #MESSAGE(STATUS "proto libs " ${PROTOBUF_LIBRARY}) LIST(APPEND SINGA_LINKER_LIBS ${PROTOBUF_LIBRARY}) #IF(USE_CBLAS) diff --git a/examples/neurostore/prepare.py b/examples/neurostore/prepare.py new file mode 100644 index 0000000000..e3c037a75d --- /dev/null +++ b/examples/neurostore/prepare.py @@ -0,0 +1,67 @@ +""" +prepare.py + +This script downloads models from Hugging Face and exports them to ONNX format. +In our example, we use three DistilBERT models: +1. google-bert/bert-base-uncased +2. lorenzozan/ME2-BERT +3. prithivMLmods/Spam-Bert-Uncased + +To download and export other models, modify: 1). the `models` list in the main function. 2). the +dummy_inputs and torch.onnx.export function parameters. +""" + +import os +import torch +from transformers import AutoModel + +DUMMY_SEQ_LENGTH = 32 + + +def export_model_to_onnx(model_name: str, save_dir: str): + """ + Downloads and exports a Hugging Face model to ONNX format. + """ + print(f"Processing {model_name}...") + try: + model = AutoModel.from_pretrained(model_name) + model.eval() + + dummy_inputs = { + "input_ids": torch.randint(0, 30522, (1, DUMMY_SEQ_LENGTH)), + "attention_mask": torch.ones((1, DUMMY_SEQ_LENGTH), dtype=torch.long) + } + + safe_model_name = model_name.replace("/", "_") + export_path = os.path.join(save_dir, f"{safe_model_name}.onnx") + + torch.onnx.export( + model, + (dummy_inputs["input_ids"], dummy_inputs["attention_mask"]), + export_path, + input_names=["input_ids", "attention_mask"], + output_names=["last_hidden_state"], + dynamic_axes={ + "input_ids": {0: "batch_size", 1: "sequence_length"}, + "attention_mask": {0: "batch_size", 1: "sequence_length"}, + "last_hidden_state": {0: "batch_size", 1: "sequence_length"} + }, + opset_version=14, + ) + print(f"Exported ONNX to {export_path}") + except Exception as e: + print(f"Failed to export {model_name}: {e}") + + +if __name__ == "__main__": + model_dir = "./models" + os.makedirs(model_dir, exist_ok=True) + + models = [ + "distilbert/distilbert-base-uncased", + "winegarj/distilbert-base-uncased-finetuned-sst2", + "eliasalbouzidi/distilbert-nsfw-text-classifier" + ] + + for model_name in models: + export_model_to_onnx(model_name, model_dir) diff --git a/java/CMakeLists.txt b/java/CMakeLists.txt index 7f4419832c..50f437062a 100644 --- a/java/CMakeLists.txt +++ b/java/CMakeLists.txt @@ -104,7 +104,7 @@ PROTOBUF_GENERATE_JAVA(proto_javas ${proto_files}) # MESSAGE(STATUS "proto javas: ${proto_javas}") execute_process( - COMMAND swig -c++ -java -I${CMAKE_SOURCE_DIR}/include + COMMAND ${SWIG_EXECUTABLE} -c++ -java -I${CMAKE_BINARY_DIR}/src/api -I${CMAKE_SOURCE_DIR}/include -I${JAVA_INCLUDE_PATH} -I${JAVA_INCLUDE_PATH2} -package org.apache.singa.swig -outdir ${CMAKE_BINARY_DIR}/java/src/main/java/org/apache/singa/swig diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 22a7c7d0f4..c71703ba23 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -94,17 +94,22 @@ file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/python/singa/proto) file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/python/rafiki) file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/src/api) -IF(USE_PYTHON3) - SET(SWIG_PYTHON3 "-py3") +IF(USE_PYTHON3 AND SWIG_VERSION VERSION_LESS "4.1") + SET(SWIG_PYTHON3 "-py3") ELSE() - SET(SWIG_PYTHON3 "") + SET(SWIG_PYTHON3 "") ENDIF() execute_process( - COMMAND swig -c++ -python ${SWIG_PYTHON3} -I${CMAKE_SOURCE_DIR}/include + COMMAND ${SWIG_EXECUTABLE} -c++ -python ${SWIG_PYTHON3} + -I${CMAKE_BINARY_DIR}/src/api -I${CMAKE_SOURCE_DIR}/include -outdir ${CMAKE_BINARY_DIR}/python/singa -o ${CMAKE_BINARY_DIR}/src/api/singa_wrap.cxx - ${CMAKE_SOURCE_DIR}/src/api/singa.i) + ${CMAKE_SOURCE_DIR}/src/api/singa.i + RESULT_VARIABLE SWIG_RESULT) +IF(NOT SWIG_RESULT EQUAL 0) + MESSAGE(FATAL_ERROR "SINGA SWIG generation failed (${SWIG_RESULT})") +ENDIF() set(python_srcs "${CMAKE_BINARY_DIR}/src/api/singa_wrap.cxx") @@ -115,7 +120,12 @@ create_symlinks(${python_source_files}) execute_process( COMMAND ${PYTHON_EXECUTABLE} -c "from __future__ import print_function; import numpy; print(numpy.get_include())" - OUTPUT_VARIABLE NUMPY_INCLUDE_DIR) + OUTPUT_VARIABLE NUMPY_INCLUDE_DIR + OUTPUT_STRIP_TRAILING_WHITESPACE + RESULT_VARIABLE NUMPY_RESULT) +IF(NOT NUMPY_RESULT EQUAL 0) + MESSAGE(FATAL_ERROR "NumPy is unavailable in ${PYTHON_EXECUTABLE}") +ENDIF() #message(status "numpy path ${NUMPY_INCLUDE_DIR}") diff --git a/python/singa/model.py b/python/singa/model.py index 5f1ed2c216..ebfd36817f 100644 --- a/python/singa/model.py +++ b/python/singa/model.py @@ -27,7 +27,7 @@ import zipfile import numpy as np from functools import wraps -from collections import Iterable +from collections.abc import Iterable from singa import tensor from singa import autograd diff --git a/python/singa/sonnx.py b/python/singa/sonnx.py index 625a30c299..528e45a81f 100755 --- a/python/singa/sonnx.py +++ b/python/singa/sonnx.py @@ -23,9 +23,10 @@ import onnx import onnx.utils +import onnxoptimizer as optimizer from onnx.backend.base import Backend, BackendRep from onnx import (checker, helper, numpy_helper, GraphProto, NodeProto, - TensorProto, OperatorSetIdProto, optimizer, mapping, + TensorProto, OperatorSetIdProto, mapping, shape_inference) import warnings @@ -38,6 +39,7 @@ from . import singa_wrap as singa import collections +from collections.abc import Iterable OrderedDict = collections.OrderedDict namedtuple = collections.namedtuple @@ -57,7 +59,7 @@ np.dtype('complex128'): None, np.dtype('uint32'): None, np.dtype('uint64'): None, - np.dtype(np.object): None + np.dtype(object): None } @@ -900,7 +902,7 @@ def singa_op_to_onnx_node(cls, op, op_t): else: translator = cls._common_singa_tensor_to_onnx_node nodes = translator(op, op_t) - if not isinstance(nodes, collections.Iterable): + if not isinstance(nodes, Iterable): nodes = [nodes] nodes = [node for node in nodes if node is not None] return nodes @@ -1826,7 +1828,7 @@ def _run_node(cls, operator, inputs): list, the output """ outputs = operator(*inputs) - if not isinstance(outputs, collections.Iterable): + if not isinstance(outputs, Iterable): outputs = [outputs] return outputs @@ -1919,10 +1921,10 @@ def prepare(cls, model, device='CPU', **kwargs): """ super(SingaBackend, cls).prepare(model, device, **kwargs) # optimize and infer the shape of the model - try: - model = onnx.utils.polish_model(model) - except IndexError as err: - model = shape_inference.infer_shapes(model) + checker.check_model(model) + model = shape_inference.infer_shapes(model) + model = optimizer.optimize(model) + checker.check_model(model) # check the opset version and ir version # SINGA supports opset version(11), ir version(1.6.0 -> 6) @@ -1939,8 +1941,8 @@ def prepare(cls, model, device='CPU', **kwargs): if model.ir_version > cls._ir_version: warnings.warn( - "The imported ir verion {} is larger than the supported version {}." - .format(cls._ir_version, imp.version)) + "The imported IR version {} is larger than the supported version {}." + .format(model.ir_version, cls._ir_version)) graph = model.graph params, inputs, outputs, layers = cls._onnx_model_to_singa_ops( diff --git a/python/singa/tensor.py b/python/singa/tensor.py index 963ad1a002..3d50a07e66 100755 --- a/python/singa/tensor.py +++ b/python/singa/tensor.py @@ -372,7 +372,7 @@ def copy_from_numpy(self, np_array, offset=0): self.data.CopyFloatDataFromHostPtr(np_array) elif dt == np.float16: self.data.CopyHalfFloatDataFromHostPtr(np_array) - elif dt == np.int or dt == np.int32: + elif dt == int or dt == np.int32: self.data.CopyIntDataFromHostPtr(np_array) else: raise NotImplementedError('Not implemented yet for ', dt) @@ -886,10 +886,10 @@ def from_numpy(np_array, dev=None): ''' assert type(np_array) is np.ndarray, 'Must input numpy array' # convert to float32 array - if np_array.dtype == np.float64 or np_array.dtype == np.float: + if np_array.dtype == np.float64 or np_array.dtype == float: np_array = np_array.astype(np.float32) - if np_array.dtype == np.int64 or np_array.dtype == np.int: + if np_array.dtype == np.int64 or np_array.dtype == int: np_array = np_array.astype(np.int32) if np_array.dtype == np.float32: @@ -1791,7 +1791,7 @@ def copy_from_numpy(data, np_array): data.CopyFloatDataFromHostPtr(np_array) elif dt == np.float16: data.CopyHalfFloatDataFromHostPtr(np_array) - elif dt == np.int or dt == np.int32: + elif dt == int or dt == np.int32: data.CopyIntDataFromHostPtr(np_array) else: raise NotImplementedError('Not implemented yet for ', dt) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5f302992ba..e14f5cf2cd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -111,7 +111,7 @@ IF(CODE_COVERAGE) ENDIF(CODE_COVERAGE) #pass configure infor to swig -FILE(REMOVE "${CMAKE_CURRENT_SOURCE_DIR}/api/config.i") -CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/api/config.i.in" "${CMAKE_CURRENT_SOURCE_DIR}/api/config.i") +FILE(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/src/api") +CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/api/config.i.in" "${CMAKE_BINARY_DIR}/src/api/config.i") diff --git a/test/python/test_tensor.py b/test/python/test_tensor.py index f7044f1d26..dc7568e122 100644 --- a/test/python/test_tensor.py +++ b/test/python/test_tensor.py @@ -208,7 +208,7 @@ def test_tensor_inplace_api(self): self.assertTrue(y is x) def test_numpy_convert(self): - a = np.asarray([[1, 0, 0], [0, 1, 0]], dtype=np.int) + a = np.asarray([[1, 0, 0], [0, 1, 0]], dtype=int) t = tensor.from_numpy(a) b = tensor.to_numpy(t) self.assertEqual(np.sum(a - b), 0)