Skip to content

Fixes #4385 about calling the Create methods when loading models from disk #4485

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

Merged
merged 16 commits into from
Nov 27, 2019

Conversation

antoniovs1029
Copy link
Member

Fixes #4385

As concluded in the discussion there, if a class has both a constructor and a create method that matches the parameter types that the ComponentCatalog is looking for, then it should use the one which is public. If both are non-public, then it should use the internal one. If both have the same visibility then it should throw an exception.

For this to happen, I modified the ComponentCatalog to work as described. I also had to change the visibility of several methods and constructors to work as described. Since, as per @yaeldekel 's instructions, the create method should be called instead of the constructor in all cases, then I did the following:

  1. For most classes, the case was that both the conflictive constructor and create method were private, so I simply made the create method internal.
  2. For LabelIndicatorTransform and SkipTakeFilter the conflictive constructors and create methods were public, so I made the constructors internal.
  3. For the following classes the constructor was internal, and the create method was private. In these cases I made the constructor private, and the create methods internal.
Microsoft.ML.Trainers.FieldAwareFactorizationMachinePredictionTransformer
Microsoft.ML.Transforms.TimeSeries.IidChangePointDetector
Microsoft.ML.Transforms.TimeSeries.IidSpikeDetector
Microsoft.ML.Transforms.TimeSeries.SrCnnAnomalyDetector
Microsoft.ML.Transforms.TimeSeries.SsaChangePointDetector
Microsoft.ML.Transforms.TimeSeries.SsaForecastingTransformer
Microsoft.ML.Transforms.TimeSeries.SsaSpikeDetector

@antoniovs1029 antoniovs1029 requested a review from a team as a code owner November 18, 2019 22:18
// Choose the one that is public, if both are non-public choose the one that isn't private
// if they have the same visibility, then throw an exception, since this shouldn't happen.
if(ctor != null && create != null)
{
Copy link
Member Author

@antoniovs1029 antoniovs1029 Nov 18, 2019

Choose a reason for hiding this comment

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

I don't really like this nested if's, I am not sure if they're legible enough, and it is ambiguous what should happen if there's a 'protected' create or constructor method (which, I believe, never happens in the codebase...). Still, this gets the job done.

I can think of a couple of ways of making this, but not sure if they would be more legible. Please, let me know if I should rewrite this in another way. #Resolved

Copy link
Member Author

@antoniovs1029 antoniovs1029 Nov 19, 2019

Choose a reason for hiding this comment

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

So I've changed this nested if's in a new iteration (see this comment) but I am not sure if I prefer the nested of's or the new solution. #Resolved

{
if(ctor.IsPublic || ctor.IsPrivate == create.IsPrivate)
{
throw Contracts.Except($"Can't load type {instType}, because it has both create and constructor methods with the same visibility. Please open an issue for this to be fixed.");
Copy link
Member Author

@antoniovs1029 antoniovs1029 Nov 18, 2019

Choose a reason for hiding this comment

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

I didn't know what text to put in the Exception. Please let me know if I should change it. #Resolved

Choose a reason for hiding this comment

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

I would change "please open an issue..." to something like: "Please indicate which one should be used by changing either the signature or the visibility of one of them".


In reply to: 347643643 [](ancestors = 347643643)

@codecov
Copy link

codecov bot commented Nov 18, 2019

Codecov Report

Merging #4485 into master will increase coverage by 0.16%.
The diff coverage is 49.25%.

@@            Coverage Diff             @@
##           master    #4485      +/-   ##
==========================================
+ Coverage    74.9%   75.07%   +0.16%     
==========================================
  Files         908      908              
  Lines      160072   160126      +54     
  Branches    17222    17240      +18     
==========================================
+ Hits       119903   120210     +307     
+ Misses      35359    35094     -265     
- Partials     4810     4822      +12
Flag Coverage Δ
#Debug 75.07% <49.25%> (+0.16%) ⬆️
#production 70.46% <49.25%> (+0.21%) ⬆️
#test 90.28% <ø> (ø) ⬆️
Impacted Files Coverage Δ
.../Microsoft.ML.TimeSeries/IidChangePointDetector.cs 75% <ø> (+5.17%) ⬆️
...oft.ML.Ensemble/Trainer/EnsembleModelParameters.cs 90.81% <ø> (+6.12%) ⬆️
.../Microsoft.ML.TimeSeries/SsaChangePointDetector.cs 82.17% <ø> (+4.65%) ⬆️
src/Microsoft.ML.TimeSeries/SsaSpikeDetector.cs 88.03% <ø> (+5.12%) ⬆️
src/Microsoft.ML.TimeSeries/IidSpikeDetector.cs 78.43% <ø> (+5.88%) ⬆️
src/Microsoft.ML.FastTree/FastTreeRanking.cs 48.68% <ø> (+0.49%) ⬆️
src/Microsoft.ML.PCA/PcaTrainer.cs 82% <ø> (+1.76%) ⬆️
src/Microsoft.ML.FastTree/GamClassification.cs 97% <ø> (+8%) ⬆️
src/Microsoft.ML.FastTree/FastTreeTweedie.cs 60.06% <ø> (+2.04%) ⬆️
...er/Multiclass/EnsembleMulticlassModelParameters.cs 89.9% <ø> (+5.5%) ⬆️
... and 76 more

// If they have the same visibility, then throw an exception, since this shouldn't happen.

if (ctor.Accessmodifier() == create.Accessmodifier())
{
Copy link
Member Author

@antoniovs1029 antoniovs1029 Nov 19, 2019

Choose a reason for hiding this comment

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

So I changed the nested if's I had (link to comment) for this other solution using the Accessmodifier() extension method (as suggested by @yaeldekel ). Although I think this one is more legible, I wouldn't be sure if it's worth it to create the extension method only for this.... So let me know your opinions, Thanks! #Resolved

Choose a reason for hiding this comment

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

To me this seems cleaner.

A couple of more ways you can decrease the amount of "if/else"s in the code:

  1. you can put the assignment inside the if condition, like this:
if ((ctor = loaderType.GetConstructor(...)) == null)
  1. If you throw or return inside the "if", then you don't need the "else":
if (ctor.Accessmodifier() == create.Accessmodifier())
    throw ...
if (ctor.Accessmodifier() > create.Accessmodifier())
{
    ...
    return true
}
if (ctor.Accessmodifier() < create.Accessmodifier())
...
etc.

In reply to: 347688260 [](ancestors = 347688260)

Copy link

@yaeldekel yaeldekel left a comment

Choose a reason for hiding this comment

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

:shipit:

@ghost ghost locked as resolved and limited conversation to collaborators Mar 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create methods not being called when loading models from disk
2 participants