Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 7 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand All @@ -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}")
Expand Down
1 change: 1 addition & 0 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
67 changes: 67 additions & 0 deletions examples/neurostore/prepare.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion java/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 16 additions & 6 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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}")

Expand Down
2 changes: 1 addition & 1 deletion python/singa/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 12 additions & 10 deletions python/singa/sonnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -38,6 +39,7 @@
from . import singa_wrap as singa

import collections
from collections.abc import Iterable
OrderedDict = collections.OrderedDict
namedtuple = collections.namedtuple

Expand All @@ -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
}


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions python/singa/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")


2 changes: 1 addition & 1 deletion test/python/test_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down