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

[Feature]: 如何看到SPU DeviceObject的具体shares的值? #963

Open
wangmn-cufe opened this issue Jan 8, 2025 · 4 comments
Open

Comments

@wangmn-cufe
Copy link

Feature Request Type

Usability

Have you searched existing issues?

Yes

Is your feature request related to a problem?

这里似乎给出了使用secretflow看到SPU DeviceObject具体shares值的方法:#908
但是在不使用secretflow而仅仅使用spu的情况下,如何获取secret shares的值?

我参照https://github.com/secretflow/spu/blob/main/spu/tests/spu_io_test.py
写了如下代码:
import spu.api as ppapi
import spu.spu_pb2 as spu_pb2

def _bytes_to_pb(msg: bytes):...与spu_io_test.py中函数定义相同

config = spu_pb2.RuntimeConfig(
protocol=spu_pb2.ProtocolKind.REF2K,
field=spu_pb2.FieldType.FM64,
fxp_fraction_bits=18,
share_max_chunk_size=4,
)
wsize=2
io = ppapi.Io(wsize, config)

x = np.random.randint(10, size=())
xs = io.make_shares(x, spu_pb2.Visibility.VIS_SECRET)
print(_bytes_to_pb(xs[0].meta))
print(xs[0].share_chunks)
print(xs[1].share_chunks)
print(x)

执行后的结果为:
data_type: DT_I64
visibility: VIS_SECRET
storage_type: "ref2k.Sec"

[b'\x08\x08\x1a\x04\x07\x00\x00\x00', b'\x08\x08\x10\x04\x1a\x04\x00\x00\x00\x00']
[b'\x08\x08\x1a\x04\x07\x00\x00\x00', b'\x08\x08\x10\x04\x1a\x04\x00\x00\x00\x00']
7
请问哪一项是secret shares?或者需要用到其他的函数?

Describe features you want to add to SPU

A clear and concise description of what you want to happen.

Describe features you want to add to SPU

A clear and concise description of any alternative solutions or features you've considered.

@w-gc
Copy link
Collaborator

w-gc commented Jan 9, 2025

print(xs[0].share_chunks)
print(xs[1].share_chunks)

这两个都是secret shares,因为你使用的是"ref2k.Sec",每方的数据都是一样的(code ref)。另外,在c++转python过程中,会对spu::Value序列化成protobuf,因此输出的结果不可读,具体的逻辑可以看MakeShares。你可以尝试使用Reconstruct对其反序列化。

@wangmn-cufe
Copy link
Author

谢谢,参考您的建议和代码ValueChunkProtoheu.py后,在本问题所述代码中添加:

rt = spu_pb2.ValueChunkProto()
rt.ParseFromString(xs[0].share_chunks[0])
print(int.from_bytes(rt.content, "little", signed=True))

rt = spu_pb2.ValueChunkProto()
rt.ParseFromString(xs[1].share_chunks[0])
print(int.from_bytes(rt.content, "little", signed=True))

执行后,得到结果:

4660903219305295116
-4660903219305295112

此时,x=4。另外,为了简单,我将share_max_chunk_size改为了8,这样share_chunks中只有一块数据。这是正确的吗?

@w-gc
Copy link
Collaborator

w-gc commented Jan 15, 2025

转成ValueChunkProto只是其中一小段,需要拼接起来的:

import spu.api as ppapi
import spu.spu_pb2 as spu_pb2
import numpy as np

config = spu_pb2.RuntimeConfig(
    protocol=spu_pb2.ProtocolKind.REF2K,
    field=spu_pb2.FieldType.FM64,
    fxp_fraction_bits=18,
    share_max_chunk_size=4,
)

wsize = 2
io = ppapi.Io(wsize, config)

x = np.random.randint(-100, 100, size=()); 
xs = io.make_shares(x, spu_pb2.Visibility.VIS_SECRET)

print(x)

for xss in xs:
    value = bytearray()
    for chk in xss.share_chunks:
        rt = spu_pb2.ValueChunkProto()
        rt.ParseFromString(chk)
        value += rt.content  # <---- follow ValueFromPyBindShare

    print(value)
    print(int.from_bytes(value, "little", signed=True))

@w-gc
Copy link
Collaborator

w-gc commented Jan 15, 2025

转成ValueChunkProto只是其中一小段,需要拼接起来的:

import spu.api as ppapi
import spu.spu_pb2 as spu_pb2
import numpy as np

config = spu_pb2.RuntimeConfig(
    protocol=spu_pb2.ProtocolKind.REF2K,
    field=spu_pb2.FieldType.FM64,
    fxp_fraction_bits=18,
    share_max_chunk_size=4,
)

wsize = 2
io = ppapi.Io(wsize, config)

x = np.random.randint(-100, 100, size=()); 
xs = io.make_shares(x, spu_pb2.Visibility.VIS_SECRET)

print(x)

for xss in xs:
    value = bytearray()
    for chk in xss.share_chunks:
        rt = spu_pb2.ValueChunkProto()
        rt.ParseFromString(chk)
        value += rt.content  # <---- follow ValueFromPyBindShare

    print(value)
    print(int.from_bytes(value, "little", signed=True))

这个做法是非常trick的,仅仅用于debug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants