Skip to content

Commit

Permalink
[NeoML-Py] Bump numpy; Fix CodeQL; Fix Transformer and TiedEmbeddings…
Browse files Browse the repository at this point in the history
… py interface (#1102)

* [NeoML-Py] Fix CodeQL warnings

Signed-off-by: Kirill Golikov <[email protected]>

* [NeoML-Py] np.asarray for numpy>=2.0.0

Signed-off-by: Kirill Golikov <[email protected]>

* [NeoML-Py] Update tests.py

Signed-off-by: Kirill Golikov <[email protected]>

* [NeoML-Py] Update neoml_onnx_backend_test.py

Signed-off-by: Kirill Golikov <[email protected]>

* [NeoML-Py] PyTiedEmbeddingsLayer embeddings_layer_path

Signed-off-by: Kirill Golikov <[email protected]>

* [NeoML-Py] PyTransformerLayer pre_norm & sa_dropout

Signed-off-by: Kirill Golikov <[email protected]>

* [NeoML-Py] Bump numpy & pybind11

Signed-off-by: Kirill Golikov <[email protected]>

* [NeoML-Py] py::array ctor checks for numpy>=2.0.0

Signed-off-by: Kirill Golikov <[email protected]>

---------

Signed-off-by: Kirill Golikov <[email protected]>
  • Loading branch information
favorart authored Sep 4, 2024
1 parent 41a9227 commit 6910163
Show file tree
Hide file tree
Showing 34 changed files with 435 additions and 160 deletions.
2 changes: 1 addition & 1 deletion NeoML/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ find_package(Python3 ${Python3_FIND_VERSION} EXACT COMPONENTS Interpreter Develo
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11.git
GIT_TAG v2.11.1
GIT_TAG v2.13.5
)

FetchContent_GetProperties(pybind11)
Expand Down
20 changes: 10 additions & 10 deletions NeoML/Python/neoml/AutoDiff.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Copyright (c) 2017-2020 ABBYY Production LLC
""" Copyright (c) 2017-2024 ABBYY
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,7 @@
import neoml.PythonWrapper as PythonWrapper
from neoml.MathEngine import MathEngine
from neoml.Blob import Blob
import numpy
import numpy as np

# ----------------------------------------------------------------------------------------------------------------------

Expand All @@ -28,15 +28,15 @@ def const(math_engine, shape, data):
if not isinstance(math_engine, MathEngine):
raise ValueError('The `math_engine` should be neoml.MathEngine.')

np_shape = numpy.array(shape, dtype=numpy.int32, copy=False)
np_shape = np.asarray(shape, dtype=np.int32)

if len(np_shape) > 7:
raise ValueError('The `shape` should have not more than 7 dimensions.')

if numpy.isscalar(data):
if np.isscalar(data):
return Blob(PythonWrapper.blob_const(math_engine._internal, np_shape, float(data)))

np_data = numpy.array(data, dtype=numpy.float32, copy=False, order='C')
np_data = np.asarray(data, dtype=np.float32, order='C')

if len(np_data.shape) > 7:
raise ValueError('The `shape` should have not more than 7 dimensions.')
Expand Down Expand Up @@ -99,7 +99,7 @@ def sum(a, axes=None):
if a.size == 0:
raise ValueError("The blob shouldn't be empty.")

axes = numpy.array([] if axes is None else axes, dtype=numpy.int32)
axes = np.array([] if axes is None else axes, dtype=np.int32)
if not neoml.Utils.check_axes(axes):
raise ValueError("`axes` should be unique and in range [0, 6].")

Expand Down Expand Up @@ -129,7 +129,7 @@ def mean(a, axes=None):
if a.size == 0:
raise ValueError("The blob shouldn't be empty.")

axes = numpy.array([] if axes is None else axes, dtype=numpy.int32)
axes = np.array([] if axes is None else axes, dtype=np.int32)
if not neoml.Utils.check_axes(axes):
raise ValueError("`axes` should be unique and in range [0, 6].")

Expand Down Expand Up @@ -207,7 +207,7 @@ def broadcast(blob, shape):
if blob.size == 0:
raise ValueError("The blobs mustn't be empty.")

np_shape = numpy.array(shape, dtype=numpy.int32, copy=False)
np_shape = np.asarray(shape, dtype=np.int32)

if len(np_shape) > 7:
raise ValueError('The `shape` should have not more than 7 dimensions.')
Expand All @@ -227,12 +227,12 @@ def reshape(blob, shape):
if blob.size == 0:
raise ValueError("The blobs mustn't be empty.")

np_shape = numpy.array(shape, dtype=numpy.int32, copy=False)
np_shape = np.asarray(shape, dtype=np.int32)

if len(np_shape) > 7:
raise ValueError('The `shape` should have not more than 7 dimensions.')

if numpy.prod(np_shape) != blob.size:
if np.prod(np_shape) != blob.size:
raise ValueError('`shape` is incompatible with current size.')

PythonWrapper.blob_reshape(blob._internal, np_shape)
Expand Down
14 changes: 7 additions & 7 deletions NeoML/Python/neoml/Blob.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Copyright (c) 2017-2020 ABBYY Production LLC
""" Copyright (c) 2017-2024 ABBYY
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -319,12 +319,12 @@ def asblob(math_engine, data, shape=None, copy=False):
if shape is None:
shape = np.ones(7, np.int32)
else:
shape = np.array(shape, dtype=np.int32, copy=False)
shape = np.asarray(shape, dtype=np.int32)

if len(shape) != 7:
raise ValueError('The `shape` must have 7 dimension sizes.')

np_data = np.array(data, copy=False, order='C')
np_data = np.asarray(data, order='C')

if len(np_data.shape) > 7:
raise ValueError('The `shape` must have not more then 7 dimensions.')
Expand Down Expand Up @@ -407,7 +407,7 @@ def tensor(math_engine, shape, dtype="float32"):
if dtype != "float32" and dtype != "int32":
raise ValueError('The `dtype` must be one of {`float32`, `int32`}.')

shape = np.array(shape, dtype=np.int32, copy=False)
shape = np.asarray(shape, dtype=np.int32)

if shape.size != 7:
raise ValueError('The `shape.size` must be == 7.')
Expand Down Expand Up @@ -449,7 +449,7 @@ def list_blob(math_engine, batch_len, batch_width, list_size, channels, dtype="f
if channels < 1:
raise ValueError('The `channels` must be > 0.')

shape = np.array((batch_len, batch_width, list_size, 1, 1, 1, channels), dtype=np.int32, copy=False)
shape = np.asarray((batch_len, batch_width, list_size, 1, 1, 1, channels), dtype=np.int32)

return Blob(PythonWrapper.tensor(math_engine._internal, shape, dtype))

Expand Down Expand Up @@ -490,7 +490,7 @@ def image2d(math_engine, batch_len, batch_width, height, width, channels, dtype=
if channels < 1:
raise ValueError('The `channels` must be > 0.')

shape = np.array((batch_len, batch_width, 1, height, width, 1, channels), dtype=np.int32, copy=False)
shape = np.asarray((batch_len, batch_width, 1, height, width, 1, channels), dtype=np.int32)

return Blob(PythonWrapper.tensor(math_engine._internal, shape, dtype))

Expand Down Expand Up @@ -536,6 +536,6 @@ def image3d(math_engine, batch_len, batch_width, height, width, depth, channels,
if channels < 1:
raise ValueError('The `channels` must be > 0.')

shape = np.array((batch_len, batch_width, 1, height, width, depth, channels), dtype=np.int32, copy=False)
shape = np.asarray((batch_len, batch_width, 1, height, width, depth, channels), dtype=np.int32)

return Blob(PythonWrapper.tensor(math_engine._internal, shape, dtype))
4 changes: 2 additions & 2 deletions NeoML/Python/neoml/Clustering.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Copyright (c) 2017-2021 ABBYY Production LLC
""" Copyright (c) 2017-2024 ABBYY
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -16,9 +16,9 @@

import numpy
from .Utils import convert_data, get_data
from scipy.sparse import csr_matrix
import neoml.PythonWrapper as PythonWrapper


class FirstCome(PythonWrapper.FirstCome) :
"""First come clustering creates a new cluster for each new vector
that is far enough from the clusters already existing.
Expand Down
3 changes: 1 addition & 2 deletions NeoML/Python/neoml/CrossValidation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Copyright (c) 2017-2021 ABBYY Production LLC
""" Copyright (c) 2017-2024 ABBYY
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,6 @@

import numpy
from .Utils import convert_data, get_data
from scipy.sparse import csr_matrix, issparse
import neoml.PythonWrapper as PythonWrapper


Expand Down
15 changes: 7 additions & 8 deletions NeoML/Python/neoml/DecisionTree.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Copyright (c) 2017-2021 ABBYY Production LLC
""" Copyright (c) 2017-2024 ABBYY
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,9 +14,8 @@
--------------------------------------------------------------------------------------------------------------
"""

import numpy
import numpy as np
from .Utils import convert_data, get_data
from scipy.sparse import csr_matrix, issparse
import neoml.PythonWrapper as PythonWrapper


Expand Down Expand Up @@ -126,20 +125,20 @@ def train(self, X, Y, weight=None):
"""

x = convert_data(X)
y = numpy.array(Y, dtype=numpy.int32, copy=False, order='C')
y = np.asarray(Y, dtype=np.int32, order='C')

if x.shape[0] != y.size:
raise ValueError('The `X` and `Y` inputs must be the same length.')

if weight is None:
weight = numpy.ones(y.size, numpy.float32, order='C')
weight = np.ones(y.size, np.float32, order='C')
else:
weight = numpy.array(weight, dtype=numpy.float32, copy=False, order='C')
weight = np.asarray(weight, dtype=np.float32, order='C')

if numpy.any(y < 0):
if np.any(y < 0):
raise ValueError('All `Y` elements must be >= 0.')

if numpy.any(weight < 0):
if np.any(weight < 0):
raise ValueError('All `weight` elements must be >= 0.')

return DecisionTreeClassificationModel(super().train_classifier(*get_data(x), int(x.shape[1]), y, weight))
8 changes: 4 additions & 4 deletions NeoML/Python/neoml/Dnn/DnnDistributed.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
""" Copyright (c) 2017-2020 ABBYY Production LLC
""" Copyright (c) 2017-2024 ABBYY
Licensed 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.
Expand All @@ -11,9 +14,6 @@
--------------------------------------------------------------------------------------------------------------*/
"""

import uuid
import os
from neoml.MathEngine import MathEngine
import neoml.PythonWrapper as PythonWrapper
from neoml.Blob import Blob
from neoml.Dnn import Dnn
Expand Down
5 changes: 2 additions & 3 deletions NeoML/Python/neoml/Dnn/MultichannelLookup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Copyright (c) 2017-2020 ABBYY Production LLC
""" Copyright (c) 2017-2024 ABBYY
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,6 @@
--------------------------------------------------------------------------------------------------------------
"""

import numpy
import neoml.PythonWrapper as PythonWrapper
from .Dnn import Layer
from .Initializer import Initializer
Expand Down Expand Up @@ -135,7 +134,7 @@ def initialize(self, initializer):
than the one set for the whole network in general.
"""
if initializer is None:
return self._internal.clear()
self._internal.clear()

if not isinstance(initializer, Initializer):
raise ValueError('The `initializer` must be an Initializer.')
Expand Down
3 changes: 1 addition & 2 deletions NeoML/Python/neoml/Dnn/SpaceAndDepth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Copyright (c) 2017-2020 ABBYY Production LLC
""" Copyright (c) 2017-2024 ABBYY
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,6 @@
import neoml.PythonWrapper as PythonWrapper
from .Dnn import Layer
from neoml.Utils import check_input_layers
import neoml.Blob as Blob


class SpaceToDepth(Layer):
Expand Down
32 changes: 16 additions & 16 deletions NeoML/Python/neoml/Dnn/Split.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Copyright (c) 2017-2020 ABBYY Production LLC
""" Copyright (c) 2017-2024 ABBYY
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,7 @@
import neoml.PythonWrapper as PythonWrapper
from .Dnn import Layer
from neoml.Utils import check_input_layers
import numpy
import numpy as np


class SplitChannels(Layer):
Expand Down Expand Up @@ -55,12 +55,12 @@ def __init__(self, input_layer, sizes, name=None):

layers, outputs = check_input_layers(input_layer, 1)

s = numpy.array(sizes, dtype=numpy.int32, copy=False)
s = np.asarray(sizes, dtype=np.int32)

if s.size > 3:
raise ValueError('The `sizes` must contain not more than 3 elements.')

if numpy.any(s < 0):
if np.any(s < 0):
raise ValueError('The `sizes` must contain only positive values.')

internal = PythonWrapper.SplitChannels(str(name), layers[0], int(outputs[0]), s)
Expand Down Expand Up @@ -104,12 +104,12 @@ def __init__(self, input_layer, sizes, name=None):

layers, outputs = check_input_layers(input_layer, 1)

s = numpy.array(sizes, dtype=numpy.int32, copy=False)
s = np.asarray(sizes, dtype=np.int32)

if s.size > 3:
raise ValueError('The `sizes` must contain not more than 3 elements.')

if numpy.any(s < 0):
if np.any(s < 0):
raise ValueError('The `sizes` must contain only positive values.')

internal = PythonWrapper.SplitDepth(str(name), layers[0], int(outputs[0]), s)
Expand Down Expand Up @@ -153,12 +153,12 @@ def __init__(self, input_layer, sizes, name=None):

layers, outputs = check_input_layers(input_layer, 1)

s = numpy.array(sizes, dtype=numpy.int32, copy=False)
s = np.asarray(sizes, dtype=np.int32)

if s.size > 3:
raise ValueError('The `sizes` must contain not more than 3 elements.')

if numpy.any(s < 0):
if np.any(s < 0):
raise ValueError('The `sizes` must contain only positive values.')

internal = PythonWrapper.SplitWidth(str(name), layers[0], int(outputs[0]), s)
Expand Down Expand Up @@ -202,12 +202,12 @@ def __init__(self, input_layer, sizes, name=None):

layers, outputs = check_input_layers(input_layer, 1)

s = numpy.array(sizes, dtype=numpy.int32, copy=False)
s = np.asarray(sizes, dtype=np.int32)

if s.size > 3:
raise ValueError('The `sizes` must contain not more than 3 elements.')

if numpy.any(s < 0):
if np.any(s < 0):
raise ValueError('The `sizes` must contain only positive values.')

internal = PythonWrapper.SplitHeight(str(name), layers[0], int(outputs[0]), s)
Expand Down Expand Up @@ -251,12 +251,12 @@ def __init__(self, input_layer, sizes, name=None):

layers, outputs = check_input_layers(input_layer, 1)

s = numpy.array(sizes, dtype=numpy.int32, copy=False)
s = np.asarray(sizes, dtype=np.int32)

if s.size > 3:
raise ValueError('The `sizes` must contain not more than 3 elements.')

if numpy.any(s < 0):
if np.any(s < 0):
raise ValueError('The `sizes` must contain only positive values.')

internal = PythonWrapper.SplitListSize(str(name), layers[0], int(outputs[0]), s)
Expand Down Expand Up @@ -300,12 +300,12 @@ def __init__(self, input_layer, sizes, name=None):

layers, outputs = check_input_layers(input_layer, 1)

s = numpy.array(sizes, dtype=numpy.int32, copy=False)
s = np.asarray(sizes, dtype=np.int32)

if s.size > 3:
raise ValueError('The `sizes` must contain not more than 3 elements.')

if numpy.any(s < 0):
if np.any(s < 0):
raise ValueError('The `sizes` must contain only positive values.')

internal = PythonWrapper.SplitBatchWidth(str(name), layers[0], int(outputs[0]), s)
Expand Down Expand Up @@ -349,12 +349,12 @@ def __init__(self, input_layer, sizes, name=None):

layers, outputs = check_input_layers(input_layer, 1)

s = numpy.array(sizes, dtype=numpy.int32, copy=False)
s = np.asarray(sizes, dtype=np.int32)

if s.size > 3:
raise ValueError('The `sizes` must contain not more than 3 elements.')

if numpy.any(s < 0):
if np.any(s < 0):
raise ValueError('The `sizes` must contain only positive values.')

internal = PythonWrapper.SplitBatchLength(str(name), layers[0], int(outputs[0]), s)
Expand Down
Loading

0 comments on commit 6910163

Please sign in to comment.