Skip to content

Fix marshalling of bool flags in MF #3210

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 3 commits into from
Apr 5, 2019
Merged
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
16 changes: 8 additions & 8 deletions src/Microsoft.ML.Recommender/SafeTrainingAndModelBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,19 @@ private struct MFParameter
/// Specify if the factor matrices should be non-negative.
/// </summary>
[FieldOffset(48)]
public int DoNmf;
public byte DoNmf;
Copy link
Member

@eerhardt eerhardt Apr 5, 2019

Choose a reason for hiding this comment

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

While this is better than what we had before, this is still not correct.

https://docs.microsoft.com/en-us/cpp/cpp/fundamental-types-cpp?view=vs-2019

bool Type bool is an integral type that can have one of the two values true or false. Its size is unspecified.

https://en.cppreference.com/w/cpp/language/types

bool - type, capable of holding one of the two values: true or false. The value of sizeof(bool) is implementation defined and might differ from 1.

Instead, to make interop/marshalling work correctly on all computers - the technique we use in .NET is to ensure the ABI (application binary interface) is stable no matter the compiler/processor/etc. This means only stable-sized types can be used in the ABI that C# code P/Invokes into.

So to fix this, the libmf C code should change to have a stable ABI. I see it uses float, int, and bool. For sure int and bool can change sizes depending on the compiler/processor. Instead, we should be using int32_t and int8_t on the C side. (Note: you can just have a wrapper export function in the C library that exposes the stable ABI and calls into your current code. So the main C/C++ code can remain unchanged.) #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for pointing this out. Another iteration is pushed. Hopefully, it can address this comment.


/// <summary>
/// Set to true so that LIBMF may produce less information to STDOUT.
/// </summary>
[FieldOffset(52)]
public int Quiet;
[FieldOffset(49)]
public byte Quiet;

/// <summary>
/// Set to false so that LIBMF may reuse and modifiy the data passed in.
/// </summary>
[FieldOffset(56)]
public int CopyData;
[FieldOffset(50)]
public byte CopyData;
}

[StructLayout(LayoutKind.Explicit)]
Expand Down Expand Up @@ -223,9 +223,9 @@ public SafeTrainingAndModelBuffer(IHostEnvironment env, int fun, int k, int nrTh
_mfParam.Eta = (float)eta;
_mfParam.Alpha = (float)alpha;
_mfParam.C = (float)c;
_mfParam.DoNmf = doNmf ? 1 : 0;
_mfParam.Quiet = quiet ? 1 : 0;
_mfParam.CopyData = copyData ? 1 : 0;
_mfParam.DoNmf = doNmf ? (byte)1 : (byte)0;
_mfParam.Quiet = quiet ? (byte)1 : (byte)0;
_mfParam.CopyData = copyData ? (byte)1 : (byte)0;
}

~SafeTrainingAndModelBuffer()
Expand Down
37 changes: 30 additions & 7 deletions src/Native/MatrixFactorizationNative/UnmanagedMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,48 @@

using namespace mf;

mf_parameter make_param(const mf_parameter_bridge *param_bridge)
{
mf_parameter param;
param.fun = param_bridge->fun;
param.k = param_bridge->k;
param.nr_threads = param_bridge->nr_threads;
param.nr_bins = param_bridge->nr_bins;
param.nr_iters = param_bridge->nr_iters;
param.lambda_p1 = param_bridge->lambda_p1;
param.lambda_p2 = param_bridge->lambda_p2;
param.lambda_q1 = param_bridge->lambda_q1;
param.lambda_q2 = param_bridge->lambda_q2;
param.eta = param_bridge->eta;
param.alpha = param_bridge->alpha;
param.c = param_bridge->c;
param.do_nmf = param_bridge->do_nmf != 0 ? true : false;
param.quiet = param_bridge->quiet != 0 ? true : false;
param.copy_data = param_bridge->copy_data != 0 ? true : false;
return param;
}

EXPORT_API(void) MFDestroyModel(mf_model *&model)
{
return mf_destroy_model(&model);
}

EXPORT_API(mf_model*) MFTrain(const mf_problem *prob, const mf_parameter *param)
EXPORT_API(mf_model*) MFTrain(const mf_problem *prob, const mf_parameter_bridge *param_bridge)
{
return mf_train(prob, *param);
auto param = make_param(param_bridge);
return mf_train(prob, param);
}

EXPORT_API(mf_model*) MFTrainWithValidation(const mf_problem *tr, const mf_problem *va, const mf_parameter *param)
EXPORT_API(mf_model*) MFTrainWithValidation(const mf_problem *tr, const mf_problem *va, const mf_parameter_bridge *param_bridge)
{
return mf_train_with_validation(tr, va, *param);
auto param = make_param(param_bridge);
return mf_train_with_validation(tr, va, param);
}


EXPORT_API(float) MFCrossValidation(const mf_problem *prob, int nr_folds, const mf_parameter *param)
EXPORT_API(float) MFCrossValidation(const mf_problem *prob, int nr_folds, const mf_parameter_bridge *param_bridge)
{
return mf_cross_validation(prob, nr_folds, *param);
auto param = make_param(param_bridge);
return mf_cross_validation(prob, nr_folds, param);
}

EXPORT_API(float) MFPredict(const mf_model *model, int p_idx, int q_idx)
Expand Down
25 changes: 22 additions & 3 deletions src/Native/MatrixFactorizationNative/UnmanagedMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,31 @@

using namespace mf;

struct mf_parameter_bridge
{
int32_t fun;
int32_t k;
int32_t nr_threads;
int32_t nr_bins;
int32_t nr_iters;
float lambda_p1;
float lambda_p2;
float lambda_q1;
float lambda_q2;
float eta;
float alpha;
float c;
uint8_t do_nmf;
uint8_t quiet;
uint8_t copy_data;
};

EXPORT_API(void) MFDestroyModel(mf_model *&model);

EXPORT_API(mf_model*) MFTrain(const mf_problem *prob, const mf_parameter *param);
EXPORT_API(mf_model*) MFTrain(const mf_problem *prob, const mf_parameter_bridge *parameter_bridge);

EXPORT_API(mf_model*) MFTrainWithValidation(const mf_problem *tr, const mf_problem *va, const mf_parameter *param);
EXPORT_API(mf_model*) MFTrainWithValidation(const mf_problem *tr, const mf_problem *va, const mf_parameter_bridge *parameter_bridge);

EXPORT_API(float) MFCrossValidation(const mf_problem *prob, int nr_folds, const mf_parameter* param);
EXPORT_API(float) MFCrossValidation(const mf_problem *prob, int nr_folds, const mf_parameter_bridge* parameter_bridge);

EXPORT_API(float) MFPredict(const mf_model *model, int p_idx, int q_idx);
2 changes: 1 addition & 1 deletion src/Native/MatrixFactorizationNative/libmf
Submodule libmf updated 1 files
+5 −0 mf.cpp