Skip to content

Commit caec9bc

Browse files
awoll-bdaiexploy-bot
authored andcommitted
Support int64 in onnxruntime
### What change is being made Add support for `int64` in onnxruntime ### Why this change is being made Support onnx policies using `int64`. ### Tested Covered by unit tests. GitOrigin-RevId: 8734297ac47e1929182415a82b846a543a23a428
1 parent b67018f commit caec9bc

4 files changed

Lines changed: 110 additions & 5 deletions

File tree

control/include/exploy/onnx_runtime.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ struct onnx_type<int32_t> {
2828
static constexpr ONNXTensorElementDataType value = ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32;
2929
};
3030

31+
template <>
32+
struct onnx_type<int64_t> {
33+
static constexpr ONNXTensorElementDataType value = ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64;
34+
};
35+
3136
template <>
3237
struct onnx_type<bool> {
3338
static constexpr ONNXTensorElementDataType value = ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL;

control/src/onnx_runtime.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ void resetTensorBuffer(Ort::Value& tensor, ONNXTensorElementDataType data_type)
6565
std::fill_n(tensor.GetTensorMutableData<int32_t>(), count, 0);
6666
break;
6767
}
68+
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64: {
69+
std::fill_n(tensor.GetTensorMutableData<int64_t>(), count, 0);
70+
break;
71+
}
6872
case ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL: {
6973
std::fill_n(tensor.GetTensorMutableData<bool>(), count, false);
7074
break;
@@ -246,6 +250,9 @@ bool OnnxRuntime::copyOutputToInput(const std::string& output_name, const std::s
246250
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32:
247251
copyTensorData.template operator()<int32_t>();
248252
break;
253+
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64:
254+
copyTensorData.template operator()<int64_t>();
255+
break;
249256
case ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL:
250257
copyTensorData.template operator()<bool>();
251258
break;

control/test/onnx_runtime_test.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ TEST_F(OnnxRuntimeTest, InputTensorNames) {
7979
// Test expected input names from the simple model
8080
EXPECT_TRUE(input_names.contains("float_input"));
8181
EXPECT_TRUE(input_names.contains("int_input"));
82+
EXPECT_TRUE(input_names.contains("int64_input"));
8283
EXPECT_TRUE(input_names.contains("bool_input"));
8384
EXPECT_TRUE(input_names.contains("init_float_input"));
8485
}
@@ -93,6 +94,7 @@ TEST_F(OnnxRuntimeTest, OutputTensorNames) {
9394
// Test expected output names from the simple model
9495
EXPECT_TRUE(output_names.contains("float_output"));
9596
EXPECT_TRUE(output_names.contains("int_output"));
97+
EXPECT_TRUE(output_names.contains("int64_output"));
9698
EXPECT_TRUE(output_names.contains("bool_output"));
9799
}
98100

@@ -196,6 +198,14 @@ TEST_F(OnnxRuntimeTest, SimpleModelWithDifferentTensorTypes) {
196198
int_buffer.value()[1] = 20;
197199
int_buffer.value()[2] = 30;
198200

201+
// Test int64 tensor
202+
auto int64_buffer = runtime.inputBuffer<int64_t>("int64_input");
203+
ASSERT_TRUE(int64_buffer.has_value());
204+
EXPECT_EQ(int64_buffer->size(), 3);
205+
int64_buffer.value()[0] = 100;
206+
int64_buffer.value()[1] = 200;
207+
int64_buffer.value()[2] = 300;
208+
199209
// Test bool tensor
200210
auto bool_buffer = runtime.inputBuffer<bool>("bool_input");
201211
ASSERT_TRUE(bool_buffer.has_value());
@@ -221,6 +231,13 @@ TEST_F(OnnxRuntimeTest, SimpleModelWithDifferentTensorTypes) {
221231
EXPECT_EQ(int_output.value()[1], 21); // 20 + 1
222232
EXPECT_EQ(int_output.value()[2], 31); // 30 + 1
223233

234+
// Check int64 output (should be input + 2)
235+
auto int64_output = runtime.outputBuffer<int64_t>("int64_output");
236+
ASSERT_TRUE(int64_output.has_value());
237+
EXPECT_EQ(int64_output.value()[0], 102); // 100 + 2
238+
EXPECT_EQ(int64_output.value()[1], 202); // 200 + 2
239+
EXPECT_EQ(int64_output.value()[2], 302); // 300 + 2
240+
224241
// Check bool output (should be logical NOT of input)
225242
auto bool_output = runtime.outputBuffer<bool>("bool_output");
226243
ASSERT_TRUE(bool_output.has_value());
@@ -259,6 +276,10 @@ TEST_F(OnnxRuntimeTest, ResetBuffersDifferentTypes) {
259276
ASSERT_TRUE(int_buffer.has_value());
260277
int_buffer.value()[0] = 99;
261278

279+
auto int64_buffer = runtime.inputBuffer<int64_t>("int64_input");
280+
ASSERT_TRUE(int64_buffer.has_value());
281+
int64_buffer.value()[0] = 12345;
282+
262283
auto bool_buffer = runtime.inputBuffer<bool>("bool_input");
263284
ASSERT_TRUE(bool_buffer.has_value());
264285
bool_buffer.value()[0] = true;
@@ -269,6 +290,7 @@ TEST_F(OnnxRuntimeTest, ResetBuffersDifferentTypes) {
269290
// Verify all buffers are reset to their default values
270291
EXPECT_EQ(float_buffer.value()[0], 0.0f);
271292
EXPECT_EQ(int_buffer.value()[0], 0);
293+
EXPECT_EQ(int64_buffer.value()[0], 0);
272294
EXPECT_EQ(bool_buffer.value()[0], false);
273295
}
274296

@@ -340,6 +362,63 @@ TEST_F(OnnxRuntimeTest, CopyOutputToInputIntType) {
340362
EXPECT_EQ(int_input.value()[2], 31);
341363
}
342364

365+
TEST_F(OnnxRuntimeTest, Int64BufferType) {
366+
OnnxRuntime runtime;
367+
ASSERT_TRUE(runtime.initialize(simple_model_path_));
368+
369+
// Test getting input buffer for existing int64 tensor
370+
auto int64_buffer = runtime.inputBuffer<int64_t>("int64_input");
371+
ASSERT_TRUE(int64_buffer.has_value());
372+
EXPECT_EQ(int64_buffer->size(), 3); // Based on simple model shape (1, 3)
373+
374+
// Test getting output buffer for existing int64 tensor
375+
auto int64_output_buffer = runtime.outputBuffer<int64_t>("int64_output");
376+
ASSERT_TRUE(int64_output_buffer.has_value());
377+
EXPECT_EQ(int64_output_buffer->size(), 3);
378+
379+
// Accessing an int64 tensor with the wrong type must fail
380+
auto wrong_type_buffer = runtime.inputBuffer<int32_t>("int64_input");
381+
EXPECT_FALSE(wrong_type_buffer.has_value());
382+
383+
// Accessing an int32 tensor as int64 must also fail
384+
auto wrong_int64_buffer = runtime.inputBuffer<int64_t>("int_input");
385+
EXPECT_FALSE(wrong_int64_buffer.has_value());
386+
}
387+
388+
TEST_F(OnnxRuntimeTest, CopyOutputToInputInt64Type) {
389+
OnnxRuntime runtime;
390+
ASSERT_TRUE(runtime.initialize(simple_model_path_));
391+
392+
// Set input values and run evaluation to get output
393+
auto int64_input = runtime.inputBuffer<int64_t>("int64_input");
394+
ASSERT_TRUE(int64_input.has_value());
395+
int64_input.value()[0] = 100;
396+
int64_input.value()[1] = 200;
397+
int64_input.value()[2] = 300;
398+
399+
ASSERT_TRUE(runtime.evaluate());
400+
401+
// Get the output values (should be input + 2)
402+
auto int64_output = runtime.outputBuffer<int64_t>("int64_output");
403+
ASSERT_TRUE(int64_output.has_value());
404+
EXPECT_EQ(int64_output.value()[0], 102); // 100 + 2
405+
EXPECT_EQ(int64_output.value()[1], 202); // 200 + 2
406+
EXPECT_EQ(int64_output.value()[2], 302); // 300 + 2
407+
408+
// Reset input to different values
409+
int64_input.value()[0] = 0;
410+
int64_input.value()[1] = 0;
411+
int64_input.value()[2] = 0;
412+
413+
// Copy output to input
414+
ASSERT_TRUE(runtime.copyOutputToInput("int64_output", "int64_input"));
415+
416+
// Verify input now contains the output values
417+
EXPECT_EQ(int64_input.value()[0], 102);
418+
EXPECT_EQ(int64_input.value()[1], 202);
419+
EXPECT_EQ(int64_input.value()[2], 302);
420+
}
421+
343422
TEST_F(OnnxRuntimeTest, CopyOutputToInputBoolType) {
344423
OnnxRuntime runtime;
345424
ASSERT_TRUE(runtime.initialize(simple_model_path_));
@@ -398,6 +477,12 @@ TEST_F(OnnxRuntimeTest, CopyOutputToInputTypeMismatch) {
398477
// Test copying int output to float input (should fail due to type mismatch)
399478
EXPECT_FALSE(runtime.copyOutputToInput("float_output", "int_input"));
400479

480+
// Test copying int32 output to int64 input (should fail due to type mismatch)
481+
EXPECT_FALSE(runtime.copyOutputToInput("int_output", "int64_input"));
482+
483+
// Test copying int64 output to int32 input (should fail due to type mismatch)
484+
EXPECT_FALSE(runtime.copyOutputToInput("int64_output", "int_input"));
485+
401486
// Test copying bool output to float input (should fail due to type mismatch)
402487
EXPECT_FALSE(runtime.copyOutputToInput("float_output", "bool_input"));
403488
}

control/test/testdata/test_onnx_generator.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,13 @@ class SimpleTestModel(torch.nn.Module):
387387
def __init__(self):
388388
super().__init__()
389389

390-
def forward(self, float_input, int_input, bool_input, init_float_input):
390+
def forward(self, float_input, int_input, int64_input, bool_input, init_float_input):
391391
float_output = float_input * 2.0 + init_float_input
392392
int_output = int_input + 1 # Simple transformation
393+
int64_output = int64_input + 2 # Simple transformation
393394
bool_output = torch.logical_not(bool_input) # Simple transformation
394395

395-
return float_output, int_output, bool_output
396+
return float_output, int_output, int64_output, bool_output
396397

397398

398399
def export_simple_model(data_dir: str):
@@ -404,17 +405,24 @@ def export_simple_model(data_dir: str):
404405
# Create test inputs with different types
405406
float_input = torch.tensor([[1.5, 2.5, 3.5]], dtype=torch.float32)
406407
int_input = torch.tensor([[10, 20, 30]], dtype=torch.int32)
408+
int64_input = torch.tensor([[100, 200, 300]], dtype=torch.int64)
407409
bool_input = torch.tensor([[True, False, True]], dtype=torch.bool)
408410
# Default values for the overridable initializer baked into the exported model.
409411
default_init_float_input = np.zeros((1, 3), dtype=np.float32)
410412
init_float_input = torch.from_numpy(default_init_float_input)
411413

412414
torch.onnx.export(
413415
simple_model,
414-
(float_input, int_input, bool_input, init_float_input),
416+
(float_input, int_input, int64_input, bool_input, init_float_input),
415417
output_path_simple,
416-
input_names=["float_input", "int_input", "bool_input", "init_float_input"],
417-
output_names=["float_output", "int_output", "bool_output"],
418+
input_names=[
419+
"float_input",
420+
"int_input",
421+
"int64_input",
422+
"bool_input",
423+
"init_float_input",
424+
],
425+
output_names=["float_output", "int_output", "int64_output", "bool_output"],
418426
)
419427

420428
onnx_model = onnx.load(output_path_simple)

0 commit comments

Comments
 (0)