Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions symforce/codegen/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ def __init__(
self.inputs = inputs
self.outputs = outputs

# Inputs must be symbols, not constants or expressions
non_symbolic_inputs = [
key
for key, value in inputs.items_recursive()
if not all(isinstance(e, sf.Symbol) for e in ops.StorageOps.to_storage(value))
]
if non_symbolic_inputs:
raise ValueError(
f"Inputs must be symbolic, found constants or expressions in: {non_symbolic_inputs}"
)

# All symbols in outputs must be present in inputs
input_symbols_list = codegen_util.flat_symbols_from_values(inputs)
input_symbols = set(input_symbols_list)
Expand Down
17 changes: 16 additions & 1 deletion test/symforce_codegen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,14 +925,29 @@ def sparse_func(x: sf.V3) -> sf.M33:
def test_invalid_codegen_raises(self) -> None:
"""
Tests:
Codegen inputs must be symbolic
Codegen outputs must be a function of given inputs
Codegen input/output names must be valid variable names
Codegen namespace must be a valid identifier
Codegen name must be a valid identifier
"""
# Outputs have symbols not present in inputs
x = sf.Symbol("x")
y = sf.Symbol("y")

# Inputs are constants
inputs = Values(a=sf.Rot3.from_storage([1, 2, 3, 4]))
outputs = Values(out=sf.Rot3.from_storage([1, 2, 3, 4]))
self.assertRaises(ValueError, codegen.Codegen, inputs, outputs, codegen.CppConfig(), "test")

# Inputs contain an expression or a constant alongside symbols
inputs = Values(a=x, b=x * y)
outputs = Values(out=x)
self.assertRaises(ValueError, codegen.Codegen, inputs, outputs, codegen.CppConfig(), "test")
inputs = Values(a=sf.V2(x, 2))
outputs = Values(out=x)
self.assertRaises(ValueError, codegen.Codegen, inputs, outputs, codegen.CppConfig(), "test")

# Outputs have symbols not present in inputs
inputs = Values(input=x)
outputs = Values(output=x + y)
self.assertRaises(ValueError, codegen.Codegen, inputs, outputs, codegen.CppConfig(), "test")
Expand Down