secretflow.security.compare package#

Submodules#

secretflow.security.compare.comparator module#

Classes:

Comparator()

The abstract comparator.

class secretflow.security.compare.comparator.Comparator[source]#

Bases: ABC

The abstract comparator.

Methods:

min(data[, axis, reveal])

The minimum of array over a given axis.

max(data[, axis, reveal])

The maximum of array over a given axis.

abstract min(data: List[DeviceObject], axis=None, reveal=False)[source]#

The minimum of array over a given axis.

abstract max(data: List[DeviceObject], axis=None, reveal=False)[source]#

The maximum of array over a given axis.

secretflow.security.compare.device_comparator module#

Classes:

DeviceComparator(device)

Comparator based on a device (PYU or SPU).

class secretflow.security.compare.device_comparator.DeviceComparator(device: Union[PYU, SPU])[source]#

Bases: Comparator

Comparator based on a device (PYU or SPU).

device#

a PYU or SPU. The device where the computation hosts.

Type

Union[secretflow.device.device.pyu.PYU, secretflow.device.device.spu.SPU]

Attributes:

device

Methods:

min(data[, axis])

The minimum of array over a given axis.

max(data[, axis])

The maximum of array over a given axis.

__init__(device)

device: Union[PYU, SPU]#
min(data: List[DeviceObject], axis=None)[source]#

The minimum of array over a given axis.

Parameters
  • data – array of device objects.

  • axis – optional. Same as the axis argument of numpy.amin().

Returns

a device object holds the minimum.

max(data: List[DeviceObject], axis=None)[source]#

The maximum of array over a given axis.

Parameters
  • data – array of device objects.

  • axis – optional. Same as the axis argument of numpy.amax().

Returns

a device object holds the maximum.

__init__(device: Union[PYU, SPU]) None#

secretflow.security.compare.plain_comparator module#

Classes:

PlainComparator(device)

Plaintext compartator.

class secretflow.security.compare.plain_comparator.PlainComparator(device: Union[PYU, SPU])[source]#

Bases: DeviceComparator

Plaintext compartator.

The computation will be performed in plaintext.

Warning

PlainAggregator is for debugging purpose only. You should not use it in production.

Examples

>>> import numpy as np
# Alice and bob are both pyu instances.
>>> a = alice(lambda : np.random.rand(2, 5))()
>>> b = bob(lambda : np.random.rand(2, 5))()
>>> comparator = PlainComparator(alice)
>>> min_a_b = comparator.min([a, b], axis=0)
>>> sf.reveal(min_a_b)
array([[0.47092903, 0.77865475, 0.05917433, 0.07155096, 0.16089967],
    [0.56598   , 0.51047045, 0.35771865, 0.23004009, 0.23400909]],
    dtype=float32)
    >>> max_a_b = comparator.max([a, b], axis=0)
>>> sf.reveal(max_a_b)
array([[0.5939065 , 0.8463326 , 0.14722177, 0.9977698 , 0.6186677 ],
    [0.65607053, 0.611439  , 0.957074  , 0.6548823 , 0.445968  ]],
    dtype=float32)

Attributes:

device: Union[PYU, SPU]#

secretflow.security.compare.spu_comparator module#

Classes:

SPUComparator(device)

Compartator based on SPU.

class secretflow.security.compare.spu_comparator.SPUComparator(device: Union[PYU, SPU])[source]#

Bases: DeviceComparator

Compartator based on SPU.

The computation will be performed on the given SPU device.

Examples

>>> import numpy as np
# Alice and bob are both pyu instances.
>>> a = alice(lambda : np.random.rand(2, 5))()
>>> b = bob(lambda : np.random.rand(2, 5))()
>>> comparator = SPUComparator(alice)
>>> min_a_b = comparator.min([a, b], axis=0)
>>> sf.reveal(min_a_b)
array([[0.47092903, 0.77865475, 0.05917433, 0.07155096, 0.16089967],
    [0.56598   , 0.51047045, 0.35771865, 0.23004009, 0.23400909]],
    dtype=float32)
>>> max_a_b = comparator.max([a, b], axis=0)
>>> sf.reveal(max_a_b)
array([[0.5939065 , 0.8463326 , 0.14722177, 0.9977698 , 0.6186677 ],
    [0.65607053, 0.611439  , 0.957074  , 0.6548823 , 0.445968  ]],
    dtype=float32)

Attributes:

device: Union[PYU, SPU]#

Module contents#

Classes:

Comparator()

The abstract comparator.

DeviceComparator(device)

Comparator based on a device (PYU or SPU).

PlainComparator(device)

Plaintext compartator.

SPUComparator(device)

Compartator based on SPU.

class secretflow.security.compare.Comparator[source]#

Bases: ABC

The abstract comparator.

Methods:

min(data[, axis, reveal])

The minimum of array over a given axis.

max(data[, axis, reveal])

The maximum of array over a given axis.

abstract min(data: List[DeviceObject], axis=None, reveal=False)[source]#

The minimum of array over a given axis.

abstract max(data: List[DeviceObject], axis=None, reveal=False)[source]#

The maximum of array over a given axis.

class secretflow.security.compare.DeviceComparator(device: Union[PYU, SPU])[source]#

Bases: Comparator

Comparator based on a device (PYU or SPU).

device#

a PYU or SPU. The device where the computation hosts.

Type

Union[secretflow.device.device.pyu.PYU, secretflow.device.device.spu.SPU]

Attributes:

device

Methods:

min(data[, axis])

The minimum of array over a given axis.

max(data[, axis])

The maximum of array over a given axis.

__init__(device)

device: Union[PYU, SPU]#
min(data: List[DeviceObject], axis=None)[source]#

The minimum of array over a given axis.

Parameters
  • data – array of device objects.

  • axis – optional. Same as the axis argument of numpy.amin().

Returns

a device object holds the minimum.

max(data: List[DeviceObject], axis=None)[source]#

The maximum of array over a given axis.

Parameters
  • data – array of device objects.

  • axis – optional. Same as the axis argument of numpy.amax().

Returns

a device object holds the maximum.

__init__(device: Union[PYU, SPU]) None#
class secretflow.security.compare.PlainComparator(device: Union[PYU, SPU])[source]#

Bases: DeviceComparator

Plaintext compartator.

The computation will be performed in plaintext.

Warning

PlainAggregator is for debugging purpose only. You should not use it in production.

Examples

>>> import numpy as np
# Alice and bob are both pyu instances.
>>> a = alice(lambda : np.random.rand(2, 5))()
>>> b = bob(lambda : np.random.rand(2, 5))()
>>> comparator = PlainComparator(alice)
>>> min_a_b = comparator.min([a, b], axis=0)
>>> sf.reveal(min_a_b)
array([[0.47092903, 0.77865475, 0.05917433, 0.07155096, 0.16089967],
    [0.56598   , 0.51047045, 0.35771865, 0.23004009, 0.23400909]],
    dtype=float32)
    >>> max_a_b = comparator.max([a, b], axis=0)
>>> sf.reveal(max_a_b)
array([[0.5939065 , 0.8463326 , 0.14722177, 0.9977698 , 0.6186677 ],
    [0.65607053, 0.611439  , 0.957074  , 0.6548823 , 0.445968  ]],
    dtype=float32)

Attributes:

device: Union[PYU, SPU]#
class secretflow.security.compare.SPUComparator(device: Union[PYU, SPU])[source]#

Bases: DeviceComparator

Compartator based on SPU.

The computation will be performed on the given SPU device.

Examples

>>> import numpy as np
# Alice and bob are both pyu instances.
>>> a = alice(lambda : np.random.rand(2, 5))()
>>> b = bob(lambda : np.random.rand(2, 5))()
>>> comparator = SPUComparator(alice)
>>> min_a_b = comparator.min([a, b], axis=0)
>>> sf.reveal(min_a_b)
array([[0.47092903, 0.77865475, 0.05917433, 0.07155096, 0.16089967],
    [0.56598   , 0.51047045, 0.35771865, 0.23004009, 0.23400909]],
    dtype=float32)
>>> max_a_b = comparator.max([a, b], axis=0)
>>> sf.reveal(max_a_b)
array([[0.5939065 , 0.8463326 , 0.14722177, 0.9977698 , 0.6186677 ],
    [0.65607053, 0.611439  , 0.957074  , 0.6548823 , 0.445968  ]],
    dtype=float32)

Attributes:

device: Union[PYU, SPU]#