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

Is it necessary to support new types of tensor? #65

Open
loloxwg opened this issue Jan 15, 2025 · 6 comments
Open

Is it necessary to support new types of tensor? #65

loloxwg opened this issue Jan 15, 2025 · 6 comments

Comments

@loloxwg
Copy link

loloxwg commented Jan 15, 2025

The input and output of reasoning frameworks are typically tensor types, but I didn't find this type in SurrealDB.

@maxwellflitton
Copy link
Contributor

@loloxwg hey thanks for looking into this package. We are coming close to a big release with updates. In terms of the return types, we need the package to gel with the SQL language and other languages such as Python. It's possible that we can build an option to return tensors. What is your use case for surrealml? I can see if I can accommodate it.

@loloxwg
Copy link
Author

loloxwg commented Jan 16, 2025

I plan to start by using it on the MNIST dataset for digit recognition. @maxwellflitton

@loloxwg
Copy link
Author

loloxwg commented Jan 16, 2025

CleanShot 2025-01-16 at 10 18 22
I want the input type for this model to be a tensor with the shape [1, 1, 28, 28].

@slygren
Copy link

slygren commented Jan 16, 2025

@loloxwg hey thanks for looking into this package. We are coming close to a big release with updates. In terms of the return types, we need the package to gel with the SQL language and other languages such as Python. It's possible that we can build an option to return tensors. What is your use case for surrealml? I can see if I can accommodate it.

@maxwellflitton - do you have any timeline on the update?

@slygren
Copy link

slygren commented Jan 27, 2025

I have a somewhat similar request - allowing for input to be of different types than pure scalars. For my use case where I'm doing state recognition of timeseries data stored in SurrealDB, I perform GAF conversion of a time series section to create images (shape (1, 3, 64, 64)) and want to pass them through a pytorch/onnx/surml model.

Unfortunately, surml does not allow inputs of required type & shape

This works

import onnxruntime as ort

model = torch.load('./cnn_model.pth', map_location=torch.device('cpu'))
model.eval()

test_input = torch.rand(64, 64, 3, dtype=torch.float32) * 2 - 1
test_input = test_input.permute(2, 0, 1).unsqueeze(0)
onnx_file_path = './cnn_model.onnx'

torch.onnx.export(
    model,
    test_input,
    onnx_file_path,
    export_params=True,
    opset_version=18,
    do_constant_folding=True,
    input_names=["input"],
    output_names=["output"],
    dynamic_axes={
        "input": {0: "batch_size"},
        "output": {0: "batch_size"}
    }
)

res = await con.query('SELECT VALUE value FROM timeserie_data:["ts_1", time::now()-3h]..=["ts_1", time::now()-2h]')
downscaled = downScale(np.array(res))
img = getAndMakeData(downscaled)
shaped_img = np.expand_dims(np.transpose(img, (2, 0, 1)), axis=0)

ort_session = ort.InferenceSession(onnx_file_path)
input_data = shaped_img.astype(np.float32)
outputs = ort_session.run(None, {"input": input_data})
print("Model output:", outputs)

while this fails

file = SurMlFile(model=onnx_model, name="tsPeriodState", engine=Engine.ONNX)
file.raw_compute(shaped_img.astype(np.float32))

with the error

TypeError: argument 'input_vector': only length-1 arrays can be converted to Python scalars

@maxwellflitton
Copy link
Contributor

@slygren I think the conversion is down to PyO3 and we're ripping this out and going for raw C bindings. @loloxwg I'm just focusing on the Python client this week and then will get back on the surrealML and work out what the estimate is.

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

3 participants