Skip to content

Polish marshalling of MF model and MF problem and enable 32-bit tests #3227

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 8, 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
72 changes: 39 additions & 33 deletions src/Microsoft.ML.Recommender/SafeTrainingAndModelBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,50 @@ namespace Microsoft.ML.Recommender.Internal
/// </summary>
internal sealed class SafeTrainingAndModelBuffer : IDisposable
{
[StructLayout(LayoutKind.Explicit)]
[StructLayout(LayoutKind.Sequential)]
private struct MFNode
{
[FieldOffset(0)]
/// <summary>
/// Row index.
/// </summary>
public int U;
[FieldOffset(4)]

/// <summary>
/// Column index;
/// </summary>
public int V;
[FieldOffset(8)]

/// <summary>
/// Matrix element's value at <see cref="U"/>-th row and <see cref="V"/>-th column.
/// </summary>
public float R;
}

[StructLayout(LayoutKind.Explicit)]
[StructLayout(LayoutKind.Sequential)]
private unsafe struct MFProblem
{
[FieldOffset(0)]
/// <summary>
/// Number of rows.
/// </summary>
public int M;
[FieldOffset(4)]

/// <summary>
/// Number of columns.
/// </summary>
public int N;
[FieldOffset(8)]

/// <summary>
/// Number of specified matrix elements in <see cref="R"/>.
/// </summary>
public long Nnz;
[FieldOffset(16)]

/// <summary>
/// Specified matrix elements.
/// </summary>
public MFNode* R;
}

[StructLayout(LayoutKind.Explicit)]
[StructLayout(LayoutKind.Sequential)]
private struct MFParameter
{
/// <summary>
Expand All @@ -58,130 +77,117 @@ private struct MFParameter
/// Fun 12 is solved by a coordinate descent method while other functions invoke
/// a stochastic gradient method.
/// </summary>
[FieldOffset(0)]
public int Fun;

/// <summary>
/// Rank of factor matrices.
/// </summary>
[FieldOffset(4)]
public int K;

/// <summary>
/// Number of threads which can be used for training.
/// </summary>
[FieldOffset(8)]
public int NrThreads;

/// <summary>
/// Number of blocks that the training matrix is divided into. The parallel stochastic gradient
/// method in LIBMF processes assigns each thread a block at one time. The ratings in one block
/// would be sequentially accessed (not randomaly accessed like standard stochastic gradient methods).
/// </summary>
[FieldOffset(12)]
public int NrBins;

/// <summary>
/// Number of training iteration. At one iteration, all values in the training matrix are roughly accessed once.
/// </summary>
[FieldOffset(16)]
public int NrIters;

/// <summary>
/// L1-norm regularization coefficient of left factor matrix.
/// </summary>
[FieldOffset(20)]
public float LambdaP1;

/// <summary>
/// L2-norm regularization coefficient of left factor matrix.
/// </summary>
[FieldOffset(24)]
public float LambdaP2;

/// <summary>
/// L1-norm regularization coefficient of right factor matrix.
/// </summary>
[FieldOffset(28)]
public float LambdaQ1;

/// <summary>
/// L2-norm regularization coefficient of right factor matrix.
/// </summary>
[FieldOffset(32)]
public float LambdaQ2;

/// <summary>
/// Learning rate of LIBMF's stochastic gradient method.
/// </summary>
[FieldOffset(36)]
public float Eta;

/// <summary>
/// Coefficient of loss function on unobserved entries in the training matrix. It's used only with fun=12.
/// </summary>
[FieldOffset(40)]
public float Alpha;

/// <summary>
/// Desired value of unobserved entries in the training matrix. It's used only with fun=12.
/// </summary>
[FieldOffset(44)]
public float C;

/// <summary>
/// Specify if the factor matrices should be non-negative.
/// </summary>
[FieldOffset(48)]
public byte DoNmf;

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

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

[StructLayout(LayoutKind.Explicit)]
[StructLayout(LayoutKind.Sequential)]
private unsafe struct MFModel
{
[FieldOffset(0)]
/// <summary>
/// See <see cref="MFParameter.Fun"/>.
/// </summary>
public int Fun;

/// <summary>
/// Number of rows in the training matrix.
/// </summary>
[FieldOffset(4)]
public int M;

/// <summary>
/// Number of columns in the training matrix.
/// </summary>
[FieldOffset(8)]
public int N;

/// <summary>
/// Rank of factor matrices.
/// </summary>
[FieldOffset(12)]
public int K;

/// <summary>
/// Average value in the training matrix.
/// </summary>
[FieldOffset(16)]
public float B;

/// <summary>
/// Left factor matrix. Its shape is M-by-K stored in row-major format.
/// </summary>
[FieldOffset(24)] // pointer is 8-byte on 64-bit machine.
public float* P;

/// <summary>
/// Right factor matrix. Its shape is N-by-K stored in row-major format.
/// </summary>
[FieldOffset(32)] // pointer is 8-byte on 64-bit machine.
public float* Q;
}

Expand Down
95 changes: 81 additions & 14 deletions src/Native/MatrixFactorizationNative/UnmanagedMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

using namespace mf;

mf_parameter make_param(const mf_parameter_bridge *param_bridge)
inline mf_parameter TranslateToParam(const mf_parameter_bridge *param_bridge)
{
mf_parameter param;
param.fun = param_bridge->fun;
Expand All @@ -30,30 +30,97 @@ mf_parameter make_param(const mf_parameter_bridge *param_bridge)
return param;
}

EXPORT_API(void) MFDestroyModel(mf_model *&model)
inline mf_problem TranslateToProblem(const mf_problem_bridge *prob_bridge)
{
return mf_destroy_model(&model);
mf_problem prob;
prob.m = prob_bridge->m;
prob.n = prob_bridge->n;
prob.nnz = prob_bridge->nnz;
prob.R = prob_bridge->R;
return prob;
}

EXPORT_API(mf_model*) MFTrain(const mf_problem *prob, const mf_parameter_bridge *param_bridge)
inline void TranslateToModelBridge(const mf_model *model, mf_model_bridge *model_bridge)
{
auto param = make_param(param_bridge);
return mf_train(prob, param);
model_bridge->fun = model->fun;
model_bridge->m = model->m;
model_bridge->n = model->n;
model_bridge->k = model->k;
model_bridge->b = model->b;
model_bridge->P = model->P;
model_bridge->Q = model->Q;
}

EXPORT_API(mf_model*) MFTrainWithValidation(const mf_problem *tr, const mf_problem *va, const mf_parameter_bridge *param_bridge)
inline void TranslateToModel(const mf_model_bridge *model_bridge, mf_model *model)
{
auto param = make_param(param_bridge);
return mf_train_with_validation(tr, va, param);
model->fun = model_bridge->fun;
model->m = model_bridge->m;
model->n = model_bridge->n;
model->k = model_bridge->k;
model->b = model_bridge->b;
model->P = model_bridge->P;
model->Q = model_bridge->Q;
}

EXPORT_API(float) MFCrossValidation(const mf_problem *prob, int nr_folds, const mf_parameter_bridge *param_bridge)
EXPORT_API(void) MFDestroyModel(mf_model_bridge *&model_bridge)
{
auto param = make_param(param_bridge);
return mf_cross_validation(prob, nr_folds, param);
// Transfer the ownership of P and Q back to the original LIBMF class, so that
// mf_destroy_model can be called.
auto model = new mf_model;
model->P = model_bridge->P;
model->Q = model_bridge->Q;
mf_destroy_model(&model); // delete model, model->P, amd model->Q.

// Delete bridge class allocated in MFTrain, MFTrainWithValidation, or MFCrossValidation.
delete model_bridge;
model_bridge = nullptr;
}

EXPORT_API(mf_model_bridge*) MFTrain(const mf_problem_bridge *prob_bridge, const mf_parameter_bridge *param_bridge)
{
// Convert objects created outside LIBMF. Notice that the called LIBMF function doesn't take the ownership of
// allocated memory in those external objects.
auto prob = TranslateToProblem(prob_bridge);
auto param = TranslateToParam(param_bridge);

// The model contains 3 allocated things --- itself, P, and Q.
// We will delete itself and transfer the ownership of P and Q to the associated bridge class. The bridge class
// will then be sent to C#.
auto model = mf_train(&prob, param);
auto model_bridge = new mf_model_bridge;
TranslateToModelBridge(model, model_bridge);
delete model;
return model_bridge; // To clean memory up, we need to delete model_bridge, model_bridge->P, and model_bridge->Q.
}

EXPORT_API(mf_model_bridge*) MFTrainWithValidation(const mf_problem_bridge *tr_bridge, const mf_problem_bridge *va_bridge, const mf_parameter_bridge *param_bridge)
{
// Convert objects created outside LIBMF. Notice that the called LIBMF function doesn't take the ownership of
// allocated memory in those external objects.
auto tr = TranslateToProblem(tr_bridge);
auto va = TranslateToProblem(va_bridge);
auto param = TranslateToParam(param_bridge);

// The model contains 3 allocated things --- itself, P, and Q.
// We will delete itself and transfer the ownership of P and Q to the associated bridge class. The bridge class
// will then be sent to C#.
auto model = mf_train_with_validation(&tr, &va, param);
auto model_bridge = new mf_model_bridge;
TranslateToModelBridge(model, model_bridge);
delete model;
return model_bridge; // To clean memory up, we need to delete model_bridge, model_bridge->P, and model_bridge->Q.
}

EXPORT_API(float) MFCrossValidation(const mf_problem_bridge *prob_bridge, int32_t nr_folds, const mf_parameter_bridge *param_bridge)
{
auto param = TranslateToParam(param_bridge);
auto prob = TranslateToProblem(prob_bridge);
return mf_cross_validation(&prob, nr_folds, param);
}

EXPORT_API(float) MFPredict(const mf_model *model, int p_idx, int q_idx)
EXPORT_API(float) MFPredict(const mf_model_bridge *model_bridge, int32_t p_idx, int32_t q_idx)
{
return mf_predict(model, p_idx, q_idx);
mf_model model;
TranslateToModel(model_bridge, &model);
return mf_predict(&model, p_idx, q_idx);
}
29 changes: 24 additions & 5 deletions src/Native/MatrixFactorizationNative/UnmanagedMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,31 @@ struct mf_parameter_bridge
uint8_t copy_data;
};

EXPORT_API(void) MFDestroyModel(mf_model *&model);
struct mf_problem_bridge
{
int32_t m;
int32_t n;
int64_t nnz;
struct mf_node *R;
};

struct mf_model_bridge
{
int32_t fun;
int32_t m;
int32_t n;
int32_t k;
float b;
float *P;
float *Q;
};

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

EXPORT_API(mf_model*) MFTrain(const mf_problem *prob, const mf_parameter_bridge *parameter_bridge);
EXPORT_API(mf_model_bridge*) MFTrain(const mf_problem_bridge *prob_bridge, const mf_parameter_bridge *parameter_bridge);

EXPORT_API(mf_model*) MFTrainWithValidation(const mf_problem *tr, const mf_problem *va, const mf_parameter_bridge *parameter_bridge);
EXPORT_API(mf_model_bridge*) MFTrainWithValidation(const mf_problem_bridge *tr, const mf_problem_bridge *va, const mf_parameter_bridge *parameter_bridge);

EXPORT_API(float) MFCrossValidation(const mf_problem *prob, int nr_folds, const mf_parameter_bridge* parameter_bridge);
EXPORT_API(float) MFCrossValidation(const mf_problem_bridge *prob, int32_t nr_folds, const mf_parameter_bridge* parameter_bridge);

EXPORT_API(float) MFPredict(const mf_model *model, int p_idx, int q_idx);
EXPORT_API(float) MFPredict(const mf_model_bridge *model, int32_t p_idx, int32_t q_idx);
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ namespace Microsoft.ML.TestFramework.Attributes
/// </summary>
public sealed class MatrixFactorizationFactAttribute : EnvironmentSpecificFactAttribute
{
public MatrixFactorizationFactAttribute() : base("Disabled - this test is being fixed as part of https://github.com/dotnet/machinelearning/issues/1441")
public MatrixFactorizationFactAttribute() : base("")
{
}

/// <inheritdoc />
protected override bool IsEnvironmentSupported()
{
return Environment.Is64BitProcess;
return true;
}
}
}