Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make singa-cpu runnable on Ubuntu 24.04 #1233

Open
wants to merge 3 commits into
base: dev-postgresql
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ __pycache__
*.cxx

build/
src/api/config.i
src/api/singa_wrap.cpp
dist/
include/singa/singa_config.h
thirdparty/*
!thirdparty/install.sh
test/samples/
Expand All @@ -30,3 +34,6 @@ test/samples/
doc/_build/*
doc/en/docs/model_zoo/
cmake-build-debug/*

# Python package
python/singa.egg-info/
7 changes: 6 additions & 1 deletion examples/cnn_ms/pkg_model_code/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@

import os
import gc
import sys
import time
import json
import zipfile
import numpy as np
from functools import wraps
from collections import Iterable

if sys.version_info < (3, 10):
from collections import Iterable
else:
from collections.abc import Iterable

from singa import tensor
from singa import autograd
Expand Down
6 changes: 3 additions & 3 deletions examples/demos/Classification/BloodMnist/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def forward(self, pic):
img = np.transpose(img, (2, 0, 1))

if img.dtype == np.uint8:
return np.array(np.float32(img)/255.0, dtype=np.float)
return np.array(np.float32(img)/255.0, dtype=np.float64)
else:
return np.float(img)
return np.float64(img)

def __repr__(self):
return self.__class__.__name__ + '()'
Expand Down Expand Up @@ -139,7 +139,7 @@ def forward(self, img: np.ndarray):
if not isinstance(img, np.ndarray):
raise TypeError('Input img should be a numpy array. Got {}.'.format(type(img)))

if not img.dtype == np.float:
if not img.dtype == float:
raise TypeError('Input array should be a float array. Got {}.'.format(img.dtype))

if img.ndim < 3:
Expand Down
6 changes: 3 additions & 3 deletions examples/healthcare/Hematologic_Disease/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def forward(self, pic):
img = np.transpose(img, (2, 0, 1))

if img.dtype == np.uint8:
return np.array(np.float32(img)/255.0, dtype=np.float)
return np.array(np.float32(img)/255.0, dtype=np.float64)
else:
return np.float(img)
return np.float64(img)

def __repr__(self):
return self.__class__.__name__ + '()'
Expand Down Expand Up @@ -139,7 +139,7 @@ def forward(self, img: np.ndarray):
if not isinstance(img, np.ndarray):
raise TypeError('Input img should be a numpy array. Got {}.'.format(type(img)))

if not img.dtype == np.float:
if not img.dtype == float:
raise TypeError('Input array should be a float array. Got {}.'.format(img.dtype))

if img.ndim < 3:
Expand Down
6 changes: 3 additions & 3 deletions examples/healthcare/data/bloodmnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ def forward(self, pic):
img = np.transpose(img, (2, 0, 1))

if img.dtype == np.uint8:
return np.array(np.float32(img) / 255.0, dtype=np.float)
return np.array(np.float32(img) / 255.0, dtype=np.float64)
else:
return np.float(img)
return np.float64(img)

def __repr__(self):
return self.__class__.__name__ + '()'
Expand Down Expand Up @@ -142,7 +142,7 @@ def forward(self, img: np.ndarray):
if not isinstance(img, np.ndarray):
raise TypeError('Input img should be a numpy array. Got {}.'.format(type(img)))

if not img.dtype == np.float:
if not img.dtype == float:
raise TypeError('Input array should be a float array. Got {}.'.format(img.dtype))

if img.ndim < 3:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@

import os
import gc
import sys
import time
import json
import zipfile
import numpy as np
from functools import wraps
from collections import Iterable

if sys.version_info < (3, 10):
from collections import Iterable
else:
from collections.abc import Iterable


from singa import tensor
from singa import autograd
Expand Down
8 changes: 4 additions & 4 deletions examples/model_selection/Trails/singa_pkg_code/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@

import os
import gc
import sys
import time
import json
import zipfile
import numpy as np
from functools import wraps
# from collections import Iterable

try:
from collections.abc import Iterable
except ImportError:
if sys.version_info < (3, 10):
from collections import Iterable
else:
from collections.abc import Iterable

from singa import tensor
from singa import autograd
Expand Down
2 changes: 1 addition & 1 deletion examples/model_selection/Trails/singa_pkg_code/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ 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:
np_array = np_array.astype(np.float32)

if np_array.dtype == np.int64 or np_array.dtype == int:
Expand Down
8 changes: 1 addition & 7 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,8 @@ 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")
ELSE()
SET(SWIG_PYTHON3 "")
ENDIF()

execute_process(
COMMAND swig -c++ -python ${SWIG_PYTHON3} -I${CMAKE_SOURCE_DIR}/include
COMMAND swig -c++ -python -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)
Expand Down
7 changes: 6 additions & 1 deletion python/singa/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@
'''

import os
import sys
import gc
import time
import json
import zipfile
import numpy as np
from functools import wraps
from collections import Iterable

if sys.version_info < (3, 10):
from collections import Iterable
else:
from collections.abc import Iterable

from singa import tensor
from singa import autograd
Expand Down
50 changes: 41 additions & 9 deletions python/singa/sonnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,48 @@

from __future__ import division

import numpy as np
import collections
import sys
import warnings
import pkg_resources

if sys.version_info < (3, 10):
from collections import Iterable
else:
from collections.abc import Iterable

import numpy as np
import onnx
import onnx.utils
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

# ref: https://github.com/onnx/onnx/issues/582
# ref: https://github.com/onnx/optimizer/blob/master/onnxoptimizer/__init__.py
ONNX_VERSION = pkg_resources.parse_version(onnx.__version__)
if ONNX_VERSION < pkg_resources.parse_version('1.9.0'):
from onnx import optimizer
import onnx.utils

OPTIMIZE_MODEL_FUNC = onnx.utils.polish_model
else:
import onnxoptimizer as optimizer

def polish_model(model): # type: (ModelProto) -> ModelProto
'''
This function combines several useful utility functions together.
'''
onnx.checker.check_model(model)
onnx.helper.strip_doc_string(model)
model = onnx.shape_inference.infer_shapes(model)
model = optimizer.optimize(model)
onnx.checker.check_model(model)
return model

OPTIMIZE_MODEL_FUNC = polish_model


from . import device
from . import autograd
Expand All @@ -37,7 +70,6 @@
from . import utils
from . import singa_wrap as singa

import collections
OrderedDict = collections.OrderedDict
namedtuple = collections.namedtuple

Expand All @@ -57,7 +89,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 +932,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 +1858,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 @@ -1920,7 +1952,7 @@ 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)
model = OPTIMIZE_MODEL_FUNC(model)
except IndexError as err:
model = shape_inference.infer_shapes(model)

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 == np.int64 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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def finalize_options(self):
print('build temp', self.build_temp)
print('build lib', self.build_lib)
super(custom_build_ext, self).finalize_options()
self.swig_opts = '-py3 -outdir {}/singa/'.format(self.build_lib).split()
self.swig_opts = '-outdir {}/singa/'.format(self.build_lib).split()
print('build temp', self.build_temp)
print('build lib', self.build_lib)

Expand Down
File renamed without changes.
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=np.int64)
t = tensor.from_numpy(a)
b = tensor.to_numpy(t)
self.assertEqual(np.sum(a - b), 0)
Expand Down
Loading