Skip to content

feat: compile non-property decls to loss - #1132

Open
ggustavs wants to merge 5 commits into
devfrom
feat/loss-outputs
Open

feat: compile non-property decls to loss#1132
ggustavs wants to merge 5 commits into
devfrom
feat/loss-outputs

Conversation

@ggustavs

Copy link
Copy Markdown
Collaborator

Add ability to compile non-property declarations to callable functions translated with the chosen DL. This is done by passing the name of the declaration via the --declaration flag, same as for properties.

@MatthewDaggitt MatthewDaggitt changed the title feat: compile non-property delcs to loss feat: compile non-property decls to loss May 16, 2026

@MatthewDaggitt MatthewDaggitt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the right idea, thank you! Got a few minor suggestions for changes.

Comment thread ChangeLog.md Outdated

### Loss backend

* Declaration flag can now be used to specify non-property declarations that will also be compiled with the chosed DL to a callable function.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The --declaration flag.
  • "chosed" -> "chosen"
  • "to a callable function"? Is this true if the declaration is x : Int; x = 2? That shouldn't be compiled to a callable right?

Comment thread vehicle/src/Vehicle/Backend/Loss.hs Outdated
| otherwise -> return Nothing
DefFunction p ident ann typ expr
| isPropertyDecl decl -> Just <$> convertPropertyDecl p ident ann typ expr
| isPropertyDecl decl || Set.member (nameOf ident) requestedDecls ->

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm should we be changing the semantics so that it only compiles the requested declarations? This seems a little odd to compile both, even if the user has only requested specific, non-property declarations?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking more about the context of needing more from a spec, not just the properties, but your approach makes more sense in general

Comment thread vehicle/src/Vehicle/Compile.hs Outdated
compileToLossFunction LossOptions {..} typedProg outputAsJSON =
logCompilerPass Loss $ do
lossTensorProg <- convertToLossTensors differentiableLogicID typedProg
lossTensorProg <- convertToLossTensors differentiableLogicID (Set.fromList declarationsToCompile) typedProg

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put the declaration set calculation on a separate line?

@@ -0,0 +1,2 @@
Error in file 'spec.vcl' at Line 4, Columns 1-9: compiling declaration 'someList' as a loss output (supported leaf types: `Bool`, `Real`, `Tensor Bool _`, `Tensor Real _`, `Vector _ _`) is not currently implemented

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This limitation feels a bit artificial. It should be possible to get it working for any declaration...

Comment thread vehicle/src/Vehicle/Backend/Loss.hs Outdated
convertOutputDecl p ident ann typ value = do
leafType <- stripPiTypes typ
case toTypeValue leafType of
VBoolTensorType {} -> emit leafType

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is completely baffling. Why are we doing the same thing for every type value? We should be doing something different for each one...

Comment thread vehicle/src/Vehicle/Backend/Loss.hs Outdated
return $ DefFunction p ident ann lossType lossExpr

stripPiTypes :: (MonadLogic m) => VType Builtin -> m (VType Builtin)
stripPiTypes typ = case toTypeValue typ of

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right either. Stripping all Pi types off means we can't convert functions... Is this trying to get rid of the implicit functions? If so I don't think they should exist at this point because the whole spec has been monomorphised?

Is there a concrete test case that we need to do this for?

Comment thread vehicle/src/Vehicle/Backend/Loss.hs Outdated
VVectorType tElem _d -> convertVectorProperty tElem
_ -> unexpectedExprError currentPass "Impossible property type"
convertMultiOutput :: (MonadLogic m) => VType Builtin -> Value Builtin -> m (Value LossBuiltin)
convertMultiOutput typ = case toTypeValue typ of

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this simply convertTypedValue?

Comment thread vehicle/src/Vehicle/Backend/Loss.hs Outdated

convertVectorProperty :: (MonadLogic m) => VType Builtin -> Value Builtin -> m (Value LossBuiltin)
convertVectorProperty typ value = do
convertVectorOutput :: (MonadLogic m) => VType Builtin -> Value Builtin -> m (Value LossBuiltin)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this simply convertVectorValue? (and similar renamings for the other functions).

ggustavs added 2 commits May 27, 2026 14:20
# Conflicts:
#	ChangeLog.md
#	vehicle/src/Vehicle/Backend/Loss.hs
#	vehicle/src/Vehicle/Backend/Loss/LossCompilation.hs
#	vehicle/src/Vehicle/Compile.hs
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

Successfully merging this pull request may close these issues.

2 participants