sony / nnabla

Neural Network Libraries
https://nnabla.org/
Apache License 2.0
2.73k stars 334 forks source link

Add onnx importer functions: ScatterND, EyeLike, Mod2, BitShift, and Unique #1212

Closed TomonobuTsujikawa closed 1 year ago

TomonobuTsujikawa commented 1 year ago

This PR adds onnx importer functions.

ScatterND

Convert ONNX output = ScatterND(data, indices, updates, reduction) to the following nnabla representation:

if len(indices_shape) == 1:
    indices_shape = [1] + indices_shape
    reshaped_indices = F.reshape(indices, indices_shape, inplace=False)
    updates_shape = [1] + updates_shape
    reshaped_updates = F.reshape(updates, updates_shape, inplace=False)

transpose_shape = np.roll(np.arange(len(indices_shape)), 1)
transpose_indices = F.transpose(reshaped_indices, transpose_shape)

duplicated_data = F.identity(data)

add = False
if reduction == ‘add’:
    add = True
y = F.scatter_nd(reshaped_updates, transpose_indices, out = duplicated_data, add = add)

Limitation

EyeLike

Convert ONNX output = EyeLike(input, dtype, k) to the following nnabla representation:

output = F.eye_like(input, k)

The interface of nnabla EyeLike function is compatible with ONNX EyeLike except dtype attribute. For details, see ONNX Operators documentation: https://github.com/onnx/onnx/blob/main/docs/Operators.md#eyelike

In ONNX importer, attribute dtype of ONNX EyeLike is ignored, and the output dtype is determined by nnabla context. This implementation does not generate information loss due to the type conversion because EyeLike outputs only 0 or 1.

Mod2

Convert ONNX C = Mod(A, B, fmod) to the following nnabla representation:

C = F.mod2(A, B, bool(fmod))

The interface of nnabla Mod2 function is compatible with ONNX Mod. For details, see ONNX Operators documentation: https://github.com/onnx/onnx/blob/main/docs/Operators.md#mod

NOTICE:

BitShift

Convert ONNX Z = BitShift(X, Y, direction) to the following nnabla representation:

Z = F.bit_shift(X, Y, direction)

The interface of nnabla BitShift function is compatible with ONNX BitShift. For details, see ONNX Operators documentation: https://github.com/onnx/onnx/blob/main/docs/Operators.md#bitshift

NOTICE:

Unique

Convert ONNX Y, indices, inverse_indices, counts = Unique(X, axis, sorted) to the following nnabla representation:

flatten = False
if axis is None:
    flatten = True
    axis = 0

# Set True or False to with_(indices|inverse|counts) according to the existence of optional outputs
with_indices = # True or False
with_inverse = # True or False
with_counts = # True or False

# Unique
Y, indices, inverse_indices, counts = F.unique(x, flatten, axis, sorted, with_index, with_inverse, with_counts)

The interface of Unique function is basically compatible with ONNX Unique. For details, see ONNX Operators documentation: https://github.com/onnx/onnx/blob/main/docs/Operators.md#unique

nnabla Unique has flatten as additional parameter because nnabla layer function cannot have optional parameters. The difference between ONNX Unique and nnabla Unique is as follows: