@@ -131,9 +131,7 @@ A context used for checking validity of a model.
131131# Fields
132132$(FIELDS)
133133"""
134- struct DebugContext{M<: Model ,C<: AbstractContext } <: AbstractContext
135- " model that is being run"
136- model:: M
134+ struct DebugContext{C<: AbstractContext } <: AbstractContext
137135 " context used for running the model"
138136 context:: C
139137 " mapping from varnames to the number of times they have been seen"
@@ -149,7 +147,6 @@ struct DebugContext{M<:Model,C<:AbstractContext} <: AbstractContext
149147end
150148
151149function DebugContext (
152- model:: Model ,
153150 context:: AbstractContext = DefaultContext ();
154151 varnames_seen= OrderedDict {VarName,Int} (),
155152 statements= Vector {Stmt} (),
@@ -158,7 +155,6 @@ function DebugContext(
158155 record_varinfo= false ,
159156)
160157 return DebugContext (
161- model,
162158 context,
163159 varnames_seen,
164160 statements,
@@ -344,7 +340,7 @@ function check_varnames_seen(varnames_seen::AbstractDict{VarName,Int})
344340end
345341
346342# A check we run on the model before evaluating it.
347- function check_model_pre_evaluation (context :: DebugContext , model:: Model )
343+ function check_model_pre_evaluation (model:: Model )
348344 issuccess = true
349345 # If something is in the model arguments, then it should NOT be in `condition`,
350346 # nor should there be any symbol present in `condition` that has the same symbol.
@@ -361,8 +357,8 @@ function check_model_pre_evaluation(context::DebugContext, model::Model)
361357 return issuccess
362358end
363359
364- function check_model_post_evaluation (context :: DebugContext , model:: Model )
365- return check_varnames_seen (context. varnames_seen)
360+ function check_model_post_evaluation (model:: Model )
361+ return check_varnames_seen (model . context. varnames_seen)
366362end
367363
368364"""
@@ -438,25 +434,23 @@ function check_model_and_trace(
438434 rng:: Random.AbstractRNG ,
439435 model:: Model ;
440436 varinfo= VarInfo (),
441- context= SamplingContext (rng),
442437 error_on_failure= false ,
443438 kwargs... ,
444439)
445440 # Execute the model with the debug context.
446441 debug_context = DebugContext (
447- model, context; error_on_failure= error_on_failure, kwargs...
442+ SamplingContext (rng, model . context) ; error_on_failure= error_on_failure, kwargs...
448443 )
444+ debug_model = DynamicPPL. contextualize (model, debug_context)
449445
450446 # Perform checks before evaluating the model.
451- issuccess = check_model_pre_evaluation (debug_context, model )
447+ issuccess = check_model_pre_evaluation (debug_model )
452448
453449 # Force single-threaded execution.
454- retval, varinfo_result = DynamicPPL. evaluate_threadunsafe!! (
455- model, varinfo, debug_context
456- )
450+ DynamicPPL. evaluate_threadunsafe!! (debug_model, varinfo)
457451
458452 # Perform checks after evaluating the model.
459- issuccess &= check_model_post_evaluation (debug_context, model )
453+ issuccess &= check_model_post_evaluation (debug_model )
460454
461455 if ! issuccess && error_on_failure
462456 error (" model check failed" )
@@ -535,14 +529,13 @@ function has_static_constraints(
535529end
536530
537531"""
538- gen_evaluator_call_with_types(model[, varinfo, context ])
532+ gen_evaluator_call_with_types(model[, varinfo])
539533
540534Generate the evaluator call and the types of the arguments.
541535
542536# Arguments
543537- `model::Model`: The model whose evaluator is of interest.
544538- `varinfo::AbstractVarInfo`: The varinfo to use when evaluating the model. Default: `VarInfo(model)`.
545- - `context::AbstractContext`: The context to use when evaluating the model. Default: [`DefaultContext`](@ref).
546539
547540# Returns
548541A 2-tuple with the following elements:
@@ -551,11 +544,9 @@ A 2-tuple with the following elements:
551544- `argtypes::Type{<:Tuple}`: The types of the arguments for the evaluator.
552545"""
553546function gen_evaluator_call_with_types (
554- model:: Model ,
555- varinfo:: AbstractVarInfo = VarInfo (model),
556- context:: AbstractContext = DefaultContext (),
547+ model:: Model , varinfo:: AbstractVarInfo = VarInfo (model)
557548)
558- args, kwargs = DynamicPPL. make_evaluate_args_and_kwargs (model, varinfo, context )
549+ args, kwargs = DynamicPPL. make_evaluate_args_and_kwargs (model, varinfo)
559550 return if isempty (kwargs)
560551 (model. f, Base. typesof (args... ))
561552 else
@@ -564,7 +555,7 @@ function gen_evaluator_call_with_types(
564555end
565556
566557"""
567- model_warntype(model[, varinfo, context ]; optimize=true)
558+ model_warntype(model[, varinfo]; optimize=true)
568559
569560Check the type stability of the model's evaluator, warning about any potential issues.
570561
@@ -573,23 +564,19 @@ This simply calls `@code_warntype` on the model's evaluator, filling in internal
573564# Arguments
574565- `model::Model`: The model to check.
575566- `varinfo::AbstractVarInfo`: The varinfo to use when evaluating the model. Default: `VarInfo(model)`.
576- - `context::AbstractContext`: The context to use when evaluating the model. Default: [`DefaultContext`](@ref).
577567
578568# Keyword Arguments
579569- `optimize::Bool`: Whether to generate optimized code. Default: `false`.
580570"""
581571function model_warntype (
582- model:: Model ,
583- varinfo:: AbstractVarInfo = VarInfo (model),
584- context:: AbstractContext = DefaultContext ();
585- optimize:: Bool = false ,
572+ model:: Model , varinfo:: AbstractVarInfo = VarInfo (model), optimize:: Bool = false
586573)
587- ftype, argtypes = gen_evaluator_call_with_types (model, varinfo, context )
574+ ftype, argtypes = gen_evaluator_call_with_types (model, varinfo)
588575 return InteractiveUtils. code_warntype (ftype, argtypes; optimize= optimize)
589576end
590577
591578"""
592- model_typed(model[, varinfo, context ]; optimize=true)
579+ model_typed(model[, varinfo]; optimize=true)
593580
594581Return the type inference for the model's evaluator.
595582
@@ -598,18 +585,14 @@ This simply calls `@code_typed` on the model's evaluator, filling in internal ar
598585# Arguments
599586- `model::Model`: The model to check.
600587- `varinfo::AbstractVarInfo`: The varinfo to use when evaluating the model. Default: `VarInfo(model)`.
601- - `context::AbstractContext`: The context to use when evaluating the model. Default: [`DefaultContext`](@ref).
602588
603589# Keyword Arguments
604590- `optimize::Bool`: Whether to generate optimized code. Default: `true`.
605591"""
606592function model_typed (
607- model:: Model ,
608- varinfo:: AbstractVarInfo = VarInfo (model),
609- context:: AbstractContext = DefaultContext ();
610- optimize:: Bool = true ,
593+ model:: Model , varinfo:: AbstractVarInfo = VarInfo (model), optimize:: Bool = true
611594)
612- ftype, argtypes = gen_evaluator_call_with_types (model, varinfo, context )
595+ ftype, argtypes = gen_evaluator_call_with_types (model, varinfo)
613596 return only (InteractiveUtils. code_typed (ftype, argtypes; optimize= optimize))
614597end
615598
0 commit comments