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

Introducing addons for rules (+scale factors, +switch node, +mixture model) #215

Merged
merged 95 commits into from
Dec 19, 2022

Conversation

bartvanerp
Copy link
Member

For solving #172

src/rule.jl Outdated
whereargs
) do
return quote
$(on_index_init)
$(m_init_block...)
$(q_init_block...)
$(body)
_addons = ()
_message = $(MacroHelpers.remove_returns(body))
Copy link
Member

Choose a reason for hiding this comment

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

Be careful with removing return statements from the whole message function body, as this may break things if a rule contains something like (even without addons)

@rule MyNode(...) (args...) = begin
    if condition
        return 1
    end
    return 2
end

Copy link
Member Author

Choose a reason for hiding this comment

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

Resolved by 68809b4. It counts the number of return statements per macro by performing some string matching. Although it is better to be replaced with a proper walk, but I didn't manage to get this working.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that is better, I wonder if its really necessary to use remove_returns for the body of the @rule macro, but rather only on the body of the @logscale macro. I would probably move the remove_returns statement from rule.jl:307 to rule.jl:417 inside macro logscale ... end.

Suggested change
_message = $(MacroHelpers.remove_returns(body))
rule:jl:307 _message = body
rule.jl:417 return MacroHelpers.remove_returns(output)

I'm afraid that we did not really impose this restriction in the @rule macro in the past and it might be breaking. @logscale macro, however, is new and it may impose new restrictions on its body (such as no or only 1 return statement).

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@bartvanerp bartvanerp self-assigned this Sep 29, 2022
@bartvanerp bartvanerp linked an issue Sep 29, 2022 that may be closed by this pull request
Copy link
Member

@bvdmitri bvdmitri left a comment

Choose a reason for hiding this comment

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

@bartvanerp Great work!

src/marginal.jl Outdated

function Base.:(==)(left::Marginal, right::Marginal)
# We need this dummy method as Julia is not smart enough to
# do that automatically if `data` is mutable
return left.is_clamped == right.is_clamped &&
left.is_initial == right.is_initial &&
left.data == right.data
left.data == right.data &&
left.addons == right.addons
Copy link
Member

Choose a reason for hiding this comment

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

We will need not to forget to add a == method for scale addon as well.

vtag :: T
vconstraint :: C
msgs_names :: N
marginals_names :: M
meta :: A
addons :: X
Copy link
Member

Choose a reason for hiding this comment

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

Here (as I understand) are technically enabled_addons, but this is too long to type, not sure what would be a better field name

src/model.jl Outdated
pipeline :: P
addons :: A
Copy link
Member

Choose a reason for hiding this comment

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

Here is enabled_addons as well, but again I'm not sure if this is the better name

src/rule.jl Outdated
@@ -179,7 +180,7 @@ function call_rule_macro_parse_fn_args(inputs; specname, prefix, proxy)
output.args = map(v -> apply_proxy(v, proxy), any.args)
return output
end
return :($(proxy)($any, false, false))
return :($(proxy)($any, false, false, ()))
Copy link
Member

Choose a reason for hiding this comment

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

That is an interesting one, we might think about how we actually are going to test rules with addons. As I understand this line of code technically does prevent you from testing such rules (for now).

src/rule.jl Outdated
error("Error in macro. Lambda body arguments specification is incorrect")

# check for number of return statements
@assert MacroHelpers.count_returns(body) < 2 "@rule macro contains multiple return statements"
Copy link
Member

Choose a reason for hiding this comment

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

I think I mentioned it somewhere else, but I think it would be better to move "remove return statements" routine inside the logscale macro such that the rule macro itself is unaffected.

src/rule.jl Outdated

possible_fix_definition = """
@rule $(spec_fform)(:$spec_on, $spec_vconstraint) ($arguments_spec, $meta_spec) = begin
@rule $(spec_fform)(:$spec_on, $spec_vconstraint) ($arguments_spec, $meta_spec, $addons_spec) = begin
Copy link
Member

Choose a reason for hiding this comment

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

Lets talk about it, I'm not sure rule addons should be a part of the rule specification, they might add some extra functionality, but do we need different set of rules for different set of addons?

@@ -5,7 +5,7 @@
q_b::NTuple{N, GammaDistributionsFamily}
) where {N} = begin
U = map(zip(q_a, q_b)) do (a, b)
return -score(
Copy link
Member

Choose a reason for hiding this comment

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

That is exactly why we should move the "remove return statements" out of the rule macro ;)

@@ -37,7 +37,7 @@ end
"""
function constvar end

constvar(name::Symbol, constval, collection_type::AbstractVariableCollectionType = VariableIndividual()) = ConstVariable(name, collection_type, constval, of(Message(constval, true, false)), 0)
constvar(name::Symbol, constval, collection_type::AbstractVariableCollectionType = VariableIndividual()) = ConstVariable(name, collection_type, constval, of(Message(constval, true, false, nothing)), 0)
Copy link
Member

Choose a reason for hiding this comment

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

Hm, I noticed that in other places you used an empty tuple to mock an empty list of addons, but here nothing has been used. We should probably think what is better (I'm ok with both as long as it is the same everywhere).

@bvdmitri bvdmitri merged commit e83dd33 into master Dec 19, 2022
@bvdmitri bvdmitri deleted the rule_addons branch December 19, 2022 09:36
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.

Rule add-ons (such as scale factors) Implement Mixture Model distribution
4 participants